-
Notifications
You must be signed in to change notification settings - Fork 771
/
SqueezeNet.py
74 lines (66 loc) · 2.81 KB
/
SqueezeNet.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
"""
2017/12/02
"""
import tensorflow as tf
import numpy as np
class SqueezeNet(object):
def __init__(self, inputs, nb_classes=1000, is_training=True):
# conv1
net = tf.layers.conv2d(inputs, 96, [7, 7], strides=[2, 2],
padding="SAME", activation=tf.nn.relu,
name="conv1")
# maxpool1
net = tf.layers.max_pooling2d(net, [3, 3], strides=[2, 2],
name="maxpool1")
# fire2
net = self._fire(net, 16, 64, "fire2")
# fire3
net = self._fire(net, 16, 64, "fire3")
# fire4
net = self._fire(net, 32, 128, "fire4")
# maxpool4
net = tf.layers.max_pooling2d(net, [3, 3], strides=[2, 2],
name="maxpool4")
# fire5
net = self._fire(net, 32, 128, "fire5")
# fire6
net = self._fire(net, 48, 192, "fire6")
# fire7
net = self._fire(net, 48, 192, "fire7")
# fire8
net = self._fire(net, 64, 256, "fire8")
# maxpool8
net = tf.layers.max_pooling2d(net, [3, 3], strides=[2, 2],
name="maxpool8")
# fire9
net = self._fire(net, 64, 256, "fire9")
# dropout
net = tf.layers.dropout(net, 0.5, training=is_training)
# conv10
net = tf.layers.conv2d(net, 1000, [1, 1], strides=[1, 1],
padding="SAME", activation=tf.nn.relu,
name="conv10")
# avgpool10
net = tf.layers.average_pooling2d(net, [13, 13], strides=[1, 1],
name="avgpool10")
# squeeze the axis
net = tf.squeeze(net, axis=[1, 2])
self.logits = net
self.prediction = tf.nn.softmax(net)
def _fire(self, inputs, squeeze_depth, expand_depth, scope):
with tf.variable_scope(scope):
squeeze = tf.layers.conv2d(inputs, squeeze_depth, [1, 1],
strides=[1, 1], padding="SAME",
activation=tf.nn.relu, name="squeeze")
# squeeze
expand_1x1 = tf.layers.conv2d(squeeze, expand_depth, [1, 1],
strides=[1, 1], padding="SAME",
activation=tf.nn.relu, name="expand_1x1")
expand_3x3 = tf.layers.conv2d(squeeze, expand_depth, [3, 3],
strides=[1, 1], padding="SAME",
activation=tf.nn.relu, name="expand_3x3")
return tf.concat([expand_1x1, expand_3x3], axis=3)
if __name__ == "__main__":
inputs = tf.random_normal([32, 224, 224, 3])
net = SqueezeNet(inputs)
print(net.prediction)