我用 tensorflow2 训练了我的模型,转换 tflite 时,我的 input_shapes:
input_shapes={“x_mixed_src”:[1,10000,513]}
那个 10000 是随便写的,因为我的模型是处理文件的,根据输入文件的不同,提取出的特征数量也不同,所以中间那个维度是不确定的
找到了一个方法,可以在使用 tflite 是重新指定输入 tensor 的 shape
再 settensor 之前掉用:
interpreter.resize_tensor_input (input_details [0][‘index’], [1, len (data [0]), 513])
可以动态的根据输入数据指定输入 tensor 的 shape
但是,在 pc 端使用 tf.lite.Interpreter 加载模型使用模型时
interpreter.set_tensor (input_details [0][‘index’], data)
这行代码会报以下错误:
File "/Users/baiyu/opt/anaconda3/envs/tensorflow2/lib/python3.7/site-packages/tensorflow/lite/python/interpreter.py", line 404, in set_tensor
self._interpreter.SetTensor (tensor_index, value)
File "/Users/baiyu/opt/anaconda3/envs/tensorflow2/lib/python3.7/site-packages/tensorflow/lite/python/interpreter_wrapper/tensorflow_wrap_interpreter_wrapper.py", line 149, in SetTensor
return _tensorflow_wrap_interpreter_wrapper.InterpreterWrapper_SetTensor (self, i, value)
ValueError: Cannot set tensor: Tensor is unallocated. Try calling allocate_tensors () first
提示掉用 allocate_tensors ()
之后我在 resize_tensor_input 之后
掉用了 interpreter.allocate_tensors ()
运行后
添加的 interpreter.allocate_tensors () 会报错:
File "/Users/baiyu/opt/anaconda3/envs/tensorflow2/lib/python3.7/site-packages/tensorflow/lite/python/interpreter.py", line 242, in allocate_tensors
return self._interpreter.AllocateTensors ()
File "/Users/baiyu/opt/anaconda3/envs/tensorflow2/lib/python3.7/site-packages/tensorflow/lite/python/interpreter_wrapper/tensorflow_wrap_interpreter_wrapper.py", line 110, in AllocateTensors
return _tensorflow_wrap_interpreter_wrapper.InterpreterWrapper_AllocateTensors (self)
RuntimeError: tensorflow/lite/kernels/reshape.cc:66 num_input_elements != num_output_elements (10519065 != 51300000) Node number 0 (RESHAPE) failed to prepare.
Process finished with exit code 1
经过 搜索后,得到的结论是:
resize_tensor_input 不起作用。。
“ ResizeInputTensor is restricted by the neural network architecture. It fails since MobileNet & MobileNet SSD can only handle fixed size input.”
所以,我想请教以下各位码友,tflite 真的不支持不确定 shape 的数据输入吗
还是需要额外的设置?