arrays - tflite 对象检测推理无法正常工作

标签 arrays tensorflow object-detection tensorflow-lite toco

  • 您使用的模型的顶级目录是什么 :/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
    

    描述问题

    我重新训练了一个 float 模型 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 数据集标签图样式的以下标签:
    ???
    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 模型在 tensorflow 检测时没有检测到。

    还有一个量化的 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
    

    最佳答案

    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 评估的推理脚本。

    关于arrays - tflite 对象检测推理无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59844475/

    相关文章:

    javascript - 计算对象数组内的数组数量

    arrays - 如何在不使用 LINQ 的情况下按降序对 FileInfo 对象数组进行排序

    java - 从数组中选择并使用随机类

    python - 为什么 Jupyter 在通过 Conda 安装时会降级 Tensorflow?

    opencv - 如何减少 YOLOv3 文件中的类数?

    python - 如何在 NumPy 中将三角矩阵转换为正方形?

    python - 在卡住图上使用 optimize_for_inference.py 后使用模型时出错

    machine-learning - 什么时候需要占位符?

    c++ - OpenCV 功能检测和匹配 - 绘图匹配段错误

    image-processing - 平面几何图形中的线检测