How to use a retrained “tensorflow for poets” graph on iOS?

空扰寡人 提交于 2020-01-25 06:30:30

问题


With "tensorflow for poets", I retrained the inceptionv3 graph. Now I want to use tfcoreml converter to convert the graph to an iOS coreML model.

But tf_coreml_converter.py stops with "NotImplementedError: Unsupported Ops of type: PlaceholderWithDefault".

I already tried "optimize_for_inference" and "strip_unused", but I can't get rid of this unsupported op "PlaceholderWithDefault".

Any idea what steps are needed after training in tensorflow-for-poets, to convert a "tensorflow-for-poets" graph (inceptionv3) to an iOS coreML model?


回答1:


I succedded in removing the PlaceholderWithDefault op from the retrained tensorflow for poets graph with this steps:

  1. Optimize graph for interference:

    python -m tensorflow.python.tools.optimize_for_inference \ --input retrained_graph.pb \ --output graph_optimized.pb \ --input_names=Mul\ --output_names=final_result

  2. Remove PlaceholderWithDefault op with transform_graph tool:

    bazel build tensorflow/tools/graph_transforms:transform_graph bazel-bin/tensorflow/tools/graph_transforms/transform_graph \ --in_graph=graph_optimized.pb \ --out_graph=graph_optimized_stripped.pb \ --inputs='Mul' \ --outputs='final_result' \ --transforms='remove_nodes(op=PlaceholderWithDefault)'

Afterwards I could convert it to coreML. But as Matthijs already pointed out, the latest version of tfcoreml from git hub does it automatically.




回答2:


Whoever created this graph used tf.placeholder_with_default() to define the placeholder (a placeholder in TF is used for the inputs to the neural network). Since tf-coreml does not support the PlaceholderWithDefault op, you cannot use this graph.

Possible solutions:

  • Define the placeholders using tf.placeholder() instead. The problem is that you'll need to retrain the graph from scratch since Tensorflow for Poets uses a pretrained graph and you can no longer use that.
  • Hack the graph to replace the PlaceholderWithDefault op with Placeholder.
  • Hack tf-coreml to use a Placeholder op whenever it encounters a PlaceholderWithDefault op. This is probably the quickest solution.

Update: From the code, it looks like a recent update to tf-coreml now simply skips the PlaceholderWithDefault layer. It should no longer give an error message. So if you use the latest version of tf-coreml (not using pip but by checking out the master branch of the GitHub repo) then you should no longer get this error.




回答3:


 import tfcoreml as tf_converter
 tf_converter.convert(tf_model_path = '/Users/username/path/tf_files/retrained_graph.pb',
     mlmodel_path = 'MyModel.mlmodel',
     output_feature_names = ['final_result:0'],
     input_name_shape_dict = {'input:0':[1,224,224,3]},
     image_input_names = ['input:0'],
     class_labels = '/Users/username/path/tf_files/retrained_labels.txt',
     image_scale=2/255.0,
     red_bias=-1,
     green_bias=-1,
     blue_bias=-1
 )

Using tfcoreml, I found success with these settings.



来源:https://stackoverflow.com/questions/48117854/how-to-use-a-retrained-tensorflow-for-poets-graph-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!