ultralytics / yolov5

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

Multi-label Classification using YOLOv5 #12032

Closed purnakar18 closed 1 year ago

purnakar18 commented 1 year ago

Search before asking

Question

Hi,

I have a custom dataset for which I am required to perform multi-label classification. I have followed the steps in https://blog.roboflow.com/train-yolov5-classification-custom-data/?ref=ultralytics for the task. So when I use Roboflow to annotate my custom dataset, I get 2 formats for downloading.

  1. Multi-label Classification csv
  2. Folder structure.

When I use the first type, the dataset comprises train/val/test folders and in each folder, there are images & a csv file with each image filename and the labels it belongs to. When I use this format to train the model I get an error that no folder for classes is found in the dataset/train directory. When I use the second folder structure for the dataset, the dataset comprises train/val/test folders and each folder consists of folders created by concatenating the label combinations found in the training set. When I give the train command, I do not get any errors but the problem is the model treats each combination as a new class which is not the correct way.

Is there any way I can achieve multi-label classification? Or do I treat this as an object detection problem giving the bounding box details as the entire image? Thanks in advance

Additional

No response

github-actions[bot] commented 1 year ago

👋 Hello @purnakar18, 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 a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Requirements

Python>=3.8.0 with all requirements.txt installed including PyTorch>=1.8. 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

YOLOv5 CI

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.

Introducing YOLOv8 🚀

We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 🚀!

Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects.

Check out our YOLOv8 Docs for details and get started with:

pip install ultralytics
glenn-jocher commented 1 year ago

@purnakar18 hi,

For multi-label classification using YOLOv5, you can treat it as an object detection problem by providing bounding box details for the entire image. In this case, each label would be considered as a separate object.

Alternatively, if you want to use the multi-label classification CSV format, make sure you have a folder named "classes" in the "dataset/train" directory. This folder should contain a single text file, where each line represents a label class.

Feel free to give either approach a try and see which one works best for your specific task. Let us know if you have any further questions or need additional assistance.

Best, Glenn

purnakar18 commented 1 year ago

Hello Glenn,

Thanks for your above suggestions. I have tried the first approach, i.e., treating it as object detection, the mAP was over 0.5 which is decent considering the type of images and the class imbalance. I wanted to try the second method, I had some queries regarding the same. You mention that under dataset/train create a folder called classes. This folder should have a single text file, should this text file have annotations of each image? e.g, if image1 has labels: 1,2 &5, and image2 has labels 2&4, should the entry to the classes.txt file have the below format: image1,1,2,5 image2,2,4 Is this the correct way? Thanks in Advance, Purna Kar

glenn-jocher commented 1 year ago

@purnakar18 hi Purna,

I'm glad to hear that treating your multi-label classification task as an object detection problem yielded good results!

Regarding your query about the second method, the text file in the "classes" folder should contain a list of all the label classes in your dataset, with each class on a separate line. This file is used to define the classes or labels that your model will be trained to predict.

For example, if your dataset has three classes (label 1, label 2, label 3), the "classes.txt" file would look like this:

label 1
label 2
label 3

There's no need to include the image names or annotations in this file; it's solely used for defining the class labels.

I hope this clarifies the process. If you have any further questions, feel free to ask.

Regards, Glenn

purnakar18 commented 1 year ago

Hello Glenn,

Thank you for your reply. One last confusion, this is the folder structure I am following for the dataset: dataset/train, dataset/val, dataset/test. I am uploading the actual images in the above locations. May I know where should I upload the annotations or the list of labels per image?

Thanks, Purna

glenn-jocher commented 1 year ago

@purnakar18 hi Purna,

In the folder structure you mentioned (dataset/train, dataset/val, dataset/test), you can place the annotations or the list of labels per image in the same location as the images themselves.

For each image in the dataset, you can create a corresponding text file with the same name but with the ".txt" extension instead. Each text file should contain the labels associated with that particular image. The labels should be in the format: class_index x_center y_center width height, where class_index is the index of the label class, and x_center, y_center, width, and height define the bounding box coordinates.

For example, if you have an image named "image1.jpg" located in the "dataset/train" folder, you would create a text file named "image1.txt" in the same folder. This text file would contain the labels associated with that image.

I hope this clears up any confusion. Let me know if you have any further questions.

Glenn

github-actions[bot] commented 1 year ago

👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

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 YOLO 🚀 and Vision AI ⭐