TF.js import error with model created using TF Lite Model Maker

Viewed 331

I've created a model using the tutorial at https://www.tensorflow.org/lite/tutorials/model_maker_image_classification and exported it in the TF.js format:

import os

import matplotlib.pyplot as plt
import tensorflow as tf
from tflite_model_maker import image_classifier, model_spec
from tflite_model_maker.config import ExportFormat, QuantizationConfig
from tflite_model_maker.image_classifier import DataLoader

image_path = tf.keras.utils.get_file(
      'flower_photos.tgz',
      'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz',
      extract=True)
image_path = os.path.join(os.path.dirname(image_path), 'flower_photos')
data = DataLoader.from_folder(image_path)
train_data, test_data = data.split(0.9)
model = image_classifier.create(train_data)
loss, accuracy = model.evaluate(test_data)

# Export model to TF.js format
model.export(export_dir='.', export_format=ExportFormat.TFJS)

When loading this model in TF.js using tf.loadLayersModel I get the following error:

Uncaught (in promise) Error: Unknown layer: HubKerasLayerV1V2. 
This may be due to one of the following reasons:
1. The layer is defined in Python, in which case it needs to be
ported to TensorFlow.js or your JavaScript code.
2. The custom layer is defined in JavaScript, but is not registered
properly with tf.serialization.registerClass()

I guess the error is due to reason (1), but how can I port the HubKerasLayerV1V2 layer to TF.js?

1 Answers

I believe this is an issue with the model converter having issues with a partial Graph inside of a Layers model.

You can probably fix this by serializing the model to the normal SaveModel format and export the HDF5. Once you have the .h5 output, use the TensorFlow.js converter (tensorflowjs_converter) to create a purely Graph model. Then try loading with tf.loadGraphModel instead.

Related