TF2训练的Tflite模型在iOS应用中可运行但无法检测目标求助
TF2 Object Detection API训练的SSD模型转TFLite后无法检测自定义目标
我用TF2 Object Detection API基于ssd_mobilenet_v2_fpnlite_640x640预训练模型训练了自定义目标检测模型,过程中参考了TF2 Object Detection API官方文档、Running TF2 Detection API Models on mobile、Converter Python API guide以及Edge TF Lite iOS tutorial。目前遇到的核心问题是:
- 训练后的模型在Python环境中预测完全正常,能准确识别自定义目标
- 导出为中间SavedModel后转换的TFLite模型,能在TF Lite官方iOS示例应用中正常加载运行,但完全检测不到训练时指定的自定义目标
我尝试了多种导出和转换方式都没能解决问题,已经卡了好几天,希望能得到技术帮助。
训练与SavedModel导出流程
训练命令
python3 model_main_tf2.py \ --pipeline_config_path={pipeline_path}\ --model_dir={output_model_dir} \ --alsologtostderr
导出SavedModel命令
python export_tflite_graph_tf2.py \ --pipeline_config_path {pipeline_path} \ --trained_checkpoint_dir {output_model_dir} \ --output_directory {exported_models_dir}
TFLite转换的几种尝试方式
1. TF2 Python API原生转换
converter = tf.lite.TFLiteConverter.from_saved_model(export_dir) tflite_model = converter.convert()
2. TF1兼容模式量化转换
converter = tf.compat.v1.lite.TFLiteConverter.from_saved_model(export_dir) converter.inference_type = tf.compat.v1.lite.constants.QUANTIZED_UINT8 input_arrays = converter.get_input_arrays() converter.quantized_input_stats = {input_arrays[0] : (0., 1.)} # mean_value, std_dev tflite_model = converter.convert()
3. 命令行量化转换
tflite_convert \ --saved_model_dir={saved_model} \ --output_file={output_dir} \ --output_format=TFLITE \ --input_shapes=1,640,640,3 \ --input_arrays='normalized_input_image_tensor' \ --output_arrays='TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1','TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3' \ --inference_type=QUANTIZED_UINT8 \ --mean_values=128 \ --std_dev_values=127 \ --change_concat_input_ranges=false \ --allow_custom_ops
注意:这个命令生成的TFLite模型只有500字节,用Neutron查看发现内容异常,显然不符合正常模型的大小。
iOS端的参数调整
我在iOS示例应用中调整了模型相关参数,确保和训练/转换时的设置一致:
// MARK: Model parameters let batchSize = 1 let inputChannels = 3 let inputWidth = 640 let inputHeight = 640 // image mean and std for floating model, should be consistent with parameters used in model training let imageMean: Float = 128 let imageStd: Float = 127
额外排查动作
我还尝试更换了其他SSD Mobilenet系列的预训练模型重新训练转换,结果都是一样:模型能正常加载运行,但完全检测不到自定义目标。
请问各位大佬,可能是什么原因导致转换后的TFLite模型无法检测自定义目标?那个500字节的异常模型又是哪里出了问题?麻烦指点排查方向或者解决方案,谢谢!
内容的提问来源于stack exchange,提问作者Juan Alejandro Galvis
相关产品推荐
相关产品推荐

