Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在转换的tflite模型上调用'allocate_tensors()'时发生运行时错误_Python_Tensorflow_Keras_Tensorflow Lite - Fatal编程技术网

Python 在转换的tflite模型上调用'allocate_tensors()'时发生运行时错误

Python 在转换的tflite模型上调用'allocate_tensors()'时发生运行时错误,python,tensorflow,keras,tensorflow-lite,Python,Tensorflow,Keras,Tensorflow Lite,我使用TF Lite部署了TensorFlow模型,一切正常。但是,当我尝试使用自己的模型(从保存的kerasmodel转换而来)时,调用allocate\u tensors()方法时会出现以下错误: --------------------------------------------------------------------------- RuntimeError Traceback (most recent call las

我使用TF Lite部署了TensorFlow模型,一切正常。但是,当我尝试使用自己的模型(从保存的
keras
model转换而来)时,调用
allocate\u tensors()
方法时会出现以下错误:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-73-6b4d64de8090> in <module>
      1 #interpreter = tflite.Interpreter(model_path='model.tflite')
      2 interpreter = tflite.Interpreter(model_path=lite_model_location)
----> 3 interpreter.allocate_tensors()

~/pyenv/srcnn/lib/python3.6/site-packages/tflite_runtime/interpreter.py in allocate_tensors(self)
    257   def allocate_tensors(self):
    258     self._ensure_safe()
--> 259     return self._interpreter.AllocateTensors()
    260 
    261   def _safe_to_run(self):

RuntimeError: external/org_tensorflow/tensorflow/lite/core/subgraph.cc BytesRequired number of elements overflowed.
Node number 0 (CONV_2D) failed to prepare.
而我的非工作
tflite
模型的输入详细信息如下:

{'name': 'input_1',
 'index': 0,
 'shape': array([1, 1, 1, 3], dtype=int32),
 'shape_signature': array([-1, -1, -1,  3], dtype=int32),
 'dtype': <class 'numpy.float32'>,
 'quantization': (0.0, 0),
 'quantization_parameters': {'scales': array([], dtype=float32),
 'zero_points': array([], dtype=int32),
 'quantized_dimension': 0}, 
 'sparsity_parameters': {}}
{'name':'input_1',
“索引”:0,
“形状”:数组([1,1,1,3],dtype=int32),
“shape_签名”:数组([-1,-1,-1,3],dtype=int32),
“数据类型”:,
“量化”:(0.0,0),
'量化参数':{'scales':数组([],dtype=float32),
“零点”:数组([],dtype=int32),
“量化的_维度”:0},
“稀疏_参数”:{}

可能是转换的原因吗?该模型在使用
keras
的开发过程中运行良好,应该能够接受可变x和y维度(图像大小)的输入。我不认为数据类型是这里的问题,因为文档中应该同时支持
uint8
float32

好的,很容易解决。当使用具有未知输入维度的CNN时(即此处
形状签名中的
-1
,由于在输入层中设置
-1
),输入张量中的未知维度设置为
1
。要使模型在使用此类模型时正确分配,您必须做两件事:

  • 手动将输入张量的形状设置为输入数据的形状,例如
    解释器。调整张量的大小\u输入(0、[1,input\u shape[0],input\u shape[1],3],strict=True)
  • 手动设置输入数据的
    dtype
    ,以匹配模型输入层的数据,如输入详细信息中的
    'dtype'
    条目所示

  • 这似乎是在常规TensorFlow中自动完成的,但在Lite版本中必须像这样准备模型。

    正如错误所示,
    bytes所需的元素数溢出
    ,数据类型
    uint8
    float32
    可能存在问题。此外,两种型号的
    形状
    也不同。这将在运行TFLite模型时引发异常。我也倾向于形状。然而,模型应该能够接受任何大小的3通道图像的输入,因此签名中的[-1,-1,-1,3]。
    'shape'
    'shape\u签名'
    必须匹配吗?我可以在分配之前在模型中更改它们吗,还是应该在模型转换时固定?
    {'name': 'input_1',
     'index': 0,
     'shape': array([1, 1, 1, 3], dtype=int32),
     'shape_signature': array([-1, -1, -1,  3], dtype=int32),
     'dtype': <class 'numpy.float32'>,
     'quantization': (0.0, 0),
     'quantization_parameters': {'scales': array([], dtype=float32),
     'zero_points': array([], dtype=int32),
     'quantized_dimension': 0}, 
     'sparsity_parameters': {}}