Tensorflow对象检测-将.pb文件转换为tflite - python

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

我使用模型动物园的ssd_mobilenet_v2_coco_2018_03_29模型对TF对象检测API的train.py文件进行了重新训练。 (好)
使用TF Object Detection API也提供的export_inference_graph.py将训练后的model.ckpt导出到冻结的模型文件。 (好)
使用GPU和仅允许CPU在python中测试冻结的图形。有用。 (好)

不利之处在于,我尝试使用以下代码:

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

首先,我尝试不向函数添加输入shapes参数,但是没有用。从那时起,我读到您可以在这里写任何无关紧要的内容。

直到这一行的输出:

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

感谢andvance!

参考方案

上周遇到相同的问题,请按照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。

将(行)函数应用于DataFrame会更改列类型 - python

列类型意外更改有问题,如下所示进行了精炼。 x列是浮点数,icol列是整数。当应用testfunction(不执行任何操作)时,列icol更改为类型float64,如以下代码所示:df = pd.DataFrame({'x':[1000, -1000, 1.0]}) df['icol'] = 1 print(df.dt…

Python GPU资源利用 - python

我有一个Python脚本在某些深度学习模型上运行推理。有什么办法可以找出GPU资源的利用率水平?例如,使用着色器,float16乘法器等。我似乎在网上找不到太多有关这些GPU资源的文档。谢谢! 参考方案 您可以尝试在像Renderdoc这样的GPU分析器中运行pyxthon应用程序。它将分析您的跑步情况。您将能够获得有关已使用资源,已用缓冲区,不同渲染状态上…

重命名TensorFlow估算器中的功能 - python

我正在使用固定的估算器,并从无法控制的来源读取数据。源代码使用snake_case存储功能,而我提供的预测功能始终在camelCase中,这也是我无法控制的。粗略地讲,我训练模型并将其导出,因此稍后可以阅读:features = ... # snake_case estimator = tf.estimator(DNNClassifier(feature_c…

Python uuid4,如何限制唯一字符的长度 - python

在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…

Tensorflow到ONNX的转换 - python

我目前正在尝试将通过本教程(https://github.com/thtrieu/darkflow)创建的已保存(正在工作)的.pb文件转换为onnx文件。我目前正在使用winML工具,但是转换的结果根本不起作用(输入参数错误+整个体系结构不正确)。我的输入参数(在自述文件的最底部指定):input:0输出节点:输出:0我想在HoloLens上运行的UWP应…