Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@tensorflow/tfjs-converter
Advanced tools
@tensorflow/tfjs-converter is a library that allows you to import TensorFlow SavedModel and Keras models into TensorFlow.js. This enables you to run pre-trained models in the browser or in Node.js, making it easier to deploy machine learning models in web applications.
Import TensorFlow SavedModel
This feature allows you to import a TensorFlow SavedModel into TensorFlow.js. The code sample demonstrates how to load a SavedModel from a specified path and log the model to the console.
const tf = require('@tensorflow/tfjs');
const tfConverter = require('@tensorflow/tfjs-converter');
async function loadModel() {
const model = await tfConverter.loadGraphModel('path/to/saved_model');
console.log(model);
}
loadModel();
Import Keras Model
This feature allows you to import a Keras model into TensorFlow.js. The code sample demonstrates how to load a Keras model from a specified JSON file and log the model to the console.
const tf = require('@tensorflow/tfjs');
const tfConverter = require('@tensorflow/tfjs-converter');
async function loadKerasModel() {
const model = await tfConverter.loadLayersModel('path/to/keras_model.json');
console.log(model);
}
loadKerasModel();
Run Inference
This feature allows you to run inference using a loaded model. The code sample demonstrates how to load a SavedModel, create a tensor as input, run the model's predict function, and print the output.
const tf = require('@tensorflow/tfjs');
const tfConverter = require('@tensorflow/tfjs-converter');
async function runInference() {
const model = await tfConverter.loadGraphModel('path/to/saved_model');
const input = tf.tensor([1, 2, 3, 4]);
const output = model.predict(input);
output.print();
}
runInference();
@tensorflow/tfjs is the core TensorFlow.js library that provides the main functionalities for defining, training, and running machine learning models in the browser and Node.js. While @tensorflow/tfjs-converter focuses on importing pre-trained models, @tensorflow/tfjs provides the tools to build and train models from scratch.
onnxjs is a library for running ONNX (Open Neural Network Exchange) models in the browser and Node.js. It provides similar functionalities to @tensorflow/tfjs-converter but focuses on the ONNX model format instead of TensorFlow SavedModel and Keras models.
brain.js is a JavaScript library for neural networks that runs in the browser and Node.js. It provides functionalities for creating, training, and running neural networks, but it does not support importing TensorFlow or Keras models like @tensorflow/tfjs-converter.
TensorFlow.js converter is an open source library to load a pretrained TensorFlow SavedModel, Frozen Model, Session Bundle or TensorFlow Hub module into the browser and run inference through TensorFlow.js.
(Note: TensorFlow has deprecated session bundle format, please switch to SavedModel.)
A 2-step process to import your model:
$ pip install tensorflowjs
Usage:
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' \
--saved_model_tags=serve \
/mobilenet/frozen_model.pb \
/mobilenet/web_model
Session bundle model example:
$ tensorflowjs_converter \
--input_format=tf_session_bundle \
--output_node_names='MobilenetV1/Predictions/Reshape_1' \
/mobilenet/session_bundle \
/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
Keras h5 model example:
$ tensorflowjs_converter \
--input_format=keras \
/tmp/my_keras_model.h5 \
/tmp/my_tfjs_model
tf.keras SavedModel model example:
$ tensorflowjs_converter \
--input_format=keras_saved_model \
/tmp/my_tf_keras_saved_model/1542211770 \
/tmp/my_tfjs_model
Note that the input path used above is a subfolder that has a Unix epoch time (1542211770) and is generated automatically by tensorflow when it saved a tf.keras model in the SavedModel format.
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, tensorflowjs for TensorFlow.js JSON format, and keras for Keras HDF5. |
--output_node_names | The names of the output nodes, separated by commas. |
--output_format | The desired output format. Must be tensorflowjs (the default) or keras . Not all pairs of input-output formats are supported. Please file a github issue if your desired input-output pair is not supported. |
--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/. |
--strip_debug_ops | Strips out TensorFlow debug operations Print , Assert , CheckNumerics . Defaults to True . |
input format | output tensorflowjs | output keras |
---|---|---|
keras | :heavy_check_mark: | :x: |
tensorflowjs | :x: | :heavy_check_mark: |
tf_frozen_model | :heavy_check_mark: | :x: |
tf_hub | :heavy_check_mark: | :x: |
tf_saved_model | :heavy_check_mark: | :x: |
tf_session_bundle | :heavy_check_mark: | :x: |
The conversion script above produces 4 types of files:
tensorflowjs_model.pb
(the dataflow graph)weights_manifest.json
(weight manifest file)model.json
(the two above, in a single file)group1-shard\*of\*
(collection of binary weight files)For keras
input files, the converter generates model.json
and group1-shard\*of\*
.
For other input formats, it generates the tensorflowjs_model.pb
, weights_manifest.json
, and group1-shard\*of\*
.
For example, here is the MobileNet model converted and served in following location:
https://storage.cloud.google.com/tfjs-models/savedmodel/mobilenet_v1_1.0_224/optimized_model.pb
https://storage.cloud.google.com/tfjs-models/savedmodel/mobilenet_v1_1.0_224/weights_manifest.json
https://storage.cloud.google.com/tfjs-models/savedmodel/mobilenet_v1_1.0_224/group1-shard1of5
...
https://storage.cloud.google.com/tfjs-models/savedmodel/mobilenet_v1_1.0_224/group1-shard5of5
Instantiate the FrozenModel class and run inference.
import * as tf from '@tensorflow/tfjs';
const MODEL_URL = 'https://.../mobilenet/tensorflowjs_model.pb';
const WEIGHTS_URL = 'https://.../mobilenet/weights_manifest.json';
const model = await tf.loadFrozenModel(MODEL_URL, WEIGHTS_URL);
const cat = document.getElementById('cat');
model.execute({input: tf.fromPixels(cat)});
Check out our working MobileNet demo.
If your server requests credentials for accessing the model files, you can provide the optional RequestOption param.
const model = await loadFrozenModel(MODEL_URL, WEIGHTS_URL,
{credentials: 'include'});
Please see fetch() documentation for details.
TensorFlow.js can be used from Node.js. See
the tfjs-node project for more details.
Unlike web browsers, Node.js can access the local file system directly.
Therefore, you can load the same frozen model from local file system into
a Node.js program running TensorFlow.js. This is done by calling loadFrozenModel
with the path
to the model files:
// Load the tfjs-node binding
import '@tensorflow/tfjs-node';
const MODEL_PATH = 'file:///tmp/mobilenet/tensorflowjs_model.pb';
const WEIGHTS_PATH = 'file:///tmp/mobilenet/weights_manifest.json';
const model = await tf.loadFrozenModel(MODEL_PATH, WEIGHTS_PATH);
You can also load the remote model files the same way as in browser, but you might need to polyfill the fetch() method.
Currently TensorFlow.js only supports a limited set of TensorFlow Ops. See the
full list.
If your model uses an unsupported ops, the tensorflowjs_converter
script will fail and
produce a list of the unsupported ops in your model. Please file issues to let us
know what ops you need support with.
If you prefer to load the weights only, you can use follow 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");
Image-based models (MobileNet, SqueezeNet, add more if you tested) are the most supported. Models with control flow ops (e.g. RNNs) are not yet supported. The tensorflowjs_converter script will validate the model you have and show a list of unsupported ops in your model. See this list for which ops are currently supported.
While the browser supports loading 100-500MB models, the page load time, the inference time and the user experience would not be great. We recommend using models that are designed for edge devices (e.g. phones). These models are usually smaller than 30MB.
Yes, we are splitting the weights into files of 4MB chunks, which enable the browser to cache them automatically. If the model architecture is less than 4MB (most models are), it will also be cached.
Not yet. We are planning to add quantization support soon.
The time of first call also includes the compilation time of WebGL shader programs for the model. After the first call the shader programs are cached, which makes the subsequent calls much faster. You can warm up the cache by calling the predict method with an all zero inputs, right after the completion of the model loading.
To build TensorFlow.js converter from source, we need to clone the project and prepare the dev environment:
$ git clone https://github.com/tensorflow/tfjs-converter.git
$ cd tfjs-converter
$ yarn # Installs dependencies.
We recommend using Visual Studio Code for
development. Make sure to install
TSLint VSCode extension
and the npm clang-format 1.2.2
or later
with the
Clang-Format VSCode extension
for auto-formatting.
Before submitting a pull request, make sure the code passes all the tests and is clean of lint errors:
$ yarn test
$ yarn lint
To run a subset of tests and/or on a specific browser:
$ yarn test --browsers=Chrome --grep='execute'
> ...
> Chrome 64.0.3282 (Linux 0.0.0): Executed 39 of 39 SUCCESS (0.129 secs / 0 secs)
To run the tests once and exit the karma process (helpful on Windows):
$ yarn test --single-run
To generate the static js file for GraphDef proto, run following steps:
$ node_modules/protobufjs/bin/pbjs -t static-module -w commonjs -o src/data/compiled_api.js --no-create --no-encode --no-verify --no-convert --no-delimited --no-beautify src/data/api.proto
$ node_modules/protobufjs/bin/pbts -o src/data/compiled_api.d.ts src/data/compiled_api.js
$ node_modules/protobufjs/bin/pbjs -t static-module -w commonjs -o src/data/compiled_api.js --no-create --no-encode --no-verify --no-convert --no-delimited --no-beautify --no-comments src/data/api.proto
FAQs
Tensorflow model converter for javascript
The npm package @tensorflow/tfjs-converter receives a total of 165,579 weekly downloads. As such, @tensorflow/tfjs-converter popularity was classified as popular.
We found that @tensorflow/tfjs-converter demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 10 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.