ultralytics / yolov5

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

Custom dataset for multi-label detection in a single bounding box #308

Closed yoni-f closed 4 years ago

yoni-f commented 4 years ago

Hello,

I have a dataset that involves images with bounding boxes which have more than 1 label.

For example, I have 5 classes (0-4), then an image might have a bounding box with label "2" and another might have a bounding box with two labels "2, 4" ie it belongs to both categories.

Two questions:

  1. Is it even possible for the model to output a single box with multi labels? Or are there changes that need to be made to the code?

  2. How would you go about building the labels .txt files?

For a single-label box, the .txt should something like this: 2 0.3 0.3 0.5 0.5

For a multi-label box, how would you go about? 2 4 0.3 0.3 0.5 0.5

Or

2 0.3 0.3 0.5 0.5 4 0.3 0.3 0.5 0.5

Thanks!

github-actions[bot] commented 4 years ago

Hello @yoni-f, thank you for your interest in our work! Please visit our Custom Training Tutorial to get started, and see our Jupyter Notebook Open In Colab, Docker Image, and Google Cloud Quickstart Guide for example environments.

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 model or data training question, please note that Ultralytics does not provide free personal support. As a leader in vision ML and AI, we do offer professional consulting, from simple expert advice up to delivery of fully customized, end-to-end production solutions for our clients, such as:

For more information please visit https://www.ultralytics.com.

glenn-jocher commented 4 years ago

@yoni-f multi-class output is on by default. This will output the same bounding box multiple times for any class above threshold. Use your second option for the labels: 2 0.3 0.3 0.5 0.5 4 0.3 0.3 0.5 0.5

github-actions[bot] commented 4 years ago

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.

wzj5133329 commented 3 years ago

@yoni-f multi-class output is on by default. This will output the same bounding box multiple times for any class above threshold. Use your second option for the labels: 2 0.3 0.3 0.5 0.5 4 0.3 0.3 0.5 0.5

@glenn-jocher hi,if use multi-labels in this project,do i need to modify the loss function? such as one label.txt: 2 0.3 0.3 0.5 0.5 4 0.3 0.3 0.5 0.5 in compute_loss() BCEcls(ps[:, 5:], t) t: [0,0,1,0,0] but really it is [0,0,1,0,1] in BCEWithLogitsLoss?

glenn-jocher commented 3 years ago

@wzj5133329 no loss function modifications are required.

wzj5133329 commented 3 years ago

@wzj5133329 no loss function modifications are required.

@glenn-jocher ok, i will have a try,thank you!

KrystalCWT commented 3 years ago

Hi, I tried to modify the label.txt to '2 0.3 0.3 0.5 0.5 4 0.3 0.3 0.5 0.5'

but when I detect, each detected box is only classified to one class. Do you know how to fix it?

wzj5133329 commented 3 years ago

Hi, I tried to modify the label.txt to '2 0.3 0.3 0.5 0.5 4 0.3 0.3 0.5 0.5'

but when I detect, each detected box is only classified to one class. Do you know how to fix it?

@KRYSTALLJY are you sure make multi_label=True? or you can debug see every class score.

glenn-jocher commented 3 years ago

@KRYSTALLJY @wzj5133329 multi_label=True is the default setting.

You can clearly see multilabel in action in inference results from detect.py. If you look at an ambiguous object competing classes will appear overlapped with multiple labels for a single box.

wzj5133329 commented 3 years ago

@KRYSTALLJY @wzj5133329 multi_label=True is the default setting.

You can clearly see multilabel in action in inference results from detect.py. If you look at an ambiguous object competing classes will appear overlapped with multiple labels for a single box.

@glenn-jocher I can get the desired results,but the class score relatively lower than signal_label in one box,is this normal?

glenn-jocher commented 3 years ago

@wzj5133329 multi-class scores can all vary from 0-1, there is no constraint and there is no softmax applied. Whatever it is you are talking about has nothing to do with multi-label.

abhigoku10 commented 3 years ago

@glenn-jocher @wzj5133329 can you please tell me how this multiclass scores work for single bounding box , though same object provided with same bounding box but different lables so that means that we get multiple boxes along with different labels rigth ? @wzj5133329 can you please share your results visualized for reference

glenn-jocher commented 3 years ago

@abhigoku10 multi-label allows one detection to have multiple labels. You can turn it on in NMS here: https://github.com/ultralytics/yolov5/blob/1922ddeac0294a91d585020bbb7290b8e353d813/utils/general.py#L607

loucif01 commented 3 years ago

@abhigoku10 multi-label allows one detection to have multiple labels. You can turn it on in NMS here:

https://github.com/ultralytics/yolov5/blob/1922ddeac0294a91d585020bbb7290b8e353d813/utils/general.py#L607

@glenn-jocher multi_label=True, this allows detection to have multiple labels through training or whene executing inference?

glenn-jocher commented 3 years ago

@loucif01 inference. NMS is not used during training.

loucif01 commented 3 years ago

@loucif01 inference. NMS is not used during training.

Thank you for your reply! now what i want to know is that does yolo support multi labels bounding boxes? because i want to train yolo on my dataset which contains multilabel bounding boxes (each bounding box can have multiple class labels), so i want to know if yolo accepts this and how to do it? Do i have just repeat the same bounding boxes for multiple labels?

