【问题标题】:reshape.cc:55 stretch_dim != -1. Node number X (RESHAPE) failed to preparereshape.cc:55 拉伸尺寸!= -1。节点号 X (RESHAPE) 准备失败
【发布时间】:2020-07-07 21:09:55
【问题描述】:

我是这一切的新手,我需要一些帮助来使用自定义 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

请帮忙。我没有想法。

【问题讨论】:

  • 我在 TF 2.3 中开始遇到这个问题。现在我只使用 TF 2.3 进行训练,我回滚到 TF 2.2 进行 tflite 导出。

标签: python tensorflow tensorflow2.0 yolo


【解决方案1】:

我可以通过以下类似的帖子解决完全相同的问题: 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    相关资源
    最近更新 更多