【问题标题】:Input images with dynamic dimensions in Tensorflow-lite在 Tensorflow-lite 中输入具有动态尺寸的图像
【发布时间】:2019-09-06 04:08:09
【问题描述】:

我有一个 tensorflow 模型,它接受不同大小的输入图像:

inputs = layers.Input(shape=(128,None,1), name='x_input')

<tf.Tensor 'x_input:0' shape=(?, 128, ?, 1) dtype=float32>

当我将此模型转换为 tensorflow-lite 时,它​​会抱怨:

converter = tf.lite.TFLiteConverter.from_frozen_graph(
  graph_def_file, input_arrays, output_arrays)
tflite_model = converter.convert() 

ValueError: None is only supported in the 1st dimension.
Tensor 'x_input_1' has invalid shape '[None, 128, None, 1]'.

我无法将图像缩放到固定大小。我看到的唯一解决方案是将图像填充到某个最大尺寸并在图中使用该尺寸,但这似乎很浪费。有没有其他方法可以使 tensorflow-lite 与动态图像尺寸一起工作?这种限制有什么理由吗?谢谢。

【问题讨论】:

    标签: python tensorflow keras tensorflow-lite


    【解决方案1】:

    是的,您可以在 TF-Lite 中使用动态张量。之所以不能直接将shape设置为[None, 128, None, 1]是因为这样以后可以轻松支持更多语言。此外,它充分利用了静态内存分配方案。对于旨在用于具有低计算能力的小型设备的框架,这是一个明智的设计选择。 以下是如何动态设置张量大小的步骤:

    0。冷冻

    您似乎正在从冻结的 GraphDef 转换,即 *.pb 文件。假设您的冻结模型具有输入形状[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。推理步骤

    诀窍是在推理过程中实时使用 TF-Lite API 的函数interpreter::resize_tensor_input(...)。我将提供它的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()
    

    就是这样。您现在可以将该模型用于形状为 (2, 128, 200, 1) 的图像,只要您的网络架构允许这样的输入形状。请注意,每次进行此类重塑时都必须执行model.allocate_tensors(),因此效率非常低。 强烈建议避免在您的程序中过多使用此函数。

    【讨论】:

      【解决方案2】:

      以上答案不再适用于较新版本的 Tensorflow。应该在转换步骤中使用形状 None 而不是虚拟形状,然后使用interpreter.resizeInput() 来工作。见这里:https://github.com/tensorflow/tensorflow/issues/41807

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-24
        • 2016-10-27
        • 2017-07-11
        • 1970-01-01
        • 2016-08-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多