8000 Why the confusion matrix when I test the network only prints the first row? · Issue #13536 · ultralytics/yolov5 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Why the confusion matrix when I test the network only prints the first row? #13536

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

Open
1 task done
giuliohubb opened this issue Mar 18, 2025 · 4 comments
Open
1 task done
Labels
detect Object Detection issues, PR's question Further information is requested

Comments

@giuliohubb
Copy link

Search before asking

Question

Hello!!

I am using YOLOv5 for a small object detection problem.

When I try to test the model after the training on the test set I use the val.py file with task = test and it works correctly. The only issue is that when it saves the confusion matrix it prints only the first row (Here's two examples, one with normalise = True and one with normalise = False).

Do you know why this is the case? I tried to look at the code but I've found no issues. I have three classes ['cosmics', 'stars', 'satellites'], cosmics are overrepresented - 63383 instances - followed by stars - 6365 instances - and satellites - 620 instances. The IOU thres is 0.5 and I haven't touched the conf_thres

Image Image

Additional

One other thing:

the image size is 532, in training it has been rounded to 544 because it must be multiple of max stride 32. When I test the network if I put the same image size - 544 - it reaches a mAP = 0.658 while if I put image size = 640 it reaches a mAP = 0.852. Do you have an explanation for this behaviour? In training the image size was 544, reaching an mAP = 0.874. And the confusion matrix in the training is printed correctly (I put it down below). I have seen the documentation many times without finding those answers. Thank you very much for your help.

Image
@giuliohubb giuliohubb added the question Further information is requested label Mar 18, 2025
@UltralyticsAssistant UltralyticsAssistant added the detect Object Detection issues, PR's label Mar 18, 2025
@UltralyticsAssistant
Copy link
Member

👋 Hello @giuliohubb, thank you for reaching out and for your detailed explanation and examples! 😊

This is an automated response to let you know that your issue is being reviewed. An Ultralytics engineer will assist you soon.

In the meantime, here are a few steps you can take to ensure we can better assist you:

  • If this is a 🐛 Bug Report, please provide a minimum reproducible example (MRE), including a clear description of the steps to reproduce the issue, your environment details (Python version, PyTorch version, etc.), and any relevant code snippets or configurations.

  • If this is a ❓ Question, please share as much detail as possible, including any relevant training logs, dataset details, and the specific commands you are using. You can also verify your setup against our Tips for Best Training Results.

Requirements

Ensure you are using Python>=3.8.0 and have installed all required dependencies from our requirements.txt, including PyTorch>=1.8. To check or update your environment, you can follow these steps:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 supports several verified environments with all dependencies preinstalled, such as:

Status

Our automated tests ensure that YOLOv5 works correctly across macOS, Windows, and Ubuntu for training, validation, inference, export, and benchmarking. You can check the status here:
YOLOv5 CI

If the issue persists or you discover new details, feel free to update this thread. We appreciate your patience and contributions! 🚀

@pderrenger
Copy link
Member

@giuliohubb the confusion matrix showing only the first row typically occurs when your model is overwhelmingly predicting one class (likely 'cosmics' due to severe class imbalance). This indicates the model isn't detecting other classes during validation. For small object detection, we recommend:

  1. Class Imbalance Mitigation: Try class_weights in your loss function or oversample minority classes
  2. Input Size Consistency: Use matching train/val image sizes (544x544 in your case). The mAP increase at 640 suggests your test data benefits from higher resolution, but this should be validated against your training setup

For confusion matrix interpretation, see our Confusion Matrix guide. To verify predictions, inspect model.predict() outputs directly.

@giuliohubb
Copy link
Author

Thank you for your answer.

'This indicates the model isn't detecting other classes during validation.'

I don't think this was the case, indeed if you look at the first confusion matrix I sent you can clearly see the colour code in the shells correctly displayed, only the numbers are missing. I think it's an issue in the plots. Also if you look at the second confusion matrix you can see that 1180/6365 stars were misclassified as cosmics, but that means that the rest were correctly classified.
Additionally during the training the class imbalance was the same, but the confusion matrix has been printed correctly.
I add here the images from the validation, related to the prediction, showing that the model isn't detecting only cosmics.

Image

@pderrenger
Copy link
Member

@giuliohubb thanks for clarifying! You're correct that the normalized confusion matrix (first image) shows color-coded relative class distributions, but numerical values may be too small to display clearly in the first row due to class imbalance. For absolute counts, we recommend using normalize=False in the val.py command (as shown in your second matrix). The plotted numbers scale with the normalized values, so low-frequency classes might not show text labels. You can inspect the raw matrix data in the results/*.json or results/*.txt files for precise counts.

Regarding image size differences: The mAP variance between 544/640px likely stems from your small objects benefiting from higher resolution during inference. For optimal results, maintain consistent train/val image sizes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
detect Object Detection issues, PR's question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants
0