将retrain.py的输出转换为tensorflow.js

将retrain.py的输出转换为tensorflow.js

问题描述:

如何为新类别重新训练图像分类器中描述的脚本retrain.py 的运行方式为

The script retrain.py described in How to Retrain an Image Classifier for New Categories was run as

python retrain.py --tfhub_module https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/feature_vector/2 --image_dir /tmp/test

并生成输出文件/tmp/output_graph.pb. 使用

and produced the output file /tmp/output_graph.pb. Converting this with

tensorflowjs_converter --input_format=tf_saved_model --output_format=tfjs_graph_model /tmp/output_graph.pb /tmp/model

失败

IOError:以下位置不存在SavedModel文件:/tmp/output_graph.pb/{saved_model.pbtxt | saved_model.pb}

IOError: SavedModel file does not exist at: /tmp/output_graph.pb/{saved_model.pbtxt|saved_model.pb}

如果文件output_graph.pb重命名为saved_model.pb(通过@edkeveked 重新命名为),则错误更改为

If the file output_graph.pb is renamed to saved_model.pb (by @edkeveked), the error changes to

RuntimeError:在SavedModel中找不到与标签"serve"关联的MetaGraphDef.要检查SavedModel中可用的标签集,请使用SavedModel CLI:saved_model_cli

saved_model_cli show --dir .报告空标签集.

如何解决?

@Ping Yu在使用MobileNet训练图像检测中暗示>,您可以使用

As hinted by @Ping Yu in Retrain image detection with MobileNet, you can use

python retrain.py --tfhub_module https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/feature_vector/2 \
    --image_dir /tmp/flower_photos --saved_model_dir /tmp/saved_retrained_model
tensorflowjs_converter --input_format=tf_saved_model \
    --output_format=tfjs_graph_model \
    --saved_model_tags=serve \
    /tmp/saved_retrained_model/ /tmp/converted_model/

这将使用保存的模型格式保存模型.

This saves the model using the saved model format.