2 回答

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超3個(gè)贊
是的,您可以在 TF-Lite 中使用動(dòng)態(tài)張量。之所以不能直接將形狀設(shè)置為,[None, 128, None, 1]是因?yàn)檫@樣以后可以輕松支持更多的語言。此外,它充分利用了靜態(tài)內(nèi)存分配方案。對(duì)于旨在用于具有低計(jì)算能力的小型設(shè)備的框架,這是一個(gè)明智的設(shè)計(jì)選擇。以下是如何動(dòng)態(tài)設(shè)置張量大小的步驟:
0. 凍結(jié)
看起來你正在從一個(gè)凍結(jié)的 GraphDef 轉(zhuǎn)換,即一個(gè)*.pb文件。假設(shè)您的凍結(jié)模型具有輸入 shape [None, 128, None, 1]。
1.轉(zhuǎn)換步驟。
在此步驟中,將輸入大小設(shè)置為您的模型可以接受的任何有效大小。例如:
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(...)在推理過程中實(shí)時(shí)使用TF-Lite API的功能。我將提供它的python實(shí)現(xiàn)。Java 和 C++ 實(shí)現(xiàn)應(yīng)該相同(因?yàn)樗鼈兙哂邢嗨频?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()
而已。您現(xiàn)在可以將該模型用于具有 shape 的圖像(2, 128, 200, 1),只要您的網(wǎng)絡(luò)架構(gòu)允許這樣的輸入形狀。請(qǐng)注意,model.allocate_tensors()每次進(jìn)行此類重塑時(shí)都必須這樣做,因此效率非常低。這是強(qiáng)烈建議,以避免在程序中使用此功能太多。

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