Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use model in tensorflow.js #7192

Closed
kasperkamperman opened this issue Apr 14, 2018 — with docs.microsoft.com · 17 comments
Closed

Use model in tensorflow.js #7192

kasperkamperman opened this issue Apr 14, 2018 — with docs.microsoft.com · 17 comments

Comments

Copy link

I hope someone could give a hint how to use the model in tensorflow.js. I suppose the .pb is a Frozen GraphDef? There is a converter tool (https://github.com/tensorflow/tfjs-converter) however I miss the "--output_node_names". Exporting directly for tensorflow.js would be even greater of course.


Document details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@jason-j-MSFT
Copy link
Contributor

@kasperkamperman
Thanks for the feedback! We are currently investigating and will update you shortly.

@jason-j-MSFT
Copy link
Contributor

@kasperkamperman
I have assigned the issue to the content author to investigate further and update the document as appropriate.

@anrothMSFT
Copy link
Contributor

This is a good question. I don't actually think anyone on the team has tried to get this to play nice with TensorFlow.js yet..

@kasperkamperman
Copy link
Author

I'm curious if there is any progress already?

Copy link

I think there is no need of converting the model into frozen GraphDef because we can use .pb model directly with python

@kasperkamperman
Copy link
Author

@ksujanpaul I don't want to use it with Python but with tensorflow.js... (which is javascript, so it's not related to Tensforflow for Python).

Copy link
Author

From my research I also understand that there are also different pb files, but I can't find which type Azure Custom Vision exports.

I found the tfjs converter. This is to convert a TensorFlow SavedModel (is the *.pb file from Azure a SavedModel?) or Keras model to a web-friendly format. However I need to fill in "output_node_names" (how do I get these?). I'm also not 100% sure if my pb file for Android is equal to a "tf_saved_model".

I hope someone has a tip or a starting point.

@kasperkamperman
Copy link
Author

With tensorboard I could view the Graph, is the 'output_node_names' simply 'import'?

model_graph

@aribornstein
Copy link

@anrothMSFT is there any progress on this issue?

@simonaco
Copy link
Contributor

@anrothMSFT any updates on this?

Copy link

jtlz2 commented Feb 25, 2019

@MarkMcGee1
Copy link
Contributor

Thank you for taking the time to share your product and documentation feedback with us. Your input is valued because it helps us create the right documentation for our customers. Due to the volume of issues in our queue, we are closing open issues older than 90 days. We hope to continue hearing from you. Thank you.

@antonsimola
Copy link

I hope someone could give a hint how to use the model in tensorflow.js. I suppose the .pb is a Frozen GraphDef? There is a converter tool (https://github.com/tensorflow/tfjs-converter) however I miss the "--output_node_names". Exporting directly for tensorflow.js would be even greater of course.

Document details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@anrothMSFT
Copy link
Contributor

Thanks for bumping this. It has generated a hallway discussion this AM about whether to add TF.js export as an export flavor (we added TF lite last week, although release notes are lagging.) Something for us to keep discussing

@harishkrishnav on our team has been playing around with tf.js and might be able to provide some hints in the interim...

@harishkrishnav
Copy link

This is a bit tricky as is and we might soon release a sample for how to load Custom Vision models on the browser using TensorFlow.js. 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).

@antonsimola
Copy link

@harishkrishnav It worked! Thank you so much!

@jtlz2
Copy link

jtlz2 commented Sep 27, 2019

@harishkrishnav Fantastic - do you want to take the SO answer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests