整形.cc:55 stretch_dim!=-1.节点编号X(RESHAPE)准备失败



我是这一切的新手,我需要一些帮助来使用自定义的tflite-yolov3微小模型运行推理。我得到的错误是:

File "/usr/local/lib/python3.6/dist-packages/tensorflow/lite/python/interpreter.py", line 524, in invoke
self._interpreter.Invoke()
RuntimeError: tensorflow/lite/kernels/reshape.cc:55 stretch_dim != -1 (0 != -1)Node number 35 (RESHAPE) failed to prepare.

我做了什么才来到这里:

  1. 使用该项目训练了一个用于对象检测的自定义yolov3微小模型,仅检测1个类https://github.com/pythonlessons/TensorFlow-2.x-YOLOv3.git
  2. 使用的默认超参数:https://github.com/pythonlessons/TensorFlow-2.x-YOLOv3/blob/master/yolov3/configs.py
  3. 每晚使用tf
  4. 模型如下:https://github.com/vladimirhorvat/y/blob/master/app/src/main/assets/converted_model.tflite

当训练模型时,我通过运行推理来测试SavedModel,它起作用了。将SavedModel转换为tflite,使用以下代码对其进行推理,并从标题中收到错误:

interpreter = tf.lite.Interpreter(model_path="converted_model.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
input_shape = input_details[0]['shape']
input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()

(这个代码来自这里,顺便说一句https://www.tensorflow.org/lite/guide/inference#load_and_run_a_model_in_python(

节点35:的数据

type: Reshape
location: 35
inputs
data: name: functional_1/tf_op_layer_Tile_3/Tile_3;StatefulPartitionedCall/functional_1/tf_op_layer_Tile_3/Tile_3
shape: name: functional_1/tf_op_layer_strided_slice_6/strided_slice_6;StatefulPartitionedCall/functional_1/tf_op_layer_strided_slice_6/strided_slice_6
outputs
reshaped: name: functional_1/tf_op_layer_strided_slice_16/strided_slice_16;StatefulPartitionedCall/functional_1/tf_op_layer_strided_slice_16/strided_slice_16

请帮忙。我没有主意了。

我通过下面这篇类似的帖子解决了完全相同的问题:https://stackoverflow.com/a/62552677/11334316

本质上,在转换您的模型时,您必须执行以下操作:

batch_size = 1
model = tf.keras.models.load_model('./yolo_model')
input_shape = model.inputs[0].shape.as_list()
input_shape[0] = batch_size
func = tf.function(model).get_concrete_function(tf.TensorSpec(input_shape, model.inputs[0].dtype))
model_converter = tf.lite.TFLiteConverter.from_concrete_functions([func])
model_lite = model_converter.convert()
f = open("./yolo_model.tflite", "wb")
f.write(model_lite)
f.close()

相关内容

  • 没有找到相关文章

最新更新