Skip to content

Hunt for the highest MAPΒ #2313

Closed
Closed

Description

@tiwarikaran

Question

How to increase MAP?

Additional context

I trained the yolov5 for arounf 1500 epochs and it had a single class only, the images I downloaded were from google and the sizes were also kinda small majority of them < 640. I made a single class detection and the MAP I got after that was ~0.3. Any ideas how to increase this? I am relatively new to the yolo family.

Any help, any comment, any single idea would be of much much help.

THANKS!

These are the labels
labels

These are the predictions
predictions

We can talk more here
https://www.linkedin.com/in/karan-tiwari-a86673200/

Activity

  1. github-actions commented on Feb 27, 2021

    @github-actions
    Contributor

    πŸ‘‹ Hello @tiwarikaran, thank you for your interest in πŸš€ YOLOv5! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

    If this is a πŸ› Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

    If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

    For business inquiries or professional support requests please visit https://www.ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

    Requirements

    Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

    $ pip install -r requirements.txt

    Environments

    YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

    Status

    CI CPU testing

    If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

  2. glenn-jocher commented on Feb 27, 2021

    @glenn-jocher
    Member

    πŸ‘‹ Hello @tiwarikaran! Thanks for asking about improving training results. Most of the time good results can be obtained with no changes to the models or training settings, provided your dataset is sufficiently large and well labelled. If at first you don't get good results, there are steps you might be able to take to improve, but we always recommend users first train with all default settings before considering any changes. This helps establish a performance baseline and spot areas for improvement.

    If you have questions about your training results we recommend you provide the maximum amount of information possible if you expect a helpful response, including results plots (train losses, val losses, P, R, mAP), PR curve, confusion matrix, training mosaics, test results and dataset statistics images such as labels.png. All of these are located in your project/name directory, typically yolov5/runs/train/exp.

    We've put together a full guide for users looking to get the best results on their YOLOv5 trainings below.

    Dataset

    • Images per class. β‰₯1.5k images per class
    • Instances per class. β‰₯10k instances (labeled objects) per class total
    • Image variety. Must be representative of deployed environment. For real-world use cases we recommend images from different times of day, different seasons, different weather, different lighting, different angles, different sources (scraped online, collected locally, different cameras)
    • Label consistency. All instances of all classes in all images must be labelled. Partial labelling will not work.
    • Label accuracy. Labels must closely enclose each object. No space should exist between an object and it's bounding box.
    • Background images. Background images are images with no objects that are added to a dataset to reduce False Positives (FPs). We recommend about 0-10% background images (COCO has 1000 background images for reference, 1% of the total).

    COCO Analysis

    Model Selection

    Larger models like YOLOv5x will produce better results in nearly all cases, but have more parameters and are slower to run. For mobile applications we recommend YOLOv5s/m, for cloud or desktop applications we recommend YOLOv5l/x. See our README table for a full comparison of all models.

    To start training from pretrained weights simply pass the name of the model to the --weights argument. Models download automatically from the latest YOLOv5 release.

    python train.py --data custom.yaml --weights yolov5s.pt
                                                 yolov5m.pt
                                                 yolov5l.pt
                                                 yolov5x.pt

    YOLOv5 Models

    Training Settings

    Before modifying anything, first train with default settings to establish a performance baseline. A full list of train.py settings can be found in the train.py argparser.

    • Epochs. Start with 300 epochs. If this overfits early then you can reduce epochs. If overfitting does not occur after 300 epochs, train longer, i.e. 600, 1200 etc epochs.
    • Image size. COCO trains at native resolution of --img 640, though due to the high amount of small objects in the dataset it can benefit from training at higher resolutions such as --img 1280. If there are many small objects then custom datasets will benefit from training at native or higher resolution. Best inference results are obtained at the same --img as the training was run at, i.e. if you train at --img 1280 you should also test and detect at --img 1280.
    • Batch size. Use the largest --batch-size that your hardware allows for. Small batch sizes produce poor batchnorm statistics and should be avoided.
    • Hyperparameters. Default hyperparameters are in hyp.scratch.yaml. We recommend you train with default hyperparameters first before thinking of modifying any. In general, increasing augmentation hyperparameters will reduce and delay overfitting, allowing for longer trainings and higher final mAP. Reduction in loss component gain hyperparameters like hyp['obj'] will help reduce overfitting in those specific loss components. For an automated method of optimizing these hyperparameters, see our Hyperparameter Evolution Tutorial.
  3. tiwarikaran commented on Feb 27, 2021

    @tiwarikaran
    Author

    Hello @glenn-jocher your wonderful and insightful comment has helped me in many unexpected ways, I thought lets write it out and try if some fellow student like me is kinda free to share his knowledge. Thanks again for this.

    I tried to run the same code today afternoon and it seemed to give me wonderful results, here's a snippet of the metrics from the latest experiment I ran.

    image

    Also when I saw your profile and learnt you're one of the many people who've made YOLO what it is today. I would say using the yolov5 is more easier than many of the auto ML libraries there exist. Yolov4 said that "this" should be accessible to everyone. V5 stands firmly on those grounds.

    PS YOLO (you only look once) is hands down the coolest name for an algorithm that exists in Computer Vision.

  4. glenn-jocher commented on Feb 27, 2021

    @glenn-jocher
    Member

    @tiwarikaran thanks buddy! Yes, looking at your results everything looks good, though as the guide says since your val losses have not started overfitting you can actually benefit from even longer training, i.e. 2k or 3k epochs. Sometimes with small datasets especially much longer training is required to get the best results.

  5. glenn-jocher commented on Feb 27, 2021

    @glenn-jocher
    Member

    @tiwarikaran also you should be very careful with your labels, I noticed not all instances are labelled in all images, which is going to produce worse results.

  6. kinoute commented on Feb 28, 2021

    @kinoute
    Contributor

    Epochs. Start with 300 epochs. If this overfits early then you can reduce epochs. If overfitting does not occur after 300 epochs, train longer, i.e. 600, 1200 etc epochs.

    @glenn-jocher Is there no way to apply early stopping? I can see we're getting a best.pt model at the end of the training, which seems to be generated according to an equation mixing Precision, Recall, mAP@.5 and mAP@.5-0.95 here:

    yolov5/train.py

    Lines 376 to 379 in c2026a5

    # Update best mAP
    fi = fitness(np.array(results).reshape(1, -1)) # weighted combination of [P, R, mAP@.5, mAP@.5-.95]
    if fi > best_fitness:
    best_fitness = fi

    Is it similar to what other kind of models are doing in image classification when they stop the training of the model if the validation loss stops decreasing after X epochs (named "patience")?

    To reformulate my question: is it safe to always use the best.pt model as it shouldn't have any sign of overfitting or should we maybe implement an early stopping feature that tracks the validation loss instead?

    Thanks!

  7. glenn-jocher commented on Feb 28, 2021

    @glenn-jocher
    Member

    @kinoute I think 'early stopping' term is used widely in TF and officially supported there, but you are right there is no comparable functionality here. So if you train with --epochs 1000 and overfitting occurs after 100 epochs it will keep training all the way to 1000.

    best.pt will work as intended, saved at the maximum fitness. We used to define fitness as inverse val loss (so best.pt would be saved at the min val loss epoch), but based on user feedback we changed this to the current combination of metrics. The two are usually never the same epoch, but instead are in the same vicinity. Still, users don't like to see best.pt test to a lower mAP than the best mAP observed in training.

    The most important point though in the above user guide is that you want to observe some overfitting in your results. If validation loss is lowest at the final epoch as in #2313 (comment) then you are not achieving your best performance, and should restart your training with longer epochs.

  8. github-actions commented on Mar 31, 2021

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  9. kinoute commented on Mar 31, 2021

    @kinoute
    Contributor

    @glenn-jocher Not sure if it's already the case but your comment #2313 (comment) is great and it might be a good idea to add it to the Wiki!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions