ultralytics / yolov5

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

Yolov5 hyperparameter tuning #13085

Open AbhishekPSI7042 opened 3 weeks ago

AbhishekPSI7042 commented 3 weeks ago

Search before asking

Question

Hi,

I'm training a YOLOv5n model for vehicle detection, and we have five classes. In training, I have 7519 images, including background images, and in validation, 2130 images. However, I'm unable to achieve accuracy in number plate detection. How can I improve the accuracy of number plate detection? Please help me to improve the accuracy of numberplate

!python train.py --epochs 150 --img 360 --batch 32 --data dev_data.yaml --weights yolov5n.pt --cache --patience 10

Accuracy achieved:- Class Images Instances P R mAP50 mAP50-95: 100%|██████████| 34/34 [00:23<00:00, 1.44it/s] all 2130 3710 0.99 0.984 0.995 0.859 Vehicle 2130 1158 0.985 0.985 0.995 0.922 NumberPlate 2130 1594 0.985 0.981 0.994 0.655 Heavy Vehicle 2130 650 1 0.984 0.995 0.921 TwoWheeler 2130 295 0.993 0.969 0.995 0.871 ThreeWheeler 2130 13 0.985 1 0.995 0.927

Additional

No response

glenn-jocher commented 3 weeks ago

@AbhishekPSI7042 hi there,

Thank you for reaching out and providing detailed information about your training setup and results. It looks like you're achieving good performance on most classes, but the NumberPlate class has a lower mAP50-95 score. Here are a few suggestions to help improve the accuracy for number plate detection:

  1. Dataset Quality and Quantity:

    • Images per Class: Ensure you have a sufficient number of images per class. For optimal results, we recommend having at least 1500 images per class and 10,000 instances per class. If your dataset for number plates is smaller, consider augmenting it with more labeled images.
    • Label Accuracy: Verify that your labels are accurate and tightly enclose the number plates. Any inaccuracies in labeling can significantly impact performance.
    • Background Images: Including background images (images without any objects) can help reduce false positives. Aim for about 0-10% background images in your dataset.
  2. Training Settings:

    • Epochs: You might want to increase the number of epochs if your model is not overfitting. Try training for 300 epochs initially and monitor the performance.
    • Image Size: Training at a higher resolution can help detect smaller objects like number plates. Consider increasing the image size to --img 640 or higher.
    • Batch Size: Use the largest batch size that your hardware can handle. This helps in better batch normalization statistics.
  3. Model Selection:

    • Larger Models: If your hardware allows, consider using a larger model like YOLOv5m or YOLOv5l. These models have more parameters and can potentially yield better results.
    • Pretrained Weights: Starting with pretrained weights can significantly improve performance, especially if your dataset is not very large.
  4. Hyperparameter Tuning:

    • Default Hyperparameters: Ensure you start with the default hyperparameters before making any changes. This helps establish a performance baseline.
    • Hyperparameter Evolution: You can use the hyperparameter evolution feature to automatically find the best hyperparameters for your dataset. Refer to the Hyperparameter Evolution Tutorial for more details.

Here is an example command to train with some of these suggestions:

python train.py --epochs 300 --img 640 --batch 32 --data dev_data.yaml --weights yolov5m.pt --cache --patience 10

For more detailed tips and best practices, please refer to our Tips for Best Training Results guide.

I hope these suggestions help improve your number plate detection accuracy. If you have any further questions or need additional assistance, feel free to ask!

AbhishekPSI7042 commented 2 weeks ago

Hii @glenn-jocher, In my case, I need a small model because I have to run it with DeepStream on Jetson, where I can't compromise on FPS. That's why I'm using the YOLOv5n model with an image size of 360. Do I need to have more images for validation? In the training dataset, we have around 7,100 images, but in the validation dataset, I have approximately 2,100 images.

glenn-jocher commented 2 weeks ago

Hi @AbhishekPSI7042,

Thank you for your question and for providing details about your use case. Running YOLOv5n on Jetson with DeepStream is a great choice for maintaining high FPS. Here are some suggestions to help you optimize your model performance:

  1. Validation Dataset Size:

    • Your current validation dataset size of 2,100 images is generally sufficient for most use cases. However, the key is to ensure that your validation set is representative of the variety and distribution of your training data. This helps in accurately evaluating the model's performance.
  2. Dataset Quality:

    • Label Accuracy: Double-check that all your labels are accurate and tightly enclose the objects. Any inaccuracies can affect the model's performance.
    • Image Variety: Ensure your dataset includes images from different times of day, weather conditions, and angles to make the model robust.
  3. Training Settings:

    • Epochs: Consider increasing the number of epochs to 300 or more if your model is not overfitting. This can help the model learn better representations.
    • Image Size: While 360 is a good compromise for speed, you might experiment with slightly higher resolutions like 480 or 512 to see if it improves accuracy without significantly impacting FPS.
  4. Model Optimization:

    • Quantization: For Jetson devices, consider using TensorRT for model optimization. TensorRT can significantly improve inference speed without compromising much on accuracy.
    • Pruning: Model pruning can help reduce the model size and increase inference speed. You might want to explore this if you haven't already.
  5. Hyperparameter Tuning:

    • Hyperparameter Evolution: Utilize the hyperparameter evolution feature to find the best hyperparameters for your specific dataset. This can lead to better performance without manual tuning.

Here is an example command to train with some of these suggestions:

