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

AssertionError: Label class 2 exceeds nc=2 in data/zhongmei.yaml. Possible class labels are 0-1 #1601

Closed
Stephenfang51 opened this issue Dec 9, 2020 · 9 comments
Labels
question Further information is requested

Comments

@Stephenfang51
Copy link

I have two classes

why the error occurs ?

Traceback (most recent call last):
  File "train.py", line 492, in <module>
    train(hyp, opt, device, tb_writer, wandb)
  File "train.py", line 191, in train
    assert mlc < nc, 'Label class %g exceeds nc=%g in %s. Possible class labels are 0-%g' % (mlc, nc, opt.data, nc - 1)
AssertionError: Label class 2 exceeds nc=2 in data/zhongmei.yaml. Possible class labels are 0-1
@Stephenfang51 Stephenfang51 added the question Further information is requested label Dec 9, 2020
@joshnarani9
Copy link

@Stephenfang51 can you please help me in resolving this, I too have same issue, I have only one class images , showing the error..

@glenn-jocher
Copy link
Member

@joshnarani9 @Stephenfang51 this is very simple. If your data.yaml shows nc=2, then valid class labels are 0-1. You have labels beyond 0-1.

@joshnarani9
Copy link

joshnarani9 commented Aug 28, 2021

@glenn-jocher I got it but already I do have class label as 1 and which is only 1 class, as I don't have label as 0 in all text files...which is why it is throwing error and my concern is without changing label to 0 in all files, I want to resolve it...

I can able to resolve only if in train.py file by commenting out assert statements and it worked fine

@glenn-jocher
Copy link
Member

@joshnarani9 I see your point. While commenting out the assert statement might temporarily resolve the issue, it's important to have the correct label values in your dataset for accurate training and inference. You can consider adding a background class with label 0 in your data.yaml and updating your label files accordingly. This would help in maintaining the integrity of your dataset. Let me know if you need further assistance with this approach.

@54HaoHao-hue
Copy link

@joshnarani9 Hi!I had to deal with the same thing.Your experience may be of great help to me. I don't want to add a background class with label 0 in my data.yaml, what assert statements should i comment out. I comment out the "assert mlc < nc, f'Label class {mlc} exceeds nc={nc} in {data}" in train_dual.py, but it still doesn't work.

@54HaoHao-hue
Copy link

@glenn-jocher can you please help me?My dataset is labeled from 1 to 21, with a total of 21 classes. If I add a background class with label 0 in my data.yaml, I worry that it will affect the training accuracy. if I don't want to add the label 0, but want to train the 21 classes with the label which from 1 to 21,what can i do?

@glenn-jocher
Copy link
Member

Hi @54HaoHao-hue,

Thank you for reaching out! If your dataset is labeled from 1 to 21 and you prefer not to add a background class with label 0, you can modify the code to accommodate your labeling scheme. However, it's generally recommended to start class labels from 0 for compatibility and consistency.

Here's a potential solution to modify the code:

  1. Update the data.yaml file:
    Ensure nc is set to 21, reflecting the number of classes you have.

  2. Modify the assertion in train.py:
    You can comment out or adjust the assertion that checks for label consistency. However, please proceed with caution as this might lead to unexpected behavior if not handled correctly.

    # Comment out or adjust this line in train.py
    # assert mlc < nc, f'Label class {mlc} exceeds nc={nc} in {data}'
  3. Adjust the label parsing logic:
    Ensure that the label parsing logic in your data loader correctly interprets your labels starting from 1. This might involve modifying how labels are read and processed.

While these steps can help you bypass the immediate issue, it's important to note that adhering to standard practices (starting labels from 0) can prevent potential issues and ensure compatibility with other tools and libraries.

If you encounter any further issues or need additional assistance, feel free to ask. The YOLO community and the Ultralytics team are here to help! 😊

@54HaoHao-hue
Copy link

@glenn-jocher Thank you very much for your patient reply, which helped me a lot

@glenn-jocher
Copy link
Member

Hi @54HaoHao-hue,

You're very welcome! I'm glad to hear that the information was helpful to you. 😊

If you encounter any more issues or have further questions, please don't hesitate to ask. The YOLO community and the Ultralytics team are always here to support you. Also, if you believe you've found a bug, please ensure you're using the latest versions of the packages and provide any relevant details to help us reproduce and address the issue.

Happy training and best of luck with your project!

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

4 participants