Use Azure custom-vision trained model with tensorflow.js

一个人想着一个人 提交于 2019-12-03 17:07:27

Just parroting what I said here to save you a click. I do hope that the option to export directly to tfjs is available soon.

These are the steps I did to get an exported TensorFlow model working for me:

  1. Replace PadV2 operations with Pad. This python function should do it. input_filepath is the path to the .pb model file and output_filepath is the full path of the updated .pb file that will be created.
import tensorflow as tf
def ReplacePadV2(input_filepath, output_filepath):
    graph_def = tf.GraphDef()
    with open(input_filepath, 'rb') as f:
        graph_def.ParseFromString(f.read())

    for node in graph_def.node:
        if node.op == 'PadV2':
            node.op = 'Pad'
            del node.input[-1]
            print("Replaced PadV2 node: {}".format(node.name))

    with open(output_filepath, 'wb') as f:
        f.write(graph_def.SerializeToString())
  1. Install tensorflowjs 0.8.6 or earlier. Converting frozen models is deprecated in later versions.
  2. When calling the convertor, set --input_format as tf_frozen_model and set output_node_names as model_outputs. This is the command I used.
tensorflowjs_converter --input_format=tf_frozen_model --output_json=true --output_node_names='model_outputs' --saved_model_tags=serve  path\to\modified\model.pb  folder\to\save\converted\output

Ideally, tf.loadGraphModel('path/to/converted/model.json') should now work (tested for tfjs 1.0.0 and above).

Partial answer:

Trying to achieve the same thing - here is the start of an answer - to make use of the output_node_names:

tensorflowjs_converter --input_format=tf_frozen_model --output_node_names='model_outputs' model.pb web_model

I am not yet sure how to incorporate this into same code - do you have anything @Kasper Kamperman?

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