Skip to content

Commit 92fe3a2

Browse files
committed
Converted to TF2 using the upgrade script and tested on TF2 without manual intervention. Now we can migrate to idiomatic TF2 code.
1 parent 194c8b4 commit 92fe3a2

File tree

8 files changed

+39
-16
lines changed

8 files changed

+39
-16
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM tensorflow/tensorflow:1.15.0-gpu-py3-jupyter
1+
FROM tensorflow/tensorflow:2.0.0-gpu-py3-jupyter
22
# see: https://www.tensorflow.org/install/docker
33
# see: https://hub.docker.com/r/tensorflow/tensorflow/
44

benchmark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
def main():
1717

18-
with tf.Session() as sess:
18+
with tf.compat.v1.Session() as sess:
1919
model_cfg, model_outputs = posenet.load_model(args.model, sess)
2020
output_stride = model_cfg['output_stride']
2121
num_images = args.num_images

docker_img_build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#!/usr/bin/env bash
22

3+
docker rmi -f posenet-python
4+
35
docker build -t posenet-python -f Dockerfile .

image_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
def main():
2020

21-
with tf.Session() as sess:
21+
with tf.compat.v1.Session() as sess:
2222
model_cfg, model_outputs = posenet.load_model(args.model, sess)
2323
output_stride = model_cfg['output_stride']
2424

posenet/converter/tfjs2python.py

100755100644
Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def to_output_strided_layers(convolution_def, output_stride):
2626
variables (see the load_variables function).
2727
:return: An array containing an element for each layer with the detailed layer specs defined in each of them.
2828
"""
29+
2930
current_stride = 1
3031
rate = 1
3132
block_id = 0
@@ -70,6 +71,7 @@ def load_variables(chkpoint, base_dir=BASE_DIR):
7071
Note for refactoring: To make this function reusable for other networks, the weights downloader should be either
7172
1/ more generic, or 2/ extracted outside this function. Apart from this, this function is likely very reusable for other networks.
7273
"""
74+
7375
manifest_path = os.path.join(base_dir, chkpoint, "manifest.json")
7476
if not os.path.exists(manifest_path):
7577
print('Weights for checkpoint %s are not downloaded. Downloading to %s ...' % (chkpoint, base_dir))
@@ -102,6 +104,7 @@ def _read_imgfile(path, width, height):
102104
:param height: The requested image target height.
103105
:return: The resized image with normalized pixels as a 3D array (height, width, channels).
104106
"""
107+
105108
img = cv2.imread(path)
106109
# The cv2.resize shape definition is indeed (width, height), while the image shape from cv2.imread is (height, width, channels).
107110
img = cv2.resize(img, (width, height))
@@ -136,13 +139,13 @@ def _depthwise_weights(layer_name):
136139
return variables["MobilenetV1/" + layer_name + "/depthwise_weights"]['x']
137140

138141
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')
140143
w = tf.nn.bias_add(w, _biases(output_layer_name), name=output_layer_name)
141144
return w
142145

143146
def _conv(inputs, stride, block_id):
144147
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')
146149
+
147150
_biases("Conv2d_" + str(block_id))
148151
)
@@ -159,19 +162,19 @@ def _separable_conv(inputs, stride, block_id, dilations):
159162
# A depthwise convolution uses a filter (kernel) with a depth of 1 instead of the channel depth to get fewer variables that
160163
# have to be learned, and so achieve a faster but less accurate network. When the rate (or dilation) is 1, then the strides
161164
# 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')
163166
w = tf.nn.bias_add(w, _biases(dw_layer))
164167
w = tf.nn.relu6(w)
165168

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')
167170
w = tf.nn.bias_add(w, _biases(pw_layer))
168171
w = tf.nn.relu6(w)
169172

170173
return w
171174

172175
x = image
173176
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'):
175178

176179
for m in layers:
177180
stride = [1, m['stride'], m['stride'], 1]
@@ -196,6 +199,16 @@ def _separable_conv(inputs, stride, block_id, dilations):
196199

197200

198201
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+
199212
cfg = load_config()
200213
checkpoints = cfg['checkpoints']
201214
image_size = cfg['imageSize']
@@ -221,12 +234,12 @@ def convert(model_id, model_dir, check=False):
221234
layers = to_output_strided_layers(mobile_net_arch, output_stride)
222235
variables = load_variables(chkpoint)
223236

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:
226239
sess.run(init)
227-
saver = tf.train.Saver()
240+
saver = tf.compat.v1.train.Saver()
228241

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')
230243
outputs = build_network(image_ph, layers, variables)
231244

232245
sess.run(
@@ -241,7 +254,7 @@ def convert(model_id, model_dir, check=False):
241254
os.makedirs(os.path.dirname(save_path))
242255
checkpoint_path = saver.save(sess, save_path, write_state=False)
243256

244-
tf.train.write_graph(cg, model_dir, "model-%s.pbtxt" % chkpoint)
257+
tf.io.write_graph(cg, model_dir, "model-%s.pbtxt" % chkpoint)
245258

246259
# Freeze graph and write our final model file
247260
freeze_graph(

posenet/model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def load_model(model_id, sess, model_dir=MODEL_DIR):
4242
convert(model_ord, model_dir, check=False)
4343
assert os.path.exists(model_path)
4444

45-
with tf.gfile.GFile(model_path, 'rb') as f:
46-
graph_def = tf.GraphDef()
45+
with tf.io.gfile.GFile(model_path, 'rb') as f:
46+
graph_def = tf.compat.v1.GraphDef()
4747
graph_def.ParseFromString(f.read())
4848
sess.graph.as_default()
4949
tf.import_graph_def(graph_def, name='')

upgrade-tf-v2.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
WORK=$(dirname $(pwd))
4+
5+
docker run --gpus all -it -v $WORK:/work posenet-python tf_upgrade_v2 \
6+
--intree posenet-python/ \
7+
--outtree posenet-python_v2/ \
8+
--reportfile posenet-python/report.txt

webcam_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
def main():
19-
with tf.Session() as sess:
19+
with tf.compat.v1.Session() as sess:
2020
model_cfg, model_outputs = posenet.load_model(args.model, sess)
2121
output_stride = model_cfg['output_stride']
2222

0 commit comments

Comments
 (0)