forked from leonidk/pytorch-tf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsqueezenet.py
89 lines (85 loc) · 3.8 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
75
76
77
78
79
80
81
82
83
84
85
86
87
import tensorflow as tf
def fire_module(x,inp,sp,e11p,e33p):
with tf.variable_scope("fire"):
with tf.variable_scope("squeeze"):
W = tf.get_variable("weights",shape=[1,1,inp,sp])
b = tf.get_variable("bias",shape=[sp])
s = tf.nn.conv2d(x,W,[1,1,1,1],"VALID")+b
s = tf.nn.relu(s)
with tf.variable_scope("e11"):
W = tf.get_variable("weights",shape=[1,1,sp,e11p])
b = tf.get_variable("bias",shape=[e11p])
e11 = tf.nn.conv2d(s,W,[1,1,1,1],"VALID")+b
e11 = tf.nn.relu(e11)
with tf.variable_scope("e33"):
W = tf.get_variable("weights",shape=[3,3,sp,e33p])
b = tf.get_variable("bias",shape=[e33p])
e33 = tf.nn.conv2d(s,W,[1,1,1,1],"SAME")+b
e33 = tf.nn.relu(e33)
return tf.concat([e11,e33],3)
class SqueezeNet:
def __init__(self):
self.image = tf.placeholder('float',shape=[None,None,None,3],name='input_image')
self.layers = []
x = self.image
with tf.variable_scope('features'):
with tf.variable_scope('layer0'):
W = tf.get_variable("weights",shape=[3,3,3,64])
b = tf.get_variable("bias",shape=[64])
x = tf.nn.conv2d(x,W,[1,2,2,1],"VALID")
x = tf.nn.bias_add(x,b)
self.layers.append(x)
with tf.variable_scope('layer1'):
x = tf.nn.relu(x)
self.layers.append(x)
with tf.variable_scope('layer2'):
x = tf.nn.max_pool(x,[1,3,3,1],strides=[1,2,2,1],padding='VALID')
self.layers.append(x)
with tf.variable_scope('layer3'):
x = fire_module(x,64,16,64,64)
self.layers.append(x)
with tf.variable_scope('layer4'):
x = fire_module(x,128,16,64,64)
self.layers.append(x)
with tf.variable_scope('layer5'):
x = tf.nn.max_pool(x,[1,3,3,1],strides=[1,2,2,1],padding='VALID')
self.layers.append(x)
with tf.variable_scope('layer6'):
x = fire_module(x,128,32,128,128)
self.layers.append(x)
with tf.variable_scope('layer7'):
x = fire_module(x,256,32,128,128)
self.layers.append(x)
with tf.variable_scope('layer8'):
x = tf.nn.max_pool(x,[1,3,3,1],strides=[1,2,2,1],padding='VALID')
self.layers.append(x)
with tf.variable_scope('layer9'):
x = fire_module(x,256,48,192,192)
self.layers.append(x)
with tf.variable_scope('layer10'):
x = fire_module(x,384,48,192,192)
self.layers.append(x)
with tf.variable_scope('layer11'):
x = fire_module(x,384,64,256,256)
self.layers.append(x)
with tf.variable_scope('layer12'):
x = fire_module(x,512,64,256,256)
self.layers.append(x)
self.features = x
with tf.variable_scope('classifier'):
with tf.variable_scope('layer0'):
x = x
self.layers.append(x)
with tf.variable_scope('layer1'):
W = tf.get_variable("weights",shape=[1,1,512,1000])
b = tf.get_variable("bias",shape=[1000])
x = tf.nn.conv2d(x,W,[1,1,1,1],"VALID")
x = tf.nn.bias_add(x,b)
self.layers.append(x)
with tf.variable_scope('layer2'):
x = tf.nn.relu(x)
self.layers.append(x)
with tf.variable_scope('layer3'):
x = tf.nn.avg_pool(x,[1,13,13,1],strides=[1,1,1,1],padding='VALID')
self.layers.append(x)
self.classifier = tf.reshape(x,[-1,1000])