Tflite对象检测推理无法正常工作。

4
  • 你使用的模型的顶级目录是什么: /home/USER/PROJECT/tf-models
  • 是否编写了自定义代码(而不是使用TensorFlow提供的示例脚本): 否
  • 操作系统平台和分发版(例如,Linux Ubuntu 16.04): Ubuntu 18.04
  • 安装TensorFlow的方式(源码或二进制): 使用conda安装的二进制
  • TensorFlow版本(使用下面的命令): 1.15
  • Bazel版本(如果从源代码编译): 1.2.1,用于运行转换
  • CUDA/cuDNN版本: 发布9.1,V9.1.85
  • GPU型号和内存: GTX 1050Ti 4GB
  • 重现的确切命令:
bazel run -c opt tensorflow/lite/toco:toco -- \
--input_file=$INPUT_PATH/tflite_graph.pb \
--output_file=$OUTPUT_PATH/detect.tflite \
--input_shapes=1,300,300,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=FLOAT \
--allow_custom_ops

描述问题

我重新训练了一个浮点模型 ssd_mobilenet_v2_coco ,以便在自己的数据集上检测一个对象。在冻结图形后,使用此笔记本运行推理时,模型表现良好且符合预期。之后,我使用以下命令导出了tflite_graph.pb:

python3 object_detection/export_tflite_ssd_graph.py \
--pipeline_config_path=/PATH/pipeline.config \
--trained_checkpoint_prefix=/PATH/model.ckpt-50000 \
--output_directory=/PATH/tflite \
--add_postprocessing_op=True

然后使用以下命令将tflite_graph.pb转换为detect.tflite:

bazel run -c opt tensorflow/lite/toco:toco -- \
--input_file=/PATH/tflite_graph.pb \
--output_file=/PATH/detect.tflite \
--input_shapes=1,300,300,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=FLOAT \
--allow_custom_ops
文件采用coco数据集的标签风格,包含以下标签:
???
Object_1

我使用这个脚本运行推理,它是这个脚本的自定义版本。

输出结果很奇怪,因为我们有负值和不准确的值,所以我们无法可视化任何检测:

image: /PATH/Inference_Notebooks/test_images/image15.jpg
boxes: 
[[  3.1171997    4.9266524  -15.893956     7.4959326 ]
 [ -1.904324     1.0337602   -7.818109    -7.9575577 ]
 [  1.4761205    2.4604938  -14.553953     8.159015  ]
 [  3.4024968    2.7483184   -9.744125     6.332321  ]
 [ -4.447262    -2.6578145   -1.9118335  -12.579478  ]
 [  1.5781038   -2.980986   -15.902752     5.9894757 ]
 [ -0.4003443  -12.641836    -5.6216025   -0.9522631 ]
 [ -1.3472033   -5.514964    -4.7609305  -11.9099045 ]
 [  2.6661258   -4.2592344  -13.687805    -4.15193   ]
 [ -0.49181542   9.271766    -3.5316777   -3.233222  ]] 
classes: [  2   0 -10   9  -4   3  -6  -7   0   2] 
scores: [ -5.54325      1.9508497   -6.1604195   -4.2281013   -0.02703065 0.707987   -11.534006     7.781439    -2.5938861   -2.5299745 ] 
number of detections: 0.0

我使用默认权重在COCO数据集上训练的SSD mobile net模型来测试推断文件。我成功可视化了定位到的汽车、行人等框。我将一个默认模型(如量化的SSD mobile net v1)的QUANTIZED tflite_graph.pb转换为detect.tflite,它能输出框和COCO数据集标签。
我不理解这些值从哪里来,也不知道为什么tflite模型无法检测,而tensorflow可以。
我还训练了一个量化的SSD mobile net v2模型,并按照相同步骤进行转换,但使用了这个命令:
bazel run -c opt tensorflow/lite/toco:toco -- \
--input_file=/PATH/tflite_graph.pb \
--output_file=/PATH/detect.tflite \
--input_shapes=1,300,300,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_values=128 \
--change_concat_input_ranges=false \
--allow_custom_ops

输出这些值:

image: /home/neox/HistorIAR_Detection/Inference_Notebooks/test_images/image15.jpg 
boxes: 
[[1.0170084e+32 1.6485735e+30 2.5740889e+31 2.6057175e+31]
 [2.4791379e+31 6.3874716e+33 1.0232719e+32 1.0043315e+32]
 [6.4686013e+33 4.0425799e+32 1.0107439e+32 2.5712148e+34]
 [1.6069700e+33 4.0430743e+32 2.5712782e+34 1.0106698e+32]
 [2.5426435e+31 1.0233461e+32 1.0232968e+32 1.0170082e+32]
 [1.6272522e+33 4.0426789e+32 1.0234205e+32 1.6272126e+33]
 [2.5266129e+31 6.5147562e+30 2.5740879e+31 2.5742122e+31]
 [1.0423612e+32 1.0296598e+32 6.5144491e+30 6.3561451e+30]
 [1.0170081e+32 1.6372740e+33 6.1586925e+30 1.6170719e+33]
 [4.0172261e+32 1.0170823e+32 6.5090083e+33 1.0106451e+32]] 
classes: [-2147483648 -2147483648 -2147483648 -2147483648 -2147483648 -2147483648
 -2147483648 -2147483648 -2147483648 -2147483648] 
scores: [6.3787720e+33 1.0090909e+35 6.3066602e+33 1.6144633e+36 1.6146259e+36 1.6042820e+36 1.6145852e+36 6.4585042e+36 1.6042415e+36 4.0624248e+35] 
num: 0.0
2个回答

2

tflite模型的输出需要进行后处理。该模型默认返回一个固定数量(这里是10个检测结果)。使用索引3处的输出张量获取有效框的数量,num_det。(即前num_det个检测结果是有效的,忽略其余部分)。

num_det = int(interpreter.get_tensor(output_details[3]['index']))
boxes = interpreter.get_tensor(output_details[0]['index'])[0][:num_det]
classes = interpreter.get_tensor(output_details[1]['index'])[0][:num_det]
scores = interpreter.get_tensor(output_details[2]['index'])[0][:num_det]

关于你的问题,没有有效的检测 num: 0.0。因此输出张量具有垃圾值。

这里有一个推理脚本的链接,包括输入预处理、输出后处理和mAP评估。


0
   `frame=cv2.resize(frame,(300,300))
    frame=np.expand_dims(frame,axis=0)
    frame=frame/128.0
    frame=frame.astype('float64')
    frame=np.interp(frame,(frame.min(),frame.max()),(-1,+1))

np.interp 的最后一行对于 SSD 模型非常重要


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接