簡體   English   中英

將可變輸入形狀的 model 轉換為 .tflite

[英]Convert .pb to .tflite for a model of variable input shape

我正在解決一個問題,我使用 Tensorflow Object 檢測 ZDB974238714CA8DE634ACE 使用 custom1D11 數據集來訓練 model。 我正在使用 tf 版本 2.2.0

output_directory = 'inference_graph'
!python /content/models/research/object_detection/exporter_main_v2.py \
--trained_checkpoint_dir {model_dir} \
--output_directory {output_directory} \
--pipeline_config_path {pipeline_config_path}

我能夠與 .ckpt 文件一起成功獲取 a.pb 文件。 但現在我需要將其轉換為.tflite。 我無法這樣做,有一些錯誤或其他。

我嘗試了寫在 TensorFlow 文檔上的基本方法,但這也不起作用。 我嘗試的另一個代碼如下:

    import tensorflow as tf
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Conv2D, Flatten, MaxPooling2D, Dense, Input, Reshape, Concatenate, GlobalAveragePooling2D, BatchNormalization, Dropout, Activation, GlobalMaxPooling2D
from tensorflow.keras.utils import Sequence

model = tf.saved_model.load(f'/content/drive/MyDrive/FINAL DNET MODEL/inference_graph/saved_model/')
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.post_training_quantize=True
converter.inference_type=tf.uint8
tflite_model = converter.convert()
open("val_converted_model_int8.tflite", "wb").write(tflite_model)

我得到的錯誤是:

() 中的 AttributeError Traceback (最近一次調用最后一次) 8 converter.post_training_quantize=True 9 converter.inference_type=tf.uint8 ---> 10 tflite_model = converter.convert() 11 open("val_converted_model_int8.tflite", "wb") .write(tflite_model)

/usr/local/lib/python3.6/dist-packages/tensorflow/lite/python/lite.py in convert(self) 837 # to None. 838 # 一旦我們對動態形狀有了更好的支持,我們就可以刪除它。 --> 839 if not isinstance(self._keras_model.call, _def_function.Function): 840 # Pass keep_original_batch_size=True將確保我們得到一個輸入 841 # 簽名,包括用戶指定的批次維度。

AttributeError: '_UserObject' object 沒有屬性 'call'

誰能幫我解決這個問題?

我認為問題不在於可變輸入形狀(而錯誤消息令人困惑)。

tf.saved_model.load返回一個SavedModel ,但tf.lite.TFLiteConverter.from_keras_model需要一個 Keras model 所以它無法處理它。

您需要使用TFLiteConverter.from_saved_model API。 像這樣的東西:

saved_model_dir = '/content/drive/MyDrive/FINAL DNET MODEL/inference_graph/saved_model/'
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)

如果您遇到其他問題,請告訴我們。

暫無
暫無

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

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