tflite对象检测推断无法正常工作

时间:2020-01-21 15:37:51

标签: arrays tensorflow object-detection tensorflow-lite toco

  • 您正在使用的模型的顶级目录是什么:/ home / USER / PROJECT / tf-models
  • 我是否编写了自定义代码(与使用TensorFlow中提供的股票示例脚本相反):否
  • OS平台和发行版(例如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以检测一个对象,在冻结图形并使用此notebook进行推理之后,该模型运行良好且符合预期。之后,我使用以下命令导出了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

labelmap.txt使用coco数据集labelmap样式包含以下标签:

???
Object_1

我使用script的自定义版本script进行推断

输出非常奇怪,因为我们有负值和不正确的值,因此我们无法可视化任何检测结果:

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移动网络模型上测试了推理文件,并且当模型检测到汽车,人等时,我能够可视化这些框。 我将量化的SSD mobilenet v1等默认模型的QUANTIZED tflite_graph.pb转换为detect.tflite,它能够输出框和coco数据集标签。

我不明白这些值是从哪里来的,为什么tflite模型在张量流这样做时却没有检测到。

我也按照相同的步骤但使用以下命令训练并转换了量化的SSD mobilenetv2模型:

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 个答案:

答案 0 :(得分:1)

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。因此输出张量具有垃圾值。

这是link的推理脚本,具有输入预处理,输出后处理和mAP评估。

答案 1 :(得分: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模型非常重要

相关问题