我正在尝试将CNN模型转换为tflite模型。我成功地转换了它,但当我尝试加载和运行模型时会发生此错误。我正在构建一个flutter应用程序。
它初始化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.
我想我已经解决了这个问题。
在花了几天时间试图解决这个问题之后。我发现我用来转换的模型是一个ImagNet预训练的模型,它是InceptionV3。问题可能是有些图层无法转换。
我使用了以下内容,它们运行得非常好。
- MobileNet和MobileNetV2
- NasNet Mobile版本
- 或者,如果你是深度学习的新手,不想训练或跳过深度学习部分,你可以使用可撕裂机器,然后将其轻松转换
我希望这能帮助到你们!!谢谢
过去几天我遇到了完全相同的问题。我试着在安卓系统上加载并运行一个tflite模型。我终于想出了解决这个问题的办法。
我创建我的模型使用:
model = Xception(include_top=False)
这里的重要部分是include_top=False
,以及默认参数input_shape=None
。
如果你看一下Xception、Inception、MobileNet或其他什么(你可以在这里找到(的源代码,你会发现在创建他们称之为的第一层之前的某个时候
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)
它在这里实现,对我们来说最重要的部分是:
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似乎真的无法使用它(可能这相当于设置了无限的图像大小(。因此,初始化解释器时出现此错误:
字节溢出所需的元素数
当我在构造函数中设置正确的input_shape
参数时,即
model = Xception(include_top=False, weights=None, input_shape=(rows, cols, channels))
然后转换后的模型在Android上运行良好。
至于为什么在相同的情况下使用MobileNetV2正确初始化,即通过创建这样的模型:
model = MobileNetV2(include_top=False)
我无法解释。。。
希望这能给你最初的问题带来答案。
事实上,这是在文档中指定的,例如在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.
尽管它不是很清楚。