如何将ssd_resnet_50张量流检查点转换为.tflite?

时间:2019-05-21 18:05:52

标签: tensorflow tensorflow-lite

我正在尝试将ssd_resnet_50模型从tensorflow Object Detection API转换为.tflite格式,但是它不起作用。

某些背景

  1. 我能够成功地将开箱即用的ssd_mobilenet_v2_quantized模型转换为.tflite并运行。tflite模型。

  2. 由于未量化s sd_resnet_50模型,因此我将以下内容添加到ssd_resnet_50 pipeline.config文件中并重新训练了模型:

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

重新训练ssd_resnet_50之后,我尝试使用以下命令将模型转换为.tflite格式:

# Produces tflite_graph.pb
python3 object_detection/export_tflite_ssd_graph.py \
        --pipeline_config_path=pipeline.config \
        --trained_checkpoint_prefix=model.ckpt-50000 \
        --output_directory=$OUTPUT_DIR \
        --add_postprocessing_op=true

# Produces detect.tflite
bazel run -c opt tensorflow/lite/toco:toco -- \
    --input_file=$OUTPUT_DIR/tflite_graph.pb \
    --output_file=$OUTPUT_DIR/detect.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_values=128 \
    --change_concat_input_ranges=false \
    --allow_custom_ops

通常,TOCO会产生可以运行的有效detect.tflite。但是,TOCO遇到以下有关量化和Relu6的错误。

有人可以帮忙吗?


错误:

2019-05-21 10:41:07.885065: F tensorflow/lite/toco/tooling_util.cc:1718] Array WeightSharedConvolutionalBoxPredictor_2/BoxPredictionTower/conv2d_0/BatchNorm/feature_2/FusedBatchNorm_mul_0, which is an input to the Add operator producing the output array WeightSharedConvolutionalBoxPredictor_2/Relu6, is lacking min/max data, which is necessary for quantization. If accuracy matters, either target a non-quantized output format, or run quantized training with your model from a floating point checkpoint to change the input graph to contain min/max information. If you don't care about accuracy, you can pass --default_ranges_min= and --default_ranges_max= for easy experimentation.
run_toco.sh: line 25:  3280 Aborted                 (core dumped) bazel run -c opt tensorflow/lite/toco:toco -- --input_file=$OUTPUT_DIR/tflite_graph.pb --output_file=$OUTPUT_DIR/detect.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_values=128 --change_concat_input_ranges=false --allow_custom_ops

1 个答案:

答案 0 :(得分:0)

在读取错误时,似乎WeightSharedConvolutionalBoxPredictor_2/BoxPredictionTower/ conv2d_0/BatchNorm /feature_2/FusedBatchNorm_mul_0中的数组WeightSharedConvolutionalBoxPredictor_2/ Relu6没有进行训练后量化所需的最小/最大信息。

您可以在Use "dummy-quantization" to try out quantized inference on a float graph.部分中查看示例和一些详细信息。

您可以在命令中添加--default_ranges_min=0 --default_ranges_max=255,但这样做会失去准确性。

bazel run -c opt tensorflow/lite/toco:toco -- \
--input_file=$OUTPUT_DIR/tflite_graph.pb \
--output_file=$OUTPUT_DIR/detect.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_values=128 \
--default_ranges_min=0 \
--default_ranges_max=255 \
--change_concat_input_ranges=false \
--allow_custom_ops

来自Tensorflow Converter command line reference

--default_ranges_min, --default_ranges_max。类型:浮点数。没有指定范围的所有阵列使用的(最小,最大)范围值的默认值。允许用户继续量化未量化或错误量化的输入文件。这些标志产生的模型精度较低。它们旨在通过“虚拟量化”轻松进行量化实验

相关问题