@@ -26,6 +26,7 @@ def to_output_strided_layers(convolution_def, output_stride):
26
26
variables (see the load_variables function).
27
27
:return: An array containing an element for each layer with the detailed layer specs defined in each of them.
28
28
"""
29
+
29
30
current_stride = 1
30
31
rate = 1
31
32
block_id = 0
@@ -70,6 +71,7 @@ def load_variables(chkpoint, base_dir=BASE_DIR):
70
71
Note for refactoring: To make this function reusable for other networks, the weights downloader should be either
71
72
1/ more generic, or 2/ extracted outside this function. Apart from this, this function is likely very reusable for other networks.
72
73
"""
74
+
73
75
manifest_path = os .path .join (base_dir , chkpoint , "manifest.json" )
74
76
if not os .path .exists (manifest_path ):
75
77
print ('Weights for checkpoint %s are not downloaded. Downloading to %s ...' % (chkpoint , base_dir ))
@@ -102,6 +104,7 @@ def _read_imgfile(path, width, height):
102
104
:param height: The requested image target height.
103
105
:return: The resized image with normalized pixels as a 3D array (height, width, channels).
104
106
"""
107
+
105
108
img = cv2 .imread (path )
106
109
# The cv2.resize shape definition is indeed (width, height), while the image shape from cv2.imread is (height, width, channels).
107
110
img = cv2 .resize (img , (width , height ))
@@ -136,13 +139,13 @@ def _depthwise_weights(layer_name):
136
139
return variables ["MobilenetV1/" + layer_name + "/depthwise_weights" ]['x' ]
137
140
138
141
def _conv_to_output (mobile_net_output , output_layer_name ):
139
- w = tf .nn .conv2d (mobile_net_output , _weights (output_layer_name ), [1 , 1 , 1 , 1 ], padding = 'SAME' )
142
+ w = tf .nn .conv2d (input = mobile_net_output , filters = _weights (output_layer_name ), strides = [1 , 1 , 1 , 1 ], padding = 'SAME' )
140
143
w = tf .nn .bias_add (w , _biases (output_layer_name ), name = output_layer_name )
141
144
return w
142
145
143
146
def _conv (inputs , stride , block_id ):
144
147
return tf .nn .relu6 (
145
- tf .nn .conv2d (inputs , _weights ("Conv2d_" + str (block_id )), stride , padding = 'SAME' )
148
+ tf .nn .conv2d (input = inputs , filters = _weights ("Conv2d_" + str (block_id )), strides = stride , padding = 'SAME' )
146
149
+
147
150
_biases ("Conv2d_" + str (block_id ))
148
151
)
@@ -159,19 +162,19 @@ def _separable_conv(inputs, stride, block_id, dilations):
159
162
# A depthwise convolution uses a filter (kernel) with a depth of 1 instead of the channel depth to get fewer variables that
160
163
# have to be learned, and so achieve a faster but less accurate network. When the rate (or dilation) is 1, then the strides
161
164
# must all be 1, see: https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/nn/depthwise_conv2d
162
- w = tf .nn .depthwise_conv2d (inputs , _depthwise_weights (dw_layer ), stride , 'SAME' , rate = dilations , data_format = 'NHWC' )
165
+ w = tf .nn .depthwise_conv2d (input = inputs , filter = _depthwise_weights (dw_layer ), strides = stride , padding = 'SAME' , dilations = dilations , data_format = 'NHWC' )
163
166
w = tf .nn .bias_add (w , _biases (dw_layer ))
164
167
w = tf .nn .relu6 (w )
165
168
166
- w = tf .nn .conv2d (w , _weights (pw_layer ), [1 , 1 , 1 , 1 ], padding = 'SAME' )
169
+ w = tf .nn .conv2d (input = w , filters = _weights (pw_layer ), strides = [1 , 1 , 1 , 1 ], padding = 'SAME' )
167
170
w = tf .nn .bias_add (w , _biases (pw_layer ))
168
171
w = tf .nn .relu6 (w )
169
172
170
173
return w
171
174
172
175
x = image
173
176
buff = [] # remove this buffer, seems like it's not used
174
- with tf .variable_scope (None , 'MobilenetV1' ):
177
+ with tf .compat . v1 . variable_scope (None , 'MobilenetV1' ):
175
178
176
179
for m in layers :
177
180
stride = [1 , m ['stride' ], m ['stride' ], 1 ]
@@ -196,6 +199,16 @@ def _separable_conv(inputs, stride, block_id, dilations):
196
199
197
200
198
201
def convert (model_id , model_dir , check = False ):
202
+ """
203
+ Download and read the weight and bias variables for MobileNetV1, create the network and instantiate it with those variables.
204
+ Then write the instantiated network to a model file and corresponding checkpoint files.
205
+
206
+ :param model_id: Refers to the model to load, as defined in the config.yaml file.
207
+ :param model_dir: Defines where the model and checkpoint files will be saved.
208
+ :param check: Indicates whether or not to verify the model by feeding it a sample image.
209
+ :return: Nothing, the model and checkpoint files are written to the filesystem.
210
+ """
211
+
199
212
cfg = load_config ()
200
213
checkpoints = cfg ['checkpoints' ]
201
214
image_size = cfg ['imageSize' ]
@@ -221,12 +234,12 @@ def convert(model_id, model_dir, check=False):
221
234
layers = to_output_strided_layers (mobile_net_arch , output_stride )
222
235
variables = load_variables (chkpoint )
223
236
224
- init = tf .global_variables_initializer ()
225
- with tf .Session () as sess :
237
+ init = tf .compat . v1 . global_variables_initializer ()
238
+ with tf .compat . v1 . Session () as sess :
226
239
sess .run (init )
227
- saver = tf .train .Saver ()
240
+ saver = tf .compat . v1 . train .Saver ()
228
241
229
- image_ph = tf .placeholder (tf .float32 , shape = [1 , None , None , 3 ], name = 'image' )
242
+ image_ph = tf .compat . v1 . placeholder (tf .float32 , shape = [1 , None , None , 3 ], name = 'image' )
230
243
outputs = build_network (image_ph , layers , variables )
231
244
232
245
sess .run (
@@ -241,7 +254,7 @@ def convert(model_id, model_dir, check=False):
241
254
os .makedirs (os .path .dirname (save_path ))
242
255
checkpoint_path = saver .save (sess , save_path , write_state = False )
243
256
244
- tf .train .write_graph (cg , model_dir , "model-%s.pbtxt" % chkpoint )
257
+ tf .io .write_graph (cg , model_dir , "model-%s.pbtxt" % chkpoint )
245
258
246
259
# Freeze graph and write our final model file
247
260
freeze_graph (
0 commit comments