Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom trained object detection model not working #8528

Closed
1 of 2 tasks
RockZombie4 opened this issue Jul 8, 2022 · 15 comments
Closed
1 of 2 tasks

Custom trained object detection model not working #8528

RockZombie4 opened this issue Jul 8, 2022 · 15 comments
Labels
bug Something isn't working Stale

Comments

@RockZombie4
Copy link

Search before asking

  • I have searched the YOLOv5 issues and found no similar bug report.

YOLOv5 Component

Training, Detection

Bug

I have recently trained a custom object detection model for YOLOv5 with makesense.ai .

I exported as a label file, and I put 76 images in the proper training directory, and 8 images in the proper validation directory.
I just need it to detect my face as a test but whenever I run detect.py with the custom weights generated by train.py, it never detects. There is no error message being output in the logs, but for some reason it just doesn't show any detection box around my face.

Here is my yolov5 custom model:
datasets.zip

I used this command to attempt to detect an image with my face:
python detect.py --data data/coco128.yaml --weights runs/train/exp10/weights/best.pt --source ~/Pictures/Webcam/2022-07-05-064922.jpg

I used this command to train the dataset:
python train.py --data data/coco128.yaml --epochs 1 --imgsz 76

Please help me!

Environment

  • YOLO - YOLOv5
  • OS - Raspbian OS 64-bit
  • Python 3.7.10

Minimal Reproducible Example

No response

Additional

No response

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!
@RockZombie4 RockZombie4 added the bug Something isn't working label Jul 8, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Jul 8, 2022

👋 Hello @RockZombie4, 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://ultralytics.com or email support@ultralytics.com.

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

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), validation (val.py), inference (detect.py) and export (export.py) on macOS, Windows, and Ubuntu every 24 hours and on every commit.

@glenn-jocher
Copy link
Member

@RockZombie4 I doubt your training will work with so few images. If you only have 76 images you'll probably want to train very many epochs, i.e. >>1000 to get enough iterations to produce results.

Alternatively simply lower your --conf-thresh.

Obviously at some point as confidence thresholds trends to zero boxes will begin to appear.

@RockZombie4
Copy link
Author

python train.py --data data/coco128.yaml --epochs 1 --imgsz 76

How do I lower the --conf-thresh?

@glenn-jocher
Copy link
Member

@RockZombie4

parser.add_argument('--conf-thres', type=float, default=0.25, help='confidence threshold')

@glenn-jocher
Copy link
Member

glenn-jocher commented Jul 8, 2022

@RockZombie4 but I have no idea why you would expect results from 1 epoch of training. This is useless. See Train Custom Data for directions on training and Tips for Improving Results tutorials:

YOLOv5 Tutorials

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

@RockZombie4
Copy link
Author

I tried training with 1000 epochs, set the image size to 96, and lowered the --conf-thresh still nothing!

Do I train with a pretrained weight?
What do I do?

@glenn-jocher
Copy link
Member

glenn-jocher commented Jul 10, 2022

@RockZombie4 👋 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

  • Images per class. ≥ 1500 images per class recommended
  • Instances per class. ≥ 10000 instances (labeled objects) per class recommended
  • Image variety. Must be representative of deployed environment. For real-world use cases we recommend images from different times of day, different seasons, different weather, different lighting, different angles, different sources (scraped online, collected locally, different cameras) etc.
  • Label consistency. All instances of all classes in all images must be labelled. Partial labelling will not work.
  • Label accuracy. Labels must closely enclose each object. No space should exist between an object and it's bounding box. No objects should be missing a label.
  • Label verification. View train_batch*.jpg on train start to verify your labels appear correct, i.e. see example mosaic.
  • Background images. Background images are images with no objects that are added to a dataset to reduce False Positives (FP). We recommend about 0-10% background images to help reduce FPs (COCO has 1000 background images for reference, 1% of the total). No labels are required for background images.

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

  • Start from Pretrained weights. Recommended for small to medium sized datasets (i.e. VOC, VisDrone, GlobalWheat). 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
                                             custom_pretrained.pt
  • Start from Scratch. Recommended for large datasets (i.e. COCO, Objects365, OIv6). Pass the model architecture yaml you are interested in, along with an empty --weights '' argument:
