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

Modifying YOLOV5 #5630

Closed myasser63 closed 2 years ago

myasser63 commented 2 years ago

Search before asking

Question

I am working on my owm improved yolov5 model. I want to know if I want to change the structure and layers of the model, where should I do my modifications?

Should I change the layers in common.py.

I appreciate any assistance on this matter

Additional

No response

glenn-jocher commented 2 years ago

@myasser63 you can adjust model.yaml architecture and then train your new yaml. If you create new modules make sure to import and handle them correctly in yolo.py.

python models/yolov5.py --cfg new.yaml  # verify
python train.py --cfg new.yaml  # train

https://github.com/ultralytics/yolov5/blob/d5b21b1ecb66b35af937ac12364aa80733222bd1/models/yolov5s.yaml#L12-L48

myasser63 commented 2 years ago

@glenn-jocher Thanks for your reply.

Can I have more information on the effect of changing model.yaml on the structure? where the changes will reflect

glenn-jocher commented 2 years ago

@myasser63 if you change a model yaml then the changes will reflect in that model yaml

myasser63 commented 2 years ago

@glenn-jocher Sorry for my basic questions. I am trying to get deeper understand of YOLOv5.

Can I know what is each of[from, number, module, args] ?

glenn-jocher commented 2 years ago

@myasser63 from means which layer is the module input coming from (-1 means the previous layer). number means how many times is the module repeated, module is the module name and args is the module arguments.

myasser63 commented 2 years ago

@myasser63 from means which layer is the module input coming from (-1 means the previous layer). number means how many times is the module repeated, module is the module name and args is the module arguments.

@glenn-jocher Thanks alot for your help

moahaimen commented 2 years ago

@myasser63 from means which layer is the module input coming from (-1 means the previous layer). number means how many times is the module repeated, module is the module name and args is the module arguments.

hi, I designed my own dataset and trained in yolov5 i but the results was confusing sometimes i get 2 bound box for the same object showing the label of another object, sometimes giving high confidence on the object but it gives another class

glenn-jocher commented 2 years ago

@moahaimen 👋 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!

moahaimen commented 2 years ago

@moahaimen 👋 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!

I am happy about your answer I do thank you, my problem is, I am a student I have research to in research I must make my dataset it custom dataset of objects, about 13 classes but most of them are 100 images 3 of the 1100 images, now I am in trouble and I may fail, if I have no results, I am re-training my model in Colab now on yolov5 and I will send you what appears in the tensor board please I need to contact you directly I can't say everything on public about my phd research

moahaimen commented 2 years ago
  • recommended I trained model in 150 epochs on yolov5s ,these almost all the mousers form tensor flow board I wish you help me in knowing how to understand them and what can I do to fix the situation image image image image

image

image

image image

image image image

moahaimen commented 2 years ago

dataset

hi, thank you for your help, I made now 50000 images about 1000 for each class, and the map increased from 0.22 to 0.59 58.7% mAP 80.8% precision 48.8% recall but I need to make it reach 0.97 I did label all the images I did everything you mentioned also Roboflow helped me a lot, but I need to understand how to make the background in the dataset and how will it affect and for Weapon detection which type of YOLOV5 model in better I am using Yolov5s, Yolov5n also, I need to build Ensembled Deep learning withYOLOV5 is it Possible?

PraveenMNaik commented 2 years ago

@glenn-jocher @myasser63 Sir, if I want to build my own neural network architecture ... which files i have to make modifications ... As per my knowledge following changes are required... 1.in yolov5/models folder <>.yaml, here 'head', 'backbone' are to be modified

  1. common.py... here if any new architecture or algo like efficientnet or mobilenet needs to be put, we need to introduce the function in common.py file...
  2. in yolov5/data folder <>.yaml, the classes and path are to be defined....

Other than these is there anywhere else changes are to be incorporated?

glenn-jocher commented 2 years ago

@PraveenMNaik just 1 and 2. Though if you introduce new modules you also need to import them and handle them in yolo.py, i.e.: https://github.com/ultralytics/yolov5/blob/898332433a71b8846b15daa276a8ac45c9efa98b/models/yolo.py#L252-L304

moahaimen commented 2 years ago

@PraveenMNaik just 1 and 2. Though if you introduce new modules you also need to import them and handle them in yolo.py, i.e.:

https://github.com/ultralytics/yolov5/blob/898332433a71b8846b15daa276a8ac45c9efa98b/models/yolo.py#L252-L304

thats great

moahaimen commented 2 years ago

Sir, if I want to build my own neural network architecture ... which files i have to make modifications ... As per my knowledge following changes are required... 1.in yolov5/models folder <>.yaml, here 'head', 'backbone' are to be modified 2. common.py... here if any new architecture or algo like efficientnet or mobilenet needs to be put, we need to introduce the function in common.py file... 3. in yolov5/data folder <>.yaml, the classes and path are to be defined....

Other than these is there anywhere else changes are to be incorporated?

Am I able to make new module to detect some determined object and add them to Yolo?

myasser63 commented 2 years ago

@moahaimen what is the project you working on?. May be we have similar goal we can share our findings

PraveenMNaik commented 2 years ago

@moahaimen can you pls guide me ... pls contact me at praveen.207it001@nitk.edu.in

moahaimen commented 2 years ago

@moahaimen what is the project you working on?. May be we have similar goal we can share our findings

hi thank you for commenting My goal is to make Weapon Detection

myasser63 commented 2 years ago

MMU?

lchunleo commented 1 year ago

hi all,

Can i check my understanding when we modified the model, i assume we need to retrain from scratch and we can't use the pretrained model as the architecture is different? thanks

ajithvcoder commented 1 year ago

hi all,

Can i check my understanding when we modified the model, i assume we need to retrain from scratch and we can't use the pretrained model as the architecture is different? thanks

yes you are correct retrain from scratch

glenn-jocher commented 11 months ago

@ajithvallabai you are absolutely correct. When modifying the model architecture, retraining from scratch is necessary as the architecture is different. Pretrained models may not be compatible with the modified architecture. Good luck with your modifications!