The purpose of this library is to import TFJS graph models into Tensorflow. This allows you to use TensorFlow.js models with Python in case you don't have access to the original formats or the models have been created in TFJS.
I'm neither a Python developer, nor do I know TensorFlow or TensorFlow.js. I created this package solely because I ran into an issue when trying to convert a pretrained TensorFlow.js model into a different format. I didn't have access to the pretrained original TF model and didn't have the resources to train it myself. I soon learned that I'm not alone with this issue so I sat down and wrote this little library.
If you find any part of the code to be non-idiomatic or know of a simpler way to achieve certain things, feel free to let me know, since I'm a beginner in both Python and especially TensorFlow (used it for the very first time in this very project).
The converter has been tested with tensorflowjs v1.7.2/v2.0.1 and tensorflow v2.1/v2.3. The Python version used was Python 3.7.7.
pip install tfjs-graph-converter
After the installation, you can run the packaged
for quick and easy model conversion.
|Path to the TFJS Graph Model directory containing the model.json|
|For output format "tf_saved_model", a SavedModel target directory. For output format "tf_frozen_model", a frozen model file.|
|Show help message and exit|
|Specifies the tags of the MetaGraphDef to save, in comma separated string format. Defaults to "serve". Applicable only if |
|Keep the input types compatible with TensorflowJS <=2.4.x|
|Shows the version of the converter and its dependencies.|
|Suppresses any output besides error messages.|
These options are intended for advanced users who are familiar with the details of TensorFlow and TensorFlow Serving.
|Specifies the outputs of the MetaGraphDef to save, in comma separated string format. Applicable only if ||--outputs=Identity|
|Specifies the key for the signature of the MetraGraphDef. Applicable only if ||--signature_key=serving_autoencode|
|Specifies the method name for the signature of the MetraGraphDef. Applicable only if ||--method_name=tensorflow/serving/classify|
|Specifies a key mapping to change the keys of outputs and inputs in the signature. The format is comma-separated pairs of old name:new name. Applicable only if ||--rename Identity:scores,model/dense256/BiasAdd:confidence|
--outputs can be useful for multi-head models to select the default
output for the main signature. The CLI only handles the default signature of
the model. Multiple signatures can be created using the API.
The method name must be handled with care, since setting the wrong value might
prevent the signature from being valid for use with TensorFlow Serving.
The option is available, because the converter only generates
predict-signatures. In case the model is a regression model or a classifier
with the matching outputs, the correct method name can be forced using the
Alternatively, you can create your own converter programs using the module's API. The API is required to accomplish more complicated tasks, like packaging multiple TensorFlow.js models into a single SavedModel.
To convert a TensorFlow.js graph model to a TensorFlow frozen model (i.e. the
most common use case?), just specify the directory containing the
followed by the path and file name of the frozen model like so:
tfjs_graph_converter path/to/js/model path/to/frozen/model.pb
We want to select only two of the four possible outputs and rename them in the model's signature, as follows:
tfjs_graph_converter \ ~/models/posenet/model-stride16.json \ ~/models/posenet_savedmodel \ --output_format tf_saved_model \ --outputs float_short_offsets,float_heatmaps \ --rename float_short_offsets:offsets,float_heatmaps:heatmaps,sub_2:input
After the conversion, we can examine the output and verify the new model signature:
saved_model_cli show --dir ~/models/posenet_savedmodel --all MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs: signature_def['serving_default']: The given SavedModel SignatureDef contains the following input(s): inputs['input'] tensor_info: dtype: DT_FLOAT shape: (1, -1, -1, 3) name: sub_2:0 The given SavedModel SignatureDef contains the following output(s): outputs['heatmaps'] tensor_info: dtype: DT_FLOAT shape: (1, -1, -1, 17) name: float_heatmaps:0 outputs['offsets'] tensor_info: dtype: DT_FLOAT shape: (1, -1, -1, 34) name: float_short_offsets:0 Method name is: tensorflow/serving/predict
The package installs the module
tfjs_graph_converter, which contains all the
functionality used by the converter script.
You can leverage the API to either load TensorFlow.js graph models directly for
use with your TensorFlow program (e.g. for inference, fine-tuning, or extending),
or use the advanced functionality to combine several TFJS models into a single
The latter is only supported using the API (it's just a single function call,
though, so don't panic 😉)