[PYTHON] [Tensorflowjs_converter] How to convert Tensorflow model to Tensorflow.js format

This article shows how to convert a model created with tensorflow to a format that can be used with Tnesorflow.js.

python environment

pip install tensorflowjs

tfjs-converter

Tensorflow.js (TF.js) can reuse existing models trained by Tensorflow (TF). tfjs-converter is a command line tool for converting TensorFlow models and supports various formats such as HDF5.

tensorflowjs_converter --help
usage: TensorFlow.js model converters. [-h]
                                       [--input_format {tensorflowjs,keras,tf_hub,keras_saved_model,tf_saved_model,tfjs_layers_model}]
                                      [--output_format {tfjs_graph_model,tfjs_layers_model,tensorflowjs,keras}]
                                       [--signature_name SIGNATURE_NAME]
                                       [--saved_model_tags SAVED_MODEL_TAGS]
                                       [--quantization_bytes {1,2}]
                                       [--split_weights_by_layer] [--version]
                                       [--skip_op_check SKIP_OP_CHECK]
                                       [--strip_debug_ops STRIP_DEBUG_OPS]
                                       [--weight_shard_size_bytes WEIGHT_SHARD_SIZE_BYTES]
                                       [input_path] [output_path]

You can convert TF's Saved Model to a Web format that can be read by TF.js with the following command.

tensorflowjs_converter \
    --input_format=tf_saved_model \
    /path/to/saved_model \
    /path/to/web_model

For --input_format, other formats such as keras, keras_saved_model, tf_hub can be selected.

portable format

It can be reused in a different location than where the model is trained. feature is,

The files generated by Tensorflow are based on protocol buffers. Therefore, it is a readable format in many programming languages. In addition, formats such as ONNX are also platform-independent formats. (Can also be used with pytorch etc.)

Save model

How to save a model in Tensorflow (Python).

mport tensorflow as tf
from tensorflow import keras

print(tf.__version__) #2.1

(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()

train_labels = train_labels[:1000]
test_labels = test_labels[:1000]

train_images = train_images[:1000].reshape(-1, 28 * 28) / 255.0
test_images = test_images[:1000].reshape(-1, 28 * 28) / 255.0

#A function that returns a short sequential model
def create_model():
  model = tf.keras.models.Sequential([
    keras.layers.Dense(512, activation='relu', input_shape=(784,)),
    keras.layers.Dropout(0.2),
    keras.layers.Dense(10, activation='softmax')
  ])
  
  model.compile(optimizer='adam', 
                loss='sparse_categorical_crossentropy',
                metrics=['accuracy'])
  
  return model

#Create an instance of the basic model
model = create_model()
model.summary()

model.fit(train_images, train_labels, epochs=5)

#Save the entire model in one HDF5 file.
model.save('my_model.h5')
tf.saved_model.save(model, "./sample/model_data")
#imported = tf.saved_model.load("./sample/model_data")

Model transformation

Convert the TF model saved in ./sample/model_data to the TF.js format model.

tensorflowjs_converter --input_format=tf_saved_model --output_node_names=output ./sample/model_data ./sample/model_tfjs_model

#hd5 data format
tensorflowjs_converter --input_format keras my_model.h5 ./sample/hd5_model

#tf-Use hub model
tensorflowjs_converter \
    --input_format=tf_hub \
    'https://tfhub.dev/google/imagenet/mobilenet_v1_100_224/classification/1' \
    ./my_tfjs_model

What is TF-hub?

Tensorflow Hub is a library that can reuse machine learning models and so on. You can use state-of-the-art machine learning technology by loading the model and matching the input and output formats.

Model loading

How to load a model in Tensorflow.js.

import * as tf from '@tensorflow/tfjs';

const MODEL_URL = 'https://path/to/model.json';
const model = await tf.loadGraphModel(MODEL_URL);

// Or

const MODEL_PATH = 'file://path/to/model.json';
const model = await tf.loadGraphModel(MODEL_PATH);

Recommended Posts

[Tensorflowjs_converter] How to convert Tensorflow model to Tensorflow.js format
How to convert Tensorflow model to Lite
How to easily convert format from Markdown
How to convert DateTimeField format in Django
How to convert Json file to CSV format or EXCEL format
How to convert 0.5 to 1056964608 in one shot
How to install TensorFlow on CentOS 7
How to convert from .mgz to .nii.gz
How to run TensorFlow 1.0 code in 2.0
[Python] How to convert db file to csv
Convert xml format data to txt format data (yolov3)
[Python] How to convert a 2D list to a 1D list
How to convert csv to tsv in CLI
How to convert Python to an exe file
Convert matplotlib graphs to emf file format
Convert PDF attached to email to text format
How to use tensorflow under docker environment
Convert Python date types to RFC822 format
[Python --open3d] How to convert obj data of 3D model to point cloud
How to convert m4a acquired by iTunes to wav
[Python] How to change the date format (display format)
How to run CNN in 1 system notation in Tensorflow 2
[Introduction to Python] How to handle JSON format data
How to convert SVG to PDF and PNG [Python]
Convert json format data to txt (using yolo)
Tensorflow, Tensorflow After all, which one (How to read Tensorflow)
How to convert (32,32,3) to 4D tensor (1,32,32,1) with ndarray type
[TF] How to build Tensorflow in Proxy environment
[PyTorch] Sample ⑧ ~ How to build a complex model ~
Learn how to inflate images from TensorFlow code
Convert binary packages for windows to wheel format
How to force build TensorFlow 2.3.0 for CUDA11 + cuDNN8
Convert strings to character-by-character list format with python
Convert to HSV
How to convert / restore a string with [] in python
How to get multiple model objects randomly in Django
How to convert Python # type for Python super beginners: str
How to use Keras ~ From simple model generation to CNN ~
How to convert horizontally held data to vertically held data with pandas
How to convert a class object to a dictionary with SQLAlchemy
How to visualize the decision tree model of scikit-learn
Convert Qiita articles to Jekyll post format for backup
How to use TensorFlow on GPUs less than Titan
How to share folders with Docker and Windows with tensorflow
How to convert floating point numbers to binary numbers in Python
I made a code to convert illustration2vec to keras model
How to convert JSON file to CSV file with Python Pandas
How to convert a mel spectrogram back to a wav file
Convert / return class object to JSON format in Python
How to build a development environment for TensorFlow (1.0.0) (Mac)
How to use the model learned in Lobe in Python
python, php, ruby How to convert decimal numbers to n-ary numbers
Convert translation resource files (.po) to XLIFF format (.xlf)
Convert Pascal VOC format xml file to COCO format json file
[How to!] Learn and play Super Mario with Tensorflow !!
How to use Spacy Japanese model in Google Colaboratory
[TF] How to save and load Tensorflow learning parameters