Package: tfjs-graph-converter
TensorFlow.js Graph Model ConverterThe 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.DisclaimerI'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 thisissueso 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).Prerequisitestensorflow 2.1+tensorflowjs 1.5.2+CompatibilityThe converter has been tested with tensorflowjs v3.13.0, tensorflow v2.8.0
and Python 3.9.10.Installationpipinstalltfjs-graph-converterUsageAfter the installation, you can run the packagedtfjs_graph_converterbinary
for quick and easy model conversion.Positional ArgumentsPositional ArgumentDescriptioninput_pathPath to the TFJS Graph Model directory containing the model.jsonoutput_pathFor output format "tf_saved_model", a SavedModel target directory. For output format "tf_frozen_model", a frozen model file.OptionsOptionDescription-h,--helpShow help message and exit--output_formatUsetf_frozen_model(the default) to save a Tensorflow frozen model.tf_saved_modelexports to a TensorflowSavedModelinstead.--saved_model_tagsSpecifies the tags of the MetaGraphDef to save, in comma separated string format. Defaults to "serve". Applicable only if--output_formatistf_saved_model-c,--compat_modeSets a compatibility mode forthe coverted model (see below)-v,--versionShows the version of the converter and its dependencies.-s,--silentSuppresses any output besides error messages.Compatibility ModesModels are converted to optmimised native Tensorflow operators by default.
This can cause problems if the converted model is subseuently converted to
another format (ONNX, TFLite, older TFJS versions, etc.) The--compat_modeoption can be used to avoid incompatible native operations such as fused
convolutions. Available options are:ModeDescriptionnoneUse all available optimisations and native TF operatorstfjsHarmonise input types for compatibility with older TFJS versionstfliteOnly use TFLite builtins in the converted modelAdvanced OptionsThese options are intended for advanced users who are familiar with the details of TensorFlow andTensorFlow Serving.OptionDescriptionExample--outputsSpecifies the outputs of the MetaGraphDef to save, in comma separated string format. Applicable only if--output_formatistf_saved_model--outputs=Identity--signature_keySpecifies the key for the signature of the MetraGraphDef. Applicable only if--output_formatistf_saved_model. Requires--outputsto be set.--signature_key=serving_autoencode--method_nameSpecifies the method name for the signature of the MetraGraphDef. Applicable only if--output_formatistf_saved_model. Requires--outputsto be set.--method_name=tensorflow/serving/classify--renameSpecifies a key mapping to change the keys of outputs and inputs in the signature. The format is comma-separated pairs ofold name:new name. Applicable only if--output_formatistf_saved_model. Requires--outputsto be set.--rename Identity:scores,model/dense256/BiasAdd:confidenceSpecifying--outputscan 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 theAPI.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 generatespredict-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--method_nameoption.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.ExampleTo convert a TensorFlow.js graph model to a TensorFlow frozen model (i.e. the
most common use case?), just specify the directory containing themodel.json,
followed by the path and file name of the frozen model like so:tfjs_graph_converterpath/to/js/modelpath/to/frozen/model.pbAdvanced ExampleConverting toTF SavedMovel formatadds a lot of options for tweaking model signatures. The following example
converts aPosenetmodel, which is a multi-head model.We want to select only two of the four possible outputs and rename them in the
model's signature, as follows:Input:input(fromsub_2)Outputs:offsetsandheatmaps(fromfloat_short_offsetsandfloat_heatmaps)tfjs_graph_converter\~/models/posenet/model-stride16.json\~/models/posenet_savedmodel\--output_formattf_saved_model\--outputsfloat_short_offsets,float_heatmaps\--renamefloat_short_offsets:offsets,float_heatmaps:heatmaps,sub_2:inputAfter the conversion, we can examine the output and verify the new model
signature:saved_model_clishow--dir~/models/posenet_savedmodel--all

MetaGraphDefwithtag-set:'serve'containsthefollowingSignatureDefs:

signature_def['serving_default']:ThegivenSavedModelSignatureDefcontainsthefollowinginput(s):inputs['input']tensor_info:dtype:DT_FLOATshape:(1,-1,-1,3)name:sub_2:0ThegivenSavedModelSignatureDefcontainsthefollowingoutput(s):outputs['heatmaps']tensor_info:dtype:DT_FLOATshape:(1,-1,-1,17)name:float_heatmaps:0outputs['offsets']tensor_info:dtype:DT_FLOATshape:(1,-1,-1,34)name:float_short_offsets:0Methodnameis:tensorflow/serving/predictUsage from within PythonThe package installs the moduletfjs_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 singleSavedModel.
The latter is only supported using the API (it's just a single function call,
though, so don't panic 😉)Important NoteBy default, Python code that includes the library will see CUDA devices
disabled (i.e. not visible in the program). This is done because the library
uses some low-level APIs that don't allow disabling GPUs from Python code.
Unfortunately some GPUs support CUDA but don't have the compute capabilities or
VRAM required to convert certain models. For this reason, CUDA devices are
disabled by default and the converter and scripts using it use CPUs only.This behaviour can now be disabled by callingenable_cuda()beforeany
Tensorflow or converter function is called. This will re-enable the use of
CUDA-capable devices, but may result in errors during model
loading/conversion depending on the installed GPU hardware.API Documentation