ultralytics / ultralytics

Ultralytics YOLO11 πŸš€
https://docs.ultralytics.com
GNU Affero General Public License v3.0
36.58k stars 7.05k forks source link

YOLOv8 Classifiction export to XML doesn't work!!! #18970

Closed image-rcdat closed 2 weeks ago

image-rcdat commented 2 weeks ago

Hi guys, I've trained a classifier of 373 classes with yolov8n. I've used this piece of code to convert my .pt model to .xml:

import openvino 
from ultralytics import YOLO
import shutil

MODEL_NAME = "./make_yoloV8_372.pt"
model = YOLO(MODEL_NAME, task='classify')
model.export(format='openvino')

But, xml model in inference time, answers are irrelevant. I've used this piece of code to check the prediction results,

import cv2
import numpy as np
from ultralytics import YOLO

onnx_model = YOLO('model_yoloV8_372.pt', task='classify')

# Load labels file
f = open('labels.txt', 'r')
lines = f.readlines()
make_cls = (lines[0].split(';'))

# Load model
ie = Core()
model = ie.read_model("model_yoloV8_372.xml")
compiled_model = ie.compile_model(model, "CPU")

# Prepare input
image = cv2.imread("test.jpeg")
input_blob = next(iter(compiled_model.inputs))
image_resized = cv2.resize(image, (640, 640))  # Adjust to model input size
image_transposed = image_resized.transpose(2, 0, 1)  # Convert to CHW
image_input = np.expand_dims(image_transposed, axis=0).astype(np.float32)

# Run inference
results = compiled_model([image_input])
results1 = onnx_model(image)

# Process results
id = (np.argmax(results[0]))
max_conf_idx = results1[0].probs.top1  # Gets the highest probability class ID

print('-----------------------------------')
print('-----------------------------------')
print("Prediction class of pt model: ", make_cls[max_conf_idx])
print('Prediction class of XML model: ', make_cls[id])

could you please help me out?

UltralyticsAssistant commented 2 weeks ago

πŸ‘‹ Hello @image-rcdat, thank you for reaching out and sharing your issue with Ultralytics πŸš€. We recommend checking out our Docs for guidance on using YOLOv8's features, where you'll find useful Python Tips and export-related details.

If this is a πŸ› Bug Report, we kindly ask you to provide a minimum reproducible example of your issue to help us better understand and debug it. Please include all relevant details like your environment configuration and ensure that the dataset, code, and inputs can be tested independently.

A Few Suggestions:

  1. Confirm you're running the latest version of the package by upgrading with:
    pip install -U ultralytics
  2. Verify your model export workflow aligns with the current YOLO export documentation.
  3. Double-check your inference pipeline, ensuring consistency in preprocessing (e.g., image resizing, normalization) between pt and xml models.

Ultralytics Environments

YOLO can run in any of the following environments if properly set up:


Help Our Team Help You πŸ› οΈ

An Ultralytics engineer will review your issue soon. To make debugging more efficient, please also confirm:

  1. What versions of Python, PyTorch, and OpenVINO are you using?
  2. Was there any warning or error during export or inference time?
  3. Are the labels.txt classes consistent with your trained model?

Finally, if you'd like to engage with our community, explore:

We appreciate your patience and your contribution to improving Ultralytics! ✨

glenn-jocher commented 2 weeks ago

@image-rcdat thanks for reaching out. Let's help resolve your OpenVINO export issue. Based on your code, there are two key points to address:

  1. Preprocessing Alignment: The OpenVINO-exported model expects normalized RGB input (0-1 range) with channels-first format. Your current preprocessing lacks normalization (/255.0) and uses BGR color space from cv2.imread. We recommend:
image = cv2.cvtColor(cv2.imread("test.jpeg"), cv2.COLOR_BGR2RGB)  # Convert to RGB
image_input = image_resized.transpose(2, 0, 1)[None]/255.0  # Add batch dim and normalize
  1. Model Loading: For direct comparison, load both models consistently using the YOLO interface:
    ov_model = YOLO('model_yoloV8_372_openvino_model/')  # Load exported OpenVINO dir
    results_ov = ov_model(image)

For reference, our OpenVINO export documentation details the required preprocessing steps. If issues persist, please share your metadata.yaml from the OpenVINO export directory to verify the model configuration.

image-rcdat commented 2 weeks ago

Thanks, It works for me, I've changed the color channels and normalized the image

glenn-jocher commented 1 week ago

@image-rcdat great to hear that resolved the issue! For future reference, our OpenVINO Export Docs detail these preprocessing requirements. If you encounter other challenges or have commercial deployment needs, feel free to explore our Enterprise License options. Happy inferencing! πŸš€