{"id":15634541,"url":"https://github.com/patlevin/tfjs-to-tf","last_synced_at":"2025-04-07T18:14:59.599Z","repository":{"id":49775717,"uuid":"226402530","full_name":"patlevin/tfjs-to-tf","owner":"patlevin","description":"A TensorFlow.js Graph Model Converter","archived":false,"fork":false,"pushed_at":"2023-01-22T10:14:08.000Z","size":962,"stargazers_count":141,"open_issues_count":5,"forks_count":20,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-31T16:21:37.587Z","etag":null,"topics":["converter","python","tensorflow","tensorflowjs-models"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/patlevin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-12-06T20:14:29.000Z","updated_at":"2025-03-26T03:49:38.000Z","dependencies_parsed_at":"2023-01-31T05:16:18.896Z","dependency_job_id":null,"html_url":"https://github.com/patlevin/tfjs-to-tf","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patlevin%2Ftfjs-to-tf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patlevin%2Ftfjs-to-tf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patlevin%2Ftfjs-to-tf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patlevin%2Ftfjs-to-tf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/patlevin","download_url":"https://codeload.github.com/patlevin/tfjs-to-tf/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247704571,"owners_count":20982298,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["converter","python","tensorflow","tensorflowjs-models"],"created_at":"2024-10-03T10:53:56.181Z","updated_at":"2025-04-07T18:14:59.566Z","avatar_url":"https://github.com/patlevin.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TensorFlow.js Graph Model Converter\r\n\r\n![TFJS Graph Converter Logo](https://github.com/patlevin/tfjs-to-tf/raw/master/docs/logo.png)\r\n\r\nThe purpose of this library is to import TFJS graph models into Tensorflow.\r\nThis allows you to use TensorFlow.js models with Python in case you don't\r\nhave access to the original formats or the models have been created in TFJS.\r\n\r\n## Disclaimer\r\n\r\nI'm neither a Python developer, nor do I know TensorFlow or TensorFlow.js.\r\nI created this package solely because I ran into an issue when trying to convert\r\na pretrained TensorFlow.js model into a different format. I didn't have access to\r\nthe pretrained original TF model and didn't have the resources to train it myself.\r\nI soon learned that I'm not alone with this [issue](https://github.com/tensorflow/tfjs/issues/1575)\r\nso I sat down and wrote this little library.\r\n\r\nIf you find any part of the code to be non-idiomatic or know of a simpler way to\r\nachieve certain things, feel free to let me know, since I'm a beginner in both\r\nPython and especially TensorFlow (used it for the very first time in this\r\nvery project).\r\n\r\n## Prerequisites\r\n\r\n* tensorflow 2.1+\r\n* tensorflowjs 1.5.2+\r\n\r\n## Compatibility\r\n\r\nThe converter has been tested with tensorflowjs v3.13.0, tensorflow v2.8.0\r\nand Python 3.9.10.\r\n\r\n## Installation\r\n\r\n```sh\r\npip install tfjs-graph-converter\r\n```\r\n\r\n## Usage\r\n\r\nAfter the installation, you can run the packaged `tfjs_graph_converter` binary\r\nfor quick and easy model conversion.\r\n\r\n### Positional Arguments\r\n\r\n | Positional Argument | Description |\r\n | :--- | :--- |\r\n | `input_path` | Path to the TFJS Graph Model directory containing the model.json |\r\n | `output_path` | For output format \"tf_saved_model\", a SavedModel target directory. For output format \"tf_frozen_model\", a frozen model file. |\r\n\r\n### Options\r\n\r\n| Option | Description |\r\n| :--- | :--- |\r\n| `-h`, `--help` | Show help message and exit |\r\n| `--output_format` | Use `tf_frozen_model` (the default) to save a Tensorflow frozen model. `tf_saved_model` exports to a Tensorflow _SavedModel_ instead. |\r\n| `--saved_model_tags` | Specifies the tags of the MetaGraphDef to save, in comma separated string format. Defaults to \"serve\". Applicable only if `--output_format` is `tf_saved_model` |\r\n| `-c`, `--compat_mode` | Sets a compatibility mode forthe coverted model (see below) |\r\n| `-v`, `--version` | Shows the version of the converter and its dependencies. |\r\n| `-s`, `--silent` | Suppresses any output besides error messages. |\r\n\r\n### Compatibility Modes\r\n\r\nModels are converted to optmimised native Tensorflow operators by default.\r\nThis can cause problems if the converted model is subseuently converted to\r\nanother format (ONNX, TFLite, older TFJS versions, etc.) The `--compat_mode`\r\noption can be used to avoid incompatible native operations such as fused\r\nconvolutions. Available options are:\r\n\r\n| Mode     | Description |\r\n| :---     | :---        |\r\n| `none`   | Use all available optimisations and native TF operators |\r\n| `tfjs`   | Harmonise input types for compatibility with older TFJS versions |\r\n| `tflite` | Only use TFLite builtins in the converted model |\r\n\r\n### Advanced Options\r\n\r\nThese options are intended for advanced users who are familiar with the details of TensorFlow and [TensorFlow Serving](https://www.tensorflow.org/tfx/guide/serving).\r\n\r\n| Option | Description | Example |\r\n| :--- | :--- | :--- |\r\n| `--outputs` | Specifies the outputs of the MetaGraphDef to save, in comma separated string format. Applicable only if `--output_format` is `tf_saved_model` | --outputs=Identity |\r\n| `--signature_key` | Specifies the key for the signature of the MetraGraphDef. Applicable only if `--output_format` is `tf_saved_model`. Requires `--outputs` to be set. | --signature_key=serving_autoencode |\r\n| `--method_name` | Specifies the method name for the signature of the MetraGraphDef. Applicable only if `--output_format` is `tf_saved_model`. Requires `--outputs` to be set. | --method_name=tensorflow/serving/classify |\r\n| `--rename` | 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 `--output_format` is `tf_saved_model`. Requires `--outputs` to be set. | --rename Identity:scores,model/dense256/BiasAdd:confidence |\r\n\r\nSpecifying ``--outputs`` can be useful for multi-head models to select the default\r\noutput for the main signature. The CLI only handles the default signature of\r\nthe model. Multiple signatures can be created using the [API](https://github.com/patlevin/tfjs-to-tf/blob/master/docs/api.rst).\r\n\r\nThe method name must be handled with care, since setting the wrong value might\r\nprevent the signature from being valid for use with TensorFlow Serving.\r\nThe option is available, because the converter only generates\r\n_predict_-signatures. In case the model is a regression model or a classifier\r\nwith the matching outputs, the correct method name can be forced using the\r\n``--method_name`` option.\r\n\r\nAlternatively, you can create your own converter programs using the module's API.\r\nThe API is required to accomplish more complicated tasks, like packaging multiple\r\nTensorFlow.js models into a single SavedModel.\r\n\r\n## Example\r\n\r\nTo convert a TensorFlow.js graph model to a TensorFlow frozen model (i.e. the\r\nmost common use case?), just specify the directory containing the `model.json`,\r\nfollowed by the path and file name of the frozen model like so:\r\n\r\n```sh\r\ntfjs_graph_converter path/to/js/model path/to/frozen/model.pb\r\n```\r\n\r\n## Advanced Example\r\n\r\nConverting to [TF SavedMovel format](https://www.tensorflow.org/guide/saved_model)\r\nadds a lot of options for tweaking model signatures. The following example\r\nconverts a [Posenet](https://github.com/tensorflow/tfjs-models/tree/master/posenet)\r\nmodel, which is a multi-head model.\r\n\r\nWe want to select only two of the four possible outputs and rename them in the\r\nmodel's signature, as follows:\r\n\r\n* Input: _input_ (from _sub_2_)\r\n* Outputs: _offsets_ and _heatmaps_ (from _float_short_offsets_ and _float_heatmaps_)\r\n\r\n```sh\r\ntfjs_graph_converter \\\r\n    ~/models/posenet/model-stride16.json \\\r\n    ~/models/posenet_savedmodel \\\r\n    --output_format tf_saved_model \\\r\n    --outputs float_short_offsets,float_heatmaps \\\r\n    --rename float_short_offsets:offsets,float_heatmaps:heatmaps,sub_2:input\r\n```\r\n\r\nAfter the conversion, we can examine the output and verify the new model\r\nsignature:\r\n\r\n```sh\r\nsaved_model_cli show --dir ~/models/posenet_savedmodel --all\r\n\r\nMetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:\r\n\r\nsignature_def['serving_default']:\r\n  The given SavedModel SignatureDef contains the following input(s):\r\n    inputs['input'] tensor_info:\r\n        dtype: DT_FLOAT\r\n        shape: (1, -1, -1, 3)\r\n        name: sub_2:0\r\n  The given SavedModel SignatureDef contains the following output(s):\r\n    outputs['heatmaps'] tensor_info:\r\n        dtype: DT_FLOAT\r\n        shape: (1, -1, -1, 17)\r\n        name: float_heatmaps:0\r\n    outputs['offsets'] tensor_info:\r\n        dtype: DT_FLOAT\r\n        shape: (1, -1, -1, 34)\r\n        name: float_short_offsets:0\r\n  Method name is: tensorflow/serving/predict\r\n```\r\n\r\n## Usage from within Python\r\n\r\nThe package installs the module `tfjs_graph_converter`, which contains all the\r\nfunctionality used by the converter script.\r\nYou can leverage the API to either load TensorFlow.js graph models directly for\r\nuse with your TensorFlow program (e.g. for inference, fine-tuning, or extending),\r\nor use the advanced functionality to combine several TFJS models into a single\r\n`SavedModel`.\r\nThe latter is only supported using the API (it's just a single function call,\r\nthough, so don't panic 😉)\r\n\r\n## Important Note\r\n\r\nBy default, Python code that includes the library will see CUDA devices\r\ndisabled (i.e. not visible in the program). This is done because the library\r\nuses some low-level APIs that don't allow disabling GPUs from Python code.\r\nUnfortunately some GPUs support CUDA but don't have the compute capabilities or\r\nVRAM required to convert certain models. For this reason, CUDA devices are\r\ndisabled by default and the converter and scripts using it use CPUs only.\r\n\r\nThis behaviour can now be disabled by calling `enable_cuda()` **before** any\r\nTensorflow or converter function is called. This will re-enable the use of\r\nCUDA-capable devices, but may result in errors during model\r\nloading/conversion depending on the installed GPU hardware.\r\n\r\n[API Documentation](./docs/modules.rst)\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatlevin%2Ftfjs-to-tf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpatlevin%2Ftfjs-to-tf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatlevin%2Ftfjs-to-tf/lists"}