如何正确地将 tflite_graph.pb 转换为 detect.tflite

How to convert tflite_graph.pb to detect.tflite properly

我正在使用 tensorflow 对象检测 api 使用来自 tensorflow ssdlite_mobilenet_v2_coco_2018_05_09 的自定义模型 model zoo.

我成功地训练了模型并使用 this tutorial 中提供的脚本对其进行了测试。

问题来了,我需要一个detect.tflite才能在我的目标机器(嵌入式系统)中使用它。但是当我实际从我的模型中制作一个 tflite 时,它​​ 几乎什么都不输出 而当它输出时,它是 错误检测 。为了制作 .tflite 文件,我首先使用 export_tflite_ssd_graph.py,然后通过以下 the doc 和一些 google 搜索,在此命令的输出中使用 toco

toco --graph_def_file=$OUTPUT_DIR/tflite_graph.pb --output_file=$OUTPUT_DIR/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' --allow_custom_ops

此外,我用于 .tflite 检测任务的代码工作正常,因为我使用 ssd_mobilenet_v3_small_coco detect.tflite 文件对其进行了测试。

问题出在 toco 命令上。我使用的一些文件已经过时并误导了我。 toco 已弃用,我应该改用 tflite_convert 工具。

这是我使用的完整命令(运行 来自您的培训目录):

tflite_convert --graph_def_file tflite_inference_graph/tflite_graph.pb --output_file=./detect.tflite --output_format=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_dev_values=127 --change_concat_input_ranges=false --allow_custom_ops

我在 ssdlite_mobilenet_v2_coco_2018_05_09 模型上进行了训练,并将其添加到我的 .config 文件的末尾。

 graph_rewriter {
  quantization {
    delay: 400
    weight_bits: 8
    activation_bits: 8
  }
}

我还使用此命令在 tflite_inference_graph 目录中生成 tflite_graph.pb:

python export_tflite_ssd_graph.py --pipeline_config_path 2020-05-17_train_ssdlite_v2/ssd_mobilenet_v2_coco.config --trained_checkpoint_prefix 2020-05-17_train_ssdlite_v2/train/model.ckpt-1146 --output_directory 2020-05-17_train_ssdlite_v2/tflite_inference_graph --add_postprocessing_op=true

注意:我想在我的嵌入式系统上使用量化模型。这就是我在配置文件中添加 graph_rewriter 并在我的 tflite_convert 命令中添加 --inference_type=QUANTIZED_UINT8 的原因。