python train.py --epochs 300 --img 480 --batch 32 --data dev_data.yaml --weights yolov5n.pt --cache --patience 10

For more detailed tips and best practices, please refer to our Tips for Best Training Results guide.

If you encounter any issues or have further questions, feel free to ask. The YOLO community and the Ultralytics team are here to help! 😊

AbhishekPSI7042 commented 2 weeks ago

Hii @glenn-jocher, I have trained model with image size 480 and epochs 300 but model accuracy didn't increased. Now what things do I need to do to increase the accuracy of numberplate. I training model on google colab pro.

Will this hyperparameter tuning work?

lr0: 0.001 # initial learning rate (SGD=1E-2, Adam=1E-3) lrf: 0.01 # final OneCycleLR learning rate (lr0 * lrf) momentum: 0.937 # SGD momentum/Adam beta1 weight_decay: 0.0005 # optimizer weight decay 5e-4 warmup_epochs: 5.0 # warmup epochs (fractions ok) warmup_momentum: 0.9 # warmup initial momentum warmup_bias_lr: 0.2 # warmup initial bias lr box: 0.05 # box loss gain cls: 0.5 # cls loss gain cls_pw: 1.0 # cls BCELoss positive_weight obj: 1.5 # obj loss gain (scale with pixels) obj_pw: 1.0 # obj BCELoss positive_weight iou_t: 0.15 # IoU training threshold anchor_t: 3.0 # anchor-multiple threshold

anchors: 3 # anchors per output layer (0 to ignore)

fl_gamma: 0.5 # focal loss gamma (efficientDet default gamma=1.5) hsv_h: 0.015 # image HSV-Hue augmentation (fraction) hsv_s: 0.7 # image HSV-Saturation augmentation (fraction) hsv_v: 0.4 # image HSV-Value augmentation (fraction) degrees: 0.0 # image rotation (+/- deg) translate: 0.2 # image translation (+/- fraction) scale: 0.5 # image scale (+/- gain) shear: 0.0 # image shear (+/- deg) perspective: 0.0 # image perspective (+/- fraction), range 0-0.001 flipud: 0.0 # image flip up-down (probability) fliplr: 0.5 # image flip left-right (probability) mosaic: 1.0 # image mosaic (probability) mixup: 0.0 # image mixup (probability) copy_paste: 0.0 # segment copy-paste (probability)

glenn-jocher commented 2 weeks ago

Hi @AbhishekPSI7042,

Thank you for your detailed follow-up. It's great to see you're experimenting with different settings to improve your model's performance. Let's address your concerns and explore further steps to enhance the accuracy of your number plate detection.

Hyperparameter Tuning

Hyperparameter tuning can indeed have a significant impact on model performance. The hyperparameters you've listed are a good starting point. Here are a few suggestions to fine-tune them:

  1. Learning Rate (lr0 and lrf):

    • The initial learning rate (lr0) and the final learning rate (lrf) are crucial. You might want to experiment with slightly lower or higher values. For example, try lr0: 0.0005 and lrf: 0.01 to see if it stabilizes the training.
  2. Loss Gains (box, cls, obj):

    • Adjusting the loss gains can help balance the different components of the loss function. Since number plates are small objects, you might want to increase the box loss gain slightly, e.g., box: 0.1.
  3. Augmentation Parameters:

    • Data augmentation can help improve model robustness. You can experiment with the augmentation parameters like hsv_h, hsv_s, hsv_v, translate, scale, and shear. For instance, increasing translate to 0.3 or scale to 0.6 might help.

Example Command with Adjusted Hyperparameters

Here's an example command incorporating some of these suggestions:

python train.py --epochs 300 --img 480 --batch 32 --data dev_data.yaml --weights yolov5n.pt --cache --patience 10 --hyp hyp.yaml

And your hyp.yaml might look like this:

lr0: 0.0005
lrf: 0.01
momentum: 0.937
weight_decay: 0.0005
warmup_epochs: 5.0
warmup_momentum: 0.9
warmup_bias_lr: 0.2
box: 0.1
cls: 0.5
cls_pw: 1.0
obj: 1.5
obj_pw: 1.0
iou_t: 0.15
anchor_t: 3.0
fl_gamma: 0.5
hsv_h: 0.015
hsv_s: 0.7
hsv_v: 0.4
degrees: 0.0
translate: 0.3
scale: 0.6
shear: 0.0
perspective: 0.0
flipud: 0.0
fliplr: 0.5
mosaic: 1.0
mixup: 0.0
copy_paste: 0.0

Additional Tips

  1. Model Architecture:

    • Since you're constrained by the need for a small model, ensure you're using the most optimized version of YOLOv5n. Sometimes, even small tweaks in the architecture can help.
  2. Data Quality:

    • Ensure that your number plate annotations are precise and consistent. Any mislabeling can significantly affect the model's ability to learn.
  3. Training Duration:

    • If your model is not overfitting, consider training for even more epochs, e.g., 600 or 1200 epochs, to allow the model more time to learn.
  4. Validation Strategy:

    • Ensure your validation set is representative of the real-world scenarios where the model will be deployed. This helps in getting a more accurate measure of the model's performance.

For more detailed tips and best practices, please refer to our Tips for Best Training Results guide.

If you encounter any issues or have further questions, feel free to ask. The YOLO community and the Ultralytics team are here to help! 😊

Best of luck with your training!