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

how to save :AssertionError: Label class 16 exceeds nc=1 in ../data.yaml. Possible class labels are 0-0 #1538

Closed
yaober opened this issue Nov 27, 2020 · 25 comments
Labels
question Further information is requested

Comments

@yaober
Copy link

yaober commented Nov 27, 2020

❔Question

Additional context

@yaober yaober added the question Further information is requested label Nov 27, 2020
@yaober yaober changed the title how how to save :AssertionError: Label class 16 exceeds nc=1 in ../data.yaml. Possible class labels are 0-0 Nov 27, 2020
@github-actions
Copy link
Contributor

github-actions bot commented Nov 27, 2020

Hello @yaober, 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://www.ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

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

@yaober
Copy link
Author

yaober commented Nov 27, 2020

image

@glenn-jocher
Copy link
Member

@yaober your dataset can only contain a class 0, as you specified in your data.yaml. You've provided labels that have up to class 16.

@yaober
Copy link
Author

yaober commented Nov 27, 2020 via email

@yaober
Copy link
Author

yaober commented Nov 27, 2020

ur dataset can only contain a class 0,

but i just have one label class,in my dataset just have one too

@yaober
Copy link
Author

yaober commented Nov 27, 2020

Uploading image.png…

@yaober
Copy link
Author

yaober commented Nov 27, 2020

image

@glenn-jocher
Copy link
Member

@glenn-jocher your dataset has classes labelled as 16. You need to fix your dataset.

@yaober
Copy link
Author

yaober commented Nov 27, 2020 via email

@glenn-jocher
Copy link
Member

glenn-jocher commented Nov 27, 2020

@yaober
Copy link
Author

yaober commented Nov 27, 2020 via email

@yaober
Copy link
Author

yaober commented Nov 27, 2020 via email

@ghost
Copy link

ghost commented Jan 20, 2021

That probably because your current execution has reloaded the labels from the .cache file in your label folder, which hasn't been updated when you modify the actual label in the .txt file. Maybe try to delete the .cache and rerun it.

@mayukhberkeley
Copy link

When training with data I got annotated in Roboflow, I saw this error when I overwrote my train, valid, and test folder with the newly downloaded dataset using cp -r
I removed the previous datasets completely and then download new ones, looks like the dataset folders had contents from my previously downloaded datasets

@glenn-jocher
Copy link
Member

glenn-jocher commented Mar 12, 2021

@mayukhberkeley this error is generated by an unsupported 3rd party notebook. Please see the official YOLOv5 Colab Notebook below, and visit the Train Custom Data Tutorial to get started with YOLOv5.
https://colab.research.google.com/github/ultralytics/yolov5/blob/master/tutorial.ipynb

Tutorials

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

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 passing. These tests evaluate proper operation of basic YOLOv5 functionality, including training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu.

@triyam
Copy link

triyam commented Dec 14, 2021

image

Your label file may be something like this

The first part is label where I have 15 labels in default in my annotation software which are dog, bike, car etc.. which I don't require.

Change it to 1 if you are working on single class.

if you are working on multiclass then, change the class number to the first part of label instead of 15.

image

I have changed it to 1 because I am working on single class

@glenn-jocher
Copy link
Member

glenn-jocher commented Dec 14, 2021

@triyam python is zero indexed so single class datasets should be labelled as class 0. See Train Custom Data tutorial for details on correctly formatting your dataset:

YOLOv5 Tutorials

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

@Kiisso
Copy link

Kiisso commented Dec 14, 2021

Hello, have you solved your problem? I have the same problem.

@kranercc
Copy link

This is because your 1.txt 2.txt information has
1 0.something 0.something
make sure that 1 is 0 if you only work with one class
here is a simple script to modify all the 1's with 0's

import os
import sys

files = os.listdir(".")


for file in files:
    if ".txt" in file:
        with open(file, "r") as f:
            lines = f.readlines()
            for line in lines:
                if line[0] == "1":
                    line = line.replace("1", "0")
                    with open(file, "w") as f:
                        f.write(line)
                    print(line)
                else:
                    print(line)
                    continue
    else:
        continue

@vocdex
Copy link

vocdex commented Sep 29, 2022

@ kranercc
Just a warning! This will replace all the "1"s inside txt with "0". Instead, we want to replace only the class ID "1" with "0".

@Robotatron
Copy link

Isnt class: 0 reserved to be a background images in COCO? I.e. in the COCO format, the first label starts with "1", not "0".

@valentin-fngr
Copy link

That probably because your current execution has reloaded the labels from the .cache file in your label folder, which hasn't been updated when you modify the actual label in the .txt file. Maybe try to delete the .cache and rerun it.

This also works on Yolov8

@glenn-jocher
Copy link
Member

@valentin-fngr thank you for sharing your experience and solution. That is correct! When dealing with custom datasets, it may be necessary to manually refresh the cache file in order to reflect the latest changes made in the label files. Your solution of deleting the cache and rerunning the code can be an effective way to do this.

Note that, depending on the specific details of your workflow, there may be different ways to handle label files and caching. In some situations, it may be more appropriate to manually refresh the cache file using certain flags or command line arguments. Thank you for sharing your insight and contributing to the community's knowledge base!

@khoshg
Copy link

khoshg commented Sep 21, 2023

Go for the Label files (.txt). Open it, and check for the class id. that is, the first number for each file. If you have one class, change the class ID to 0. That's it. Make a program to do this if you have so many files.

@glenn-jocher
Copy link
Member

@khoshg to modify the class ID in the label files, you can follow these steps:

  1. Open each label file (.txt) and locate the class ID, which is the first number in each line.
  2. If you have only one class, change the class ID to 0.
  3. Repeat this process for all the label files.

If you have a large number of files, you can consider writing a program to automate this process. The program can iterate through each file, open it, and update the class ID as necessary. Remember to save the modified label files after making the changes.

Please let me know if you have any further questions or need any additional assistance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

10 participants