ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.22k stars 16.21k forks source link

Questions about performance of focal loss and label smoothing #8469

Closed kimkihoon0515 closed 2 years ago

kimkihoon0515 commented 2 years ago

Search before asking

Question

I tried two methods to improve performance. However, the performance was the best when applied as a baseline. Can you roughly know for what reason?

Additional

No response

github-actions[bot] commented 2 years ago

👋 Hello @kimkihoon0515, 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://ultralytics.com or email support@ultralytics.com.

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

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

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), validation (val.py), inference (detect.py) and export (export.py) on macOS, Windows, and Ubuntu every 24 hours and on every commit.

glenn-jocher commented 2 years ago

@kimkihoon0515 👋 Hello! Thanks for asking about improving YOLOv5 🚀 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

COCO Analysis

Model Selection

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

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.

Further Reading

If you'd like to know more a good place to start is Karpathy's 'Recipe for Training Neural Networks', which has great ideas for training that apply broadly across all ML domains: http://karpathy.github.io/2019/04/25/recipe/

Good luck 🍀 and let us know if you have any other questions!

kimkihoon0515 commented 2 years ago

@glenn-jocher thx for the brief response. Can u tell me why u think those methods didn't work well on your training environments?

buxihuo commented 2 years ago

@glenn-jocher thx for the brief response. Can u tell me why u think those methods didn't work well on your training environments?

@kimkihoon0515 Hello, I have some ideas about these two technologies. I wrote the answer with translation software. Focal loss can alleviate the imbalance of foreground background proportion in single-stage object detection, while the classification loss of yolov5 is only calculated on foreground objects, and the object loss is not seriously unbalanced. Moreover, focal has some parameters, so it is not as good as BCE loss function (baseline). While the label smoothing technology improves the robustness, it will cause the performance growth to slow down in the early stage of training, and will lose in class information. It also needs to adjust the degree of smoothing. Different values need to be adjusted for different data sets to obtain better results, and the growth effect is not great.

ckyrkou commented 2 years ago

alleviate the imbalance of foreground background proportion in single-stage object detection, while the classification loss of yolov5 is only calculated on foreground objects, and the object loss is not seriously unbalance

I am curious why you say that the objectness loss in not imbalanced. A detection layer can have size of 26x26 and only a few objects say 10, so it could be in the range of 10:670. Since the majority of these locations are non-objects then the output will tend to be zero, hence no object are detected.

github-actions[bot] commented 2 years ago

👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 🚀 resources:

Access additional Ultralytics ⚡ resources:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐!

glenn-jocher commented 11 months ago

@ckyrkou Good point! The output distribution could indeed become imbalanced, especially for the small objects you mentioned. Focal loss is designed to address this issue by down-weighting the easy examples. However, in YOLOv5, the default settings and anchor box system tend to balance the object/background ratio, often at the pixel level. This means that in practice, the objectness loss in YOLOv5 is often balanced internally. While focal loss could still be beneficial in certain scenarios, the default balance commonly minimizes the need for it. Thanks for bringing up the discussion! Let me know if there's anything else you'd like to explore.

ckyrkou commented 10 months ago

Thanks for the reply so it turns out that the issues I spent two years on trying to identify between my efforts to reproduce yolo with custom code and the yolov5 repo was due to the facts that: 1) The default display threshold for bounding boxes is 0.25 not the one used during training 2) The map evaluation uses a confidence threshold of 0.001

So the code I was writing was correct but I could never match the same results because the evaluation protocol was different and it didn't even cross my mind that these parameters would be hard coded in such a way. For the map I have neve seen this threshold used before.

Anyway it was a fun experience and chance to dig deep into the yolov5 codebase and appreciate how well written it is :)

glenn-jocher commented 10 months ago

@ckyrkou that's a huge discovery! It's fascinating how such subtle differences can have significant impacts on results. 🕵️‍♂️ Thanks for sharing your insights, and we're glad to hear that your deep dive into the YOLOv5 codebase was a rewarding experience. 🚀 Should you have any further questions or insights to share, feel free to reach out. Keep up the great work!

ckyrkou commented 10 months ago

Well not so huge discovery I would say. But having a reference to the correct way to compute mAP evaluation would be nice. For instance how is this done in the COCO evaluator?

glenn-jocher commented 10 months ago

@ckyrkou the COCO evaluator computes mAP by considering all bounding boxes with a confidence score above a certain threshold (default 0.001). The evaluation is performed by comparing the predicted bounding boxes with the ground truth boxes using different Intersection over Union (IoU) thresholds, typically ranging from 0.5 to 0.95.

You can find more details on how the COCO evaluator computes mAP and handles confidence thresholds in the official COCO repository and the associated documentation. This could serve as a valuable reference for understanding the evaluation process. If you have any further questions or need additional insights, feel free to ask.

ckyrkou commented 10 months ago

Great to know. Thanks for the prompt response.

ckyrkou commented 10 months ago

I would appreciate if you can pinpoint me to the exact place in the COCO repository and the associated documentation where these are mentioned.

glenn-jocher commented 10 months ago

@ckyrkou apologies for any confusion, but there seems to have been a misunderstanding. The details I referred to aren't directly documented in the COCO repository, as the specific implementation details can vary across different frameworks and repositories. For insights on the COCO evaluation specifics, I recommend referring to the official COCO dataset documentation and exploring related research papers for a deeper understanding.

If you have any more questions or need further assistance, feel free to ask!