ultralytics / yolov5

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

training with mosaic() and negative sample questions #6304

Closed jamjamjon closed 2 years ago

jamjamjon commented 2 years ago

Search before asking

Question

Hi, sorry to interrupt you. I met some problem about mosaic skills when training.

here is the thing. I recently have a project which is to detect people who ride a motorcycle, not to detect motor or people individually, but detect them all, as a whole. I've used Darknet YOLOv4 and found the result is not good, so I decided to use YOLOv5 to train it. My datasets has 10,000+ images, which contains 1/5 images has only people or only motorcycle or bycicle as negative samples. Those negative images’ label file are empty .txt.

Training results is great but has some strange phenomenon: some image only has people or motorcycle, and the people or motorcycle is detected individually. This is surely not what I want. I have checked the training datasets and the labels is right, but using the trained model to infer the negative sample images in training datasets, some of the images would be detected with only people or motorcycle.

Hyp in training is using med.yaml. Mosaic =1.0 .

Then I check the load_mosaic() function and found out some segments in img4 generated by 4 images after mosaic() will be covered or hide. Therefore, image(man ride on a motorcycle ) after mosaic would probably lost the front of the object(people and part of motorcycle), only the back of the motorcycle will be one the img4 used for training. I think this the main reason caused this problem.

I want to known how to fix this problem except using -rect or not using mosaic directly. Besides, will it work of the way I do with negative sample images which just use empty .txt label file? And should I adjust the hyper-parameters in hyp-med.yaml to deal with negative sAmples?

My English is not good, if I did not express clear, please let me know.

Really looking forward for your suggestion. Thanks!

Additional

original image :

original-image

after mosaic() might be:

after-mosaic-might-be
zhiqwang commented 2 years ago

Hi @Jamjamjamjon ,

Why not just use a classification model to do this task, and would a detection box be particularly helpful for your scenario?

glenn-jocher commented 2 years ago

@Jamjamjamjon I think you might be confusing augmentations. Mosaic will join 4 images together and then crop the outside to maintain imgsz. That's it. What you are showing is one image with scale and translate augmentation. Scale, translate and mosaic are 3 separate augmentations that are independent of each other and can be modified in your hyp file:

https://github.com/ultralytics/yolov5/blob/436ffc417ac2312de18287ddc4f87bdc2f7f5734/data/hyps/hyp.scratch.yaml#L25-L34

About reducing FPs, you just need more background images and to make sure you train enough that overtraining occurs. A full list of recommendations for improving results is below.

πŸ‘‹ 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!

glenn-jocher commented 2 years ago

@Jamjamjamjon also you can verify your labels by examining your train*.jpg batches generated on training start.

jamjamjon commented 2 years ago

@Jamjamjamjon also you can verify your labels by examining your train*.jpg batches generated on training start.

I found the mistake! I double check the training datasets and found out that there is a Negative sample image has been labeled! I haven’t finish examining all the dataset, I guess there would be several mistakes like like still exist! Thanks for your reply! Have a good day!

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 ⭐!

njustczr commented 2 years ago

i think you should set translate=0.0

glenn-jocher commented 11 months ago

@njustczr thanks for the suggestion! We recommend users first train with all default settings before considering any changes, but adjusting the translate parameter might be something you could experiment with. Feel free to give it a try and let us know how it goes! If you have any other questions, feel free to ask. Good luck!