Reputation: 21
I've been building the yolov5 environment and trying to run it for the last few days. I used the following code to test whether My setup was successful.
python train.py --img 640 --data data/coco128.yaml --cfg models/yolov5s.yaml --weights weights/yolov5s.pt --batch-size 16 --epochs 100
And then it gave me the following error, and I tried to find answers on Google, but I didn't see anything useful. I'm devastated right now. Can someone give me a hand? I really appreciate it.
Transferred 362/370 items from weights/yolov5s.pt
Optimizer groups: 62 .bias, 70 conv.weight, 59 other
Scanning labels data\coco128\labels\train2017.cache (32 found, 0 missing, 0 empty, 0 duplicate, for 32 images): 32it [00:00, 3270.57it/s]
Traceback (most recent call last):
File "train.py", line 456, in <module>
train(hyp, opt, device, tb_writer)
File "train.py", line 172, 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 15 exceeds nc=1 in data/coco128.yaml. Possible class labels are 0-0
I really don't use this site. Forgive me.
Upvotes: 2
Views: 14207
Reputation: 1
Yolo model's annotation format is (class x_center y_center width height) format. this issue occurs when you have unwanted label in your txt file and it's tough to check every txt file individually.so this script will help to find out class which cause error.
import os
path = './labels_path' # path of labels
labels = os.listdir(path)
for x in labels:
with open(os.path.join(path, x)) as f:
lines = f.read().splitlines()
for y in lines:
try:
if int(y[:2])>10: #if your last label is 2 digit ie nc=11 [0-10] label or change slice value
print(x) # it will print .txt file name
except:
pass
Upvotes: 0
Reputation: 1192
I was getting "corrupt image" .. "Possible class labels are".. until I nuked the labels.cache.
This was with YOLOv8.0.123.
I'd gone from classes 0..3 to 0..6 and tripled the images at that point (if that mattered).
Upvotes: 2
Reputation: 31
I have got the same problem while trained my dataset using YoloV5 model. I figured out the issue, that was in my classes.txt(inside data directory) file contains 2 class and I declare 1 class in my dataset.yaml. Also I change the colab run time as GPU.
Upvotes: 0
Reputation: 1
I also faced this problem and tried few solutions and solved as below:
Actually the dataset is consist of 11 class. And when i check .xml files which is include label of image, i saw label:11. So :
Dont forget to remove label chache !rm -f data/train/labels.npy
Upvotes: 0
Reputation: 118
I found this exact error too.
In your .txt files you've created for the annotations, there will be an integer number followed by four floats (ie, 13 0.3434 0.251 0.4364 0.34353) - something like that.
This error essentially articulates that your number of classes (ie, the number of different objects you're trying to train into the model) is too low for the ID number of the classes you're using. In my example above, the ID is 13 (the 14th class since 0 is included). If I set nc=1, then I can only have on class (0). I would need to set nc=14 and ensure that 0-12 existed.
To fix this, simply change the classes so that the IDs sit inside your chose number of classes. For nc=1, you'll only need class/ID = 0.
As a note (and I fell foul of this), delete train.cache before you re-run the training. That caused me a bit of a nuisance since it still was certain I had classes of >0, when I didn't.
Upvotes: 3
Reputation: 1
The error is caused by the fact that you have one or many labels with the number 15 as a class. You have to change the class to an allowed class value (which in your case appears to be only 0), you can do it manually or with a script. I changed the values of the classes manually in my dataset, for finding the files that contained the non-allowed classes, I ran a python script, which I have adapted for your situation:
path = 'C:/foo/bar' #path of labels
labels = os.listdir('path')
for x in labels:
with open('path'+x) as f:
lines = f.read().splitlines()
for y in lines:
if y[:1]!='0':
print(x)
This snippet will print all of the files that contain a class different from 0.
For anyone that finds this and has more than one class, you must substitute the value of 0 with the value or values(you could iterate through a list of possible values) of the class or classes that are higher than the number of classes you stated before.
Upvotes: 0