【问题标题】:prediction of the yolov5 model using its export in tflite使用在 tflite 中的导出预测 yolov5 模型
【发布时间】:2022-11-10 18:09:54
【问题描述】:

有人可以帮帮我吗,我已经训练了一个模型并将其导出为 tflite 的文件,best-fp16.tflite,但是我无法做出正确的预测,我想是这样,或者我什么都没看到类似,我已经在图像上绘制了所有框,但它们与 yolov5 中的结果完全不同。

我的代码如下

img = cv2.resize(img, (640, 640))
newimg = cv2.resize(img, (640, 640))

insigni = interpreter.get_signature_list()
print("insigni",insigni)
input_shape = input_details[0]['shape']
input_tensor= np.array(np.expand_dims(img,0), dtype=np.float32)
input_details = interpreter.get_input_details()[0]
interpreter.set_tensor(input_details['index'],input_tensor)
interpreter.invoke()

output_data = interpreter.get_tensor(output_details[0]['index'])  # get tensor  x(1, 25200, 7)
output_data = output_data[0]  # x(1, 25200, 7) to x(25200, 7)
print("output_data",len(output_data))
xywh = output_data[..., :4]  # boxes  [25200, 4]
print("xywh",xywh)
conf = output_data[..., 4:5]  # confidences  [25200, 1]
cls = tf.reshape(tf.cast(tf.argmax(output_data[..., 5:], axis=1), tf.float32), (-1, 1))  # classes  x(25200, 1) 
output = np.squeeze(tf.concat([conf, cls, xywh], 1))  #  [25200, 1], x(25200, 1), [25200, 4] to [25200, 6] (confidences, classes, x, y, w, h)

scores = output[..., 0]  # scores [25200]
classes = output[..., 1]  # classes [25200]
boxes = output[..., 2:]  # boxes [25200, 4]
# Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right
x, y, w, h = boxes[..., 0], boxes[..., 1], boxes[..., 2], boxes[..., 3] #xywh
x,y,x2,y2 = [x - w / 2, y - h / 2, x + w / 2, y + h / 2]  # xywh to xyxy   [25200, 4]

h1,w1,c =img.shape

print("scores",scores)
for i in range(len(x)):
  if scores[i] >0.99 and c:
    y_min = int(max(1, (y[i] * h1)))
    x_min = int(max(1, (x[i] * w1)))
    y_max = int(min(h1, (y2[i] * h1)))
    x_max = int(min(w1, (x2[i] *w1)))
    cv2.rectangle(img, (x_min, y_min), (x_max, y_max), (255, 255, 255), 2)

cv2.imwrite("/content/imagenee.png",img)  


[(https://i.stack.imgur.com/fQECc.png)](https://i.stack.imgur.com/fQECc.png)

but the result on the same image in yolov5 is

[(https://i.stack.imgur.com/B7L2T.jpg)](https://i.stack.imgur.com/B7L2T.jpg)```


【问题讨论】:

    标签: yolov5 tflite


    【解决方案1】:

    在自己努力解决这个问题并偶然发现这个线程之后,我找到了答案。

    YOLOv5 训练模型期望您的图像在传递到张量时被归一化。

    对我有用的是

    image_array = np.asarray(img)
    
    normalized_image_array = image_array.astype(np.float32) / 255.0
    
    data  = np.zeros((1, 640, 640, 3), dtype='float32')
    
    data[0] = normalized_image_array
    interpreter.set_tensor(input_details[0]['index'], data)
    interpreter.invoke()
    

    请注意,如果您使用 640x640 以外的其他分辨率进行训练,则必须更改 data 的初始化。

    我从https://github.com/quoctoann3/yolov5_to_tflite_inference/tree/ba22b4849d6a92637abc71b1586a25a7352afa6b的源代码中找到了答案

    【讨论】:

      猜你喜欢
      • 2022-07-19
      • 2022-07-09
      • 2021-08-11
      • 2022-08-08
      • 2023-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多