无法使用由TensorFlowJS转换器创建的模型

时间:2019-09-13 09:10:04

标签: tensorflow.js tensorflowjs-converter

我正在尝试重用由tensorflow创建的tensorflowjs模型。为了了解转换器的工作原理,我尝试了转换mobilenetv2模型:

tensorflowjs_converter --input_format=tf_hub --output_format=tensorflowjs   'https://tfhub.dev/google/imagenet/mobilenet_v2_050_224/classification/2' ./web_model

这似乎可行。然后,我尝试通过更改模型的加载方式在mobilenet demo中使用这个新的转换模型:

// const model = await mobilenet.load({version, alpha});
// replaced by
const model = await mobilenet.load({ modelUrl: './web_model/model.json', version, alpha, inputRange: [0, 1], fromTFHub: true });

// Classify the image.
const predictions = await model.classify(img);

分类调用触发错误:

Uncaught (in promise) Error: Activation relu6 has not been implemented for the WebGL backend.

我不知道如何生成正式的tensorflowjs mobilenet模型:(

3 个答案:

答案 0 :(得分:0)

from keras.applications import MobileNetV2
model = MobileNetV2(weights='imagenet', include_top=False)

save_model(
    model,
    "mobilenet2.h5",
    overwrite=True,
)

将mobilenet功能提取器转换为js

tensorflowjs_converter --input_format keras \
                       path/to/mobilenet2.h5 \
                       path/to/tfjs_target_dir

答案 1 :(得分:0)

relu6的运算符刚刚在1周前添加。它应该在下一个TensorFlow.js版本中可用。

发布后,请尝试使用最新版本。

请参阅:https://github.com/tensorflow/tfjs/pull/2016

答案 2 :(得分:0)

此问题与新版本无关。我遇到了同样的问题,转了一圈。如果您在GPU运行时中工作(我使用了Colab GPU运行时),则会发生此问题。您只需要在CPU模式下拟合/拟合模型,模型就可以在快乐状态下准备就绪。