TFLite动态输入形状

时间:2020-10-08 14:32:11

标签: tensorflow recurrent-neural-network tf-lite

我正在使用TensorFlow 2.1,并尝试在转换为TFLite后将可变长度输入序列用于递归神经网络。我首先使用KerasTFLite模型转换为TFLite Converter模型。我用输入形状[(None,40),(1、6、2、32)]构建了模型。但是在转换为TFLite之后,模型仅接受输入形状[(1,40),(1、6、2、32)]。我希望我的TFLite模型接受无值的变量值。我尝试使用resize_tensor_input来调整张量输入的大小,但是输入的形状仍然没有改变。

我在下面粘贴了我的代码段及其输出。

interpreter = tf.lite.Interpreter(model_path=my_model_path)

input_details = interpreter.get_input_details()

interpreter.resize_tensor_input(0, [3, 40], strict=False)
interpreter.allocate_tensors()

output_details = interpreter.get_output_details()

interpreter.set_tensor(input_details[0]["index"], np.random.uniform(size=[3, 40]).astype(np.float32))
interpreter.set_tensor(input_details[1]["index"], np.zeros(shape=[1, 6, 2, 32]).astype(np.float32))
tf.print("Input details : ", input_details)

interpreter.invoke()

result = interpreter.get_tensor(output_details[0]["index"])
final_state = interpreter.get_tensor(output_details[1]["index"])

我将输入详细信息打印在上面粘贴的代码中,而输出则粘贴在下面。在这里,我得到的第一个输入形状是[1,40],而不是[3,40]。

Input details :  [{'dtype': <class 'numpy.float32'>,
  'index': 0,
  'name': 'input_1',
  'quantization': (0.0, 0),
  'shape': array([ 1, 40], dtype=int32)},
 {'dtype': <class 'numpy.float32'>,
  'index': 1,
  'name': 'input_2',
  'quantization': (0.0, 0),
  'shape': array([ 1,  6,  2, 32], dtype=int32)}]
Output shape : (1, 1, 32)
Final state shape : (1, 6, 2, 32)

以上代码中我在做什么错?还是如果我的方法无法达到预期的结果,请帮助我找到正确的方法来达到相同的效果,或者是否有其他解决方法?

0 个答案:

没有答案