【问题标题】:tensorflow/lite/core/subgraph.cc BytesRequired number of elements overflowed. Node number 1 (CONV_2D) failed to prepare. tflitetensorflow/lite/core/subgraph.cc BytesRequired 溢出的元素数。节点号 1 (CONV_2D) 准备失败。 tflite
【发布时间】:2020-08-20 07:18:40
【问题描述】:

我正在尝试将 CNN 模型转换为 tflite 模型。我成功转换了它,但是当我尝试加载和运行模型时会发生此错误。 我正在构建一个颤振应用程序。

它会初始化 Tensorflow Lite 运行时,但随后会引发此错误。

I/tflite  (27856): Initialized TensorFlow Lite runtime.
E/flutter (27856): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: PlatformException(Failed to load model, Internal error: Unexpected failure when preparing tensor allocations: tensorflow/lite/core/subgraph.cc BytesRequired number of elements overflowed.
E/flutter (27856): 
E/flutter (27856): Node number 1 (CONV_2D) failed to prepare.

【问题讨论】:

    标签: flutter tensorflow-lite


    【解决方案1】:

    我想我已经解决了问题。

    花了几天时间试图解决这个问题。我发现我用来转换的模型是一个 ImagNet 预训练模型,即 InceptionV3。问题可能是有些层无法转换。

    我使用了以下,它们工作得很好。

    • MobileNet 和 MobileNetV2。
    • NasNet 移动版。
    • 或者,如果您不熟悉深度学习并且不想训练或跳过深度学习部分,您可以使用 Teachable Machine,然后轻松转换。

    希望对大家有帮助!!谢谢

    【讨论】:

      【解决方案2】:

      过去几天我遇到了完全相同的问题。我试图在 Android 上加载和运行一个 tflite 模型。我终于想出了如何解决这个问题。

      我正在使用以下方法创建模型:

      model = Xception(include_top=False)
      

      这里的重要部分是include_top=False,以及默认参数input_shape=None

      如果您查看 Xception、Inception、MobileNet 或其他任何东西的源代码(您可以找到 here),您会在创建它们调用的第一层之前的某个时刻看到这一点

      input_shape = imagenet_utils.obtain_input_shape(
          input_shape,
          default_size=<default_size>,
          min_size=<min_size>,
          data_format=backend.image_data_format(),
          require_flatten=include_top,
          weights=weights)
      

      实现here,对我们来说最重要的部分是:

      if input_shape:
          ...
      else:
          if require_flatten:
              input_shape = default_shape
          else:
              if data_format == 'channels_first':
                  input_shape = (3, None, None)
              else:
                  input_shape = (None, None, 3)
      

      因此,如果我没记错的话,当我们将include_top 设置为False 时,我们最终会得到未定义的行数和列数,而不是获得默认形状。我不确定这是如何转换为 tflite 的,虽然在转换过程中没有引发错误,但似乎 Android 无法使用它(可能这相当于设置无限图像大小)。因此初始化解释器时出现此错误:

      BytesRequired 溢出的元素数

      当我在构造函数中设置正确的input_shape 参数时,即

      model = Xception(include_top=False, weights=None, input_shape=(rows, cols, channels))
      

      然后转换后的模型在 Android 上运行良好。

      至于为什么它在相同情况下使用 MobileNetV2 正确初始化,即通过这样创建模型:

      model = MobileNetV2(include_top=False)
      

      我无法解释...

      希望这可以回答您最初的问题。

      【讨论】:

        【解决方案3】:

        事实上,这是在文档中指定的,例如在Xception

         input_shape: optional shape tuple, only to be specified
           if `include_top` is False (otherwise the input shape
           has to be `(299, 299, 3)`.
           It should have exactly 3 inputs channels,
           and width and height should be no smaller than 71.
           E.g. `(150, 150, 3)` would be one valid value.
        

        而对于MobileNetV2

         input_shape: Optional shape tuple, to be specified if you would
           like to use a model with an input image resolution that is not
           (224, 224, 3).
           It should have exactly 3 inputs channels (224, 224, 3).
           You can also omit this option if you would like
           to infer input_shape from an input_tensor.
           If you choose to include both input_tensor and input_shape then
           input_shape will be used if they match, if the shapes
           do not match then we will throw an error.
           E.g. `(160, 160, 3)` would be one valid value.
        

        虽然不是很清楚。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-02
          • 1970-01-01
          • 2018-09-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多