ultralytics / yolov5

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

Loss scaling in ComputeLoss. #2704

Closed Itto1992 closed 3 years ago

Itto1992 commented 3 years ago

❔Question

Thank you for your creating such a nice repo!

I want to know why the loss is scaled by batch size at the last line of ComputeLoss. https://github.com/ultralytics/yolov5/blob/9ccfa85249a2409d311bdf2e817f99377e135091/utils/loss.py#L161

In my understanding, the loss value has already been summed up across a batch and proportional to the batch size. Is is a special technique for training yolov5?

Thanks!

Additional context

github-actions[bot] commented 3 years ago

👋 Hello @Itto1992, 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.

glenn-jocher commented 3 years ago

@Itto1992 yes the loss is scaled by batch size before optimizing! The loss is scaled by batch size to allow everyone to achieve the same results no matter what batch they use. This way people on smaller GPUs (that can only fit smaller batches) should be able to achieve comparable results to people with expensive larger GPUs that can fit larger batches.

This also means you can use most any batch size without having to worry about it's effect on your training. We actually performed a recent study verifying this is practice:

🌟💡 YOLOv5 Study: batch size #2452

Itto1992 commented 3 years ago

@glenn-jocher

Thanks for your reply! I interpreted this implementation as the linear rule on learning rate and batch size stated in this paper, since loss scaling has the same effect as scaling learning rate. This paper says the learning rate should be proportional to the batch size. From this point of view, the loss scale (= learning rate) seems to be proportional to the square of the batch size. I suspected that it can result in too large gradient and unstable learning behavior. But your study shows it is needless fear!

I'm looking forward to reading the paper of this repo to be published :) Thanks!

github-actions[bot] commented 3 years ago

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.