1

I'm trying to convert this CPM-TF model to TFLite, but to use the TocoConverter, I need to specify input and output tensors. https://github.com/timctho/convolutional-pose-machines-tensorflow

I ran the included run_freeze_model.py and got the cpm_hand_frozen.pb (GraphDef?) file.

From this post I copied the code snippet for converting the ProtoBuf file with known inputs and outputs. But looking through the model definition code, I have some trouble finding the correct answers for the in- and outputs. Tensorflow Convert pb file to TFLITE using python

import tensorflow as tf
import numpy as np
from config import FLAGS

path_to_frozen_graphdef_pb = 'frozen_models/cpm_hand_frozen.pb'


def main(argv):
    input_tensors = [1, FLAGS.input_size, FLAGS.input_size, 3]
    output_tensors = np.zeros(FLAGS.num_of_joints)
    frozen_graph_def = tf.GraphDef()

    with open(path_to_frozen_graphdef_pb, 'rb') as f:
        frozen_graph_def.ParseFromString(f.read())

    tflite_model = tf.contrib.lite.toco_convert(frozen_graph_def, input_tensors, output_tensors)

if __name__ == '__main__':
    tf.app.run()

I'm quite new to Tensorflow, but I think the input should be defined as
[1, FLAGS.input_size, FLAGS.input_size, 3] Found that here: https://github.com/timctho/convolutional-pose-machines-tensorflow/blob/master/models/nets/cpm_hand.py#L23

Not sure what 1 represents, but None does not work and I guess the other parameters are the image size and color channels.

However, with that input, it returns an error: AttributeError: 'int' object has no attribute 'dtype'

I got no clue on what the output should be, other than it should be an array.


UPDATE 1

Looking through the TF docs, it appears that I need to define the input as a tensor (obvious!). https://www.tensorflow.org/lite/convert/python_api

input_tensors = tf.placeholder(name="img", dtype=tf.float32, shape=(1,FLAGS.input_size, FLAGS.input_size, 3))

This does not return an error, but I still need to figure out if the input are correct and what the output should be like.


UPDATE 2 Alright, so I finally got it to spit out the tflite model with this code snippet
def tflite_converter():
    graph_def_file = os.path.join('frozen_models', '{}_frozen.pb'.format('cpm_hand'))

    input_arrays = ['input_placeholer']
    output_arrays = [FLAGS.output_node_names]

    converter = tf.lite.TFLiteConverter.from_frozen_graph(graph_def_file, input_arrays, output_arrays)
    tflite_model = converter.convert()

    open('{}.tflite'.format('cpm_hand'), 'wb').write(tflite_model)

I hope that I did it correctly. I will try to do inference on the model on Android. I do also think there is a misspelling in the input tensor input_placeholder. It appears to be corrected in the code itself, but from printing out all node names from the pretrained model, the spelling input_placeholer is present.

Node names can be seen here: https://github.com/timctho/convolutional-pose-machines-tensorflow/issues/59


My setup is:

Ubuntu 18.04
CUDA 9.1 & cuDNN 7.0
Python 3.6.5
Tensorflow GPU 1.6

Inference works like a charm, so there should be no issue on the setup itself.

Nupur Garg
  • 524
  • 2
  • 9
  • This answer may help -> https://stackoverflow.com/a/55320256/10878733 – Shubham Panchal Apr 04 '19 at 13:12
  • @ShubhamPanchal Thank you, but I'm not familiar with TF enough to apply that to my problem. I changed the input tensor to a tf.placeholder() object, which didn't return any errors, but I guess that my output is different from my input, right? – Christian Skjerning Apr 04 '19 at 13:20
  • `inputs` and `outputs` are lists of tensors and not their names or input shapes. – Shubham Panchal Apr 04 '19 at 15:01
  • So just to make it dead simple. The tensor I have here `tf.placeholder(name="img", dtype=tf.float32, shape=(1,FLAGS.input_size, FLAGS.input_size, 3))` should be inside an array like this? Or is it done in another way? `[ tf.placeholder(name="img", dtype=tf.float32, shape=(1,FLAGS.input_size, FLAGS.input_size, 3)) ]` – Christian Skjerning Apr 04 '19 at 18:04
  • @ShubhamPanchal I updated the pose once again - could I please get you to have a look at my solution? Do you think it would work? – Christian Skjerning Apr 04 '19 at 22:00
  • And also, where do I give you reputation? :p – Christian Skjerning Apr 04 '19 at 22:02
  • The code seems to be correct. The answer which I provided is written by me. You can upvote it. – Shubham Panchal Apr 05 '19 at 02:13
  • Great! I've done so. Is it possible that multiple tensors should be the input? I mean, along `input_placholer`, there is `cmap_placeholder` and `gt_hmap_placeholder`, but adding those results in error `ValueError: Invalid tensors 'cmap_placeholder,gt_hmap_placeholder' were found.` – Christian Skjerning Apr 05 '19 at 11:37

0 Answers0