TF.js 使用 TF Lite Model Maker 创建的模型导入错误

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

我使用 https://www.tensorflow.org/lite/tutorials/model_maker_image_classification 上的教程创建了一个模型,并以 TF.js 格式导出:

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)

使用 tf.loadLayersModel 在 TF.js 中加载此模型时出现以下错误:

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()

我猜错误是由于原因(1)引起的,但是如何将 HubKerasLayerV1V2 层移植到 TF.js?

我认为这是模型转换器的问题,图层模型中的部分图形存在问题。

您可能可以通过将模型序列化为正常 SaveModel format and export the HDF5. Once you have the .h5 output, use the TensorFlow.js converter (tensorflowjs_converter) 来创建纯图形模型来解决此问题。然后尝试使用 tf.loadGraphModel 加载。