tensorflow/tfjs

How to convert tfjs_graph_model to tf_saved_model

take-kuma opened this issue · 14 comments

To get help from the community, we encourage using Stack Overflow and the tensorflow.js tag.

TensorFlow.js version

tensorflowjs 1.0.1 pip package, with python3.6 on Ubuntu16.04.

Describe the problem or feature request

I am porting BodyPix to python. So I want to convert the BodyPix tfjs models to the python-friendly format.
The tensorflowjs_converter can convert tf_saved_model to tfjs_graph_model. But the reverse conversion is not possible.
Is there a good way?
(Other formats are also OK if they are loadable with python.)

@take-kuma Currently we do not support the converting the tf_graph_model back to tf_saved_model.
But theoretically, since tf_graph_model is a frozen model, it can be converted back to frozen graph format.

The model.json file contains the network topology and the .bin files contain the weights. You can potentially use the graph model loader to retrieve the graph and weights, and use protobuf.js to reconstruct the protobuf file.

@pyu10055 Thank you for your advice. I'll try to reconstruct the frozen graph.

I'm happy if the tensorfrowjs_converter supports reverse conversion in future versions.

@take-kuma, you might find this answer helpful.

@pyu10055, would adding a Python script to the toolbox be useful? Seems that this is a very common problem.

@take-kuma, I now think that you might be better install a previous version of tfjs-converter and proceed from there. I have made a PR with instructions on how to do that.

@sdll , Thank you for your advice.
I tried tensorflowjs_converter 0.8.5 but I have not been able to convert the web-friendly format files of BodyPix and PoseNetV2 to the other format yet.

It is also OK to extract weights data from the web-friendly format instead of converting it. Is there a good way?
Or, is it impossible to release the original weights data before conversion?

I think PoseNet and BodyPix are good methods that can detect human pose at a high speed and they can be used for various purposes. But Javascript is not suitable for my project at all.

@sdll , Thank you for your advice.
I tried tensorflowjs_converter 0.8.5 but I have not been able to convert the web-friendly format files of BodyPix and PoseNetV2 to the other format yet.

It is also OK to extract weights data from the web-friendly format instead of converting it. Is there a good way?
Or, is it impossible to release the original weights data before conversion?

I think PoseNet and BodyPix are good methods that can detect human pose at a high speed and they can be used for various purposes. But Javascript is not suitable for my project at all.

I have the same problem. Both of them can be useful on various platform but tfjs handcuffs their capabilities.

@darcula1993 , I studied the tfjs web-freiendly format a little. Maybe we can get the weight data in the following way.

  • Combine all binaries (group1-shard*.bin) into one file.
  • Extract data from the binary in the order described in the "weightsManifest" node in the model.json.

For example, if a layer shape is (3, 3, 128, 1) and dtype is float, we need to extract 3 * 3 * 128 * 1 * sizeof(float) = 4608 Bytes from the binary.
I think the binary does not contain any extra padding and header data. It seems to be packed with only the weight data.

In addition, we may construct the graph using the "modelTopology" node in the model.json. I'm trying it now.
If the tfjs developpers release a reverse conversion tool, I don't have to do that...

@darcula1993 , I studied the tfjs web-freiendly format a little. Maybe we can get the weight data in the following way.

  • Combine all binaries (group1-shard*.bin) into one file.
  • Extract data from the binary in the order described in the "weightsManifest" node in the model.json.

For example, if a layer shape is (3, 3, 128, 1) and dtype is float, we need to extract 3 * 3 * 128 * 1 * sizeof(float) = 4608 Bytes from the binary.
I think the binary does not contain any extra padding and header data. It seems to be packed with only the weight data.

In addition, we may construct the graph using the "modelTopology" node in the model.json. I'm trying it now.
If the tfjs developpers release a reverse conversion tool, I don't have to do that...

thank you for the info! @take-kuma.
I am also digging into the structure, seems it is not that hard. So the only thing different from previous model weights format is that they combine all layer weights into one .bin file?

I faced the same problem and wrote a tfjs to tf converter if anyone is still interested I can create a repo for it and post a link to it here.

I faced the same problem and wrote a tfjs to tf converter if anyone is still interested I can create a repo for it and post a link to it here.

Great, I'm interested.

I faced the same problem and wrote a tfjs to tf converter if anyone is still interested I can create a repo for it and post a link to it here.

Yes! I am also interested!

I faced the same problem and wrote a tfjs to tf converter if anyone is still interested I can create a repo for it and post a link to it here.

Yes! I am also interested!

Here you go: https://github.com/patlevin/tfjs-to-tf

I faced the same problem and wrote a tfjs to tf converter if anyone is still interested I can create a repo for it and post a link to it here.

Yes! I am also interested!

Here you go: https://github.com/patlevin/tfjs-to-tf

Thank you very much! It works very well!

closing this issue due to lack of activity , feel free to reopen. Thank you.