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()每次進行此類重塑時都必須這樣做,因此效率非常低。這是強烈建議,以避免在程序中使用此功能太多。

TA貢獻1796條經驗 獲得超4個贊
上述答案不再適用于較新版本的 Tensorflow。應該在轉換步驟中使用形狀 None 而不是虛擬形狀,然后使用interpreter.resizeInput() 來工作。見這里:https : //github.com/tensorflow/tensorflow/issues/41807
添加回答
舉報