ultralytics / yolov5

YOLOv5 πŸš€ in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.65k stars 16.32k forks source link

Can't detect small objects #3525

Closed hzitoun closed 3 years ago

hzitoun commented 3 years ago

❔Question

I've trained a yolov5 S and M on a custom dataset. The goal is to detect cardboards in cities. My training data has an aspect ratio close to inference data. However, on some inference images small objects can't be detected.

There is 3 classes to learn. Training set size = 2100 images et valid size = 260 images.

I resize both training and inference images to 640x640.

Could you please provide some recommendations on how to improve performance and help the yolov5 better detect such objects ?

Additional context

Can't figure out how to make the model detect such small objects. I've a mAP above 0.7 on a valid data different from training data (not close in term of context).

github-actions[bot] commented 3 years ago

πŸ‘‹ Hello @hzitoun, 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

@hzitoun πŸ‘‹ Hello! Thanks for asking about improving training results. For detection of small objects you should train and deploy at larger image sizes. A full list of recommendations is below.

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/

hzitoun commented 3 years ago

@glenn-jocher thank you for your reply! Your recommendations should be very helpful! I've two unclear points if you could explain please:

For detection of small objects you should train and deploy at larger image sizes

Meaning that I should resize my image before training to what resolution? Should I use the P6 models at 1280 input size (and resize my training and inference images to 1280) instead of P5 with 640 input size (I can't use GPU for inference so a P6 takes too much time).

Background images. Background images are images with no objects that are added to a dataset to reduce False Positives (FP).

Do you mean I should add images to my training data that have empty annotation txt file or without annotation txt files?

glenn-jocher commented 3 years ago

@hzitoun to train, test, or detect at different image sizes you simply use the --img argument:

python train.py --img 1280
python test.py --img 1280
python dertect.py --img 1280

P5 and P6 models can both be used at any size. P6 models provide improved detection of large objects.

Background images require no label files, you just add them to your images directory.

hzitoun commented 3 years ago

@glenn-jocher amazing! Thanks! so I can keep my training & detection images at their original sizes (no need to resize them) and just specify --img 1280 when running the model?

glenn-jocher commented 3 years ago

@hzitoun no, you never need to modify your dataset, that's what the different training settings are for.

hzitoun commented 3 years ago

@glenn-jocher great! Thank you for all your clarifications! Just another question please, should training data have different aspect ratio of the objects (too small, small, medium, big and too big) or should it only contain objects with the aspect ratio the closer to inference (small objects)?

glenn-jocher commented 3 years ago

@hzitoun its not clear to me when you say aspect ratio you understand the meaning of the term. Aspect ratio is the ratio of one dimension to another, i.e. a circle will have aspect ratio 1.0 at any size.

hzitoun commented 3 years ago

@glenn-jocher I see. Sorry for the confusion Glenn. I update my question.

glenn-jocher commented 3 years ago

@hzitoun our full list of recommendations is in https://github.com/ultralytics/yolov5/issues/3525#issuecomment-856792453, I have nothing more to add. If I had any extra recommendations I would have provided them in my original message.

hzitoun commented 3 years ago

@glenn-jocher okay thank you!

github-actions[bot] commented 3 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 ⭐!

fcakyon commented 3 years ago

@hzitoun you can use https://github.com/obss/sahi for sliced yolov5 training&inference πŸ‘