glenn-jocher commented 3 years ago

@loucif01 yes you can simply add additional rows for additional classes with the same box, though our loss function is not specialized for multi-class, so I think there's a side effect of reduced confidence on these that another user discovered.

You do not actually need to train with multi-label labels though, for example the default COCO models are multi-label capable and will display multiple labels for an object (i.e. 0.7 car, 0.6 truck) when multi_label=True.

loucif01 commented 3 years ago

@glenn-jocher i did exactly what you said, but unfortunately i didn't get good results. Here is my results:

Class Images Targets P R mAP@.5 mAP@.5:.95: 100% 7/7 [00:06<00:00, 1.16it/s] all 204 1.82e+03 0.342 0.276 0.231 0.0871 CorrosionStain 204 321 0.44 0.302 0.262 0.104 Crack 204 550 0.123 0.0982 0.0365 0.00753 Efflorescence 204 172 0.195 0.151 0.0855 0.0206 ExposedBars 204 356 0.492 0.444 0.391 0.151 Spallation 204 422 0.459 0.386 0.38 0.152

Do you have any suggestion how to improuve it? my dataset have 5 classes : ['CorrosionStain', 'Crack', 'Efflorescence', 'ExposedBars', 'Spallation'], i trained YOLOv5, YOLOR and YOLOX but i still can't get mAP@0.5 > 0.27! Thanks you for your help! PS: I followed this tuto to train my models : https://www.youtube.com/watch?v=MdF6x6ZmLAY He got good results but not me!

glenn-jocher commented 3 years ago

@loucif01 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/

abhigoku10 commented 3 years ago

@glenn-jocher @loucif01 thanks for the response,

  1. I enabled the multilabel=True in the code and ran the pre-trained coco weights yet I am getting a single box and single labels, can we get multilabel for COCO classes also
    1. I trained on a sample set(20 imgs) that has a single box and multiple labels (ie same bbox values in .txt only class name changed) for few epochs and enabled multilabel = True yet i am not getting single box multiple label but getting multiple boxes similar to detections

Can you let me knw what error i am doing should i change anywhere else . THanks in adavance

glenn-jocher commented 3 years ago

@abhigoku10 as I mentioned above multi-label training is not in place currently.

multi-label inference is automatic if multi_label is true, as I also mentioned above. This is quite obvious in COCO, especially at reduced --conf levels.

abhigoku10 commented 3 years ago

@glenn-jocher thanks for the response, I forgot to mention previously I changed the --conf thresh to 0.08,0.05 yet i am not getting the results ie for single bounding box multiple labels. I am doing something wrong in testing this is again for pre-trained coco model

glenn-jocher commented 3 years ago

@abhigoku10 not sure what you mean.

Source image: https://www.purelypetsinsurance.co.uk/media/1138/dog-and-kitten-meeting.jpg

multi_label=False

image 2/3 yolov5/data/images/dog-and-kitten-meeting.jpeg: 448x640 1 cat, 1 dog, Done. (0.174s)

dog-and-kitten-meeting

multi_label=True

image 2/3 yolov5/data/images/dog-and-kitten-meeting.jpeg: 448x640 2 cats, 2 dogs, Done. (0.174s)

dog-and-kitten-meeting

PlinyTheMid commented 2 years ago

I think the actual problem is trying to train custom dataset with multiple class annotations in a single image. For eg. If there are 3 classes, some images contain only class 1 and the annotation txt file has only annotation pertaining to the class, but if a image has 2 class labels in it, we can only annotate them in the same txt file, to match the image file name.

The Image Screenshot 2022-10-11 at 3 38 11 AM

The Annotation file Screenshot 2022-10-11 at 3 40 06 AM

So when we train with a dataset like this, we often get Precision, Recall and mAP scores as absolute 0s. The question is if there's any flag or anything like NMS to enable training with multiple labels in a single image.

nikitabelooussovbtis commented 2 years ago

What would you recommend for making the labels more readable when multilabel is enabled. Once one object has more than two boxes is becomes difficult to read.

ma-aquifi commented 1 year ago

Can i follow a similar procedure for multi labeling in YOLOv7 ?

glenn-jocher commented 12 months ago

@ma-aquifi For multi-labeling with YOLOv7, you can follow a similar procedure as with YOLOv5. First, ensure that you have the multi-label flag enabled in the configuration. Then annotate your images and create label files that include multiple class labels for the same bounding box.

When working with multiple labels in a single image, it's important to format the labels in a way that makes them readable and understandable. One approach is to have each class label on a separate line in the label file, with the corresponding bounding box coordinates. Additionally, you can consider using different colors or annotations to visually distinguish between the different labels in the image.

As for YOLOv7, please note that YOLOv7 is not an official release from the original YOLO creators. The original YOLOv7 was developed by the Ultralytics team and is based on YOLOv5, with various enhancements and improvements. Therefore, you can apply similar multi-label training techniques in YOLOv7 as well.

I recommend referring to the official documentation and forums for YOLOv7 for specific guidance on multi-label training and any additional features or changes introduced in that version.