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

PR curve of the model trained with 35 classes #13106

Open
1 task done
kim2429 opened this issue Jun 19, 2024 · 2 comments
Open
1 task done

PR curve of the model trained with 35 classes #13106

kim2429 opened this issue Jun 19, 2024 · 2 comments
Labels
question Further information is requested

Comments

@kim2429
Copy link

kim2429 commented Jun 19, 2024

Search before asking

Question

Hello,
I used the YOLOv5s model for training the dataset consisting of 35 classes.
The PR curve displays the per-class legend if classes are less than 21.
How can I get a PR curve displaying the per-class legend in my case?

Additional

No response

@kim2429 kim2429 added the question Further information is requested label Jun 19, 2024
@glenn-jocher
Copy link
Member

Hello,

Thank you for reaching out and for your detailed question! The PR curve visualization in YOLOv5 is indeed optimized for datasets with fewer classes, which is why you see the per-class legend for datasets with up to 20 classes. For datasets with more classes, the legend can become cluttered and less readable.

To address your specific need for displaying the per-class legend with 35 classes, you can modify the plotting script to accommodate more classes. Here's a step-by-step guide to help you achieve this:

  1. Locate the Plotting Script: The relevant code for plotting the PR curve is in the utils/plots.py file.

  2. Modify the Plotting Code: You can adjust the code to ensure that all classes are displayed in the legend. Look for the plot_pr_curve function and modify it to handle more classes. Here's an example modification:

def plot_pr_curve(px, py, ap, save_dir='pr_curve.png', names=()):
    # Plot PR curve
    fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True)
    ax.plot(px, py, linewidth=1, color='grey')
    ax.set_xlabel('Recall')
    ax.set_ylabel('Precision')
    ax.set_xlim(0, 1)
    ax.set_ylim(0, 1)
    ax.grid(True)
    ax.set_title(f'Precision-Recall curve: mAP @kim2429.5 = {ap:.3f}')
    
    # Add per-class legend
    for i, name in enumerate(names):
        ax.plot(px[:, i], py[:, i], linewidth=1, label=f'{name} ({ap[i]:.3f})')
    
    ax.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
    fig.savefig(save_dir, dpi=200)
    plt.close(fig)
  1. Save and Run: Save your changes and re-run your training or evaluation script to generate the updated PR curve with the per-class legend.

This should allow you to visualize the PR curve with all 35 classes included in the legend. If you encounter any issues or need further assistance, please provide a minimum reproducible example of your code and ensure you are using the latest versions of torch and yolov5. You can find more details on creating a minimum reproducible example here.

I hope this helps! If you have any further questions, feel free to ask. 😊

@kim2429
Copy link
Author

kim2429 commented Jun 20, 2024

Thank you for getting back to me.

I adjusted the code in utils/metrics.py following your suggestion.

Unfortunately, I found the plot_pr_curve function in utils/metrics.py, not plots.py.

However, the val.py code did not work.

This is the error message.

Please confirm this.

Traceback (most recent call last):
File "/home/jil/yolov5_v7/val.py", line 39, in
from models.common import DetectMultiBackend
File "/home/jil/yolov5_v7/models/common.py", line 41, in
from utils.dataloaders import exif_transpose, letterbox
File "/home/jil/yolov5_v7/utils/dataloaders.py", line 31, in
from utils.augmentations import (Albumentations, augment_hsv, classify_albumentations, classify_transforms, copy_paste,
File "/home/jil/yolov5_v7/utils/augmentations.py", line 15, in
from utils.general import LOGGER, check_version, colorstr, resample_segments, segment2box, xywhn2xyxy
File "/home/jil/yolov5_v7/utils/general.py", line 52, in
from utils.metrics import box_iou, fitness
File "/home/jil/yolov5_v7/utils/metrics.py", line 186
@tryexcept('WARNING ?� ConfusionMatrix plot failure')
^
SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xa0 in position 9: invalid start byte

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

No branches or pull requests

2 participants