Closed jaqub-manuel closed 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.
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
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.
--img 640
, though due to the high amount of small objects in the dataset it can benefit from training at higher resolutions such as --img 1280
. If there are many small objects then custom datasets will benefit from training at native or higher resolution. Best inference results are obtained at the same --img
as the training was run at, i.e. if you train at --img 1280
you should also test and detect at --img 1280
.--batch-size
that your hardware allows for. Small batch sizes produce poor batchnorm statistics and should be avoided.hyp['obj']
will help reduce overfitting in those specific loss components. For an automated method of optimizing these hyperparameters, see our Hyperparameter Evolution Tutorial.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/
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...
@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
@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.
Many thanks @glenn-jocher, I will try and upload if I get better results.
@glenn-jocher, I am now using evolve, I wonder if the below code is correct if it is only F1 oriented?
def fitness(x):
**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)
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.
@jaqub-manuel
do you get a better result by doing evolution of hyperparameters?
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.
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?
@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.
@Lg 9550.444置信阈值为数据集生成最大F1值,是设置已部署的推理置信阈值的好点。
If I want to get the highest mAP, 0.444 for conf-thres is the best?
@Lg955 no. The highest mAP will be produced at confidence threshold 0.0.
@Lg955 no. The highest mAP will be produced at confidence threshold 0.0.
Oh, I see. Thank you!
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.
@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.
@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.
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.
--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
custom_pretrained.pt
--weights ''
argument:
python train.py --data custom.yaml --weights '' --cfg yolov5s.yaml
yolov5m.yaml
yolov5l.yaml
yolov5x.yaml
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.
--img 640
, though due to the high amount of small objects in the dataset it can benefit from training at higher resolutions such as --img 1280
. If there are many small objects then custom datasets will benefit from training at native or higher resolution. Best inference results are obtained at the same --img
as the training was run at, i.e. if you train at --img 1280
you should also test and detect at --img 1280
.--batch-size
that your hardware allows for. Small batch sizes produce poor batchnorm statistics and should be avoided.hyp['obj']
will help reduce overfitting in those specific loss components. For an automated method of optimizing these hyperparameters, see our Hyperparameter Evolution Tutorial.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!
Thanks for the quick response
Here my training results.
https://wandb.ai/1chimarugin/YOLOv5?workspace=user-1chimarugin
@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?
@srn-source sure
@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?
IoU can be tuned as well. Default detect.py value is 0.45.
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