【问题标题】:Tensorflow Object Detection - Convert .pb file to tfliteTensorflow 对象检测 - 将 .pb 文件转换为 tflite
【发布时间】:2019-02-22 09:04:37
【问题描述】:

我尝试将冻结的 SSD mobilenet v2 模型转换为 TFLITE 格式以供 Android 使用。 这是我的所有步骤:

  1. 我使用 ssd_mobilenet_v2_coco_2018_03_29 模型通过模型动物园的 TF 对象检测 API 的 train.py 文件重新训练。 (好的)

  2. 使用同样由 TF Object Detection API 提供的 export_inference_graph.py 将训练好的 model.ckpt 导出到冻结模型文件。 (好的)

  3. 在 python 中使用 GPU 测试冻结图,并且只允许使用 CPU。有用。 (好的)

缺点来了,我尝试使用以下代码:

import tensorflow as tf
tf.enable_eager_execution()
saved_model_dir = 'inference_graph/saved_model/'
converter = tf.contrib.lite.TFLiteConverter.from_saved_model(saved_model_dir,input_arrays=input_arrays,output_arrays=output_arrays,input_shapes={"image_tensor": [1, 832, 832, 3]})
converter.post_training_quantize = True

首先我尝试不向函数添加输入形状参数,但它不起作用。从那以后我读到你可以在那里写任何无关紧要的东西。

直到这一行的输出:

INFO:tensorflow:Saver not created because there are no variables in the graph to restore
INFO:tensorflow:The specified SavedModel has no variables; no checkpoints were restored.
INFO:tensorflow:The given SavedModel MetaGraphDef contains SignatureDefs with the following keys: {'serving_default'}
INFO:tensorflow:input tensors info: 
INFO:tensorflow:Tensor's key in saved_model's tensor_map: inputs
INFO:tensorflow: tensor name: image_tensor:0, shape: (-1, -1, -1, 3), type: DT_UINT8
INFO:tensorflow:output tensors info: 
INFO:tensorflow:Tensor's key in saved_model's tensor_map: num_detections
INFO:tensorflow: tensor name: num_detections:0, shape: (-1), type: DT_FLOAT
INFO:tensorflow:Tensor's key in saved_model's tensor_map: detection_boxes
INFO:tensorflow: tensor name: detection_boxes:0, shape: (-1, 100, 4), type: DT_FLOAT
INFO:tensorflow:Tensor's key in saved_model's tensor_map: detection_scores
INFO:tensorflow: tensor name: detection_scores:0, shape: (-1, 100), type: DT_FLOAT
INFO:tensorflow:Tensor's key in saved_model's tensor_map: detection_classes
INFO:tensorflow: tensor name: detection_classes:0, shape: (-1, 100), type: DT_FLOAT
INFO:tensorflow:Saver not created because there are no variables in the graph to restore
INFO:tensorflow:The specified SavedModel has no variables; no checkpoints were restored.
INFO:tensorflow:Froze 0 variables.
INFO:tensorflow:Converted 0 variables to const ops.

然后我想转换:

tflite_quantized_model = converter.convert()

这是输出:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-6-61a136476642> in <module>
----> 1 tflite_quantized_model = converter.convert()

~/.local/lib/python3.5/site-packages/tensorflow/contrib/lite/python/lite.py in convert(self)
    451           input_tensors=self._input_tensors,
    452           output_tensors=self._output_tensors,
--> 453           **converter_kwargs)
    454     else:
    455       # Graphs without valid tensors cannot be loaded into tf.Session since they

~/.local/lib/python3.5/site-packages/tensorflow/contrib/lite/python/convert.py in toco_convert_impl(input_data, input_tensors, output_tensors, *args, **kwargs)
    340   data = toco_convert_protos(model_flags.SerializeToString(),
    341                              toco_flags.SerializeToString(),
--> 342                              input_data.SerializeToString())
    343   return data
    344 

~/.local/lib/python3.5/site-packages/tensorflow/contrib/lite/python/convert.py in toco_convert_protos(model_flags_str, toco_flags_str, input_data_str)
    133     else:
    134       raise RuntimeError("TOCO failed see console for info.\n%s\n%s\n" %
--> 135                          (stdout, stderr))
    136 
    137 

RuntimeError: TOCO failed see console for info.

我无法在此处复制控制台输出,因此它超过了 30000 个字符的限制,但您可以在这里看到:https://pastebin.com/UyT2x2Vk

请在这一点上提供帮助,我该怎么做才能使它工作:(

我的配置: Ubuntu 16.04,Tensorflow-GPU 1.12

谢谢你!

【问题讨论】:

    标签: python tensorflow converters tensorflow-lite toco


    【解决方案1】:

    上周遇到了同样的问题,按照here 描述的步骤解决了。

    基本上问题是他们的主脚本不支持 SSD 模型。 我没有使用bazel 来执行此操作,而是使用tflite_convert 实用程序。

    小心export_tflite_ssd_graph.py 脚本,在使用它之前阅读它的所有选项(主要是拯救我生命的--max_detections)。

    希望这会有所帮助。

    编辑: 您的第 2 步无效。如果 save_model 包含 SSD,则无法将其转换为 tflite 模型。 您需要使用export_tflite_ssd_graph.py 脚本导出训练好的model.ckpt,并使用创建的.pb 文件通过tflite_convert 工具将其转换为tflite。

    【讨论】:

    • 您好,我尝试使用以下命令:tflite_convert --output_file=models/research/object_detection/tflite_inference/bike.tflite --saved_model_dir=models/research/object_detection/inference_graph/saved_model --input_shapes= 1,300,300,3 --input_arrays=image_tensor 但我得到的输出与前面提到的相同。你可以在 pastebin 链接上看到它:(
    • 好的,我使用了 tflite_convert 工具!但是我训练了 19 个类,现在我用 tf.contrib.lite.Interpreter 加载回 tflite 模型,它显示以下输入和输出:输入:[{'dtype':numpy.float32,'index':253,' name': 'normalized_input_image_tensor', 'quantization': (0.0, 0), 'shape': array([ 1, 300, 300, 3], dtype=int32)}] 输出: [ {'dtype': numpy.float32 , 'index': 255, 'name': 'raw_outputs/class_predictions', 'quantization': (0.0, 0), 'shape': array([ 1, 1917, 20], dtype=int32)}] 1917数字代表什么? :O
    • 好的,解决了与原始帖子相关的所有问题,但预测没有预测任何内容:/您有类似的东西吗?
    • 是的,我的预处理错误。为了让它工作,我必须将每个像素 rgb 值转换为范围 [-1, 1]。如果有帮助的话。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 2018-01-11
    相关资源
    最近更新 更多