亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Tensorflow-lite 中輸入具有動態尺寸的圖像

在 Tensorflow-lite 中輸入具有動態尺寸的圖像

桃花長相依 2022-01-11 16:07:01
我有一個 tensorflow 模型,它接受不同大小的輸入圖像:inputs = layers.Input(shape=(128,None,1), name='x_input')<tf.Tensor 'x_input:0' shape=(?, 128, ?, 1) dtype=float32>當我將此模型轉換為 tensorflow-lite 時,它會抱怨:converter = tf.lite.TFLiteConverter.from_frozen_graph(  graph_def_file, input_arrays, output_arrays)tflite_model = converter.convert() ValueError: None is only supported in the 1st dimension.Tensor 'x_input_1' has invalid shape '[None, 128, None, 1]'.我無法將圖像縮放到固定大小。我看到的唯一解決方案是將圖像填充到某個最大尺寸并在圖中使用該尺寸,但這似乎很浪費。有沒有其他方法可以使 tensorflow-lite 與動態圖像尺寸一起工作?這種限制有什么理由嗎?謝謝。
查看完整描述

2 回答

?
慕森王

TA貢獻1777條經驗 獲得超3個贊

是的,您可以在 TF-Lite 中使用動態張量。之所以不能直接將形狀設置為,[None, 128, None, 1]是因為這樣以后可以輕松支持更多的語言。此外,它充分利用了靜態內存分配方案。對于旨在用于具有低計算能力的小型設備的框架,這是一個明智的設計選擇。以下是如何動態設置張量大小的步驟:


0. 凍結

看起來你正在從一個凍結的 GraphDef 轉換,即一個*.pb文件。假設您的凍結模型具有輸入 shape [None, 128, None, 1]。


1.轉換步驟。

在此步驟中,將輸入大小設置為您的模型可以接受的任何有效大小。例如:


tflite_convert \

  --graph_def_file='model.pb' \

  --output_file='model.tflite' \

  --input_shapes=1,128,80,1 \     # <-- here, you set an

                                  #     arbitrary valid shape

  --input_arrays='input' \         

  --output_arrays='Softmax'

2.推理步驟

訣竅是interpreter::resize_tensor_input(...)在推理過程中實時使用TF-Lite API的功能。我將提供它的python實現。Java 和 C++ 實現應該相同(因為它們具有相似的 API):


from tensorflow.contrib.lite.python import interpreter


# Load the *.tflite model and get input details

model = Interpreter(model_path='model.tflite')

input_details = model.get_input_details()


# Your network currently has an input shape (1, 128, 80 , 1),

# but suppose you need the input size to be (2, 128, 200, 1).

model.resize_tensor_input(

    input_details[0]['index'], (2, 128, 200, 1))

model.allocate_tensors()

而已。您現在可以將該模型用于具有 shape 的圖像(2, 128, 200, 1),只要您的網絡架構允許這樣的輸入形狀。請注意,model.allocate_tensors()每次進行此類重塑時都必須這樣做,因此效率非常低。這是強烈建議,以避免在程序中使用此功能太多。


查看完整回答
反對 回復 2022-01-11
?
SMILET

TA貢獻1796條經驗 獲得超4個贊

上述答案不再適用于較新版本的 Tensorflow。應該在轉換步驟中使用形狀 None 而不是虛擬形狀,然后使用interpreter.resizeInput() 來工作。見這里:https : //github.com/tensorflow/tensorflow/issues/41807


查看完整回答
反對 回復 2022-01-11
  • 2 回答
  • 0 關注
  • 711 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號