KerasCV RetinaNet转TensorFlow.js后predict()输出异常求助
KerasCV RetinaNet模型转TensorFlow.js后预测结果异常问题
补充说明:根据@mrk的评论提供更多信息
- 我使用KerasCV RetinaNet检测特定对象,并用以下代码保存模型:
model.save("saved_models/retinanet_model.keras")
该模型在Python中运行正常。
- 我希望在Node.js中使用此模型,因此通过以下命令转换:
tensorflowjs_converter --input_format=keras --output_format=tfjs_graph_model saved_models/retinanet_model.keras tfjs_models/retinanet_model
转换时出现如下警告:
WARNING:tensorflow:Compiled the loaded model, but the compiled metrics have yet to be built. 'model.compile_metrics' will be empty until you train or evaluate the model.
- 随后我在Node.js中加载模型:
retinaNetModel = await tf.loadGraphModel("file://./tfjs_models/retinanet_model/model.json");
然而,retinaNetModel.predict()的输出与原模型不符,无法完成预测。
我观察到几个异常现象,认为模型转换可能存在问题,尽管仅出现警告而非错误:
- 在Python中,当模型未检测到对象时,bounding_box、classes等会返回-1;但在TensorFlow.js中得到的是随机负值,返回两个张量,第一个包含bounding_box值,第二个包含类别概率,例如第二个张量内容如下:
[ [ [ -4.490991592407227, -4.7061991691589355, -4.518319606781006 ], [ -4.743373394012451, -4.800686359405518, -4.49501371383667 ], ... [ -4.731906414031982, -4.996838569641113, -4.9045610427856445 ], ] ]
- 另一个异常是输出张量的大小:在TensorFlow.js中,classes和bounding_boxes有76725个元素/数组,而Python中原模型生成的是长度为100的数组,为何数量差异如此之大?
我在同一台机器上完成了RetinaNet训练、模型转换及在Node.js中加载TensorFlow.js模型的全部操作。pip list | grep tensorflow的输出如下:
tensorflow 2.15.0 tensorflow-datasets 4.9.3 tensorflow-decision-forests 1.8.1 tensorflow-estimator 2.15.0 tensorflow-hub 0.15.0 tensorflow-io-gcs-filesystem 0.34.0 tensorflow-macos 2.15.0 tensorflow-metadata 1.14.0 tensorflowjs 4.14.0
我使用了完全相同的图像数据,甚至将Python中的图像数组导出后在Node.js中读取,确保数据完全一致。
我通过let imageTensor = tf.tensor(pixeldata).reshape([640, 640, 3]);重塑数据数组,因为我的图像尺寸是640x640。
希望能得到您的建议或指导,非常感谢。
此致,
Doug
内容的提问来源于stack exchange,提问作者Doug
相关产品推荐
相关产品推荐

