ultralytics / yolov5

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

How to increase recall #2449

Closed jaqub-manuel closed 3 years ago

jaqub-manuel commented 3 years ago

Dear, @glenn-jocher Although I have done many trials, the recall value is low compared to the precision value. Although I set the recall value of the fitness function to 0.8 and the precision value to 0.2, I did not get a good result. Can you share some tips if you have some suggestions? thanks.

https://github.com/ultralytics/yolov5/blob/886f1c03d839575afecb059accf74296fad395b6/utils/metrics.py#L12

results

glenn-jocher commented 3 years ago

@jaqub-manuel sure I can share some tips! 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 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.

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/

jaqub-manuel commented 3 years ago

Dear @glenn-jocher , many thanks for your quick reply. My dataset consists of medical data set and 700 images (416x384) for train and test set consists of 200 images (1200x900) and labeled by expert doctors. I have trained Yolov5s for m, l and x but generally s and m works better than l and x. Everything I used default first, then I used hyp.finetune. I run each training at least 5 times for the same model and each time has different results. (different, map=i.e 70+- 10) I did hyperparameter optimization with genetic algorithm, but still the recall value in the results is small. i am stitching all the pictures. I am interested in F1, TP, FP, FN instead of mAP. (it is medical data) Thank you in advance for your help...

results confusion_matrix F1_curve labels labels_correlogram P_curve PR_curve R_curve

glenn-jocher commented 3 years ago

@jaqub-manuel yes your recall is low. The quantity of your data is about an order of magnitude too small vs other datasets like COCO, and also your image sizes differ substantially between your train and test sets. You want your training data and your val/test data to be sampled from the same image space, and they should share similar characteristics and variations.

Also better results with smaller models is likely independently indicative of a problem in your training pipeline.

You may also want to use P6 models, which outperform their P5 counterparts across the board:

python train.py --weights yolov5s6.pt
Screen Shot 2021-01-07 at 9 18 07 PM
glenn-jocher commented 3 years ago

@jaqub-manuel also you can evolve hyperparameters with a custom fitness definition better suited to your requirements. The default fitness will favor mutations that produce higher mAP@0.5:0.95 mostly. You can shift this to F1 or mAP@0.5 to reduce the influence of accurate bounding boxes by modifying the fitness function here: https://github.com/ultralytics/yolov5/blob/747c2653eecfb870b1ed40b1e00e0ef209b036e9/utils/metrics.py#L12-L16

Though in general you will obtain much better results via dataset improvement than training improvement considering the small size of your dataset.

jaqub-manuel commented 3 years ago

Many thanks @glenn-jocher, I will try and upload if I get better results.

jaqub-manuel commented 3 years ago

@glenn-jocher, I am now using evolve, I wonder if the below code is correct if it is only F1 oriented?

def fitness(x):

Model fitness as a weighted combination of metrics

 **w = [0.0, 0.0, 0.0, 0.0, 1.0]**  # weights for [P, R, mAP@0.5, mAP@0.5:0.95] 
 return (x[:, :4] * w).sum(1) 
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.

di-dii commented 3 years ago

@jaqub-manuel
do you get a better result by doing evolution of hyperparameters?

jaqub-manuel commented 3 years ago

hey @di-dii, I always got different result because I have about 500 images for training (it should be over 1000 for better result). For fine tune, I did not get better results.

Lg955 commented 3 years ago

Hi @glenn-jocher , I have a question: After training, the F1_curve shows that "all classes 0.82 at 0.444", so the conf-thres should be set as 0.444 when detecting? F1_curve PR_curve results

glenn-jocher commented 3 years ago

@Lg955 0.444 confidence threshold produces the maximum F1 value for your dataset and is a good point for setting your deployed inference confidence threshold.

Lg955 commented 3 years ago

@Lg 9550.444置信阈值为数据集生成最大F1值,是设置已部署的推理置信阈值的好点。

If I want to get the highest mAP, 0.444 for conf-thres is the best?

glenn-jocher commented 3 years ago

@Lg955 no. The highest mAP will be produced at confidence threshold 0.0.

Lg955 commented 3 years ago

@Lg955 no. The highest mAP will be produced at confidence threshold 0.0.

Oh, I see. Thank you!

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.

1chimaruGin commented 2 years ago

@glenn-jocher Is there a tip to improve model recall if object and background are similar. In my case val/obj loss is keep increasing.

W B Chart 1_7_2022, 9_47_17 PM

b77c1347-defc-4842-a22f-6a102b82d57f

c67c0404-fcf2-4798-8711-1df26225d7df 4111daa9-1a17-4bea-849d-111de8a1fbf1

glenn-jocher commented 2 years ago

@1chimaruGin 👋 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/](https://github.com/ultralytics/yolov5/issues/2844#issuecomment-851338384)

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

1chimaruGin commented 2 years ago

Thanks for the quick response

Here my training results.

https://wandb.ai/1chimarugin/YOLOv5?workspace=user-1chimarugin

srn-source commented 2 years ago

@glenn-jocher, I am now using evolve, I wonder if the below code is correct if it is only F1 oriented?

def fitness(x): # Model fitness as a weighted combination of metrics w = [0.0, 0.0, 0.0, 0.0, 1.0] # weights for [P, R, mAP@0.5, mAP@0.5:0.95] return (x[:, :4] * w).sum(1)

@glenn-jocher If i want to monitor only F1, do i have to change something like this?

glenn-jocher commented 2 years ago

@srn-source sure

Alberto1404 commented 2 years ago

@Lg955 0.444 confidence threshold produces the maximum F1 value for your dataset and is a good point for setting your deployed inference confidence threshold.

What about IoU threshold? Should be leave 0.45 as the default one, or can be inferred given the training result metrics, as you are saying about confidence threshold?

glenn-jocher commented 2 years ago

IoU can be tuned as well. Default detect.py value is 0.45.