You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

YOLOv5导出的best-fp16.tflite模型预测结果异常求助

YOLOv5导出FP16 TFLite后检测框异常问题求助

我训练了YOLOv5模型并将其导出为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)  

预测结果对比

  • TFLite预测结果:TFLite生成的错误检测框
  • YOLOv5原生预测结果:YOLOv5原生生成的正确检测框

内容的提问来源于stack exchange,提问作者deivith amaya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 00:35:25