Open glenn-jocher opened 4 years ago
Can I use it in version 1?
Can I use it in version 1?
Can I use it in version 1?
Can I use it in version 1?
Can I use it in version 1?
Can I use it in version 1?
Can I use it in version 1?
what's influence of model ensemble
compare to test-time augmentation? which one will be better?
Well, I see, the model ensembling method is actually more like using a poor model to find missed detections for a good model. In contrast, TTA can also find missed detections by changing the input, while maintaining using the best model.
@Zzh-tju ensembling and TTA are not mutually exclusive. You can TTA a single model, and you can ensemble a group of models with or without TTA:
python detect.py --weights model1.pt model2.pt --augment
@Zzh-tju ensembling runs multiple models, while TTA tests a single model at with different augmentations. Typically I've seen the best result when merging output grids directly, (i.e. ensembling YOLOv5l and YOLOv5x), rather than simply appending boxes from multiple models for NMS to sort out. This is not always possible however, for example Ensembling an EfficientDet model with YOLOv5x, you can not merge grids, you must use NMS or WBF (or Merge NMS) to get a final result.
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.
How can I ensemble EfficientDet D7 with YOLO V5x?
@Blaze-raf97 with the right amount of coffee anything is possible.
How to solve this problem? COCO mAP with pycocotools... saving detections_val2017__results.json... ERROR: pycocotools unable to run: invalid literal for int() with base 10: 'Image_20200930140952222'
@LokedSher pycocotools is only intended for mAP on COCO data using coco.yaml. https://pypi.org/project/pycocotools/
@LokedSher pycocotools is only intended for mAP on COCO data using coco.yaml. https://pypi.org/project/pycocotools/
Thanks for your reply!
@LokedSher I also encountered the same problem as you, but after I read your Q&A, I still don't know how to improve to get the picture given by the author.
I want to ensemble yolov3-spp and yolov5x which are trained by using your excellent work yolov3 and yolov5. Few months ago i got the ensemble result ,but i try it again now, error encountered, can you help me? ths!
python detect.py --weights runs/train/exp13/weights/best.pt /home/work/pretrained_weights/best.pt --source /home/work/data
Fusing layers...
Model Summary: 484 layers, 88390614 parameters, 0 gradients
Traceback (most recent call last):
File "detect.py", line 172, in
@PromiseXu1 we are currently updating YOLOv3 models and should have them available for autodownload within this repo at the end of the week. In the meantime if you have an existing YOLOv3 model that runs inference correctly with this repo, you can modify the ensemble type to NMS ensemble to allow it to ensemble with the YOLOv5 models. v3 and v5 models have different heads (FPN and PANet), meaning the current ensemnble method will not work as it expects every output to have the same size and shape. You can modify ensemble type in the Ensemble() class: https://github.com/ultralytics/yolov5/blob/201bafc7cf9545362552ad5b0fc5033d64d7ae77/models/experimental.py#L117-L130
@PromiseXu1 we are currently updating YOLOv3 models and should have them available for autodownload within this repo at the end of the week. In the meantime if you have an existing YOLOv3 model that runs inference correctly with this repo, you can modify the ensemble type to NMS ensemble to allow it to ensemble with the YOLOv5 models. v3 and v5 models have different heads (FPN and PANet), meaning the current ensemnble method will not work as it expects every output to have the same size and shape. You can modify ensemble type in the Ensemble() class: https://github.com/ultralytics/yolov5/blob/201bafc7cf9545362552ad5b0fc5033d64d7ae77/models/experimental.py#L117-L130
WOw ! thanks for your prompt response, i will try and wish you all the best in your work ~
@Zzh-tju ensembling runs multiple models, while TTA tests a single model at with different augmentations. Typically I've seen the best result when merging output grids directly, (i.e. ensembling YOLOv5l and YOLOv5x), rather than simply appending boxes from multiple models for NMS to sort out. This is not always possible however, for example Ensembling an EfficientDet model with YOLOv5x, you can not merge grids, you must use NMS or WBF (or Merge NMS) to get a final result.
Hi, I understand how the NMS ensemble works. But, I have not understood how exactly the mean and max ensemble types work in the context of ensembling two Yolov5 networks (Is it just the mean or max of the final predictions from each model?). Could you please provide more insight. Thanks a lot!
@AJ-RR see https://docs.ultralytics.com/yolov5/tutorials/model_ensembling#issuecomment-732088097 for forward method of each.
@AJ-RR see #318 (comment) for forward method of each.
Thanks! I understand it now
I have a self trained model with different picture sizes which works fine if I apply it without model ensembling. I want to detect 2 objects: persons which I use the coco trained dataset of yolov5 and another object trained with my custom trained dataset. Unfortunately, it fails with the following when applied together:
Fusing layers...
Model Summary: 232 layers, 7459581 parameters, 0 gradients, 17.5 GFLOPS
Fusing layers...
Model Summary: 232 layers, 7246518 parameters, 0 gradients, 16.8 GFLOPS
Ensemble created with ['yolov5s.pt', 'object.pt']
Traceback (most recent call last):
File "detect.py", line 174, in <module>
detect()
File "detect.py", line 61, in detect
_ = model(img.half() if half else img) if device.type != 'cpu' else None # run once
File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 744, in _call_impl
result = self.forward(*input, **kwargs)
File "/usr/src/app/models/experimental.py", line 109, in forward
y = torch.stack(y).mean(0) # mean ensemble
RuntimeError: stack expects each tensor to be equal size, but got [1, 25200, 85] at entry 0 and [1, 25200, 6] at entry 1
A short research of that message tells me the different sizes of the pictures of the 2 datasets might be the problem. Is that really a problem?
My training command was the following:
python train.py --data object.yml --cfg yolov5s.yaml --weights 'yolov5s.pt' --batch-size 64
I am still not sure if that's the best way or even a good way to combine these two models but it seemed to be the easiest way.
@philippneugebauer the recent v4.0 release updated the default ensembling method from mean
to nms
, which should allow dissimilar models to ensemble together more easily. You may want to git pull
to receive this update and retry.
https://github.com/ultralytics/yolov5/blob/69be8e738f45e7908c1270eedd350f98c0c7bfa4/models/experimental.py#L109
In any case ensembling is intended for models that share the same classes. It looks like your method will intersect custom classes with COCO classes at those same indices, leading to incorrect results (silent failure mode).
I pulled the latest state from dockerhub and receive now the following error:
Traceback (most recent call last):
File "detect.py", line 173, in <module>
detect()
File "detect.py", line 61, in detect
_ = model(img.half() if half else img) if device.type != 'cpu' else None # run once
File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 744, in _call_impl
result = self.forward(*input, **kwargs)
File "/usr/src/app/models/experimental.py", line 109, in forward
y = torch.cat(y, 1) # nms ensemble
RuntimeError: Sizes of tensors must match except in dimension 2. Got 6 and 85 (The offending index is 0)
Ah, so do I understand that correctly, that both of my datasets have a class with index 0 and that's why it crashes?
Yeah, I want to detect different classes from 2 datasets. Is there a better way to do? I was thinking about training all of them together but then I wanted to avoid that effort to confirm it works together first.
@philippneugebauer you can train on multiple datasets by passing a list of directories or txt files, though this is only possible for datasets that share the same classes. https://github.com/ultralytics/yolov5/blob/fda8df7e88a5fe3f500545eb0e3b3ed5d7c49e43/data/coco.yaml#L12-L13
@glenn-jocher sorry, I get confused. If I have two datasets: dataset1: there are cat, dog and python, and labeled as 0 , 1 and 2 dataset2: there are cat, fox and fish, and also labeled as 0 , 1 and 2 so, in this case, I cannot do model ensembling, right?
I have to intersect them to: dataset: cat, dog, python, fox and fish, and relabeled them as 0 , 1, 2, 3, 4 and train them separatly: dataset1_new: cat, dog and python, as 0, 1, 2, 3 4 (although there are no 3, 4, I have to label them in classes.txt to share same classes) dataset2_new: cat, fox and fish, as 0, 1, 2, 3 4 (although there are no 1, 2, I have to label them in classes.txt to share same classes)
do I understand correctly?
@ryan994 only models with identical classes may be ensembled. How you produce those models is up to you.
@AJ-RR see #318 (comment) for forward method of each.
@glenn-jocher, I feel I still lack a clear understanding. Are there any references that I can use (and might have to cite) to exactly understand how the mean is computed?
@AJ-RR you can view the Ensemble() module source here: https://github.com/ultralytics/yolov5/blob/f59f80114cbe4a7b3d4ea743b771aa1919fa8c8a/models/experimental.py#L98-L111
@glenn-jocher Is it possible to ensemble 2 different models together? I want to ensemble the yolov5l.pt
(80 classes) with my own custom model which has 40 classes. I can do it by loading them separately and go through the models with a separate NMS and loop for each one!
@Auth0rM0rgan yes you can ensemble as many different models as you want, but they should have intersecting or complementary classes. For example a custom COCO trained model can be ensembled with a pretrained model, or alternatively a custom model with class indices 80-90 can be ensembled with a COCO model. You can't ensemble the same custom 10 class model if the indices are denoted 0-9 with a COCO model, as the classes will conflict.
@glenn-jocher I got two models trianed based on two datasets model1-dataset1:['eye', 'nose', 'mouth', 'head'] model2-dataset2:['eye', 'nose', 'mouth'] according what you said, these two models cannot be essembled, right?(Actually, I have tried this but failed).
And I am confused that if I have a model trained based on ['eye', 'nose', 'mouth'] and I want to make a new model with ['eye', 'nose', 'mouth', 'head'], can I achieve this by only training the ['head'] part? Thank you!!
@letty825 ok looking back at this ensembling is intended for models with identical class vectors. Any other combination is too complicated to support easily. If there's concise modifications to the codebase that supports your use case feel free to submit a PR, thanks!
@glenn-jocher Does this ensemble method alleviate false positive detections or does it just prevent false negatives?
@connorlee77 the current ensemble technique (NMS ensemble) simply appends detections to a list from each model. The mechanism for increasing mAP is increasing recall, which will increase TPs and FPs, and reduce FNs.
@glenn-jocher I want to ensemble a model that I custom trained in yolov5 with one of the pretrained model in yolov5 . If yes how am I supposed to do it??
@pragyan430 if the classes are the same, you ensemble normally per the above tutorial.
@glenn-jocher Thanks for the quick reply, Is there no way to use the pretrained model yolov5s.pt that is available??
@pragyan430 you can ensemble any YOLOv5 models that share the same classes.
Hi @glenn-jocher
How can we combine more than 1 model together for detecting multiple classes together ?
Eg :-
Model 1 (classes : A , B , C) Model 2 (classes : P , Q)
while detection can we use , ( Model 1 and Model 2) for detecting (A,B,C,P,Q) Is it possible ?
How to handle class imbalance ?
Class A : 100 images Class B : 500 images
Does we have data augmentation for images and annotations like roboflow ?
Can you share any technique to handle this issue.
Thanks ,
@VinayChaudhari1996 only models trained on the same dataset may be ensembled, i.e. all YOLOv5 models may be ensembled together since they were all trained on COCO.
This is a Model Ensembling Tutorial. For questions unrelated questions raise a new issue or starting a new discussion.
@glenn-jocher : You are doing really a good job! :)
I was wondering how to do ensembling of models from torch hub for yolov5?
Regards
@gauravgund Torch Hub models are individual models with no ensemble capability currently. So far only test.py and detect.py have built-in ensembling capability. I'll keep this feature request in mind going forward.
When ensembling two models, I'm getting following error:
fatal: not a git repository (or any parent up to mount point /kaggle) Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set). detect: weights=['/kaggle/input/weights-of-yolov5-150-epochs/best.pt', '/kaggle/input/siim-cov19-yolov5-train/yolov5/runs/train/exp/weights/best.pt'], source=/kaggle/tmp/test/image, imgsz=512, conf_thres=0.005, iou_thres=0.5, max_det=1000, device=, view_img=False, save_txt=True, save_conf=True, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=True, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=True, line_thickness=3, hide_labels=False, hide_conf=False, half=False Ensemble created with ['/kaggle/input/weights-of-yolov5-150-epochs/best.pt', '/kaggle/input/siim-cov19-yolov5-train/yolov5/runs/train/exp/weights/best.pt'] image 1/2 /kaggle/tmp/test/image/51759b5579bc_image.png: Traceback (most recent call last): File "detect.py", line 228, in
main(opt) File "detect.py", line 223, in main run(vars(opt)) File "/opt/conda/lib/python3.7/site-packages/torch/autograd/grad_mode.py", line 26, in decorate_context return func(*args, *kwargs) File "detect.py", line 106, in run visualize=increment_path(save_dir / 'features', mkdir=True) if visualize else False)[0] File "/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(input, kwargs) TypeError: forward() got an unexpected keyword argument 'visualize'
and code is:
weights_dir = '/kaggle/input/weights-of-yolov5-150-epochs/best.pt'
weights_dir_1 = '/kaggle/input/siim-cov19-yolov5-train/yolov5/runs/train/exp/weights/best.pt'
!python detect.py --weights $weights_dir $weights_dir_1\
--img 512\
--conf 0.005\
--iou 0.5\
--source $test_dir\
--augment\
--save-txt --save-conf --exist-ok
works fine when I use either of the model.
@Muhammad4hmed your code is out of date, update your code:
git pull
from within your yolov5/
directory or git clone https://github.com/ultralytics/yolov5
againmodel = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
sudo docker pull ultralytics/yolov5:latest
to update your image Hi,
I have a question. This ensemble method can be used in PyTorch API ? I want to load multiple weights like in command line but in API.
for example;
model = torch.hub.load('ultralytics/yolov5', 'custom', path=['path/to/best1.pt','path/to/best2.pt'])
I can't find any reference, is there any other way around thanks
π This guide explains how to use YOLOv5 π model ensembling during testing and inference for improved mAP and Recall. UPDATED 25 September 2022.
From https://www.sciencedirect.com/topics/computer-science/ensemble-modeling:
Before You Start
Clone repo and install requirements.txt in a Python>=3.7.0 environment, including PyTorch>=1.7. Models and datasets download automatically from the latest YOLOv5 release.
Test Normally
Before ensembling we want to establish the baseline performance of a single model. This command tests YOLOv5x on COCO val2017 at image size 640 pixels.
yolov5x.pt
is the largest and most accurate model available. Other options areyolov5s.pt
,yolov5m.pt
andyolov5l.pt
, or you own checkpoint from training a custom dataset./weights/best.pt
. For details on all available models please see our README table.Output:
Ensemble Test
Multiple pretraind models may be ensembled togethor at test and inference time by simply appending extra models to the
--weights
argument in any existing val.py or detect.py command. This example tests an ensemble of 2 models togethor:Output:
Ensemble Inference
Append extra models to the
--weights
argument to run ensemble inference:Output:
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
If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training, validation, inference, export and benchmarks on MacOS, Windows, and Ubuntu every 24 hours and on every commit.