使用OpenVINO Runtime运行导出的YOLOv8模型异常问题求助
使用OpenVINO Runtime运行YOLOv8导出模型结果异常问题
我用OpenVINO Runtime运行导出为OpenVINO格式的YOLOv8模型时,模型能正常启动运行,但返回的检测结果完全不符合预期。用YOLO框架直接加载该导出模型进行预测能得到正确结果,但通过OpenVINO Core运行就出现错误结果。我需要用OpenVINO Runtime对接OpenVINO Model Server,已经尝试调整模型导出参数和图像预处理方式,问题仍未解决,推测是输出结果的处理环节存在问题。
测试YOLO代码(含模型导出与预测)
from shutil import copyfile from ultralytics import YOLO model = YOLO("best.pt") model.predict("test.jpg", save=True) copyfile("test.jpg", "test_openvino.jpg") model.export(format="openvino", dynamic=True) openvino_model = YOLO("best_openvino_model/") openvino_model.predict("test_openvino.jpg", save=True)
测试OpenVINO Runtime代码
import cv2 import numpy as np from openvino.runtime import Core ie = Core() model = ie.read_model(model="best_openvino_model/best.xml") compiled_model = ie.compile_model(model=model, device_name="CPU") input_layer_ir = compiled_model.input(0) output_layer_ir = compiled_model.output() image = cv2.imread("test.jpg") # N, C, H, W = input_layer_ir.shape N, C, H, W = 1, 1, 480, 480 resized_image = cv2.resize(image, (W, H)) input_image = np.expand_dims(resized_image.transpose(2, 0, 1), 0) output = compiled_model([input_image])[output_layer_ir] output = cv2.transpose(output[0]) boxes = [] scores = [] class_ids = [] for row in output: # Each row is [x, y, width, height, probability class 0, probability class 1, ...] classes_scores = row[4:] (_min_score, max_score, _min_class_loc, (_x, max_class_index)) = cv2.minMaxLoc(classes_scores) if max_score >= 0.25: box = [row[0] - (0.5 * row[2]), row[1] - (0.5 * row[3]), row[2], row[3]] boxes.append(box) scores.append(max_score) class_ids.append(max_class_index) [height, width, _] = image.shape length = max((height, width)) scale = length/480 # Apply NMS (Non-maximum suppression) RESULT_BOXES = cv2.dnn.NMSBoxes(boxes, scores, 0.5, 0.6, 0.5) for index in RESULT_BOXES: box = boxes[index] x, y = round(box[0] * scale), round(box[1] * scale) x_plus_w, y_plus_h = round((box[0] + box[2]) * scale), round((box[1] + box[3]) * scale) image = cv2.rectangle(cv2.UMat(image), (x, y), (x_plus_w, y_plus_h), (0, 0, 255), 8) image = cv2.putText(cv2.UMat(image), f"handgun {round(scores[index], 2)}", (x - 10, y - 12), cv2.FONT_HERSHEY_SIMPLEX, 3, (0,0,255), 8) cv2.imwrite("test_openvino_runtime.jpg", image)
相关信息
- YOLOv8模型:
best.pt - 测试图片:
test.jpg - 使用的Python库版本:
- ultralytics==8.0.196
- onnx==1.15.0
- onnxruntime==1.16.2
- openvino==2023.2.0
- openvino-dev==2023.2.0
内容的提问来源于stack exchange,提问作者mattmy
相关产品推荐
相关产品推荐