python train.py --data custom.yaml --weights '' --cfg yolov5s.yaml
                                                      yolov5m.yaml
                                                      yolov5l.yaml
                                                      yolov5x.yaml

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.

  • Epochs. Start with 300 epochs. If this overfits early then you can reduce epochs. If overfitting does not occur after 300 epochs, train longer, i.e. 600, 1200 etc epochs.
  • Image size. COCO trains at native resolution of --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. Use the largest --batch-size that your hardware allows for. Small batch sizes produce poor batchnorm statistics and should be avoided.
  • Hyperparameters. Default hyperparameters are in hyp.scratch-low.yaml. We recommend you train with default hyperparameters first before thinking of modifying any. In general, increasing augmentation hyperparameters will reduce and delay overfitting, allowing for longer trainings and higher final mAP. Reduction in loss component gain hyperparameters like hyp['obj'] will help reduce overfitting in those specific loss components. For an automated method of optimizing these hyperparameters, see our Hyperparameter Evolution Tutorial.

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!

@github-actions
Copy link
Contributor

github-actions bot commented Aug 10, 2022

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

@HusseinA190
Copy link

@RockZombie4 but I have no idea why you would expect results from 1 epoch of training. This is useless. See Train Custom Data for directions on training and Tips for Improving Results tutorials:

#but i have trained my model on a large data like 5,000 images and trained them on 100 epoch and the weights file dosen't even detect anything and the accuracy was 86%

@greeshmathilakan369
Copy link

Bug
I have trained a custom object detection model for YOLOv5. There is no error message being output in the logs, but for some reason it just doesn't show any detection box around my face.

image 1/1: 720x1280 (no detections)
Speed: 1026.1ms pre-process, 167.8ms inference, 1.0ms NMS per image at shape (1, 3, 384, 640)

@glenn-jocher
Copy link
Member

@RockZombie4 👋 Hello! Thanks for reaching out.

If you are getting an accuracy of 86% but no detections are being made, there could be several reasons why this is happening. I suggest you do the following:

  1. Check that your dataset is labeled correctly. Ensure that all of your images have accurate annotations and bounding boxes around the target objects you want to detect. Ensure that the annotations are in xywh format and are saved in a compatible format, such as YOLO format or COCO format.

  2. Try testing your model on different images. There is always a possibility that your dataset may not include enough variety of images for the model to generalize well. Therefore, testing on various other images would enable you to check the model's robustness.

  3. Change the Confidence Threshold. Try using different values of --conf-thres to see if it resolves the issues with lower detection rates. By default, YOLOv5 uses a confidence threshold of 0.25. A higher threshold value will produce less detections, but higher precision.

  4. Try training with more epochs. Try training the model for more epochs and see if it improves the accuracy of detection.

  5. Data Augmentation Changes. Try varying the data augmentation methods, such as flipping, scaling, translation, to get enough augmentations for better model generalization.

If none of these solutions work, then provide detail information about your problem as well as the information in the log files so that we can diagnose the issue further.

@harshrocks
Copy link

@RockZombie4 I doubt your training will work with so few images. If you only have 76 images you'll probably want to train very many epochs, i.e. >>1000 to get enough iterations to produce results.

Alternatively simply lower your --conf-thresh.

Obviously at some point as confidence thresholds trends to zero boxes will begin to appear.

Lowering the --conf value did the work thank you so much! You're an angel!😊😊😇

@glenn-jocher
Copy link
Member

@harshrocks glad to hear that lowering the --conf value resolved the issue for you! It's always a good idea to experiment with different threshold values to fine-tune the detection output according to your requirements. If you have any further questions or need assistance with anything else, please feel free to ask. Happy detecting! 😊😇

@alienCarpet14
Copy link

@RockZombie4 but I have no idea why you would expect results from 1 epoch of training. This is useless. See Train Custom Data for directions on training and Tips for Improving Results tutorials:

#but i have trained my model on a large data like 5,000 images and trained them on 100 epoch and the weights file dosen't even detect anything and the accuracy was 86%

Had similar issue here. Trained my dataset it was accurate on validation set but my dumb self predicted with targeting to classes=[9] because I was reusing code that detected Traffic Lights with yolov8n where Traffic Light was on the 9th index of classes that it detects. And I trained my model to detect only one class so I had to change it to classes=[0].
After reading this forum I had come to this idea. Was going mad last 2 days about this. Funny how all bugs occur somewhere between chair and a monitor.

@glenn-jocher
Copy link
Member

@alienCarpet14 it's great to hear that you were able to identify the source of the issue! It's not uncommon for these kinds of challenges to arise during the development and deployment of models. Thank you for sharing your experience, and we hope your insights will be helpful to others encountering similar issues. If you have any further questions or need assistance, please feel free to reach out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Stale
Projects
None yet
Development

No branches or pull requests

6 participants