How to deploy custom tensorflow model to web?

Viewed 220

so im facing a problem about deployment my custom sign-language recognition model. I converted my_ssd_mobnet with exporter_main_v2.py to saved_model.pb and then i tried to use the tensorflowjs convertor with this code:

from tensorflow import keras
import tensorflowjs as tfjs

def importModel(modelPath):
    model = tf.keras.models.load_model(modelPath)
    tfjs.converters.save_tf_model(model, "tfjsmodel")

importModel("saved_model")
#importModel("modelDirectory")

then i got an error like this.. ValueError: Unable to create a Keras model from this SavedModel. This SavedModel was created with tf.saved_model.save, and lacks the Keras metadata.Please save your Keras model by calling model.saveor tf.keras.models.save_model.

Finally i decide to convert my model to h5, but.. i don't know how. How can i convert my_ssd_mobnet model to h5? Thanks!

2 Answers

First and foremost, if you have used "exporter_main_v2.py" script to export the model, you will only get the model format in tensorflow model. This way of exporting is mainly used to make inference on the trained model. So the main problem in your code is that you are trying to import a "keras model" with that tf.keras.models.load_model() function. Instead of using "exporter_main_v2.py" you have to use tf.keras.models.save_model() function to export/save your model.

I am also giving you a simple video explanation link to clarify a few things for you

https://www.youtube.com/watch?v=Lx7OCFXPG8o

After watching the video you might want to checkout the following colab notebook

https://colab.research.google.com/github/tensorflow/examples/blob/master/courses/udacity_intro_to_tensorflow_for_deep_learning/l07c01_saving_and_loading_models.ipynb

This is a material provided by Udacity from its introduction to tensorflow training course. That should be very helpful in your case to understand the difference between tensorflow model file and keras model file.

Have a nice day.

Edit:

HDF5 format Keras provides a basic save format using the HDF5 standard.

Create and train a new model instance. model = create_model() model.fit(train_images, train_labels, epochs=5)

Save the entire model to a HDF5 file. The '.h5' extension indicates that the model should be saved to HDF5. model.save('my_model.h5')

You should add '.h5' extension to filename when calling model.save function, by this way the model will be saved in h5 format.

If you're creating a custom Keras layer in python and wanting to export it to tfjs for the browser to predict, then you'll most likely encounter "Unknown layer" and will have to implement them yourself in JS.

Instead of exporting the layers, it's best to export a graph since you're only using it for prediction and not training in the browser.

tf.saved_model.save(model, 'saved_model')

This will save the files in the saved_model folder and contains the .pb file.

Use the tensorflowjs_converter tool to convert the model into a graph tfjs model.

tensorflow_converter --input_format=tf_saved_model saved_model model

This will convert your saved model into the browser-compatible tfjs model without the custom layer. (The Keras layers will be built in.) Move this folder to your website's public folder.

In the browser:

const model = await tf.loadGraphModel('/model/model.json')
const img = tf.browser.fromPixels(imageData, 3) // imageElement, videoElement, ImageData
              .toFloat().resizeBilinear([224, 224]) // mobilenet dims
              .div(tf.scalar(255)) // mobilenet [0,1] normalization
              .expandDims()
const { values, indices } = model.predict(img).topk()
const label = indices.dataSync()[0]
const confidence = values.dataSync()[0]

NOTE: The .bin files will end up in the 10's of MB so put this inside a webworker. You can send a buffered data from the main thread to the worker thread for processing.

Related