Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Classification model works way worse in YoloDotNet than in Python #32

Open
wrzr123 opened this issue Oct 31, 2024 · 0 comments
Open

Classification model works way worse in YoloDotNet than in Python #32

wrzr123 opened this issue Oct 31, 2024 · 0 comments

Comments

@wrzr123
Copy link

wrzr123 commented Oct 31, 2024

Hello,

I trained a classification model in Python using the Ultralytics Yolo module, and it performs quite well. Then I exported the model to the ONNX format and used it in a .NET web service with this nuget package. There the classification results are way worse than with the original .pt model in the Python environment.

To be a bit more precise on what "worse" means: All my test images (not part of the training and test set of the model training) were classified correctly in Python, and also always with a certainty of >99%. In C#, most of the images are also classified correctly, but the certainty varies a lot. In some cases, the classification also leads to a wrong result, with something like 52% certainty for the wrong class and 47% certainty for the correct class.

I'm new to computer vision (AI), so it's probably not really a bug but just something that I'm missing.

This is the Python code I used for the training (I'm using a MacBook Air with M1):

from ultralytics import YOLO

# Load a model
model = YOLO("yolo11s-cls.pt")  # load a pretrained model (recommended for training)

# Train the model with MPS
results = model.train(data="/Users/admin/PycharmProjects/trainingset", epochs=200, imgsz=640, device="mps")

This is the Python code I used to test the classification for several images (I used images which were neither part of the training nor part of the test set):

from ultralytics import YOLO

# Load a model
model = YOLO("./final_models/classify_amount.pt") 

# Run batched inference on a list of images
results = model(["/Users/admin/Downloads/Card_1.jpeg"])  # return a list of Results objects

# Process results list
for result in results:
    probs = result.probs  # Probs object for classification outputs
    result.show()  # display to screen
    result.save(filename="result.jpg")  # save to disk

This is the simplified C# class I use in my web service which runs the classification:

public static class YoloImageProcessing
{
    // Instantiate the yolo detection model
    private static readonly Yolo AmountClassificationModel = new Yolo(new YoloOptions
    {
        OnnxModel = @"./YoloModels/classify_amount.onnx", // Your Yolo model in onnx format
        ModelType = ModelType.Classification, // Set your model type
        Cuda = false, // Use CPU or CUDA for GPU accelerated inference. Default = true
        GpuId = 0, // Select Gpu by id. Default = 0
        PrimeGpu = false, // Pre-allocate GPU before first inference. Default = false
    });

    public static List<Classification> ProcessCardAmount(IFormFile imageFormFile)
    {
        using var image = SKImage.FromEncodedData(imageFormFile.OpenReadStream());
        return AmountClassificationModel.RunClassification(image, 3);
    }
}

Are there any obvious issues in my code, or are there any fundamental misunderstandings on my side? Is it normal that the ONNX models behave differently (worse) than the pt models?
In case you need any further information about training data, training results or the general use case, I can share everything that's needed.

Thanks in advance for the support!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant