-
Notifications
You must be signed in to change notification settings - Fork 0
/
fashion_net_base.py
185 lines (150 loc) · 7.05 KB
/
fashion_net_base.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
slim = tf.contrib.slim
from tg_layers import pad_conv2d, pad_max_pool2d, pad_avg_pool2d
def build_inception_module(name, net, nfilters):
with tf.variable_scope(name):
with tf.variable_scope('branch_0'):
branch_0 = slim.conv2d(net, nfilters[0], kernel_size=1, scope='1x1')
with tf.variable_scope('branch_1'):
branch_1 = slim.conv2d(net, nfilters[1], kernel_size=1, scope='3x3_reduce')
branch_1 = pad_conv2d(branch_1, nfilters[2], kernel_size=3,
padding=1, scope='3x3')
with tf.variable_scope('branch_2'):
branch_2 = slim.conv2d(net, nfilters[3], kernel_size=1, scope='5x5_reduce')
branch_2 = pad_conv2d(branch_2, nfilters[4], kernel_size=5,
padding=2, scope='5x5')
with tf.variable_scope('branch_3'):
branch_3 = pad_max_pool2d(net, kernel_size=3, stride=1,
padding=1, scope='pool')
branch_3 = slim.conv2d(branch_3, nfilters[5], kernel_size=1, scope='pool_proj')
net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
return net
def fashion_net_base(inputs,
final_endpoint='common_layer',
common_layer_activation_fn=None,
is_training=True,
dropout_keep_prob=0.6,
reuse=False,
scope=None):
"""Defines the fashion_net base architecture.
Args:
inputs: a tensor of size [batch_size, height, width, channels].
final_endpoint: specifies the endpoint to construct the network up to.
is_training: whether is training or not.
dropout_keep_prob: the percentage of activation values that are retained.
reuse: whether or not the network and its variables should be reused. To be
able to reuse 'scope' must be given.
scope: Optional variable_scope.
Returns:
A dictionary from components of the network to the corresponding activation.
Raises:
ValueError: if final_endpoint is not set to one of the predefined values.
"""
end_points = {}
with tf.variable_scope(scope, 'fashion_net_base', [inputs], reuse=reuse):
with slim.arg_scope(
[slim.conv2d], # NOTE: pad_conv2d's slim.conv2d also inherits this arg_scope
stride=1,
):
end_point = 'conv1_7x7_s2'
net = pad_conv2d(inputs, 64, kernel_size=7, stride=2,
padding=3, scope=end_point)
end_points[end_point] = net
if final_endpoint == end_point: return net, end_points
end_point = 'pool1_3x3_s2'
net = slim.max_pool2d(net, kernel_size=3, stride=2,
padding='SAME', scope=end_point)
end_points[end_point] = net
if final_endpoint == end_point: return net, end_points
end_point = 'pool1_norm1'
net = tf.nn.lrn(net, 2, 1.0, 2e-05, 0.75)
end_points[end_point] = net
if final_endpoint == end_point: return net, end_points
end_point = 'conv2_3x3_reduce'
net = slim.conv2d(net, 64, kernel_size=1, scope=end_point)
end_points[end_point] = net
if final_endpoint == end_point: return net, end_points
end_point = 'conv2_3x3'
net = pad_conv2d(net, 192, kernel_size=3,
padding=1, scope=end_point)
end_points[end_point] = net
if final_endpoint == end_point: return net, end_points
end_point = 'conv2_norm2'
net = tf.nn.lrn(net, 2, 1.0, 2e-05, 0.75)
end_points[end_point] = net
if final_endpoint == end_point: return net, end_points
end_point = 'pool2_3x3_s2'
net = slim.max_pool2d(net, kernel_size=3, stride=2, scope=end_point)
end_points[end_point] = net
if final_endpoint == end_point: return net, end_points
end_point = 'inception_3a'
nfilters = [64, 96, 128, 16, 32, 32]
net = build_inception_module(end_point, net, nfilters)
end_points[end_point] = net
if final_endpoint == end_point: return net, end_points
end_point = 'inception_3b'
nfilters = [128, 128, 192, 32, 96, 64]
net = build_inception_module(end_point, net, nfilters)
end_points[end_point] = net
if final_endpoint == end_point: return net, end_points
end_point = 'pool3_3x3_s2'
net = slim.max_pool2d(net, kernel_size=3, stride=2, scope=end_point)
end_points[end_point] = net
if final_endpoint == end_point: return net, end_points
end_point = 'inception_4a'
nfilters = [192, 96, 208, 16, 48, 64]
net = build_inception_module(end_point, net, nfilters)
end_points[end_point] = net
if final_endpoint == end_point: return net, end_points
end_point = 'inception_4b'
nfilters = [160, 112, 224, 24, 64, 64]
net = build_inception_module(end_point, net, nfilters)
end_points[end_point] = net
if final_endpoint == end_point: return net, end_points
end_point = 'inception_4c'
nfilters = [128, 128, 256, 24, 64, 64]
net = build_inception_module(end_point, net, nfilters)
end_points[end_point] = net
if final_endpoint == end_point: return net, end_points
end_point = 'inception_4d'
nfilters = [112, 144, 288, 32, 64, 64]
net = build_inception_module(end_point, net, nfilters)
end_points[end_point] = net
if final_endpoint == end_point: return net, end_points
end_point = 'inception_4e'
nfilters = [256, 160, 320, 32, 128, 128]
net = build_inception_module(end_point, net, nfilters)
end_points[end_point] = net
if final_endpoint == end_point: return net, end_points
end_point = 'pool4_3x3_s2'
net = slim.max_pool2d(net, kernel_size=3, stride=2, scope=end_point)
end_points[end_point] = net
if final_endpoint == end_point: return net, end_points
end_point = 'inception_5a'
nfilters = [256, 160, 320, 32, 128, 128]
net = build_inception_module(end_point, net, nfilters)
end_points[end_point] = net
if final_endpoint == end_point: return net, end_points
end_point = 'inception_5b'
nfilters = [384, 192, 384, 48, 128, 128]
net = build_inception_module(end_point, net, nfilters)
end_points[end_point] = net
if final_endpoint == end_point: return net, end_points
end_point = 'pool5_6x6_s1'
net = pad_avg_pool2d(net, kernel_size=6, stride=6, scope=end_point)
net = tf.squeeze(net, [1, 2], name='spatial_squeeze')
end_points[end_point] = net
if final_endpoint == end_point: return net, end_points
end_point = 'common_layer'
net = slim.fully_connected(net, 1024,
activation_fn=common_layer_activation_fn,
scope=end_point)
end_point_dropout = 'common_layer_dropout'
net = slim.dropout(net, dropout_keep_prob,
is_training=is_training, scope=end_point_dropout)
end_points[end_point] = net
if final_endpoint == end_point: return net, end_points
raise ValueError('Unknown final endpoint %s' % final_endpoint)