Import a TensorFlow model into TensorFlow.js Skip to main content Install Learn Introduction New to TensorFlow? Tutorials Learn how to use TensorFlow with end-to-end examples Guide Learn framework concepts and components Learn ML Educational resources to master your path with TensorFlow API TensorFlow (v2.16.1) Versions… TensorFlow.js TensorFlow Lite TFX Ecosystem LIBRARIES TensorFlow.js Develop web ML applications in JavaScript TensorFlow Lite Deploy ML on mobile, microcontrollers and other edge devices TFX Build production ML pipelines All libraries Create advanced models and extend TensorFlow RESOURCES Models & datasets Pre-trained models and datasets built by Google and the community Tools Tools to support and accelerate TensorFlow workflows Responsible AI Resources for every stage of the ML workflow Recommendation systems Build recommendation systems with open source tools Community Groups User groups, interest groups and mailing lists Contribute Guide for contributing to code and documentation Blog Stay up to date with all things TensorFlow Forum Discussion platform for the TensorFlow community Why TensorFlow About Case studies / English Español Español – América Latina Français Indonesia Italiano Polski Português Português – Brasil Tiếng Việt Türkçe Русский עברית العربيّة فارسی हिंदी বাংলা ภาษาไทย 中文 – 简体 日本語 한국어 GitHub Sign in For JavaScript Overview Tutorials Guide Models Demos API Install Learn More Overview Tutorials Guide Models Demos API API More Ecosystem More Community More Why TensorFlow More GitHub Get started Set up a TensorFlow.js project Upgrade to TensorFlow.js 3.0 Train models Fit a curve to two-dimensional data Recognize handwritten digits with CNNs Predict baseball pitch types in Node.js Train a model using a web worker Transfer learning What is transfer learning? Build an image classifier Build an audio recognizer Import Python models Use a pre-trained model Import a Keras model Import a TensorFlow SavedModel Deployment & Optimization Generating size-optimized browser bundles Deploy a Chrome extension Web ML applications Predictive prefetching React Native Introduction Tutorials Guide Learn ML TensorFlow (v2.16.1) Versions… TensorFlow.js TensorFlow Lite TFX LIBRARIES TensorFlow.js TensorFlow Lite TFX All libraries RESOURCES Models & datasets Tools Responsible AI Recommendation systems Groups Contribute Blog Forum About Case studies New to machine learning? Watch a video course to get practical working knowledge of ML using web technologiesView series TensorFlow Learn For JavaScript Tutorials Import a TensorFlow model into TensorFlow.js Stay organized with collections Save and categorize content based on your preferences. TensorFlow GraphDef-based models (typically created via the Python API) can be saved in one of following formats: TensorFlow SavedModel Frozen Model Tensorflow Hub module All of the above formats can be converted by the TensorFlow.js converter into a format that can be loaded directly into TensorFlow.js for inference. (Note: TensorFlow has deprecated the session bundle format. Please migrate your models to the SavedModel format.) Requirements The conversion procedure requires a Python environment; you may want to keep an isolated one using pipenv or virtualenv. To install the converter, run the following command: pip install tensorflowjs Importing a TensorFlow model into TensorFlow.js is a two-step process. First, convert an existing model to the TensorFlow.js web format, and then load it into TensorFlow.js. Step 1. Convert an existing TensorFlow model to the TensorFlow.js web format Run the converter script provided by the pip package: SavedModel example: tensorflowjs_converter \ --input_format=tf_saved_model \ --output_node_names='MobilenetV1/Predictions/Reshape_1' \ --saved_model_tags=serve \ /mobilenet/saved_model \ /mobilenet/web_model Frozen model example: tensorflowjs_converter \ --input_format=tf_frozen_model \ --output_node_names='MobilenetV1/Predictions/Reshape_1' \ /mobilenet/frozen_model.pb \ /mobilenet/web_model Tensorflow Hub module example: tensorflowjs_converter \ --input_format=tf_hub \ 'https://tfhub.dev/google/imagenet/mobilenet_v1_100_224/classification/1' \ /mobilenet/web_model Positional Arguments Description input_path Full path of the saved model directory, session bundle directory, frozen model file or TensorFlow Hub module handle or path. output_path Path for all output artifacts. Options Description --input_format The format of input model. Use tf_saved_model for SavedModel, tf_frozen_model for frozen model, tf_session_bundle for session bundle, tf_hub for TensorFlow Hub module and keras for Keras HDF5. --output_node_names The names of the output nodes, separated by commas. --saved_model_tags Only applicable to SavedModel conversion. Tags of the MetaGraphDef to load, in comma separated format. Defaults to serve. --signature_name Only applicable to TensorFlow Hub module conversion, signature to load. Defaults to default. See https://www.tensorflow.org/hub/common_signatures/ Use following command to get a detailed help message: tensorflowjs_converter --help Converter generated files The conversion script above produces two types of files: model.json: The dataflow graph and weight manifest group1-shard\*of\*: A collection of binary weight files For example, here is the output from converting MobileNet v2: output_directory/model.json output_directory/group1-shard1of5 ... output_directory/group1-shard5of5 Step 2: Loading and running in the browser Install the tfjs-converter npm package: yarn add @tensorflow/tfjs or npm install @tensorflow/tfjs Instantiate the FrozenModel class and run inference. import * as tf from '@tensorflow/tfjs'; import {loadGraphModel} from '@tensorflow/tfjs-converter'; const MODEL_URL = 'model_directory/model.json'; const model = await loadGraphModel(MODEL_URL); const cat = document.getElementById('cat'); model.execute(tf.browser.fromPixels(cat)); Check out the MobileNet demo. The loadGraphModel API accepts an additional LoadOptions parameter, which can be used to send credentials or custom headers along with the request. For details, see the loadGraphModel() documentation. Supported operations Currently TensorFlow.js supports a limited set of TensorFlow ops. If your model uses an unsupported op, the tensorflowjs_converter script will fail and print out a list of the unsupported ops in your model. Please file an issue for each op to let us know which ops you need support for. Loading the weights only If you prefer to load the weights only, you can use the following code snippet: import * as tf from '@tensorflow/tfjs'; const weightManifestUrl = "https://example.org/model/weights_manifest.json"; const manifest = await fetch(weightManifestUrl); this.weightManifest = await manifest.json(); const weightMap = await tf.io.loadWeights( this.weightManifest, "https://example.org/model"); // Use `weightMap` ... Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Last updated 2023-10-12 UTC. [[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-10-12 UTC."],[],[]] Stay connected Blog Forum GitHub Twitter YouTube Support Issue tracker Release notes Stack Overflow Brand guidelines Cite TensorFlow Terms Privacy Manage cookies Sign up for the TensorFlow newsletter Subscribe English Español Español – América Latina Français Indonesia Italiano Polski Português Português – Brasil Tiếng Việt Türkçe Русский עברית العربيّة فارسی हिंदी বাংলা ภาษาไทย 中文 – 简体 日本語 한국어