-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
80 lines (56 loc) · 3.17 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import tensorflow as tf
def mynet(inp, reuse=False):
with tf.variable_scope("model"):
print(inp.shape)
with tf.variable_scope("conv1") as scope:
net = tf.contrib.layers.conv2d(inp, 32, 15,
stride=1,
activation_fn=tf.nn.relu,
padding='VALID',
weights_initializer=tf.keras.initializers.he_normal(),
scope=scope,
reuse=reuse)
print(net.shape)
net = tf.contrib.layers.max_pool2d(net, kernel_size=2, stride=2)
net = tf.contrib.layers.batch_norm(net, reuse=reuse, scope=scope)
print(net.shape)
with tf.variable_scope("conv2") as scope:
net = tf.contrib.layers.conv2d(net, 64, 8,
stride=1,
activation_fn=tf.nn.relu,
padding='VALID',
weights_initializer=tf.keras.initializers.he_normal(),
scope=scope,
reuse=reuse)
print(net.shape)
net = tf.contrib.layers.max_pool2d(net, kernel_size=3, stride=3)
net = tf.contrib.layers.batch_norm(net, reuse=reuse, scope=scope)
print(net.shape)
with tf.variable_scope("conv3") as scope:
net = tf.contrib.layers.conv2d(net, 256, 5,
stride=1,
activation_fn=tf.nn.relu,
padding='VALID',
weights_initializer=tf.keras.initializers.he_normal(),
scope=scope,
reuse=reuse)
print(net.shape)
net = tf.contrib.layers.max_pool2d(net, kernel_size=2, stride=2)
net = tf.contrib.layers.batch_norm(net, reuse=reuse, scope=scope)
print(net.shape)
with tf.variable_scope("fc1") as scope:
net = tf.contrib.layers.flatten(net)
print(net.shape)
with tf.variable_scope("fc2") as scope:
net = tf.contrib.layers.fully_connected(net, 64,
activation_fn=tf.nn.relu,
reuse=reuse,
scope=scope)
print(net.shape)
return net
def contrastive_loss(model1, model2, y, margin):
with tf.name_scope("contrastive-loss"):
d = tf.sqrt(tf.reduce_sum(tf.pow(model1-model2, 2), 1, keepdims=True))
tmp = y * tf.square(d)
tmp2 = (1 - y) * tf.square(tf.maximum((margin - d),0))
return tf.reduce_mean(tmp + tmp2)/2