tflite.allocate_tensors() 在更改输入大小后失败

tflite.allocate_tensors() fails after changing input size

我正在尝试使用 tflite 模型对批次进行推理。我使用以下代码:

interpreter = tf.lite.Interpreter(model_path=model_path)
input_details = interpreter.get_input_details()
interpreter.resize_tensor_input(input_details[0]["index"], [batch_size, 513, 513, 3])
interpreter.allocate_tensors()

代码崩溃并给出以下错误:

RuntimeError: tensorflow/lite/kernels/reshape.cc:58 num_input_elements != num_output_elements (1579014 != 789507)Node number 0 (RESHAPE) failed to prepare.

查看输出细节时,它的形状仍然是 [1, 513, 513, output_channels] 而不是我希望的 [batch_size, 513, 513, output_channels]

有什么想法吗?

这是一个已知错误 https://github.com/tensorflow/tensorflow/issues/16216 但目前没有补丁

我遇到了类似的问题并搜索了很多相关内容。作为在 tensorflow github link 上推荐的解决方案,我使用了 tf-nightly 包并且它工作得很好。 安装-

pip install tf-nightly

通过上述步骤安装tensorflow后,您可以按照以下步骤操作:

from tensorflow.contrib.lite.python import interpreter
model = Interpreter(model_path='cls_prob.tflite')
input_details = model.get_input_details()
req_input_size = (2, 128, 200, 1) #Your input size
model.resize_tensor_input(input_details[0]['index'], req_input_size)
model.allocate_tensors()