簡體   English   中英

Tensorflow對象檢測-將.pb文件轉換為tflite

[英]Tensorflow Object Detection - Convert .pb file to tflite

我嘗試將凍結的SSD mobilenet v2模型轉換為TFLITE格式以供Android使用。 這是我所有的步驟:

  1. 我使用模型動物園的ssd_mobilenet_v2_coco_2018_03_29模型對TF Object Detection API的train.py文件進行了重新訓練。 (好)

  2. 使用TF Object Detection API也提供的export_inference_graph.p y將經過訓練的model.ckpt導出到凍結的模型文件。 (好)

  3. 使用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!

上周遇到了同樣的問題,請按照此處描述的步驟解決。

基本上,問題在於它們的主腳本不支持SSD模型。 我沒有使用bazel來執行此操作,而是使用了tflite_convert實用程序。

小心export_tflite_ssd_graph.py腳本,在使用它之前請先閱讀其所有選項(主要是--max_detections挽救了我的性命)。

希望這可以幫助。

編輯:您的第2步無效。 如果save_model包含SSD,則無法將其轉換為tflite模型。 您需要使用export_tflite_ssd_graph.py腳本導出訓練有素的export_tflite_ssd_graph.py並使用創建的.pb文件通過tflite_convert工具將其轉換為tflite。

您的.pb文件格式不正確。 這是解決方案: https : //github.com/peace195/tensorflow-lite-yolo-v3

我們需要執行2個步驟:

  1. 將.weights轉換為SavedModel。

  2. 使用tflite_convert將保存的模型轉換為tflite格式。

請使用docker設置環境並仔細按照說明進行操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM