YOLOV11模型转换为Tflite,在Tensorflow中未产生正确的输出

问题描述 投票:0回答:1
转换为Tflite:

import ultralytics as yolo !yolo detect export model=/content/runs/detect/yolov11_anpr/weights/best.pt imgsz=640 batch=1 format=tflite

我当前的Python推理代码(超级分析YOLO) .pt和.tflite模型在超级推理管道中都可以正常工作:
from PIL import Image
from ultralytics import YOLO

image = Image.open("/content/Screenshot From 2025-03-08 16-37-15.png")
model = YOLO('/content/runs/detect/yolov11_anpr/weights/best_saved_model/best_float32.tflite')
results = model(image)

result = results[0]
result.show()

该成功检测到波斯数字:

there是使用Ultrytics Yolo成功检测的视觉表示:licence plate

问题 但是,用张量流(无效)直接推断不会产生正确的检测。输出数据不正确或完全丢失。

问题:

为什么使用Ultrytics Yolo的推理可以起作用,而直接张紧推理则没有? 我在Yolov11 Tflite推理带有Tensorflow的Yolov11 Tflite推理中缺少什么预处理或后处理步骤? 任何直接与TensorFlow正确使用TFLITE模型正确使用TFLITE模型的见解或解决方案都将不胜感激! 您可以使用以下链接下载并测试我的TFLITE模型: https://drive.google.com/file/d/1p4cafl9g2gpjgud68xlr_eqlxz-zz-umtre/view?usp =sharing

为了进行预处理,您需要通过OpenCV加载图像,然后执行以下步骤:

Converver bgr torgb

python tensorflow artificial-intelligence data-science yolo
1个回答
0
投票

将图像加固到模型的预期输入图像大小(在我的情况下,640x640)

  • 通过将值除以255

  • 扩展尺寸

  • 在这里是代码:

    # Load and preprocess image def preprocess_image(image_path, input_shape): image = cv2.imread(image_path) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = cv2.resize(image, (input_shape[1], input_shape[2])) image = image.astype(np.float32) / 255.0 # Normalize to [0, 1] image = np.expand_dims(image, axis=0) # Add batch dimension return image
  • 对于后处理,您需要检查模型的输出维度;就我而言,是(1、6、8400)

    这里是批处理大小,分别为6(4 +类)是前四个值,X中心,Y中心,宽度和检测到的网格高度和86400是置信度分数。

    我在项目中的做法是:
image_width, image_height = image_shape detections = output_data[0] # Shape: (6, 8400) xc = detections[0] # (8400,) - normalized center x yc = detections[1] # (8400,) - normalized center y w = detections[2] # (8400,) - normalized width h = detections[3] # (8400,) - normalized height confs = detections[4:] # (8400,) - classes confidence scores # Apply confidence threshold (e.g., 0.5) threshold = 0.5 # x_min, y_min, x_max, y_max, class_id, confidence boxes = [] for class_id, conf in enumerate(confs): for i in range(len(conf)): if conf[i] > threshold: # Convert to pixel coordinates x_min = int((xc[i] - (w[i] / 2)) * image_width) y_min = int((yc[i] - (h[i] / 2)) * image_height) x_max = int((xc[i] + (w[i] / 2)) * image_width) y_max = int((yc[i] + (h[i] / 2)) * image_height) boxes.append([x_min, y_min, x_max, y_max, class_id, conf[i]])

您以所需的格式输出后,您可以在检测到的框上执行NMS,以消除重叠。毕竟,使用OpenCV注释您的图像:
# Visualize the final boxes on the image
image = cv2.imread('image.jpg')

# Assuming final_boxes is a list of bounding boxes with (x_min, y_min, x_max, y_max, score)
for box in final_boxes:
    x_min, y_min, x_max, y_max, class_id, score = box

    print(x_min, y_min, x_max, y_max, score)
    
    # Draw bounding box
    cv2.rectangle(image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
    
    # Draw score text
    cv2.putText(image, f"{class_id} {score:.2f}", (x_min, y_min - 10),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)    


cv2.imwrite('output.jpg', image)


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.