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

Custom Data Training Issue #424

Closed
DundieDev opened this issue Jul 16, 2020 · 31 comments
Closed

Custom Data Training Issue #424

DundieDev opened this issue Jul 16, 2020 · 31 comments

Comments

@DundieDev
Copy link

I am using Google Colab to train and test my network. I have been working with it the past few days and following your tutorial which worked fine.

I wanted to create my own dataset. So I labeled all my images using LabelImg (I set format to Yolo). I ended up with 20 classes. So I created a data.yaml file (down below) in where I put my nc to 20. I edited the yolov5m.yaml to match those 20 classes.

But whenever I want to start the training process I get this error:

Traceback (most recent call last):
  File "train.py", line 405, in <module>
    train(hyp)
  File "train.py", line 168, in train
    assert mlc < nc, 'Label class %g exceeds nc=%g in %s. Correct your labels or your model.' % (mlc, nc, opt.cfg)
AssertionError: Label class 20 exceeds nc=20 in /content/drive/My Drive/ColabNotebooks/yolov5m.yaml. Correct your labels or your model.

I am starting it with this command:
!python train.py --img 640 --batch 16 --epochs 50 --data /content/yolov5/data.yaml --cfg /content/drive/My\ Drive/ColabNotebooks/yolov5m.yaml --weights ''

My data.yaml looks like this:

train: ./train/images
val: ./valid/images

nc: 20
names: [ 'Class 1', 'Class 2', 'Class 3', 'Class 4', 'Class 5', 
'Class 6', 'Class 7', 'Class 8', 'Class 9', 'Class 10', 
'Class 11', 'Class 12', 'Class 13', 'Class 14', 'Class 15', 
'Class 16', 'Class 17', 'Class 18', 'Class 19', 'Class 20' ]

I don't have any idea what I have to do. I have been working on this problem for a day or two and still don't have any clue.

Any help appreciated!

@github-actions
Copy link
Contributor

github-actions bot commented Jul 16, 2020

Hello @DundieDev, 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:

  • Cloud-based AI systems operating on hundreds of HD video streams in realtime.
  • Edge AI integrated into custom iOS and Android apps for realtime 30 FPS video inference.
  • Custom data training, hyperparameter evolution, and model exportation to any destination.

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

@DundieDev
Copy link
Author

Okay, even when I changed in yolov5m.yaml the nc: 30. The same error occurs...

In my data.yaml I still have nc set to 20. And there are 20 classes in the names section...

When I change in my data.yaml file the nc: 30, I get this output:

Traceback (most recent call last):
  File "train.py", line 405, in <module>
    train(hyp)
  File "train.py", line 72, in train
    assert len(names) == nc, '%g names found for nc=%g dataset in %s' % (len(names), nc, opt.data)  # check
AssertionError: 20 names found for nc=30 dataset in /content/yolov5/data.yaml

Any help appreciated.

@xevolesi
Copy link

Hi, @DundieDev !
Try to check all your .txt files for incorrect class ids. Maybe it helps.

@glenn-jocher
Copy link
Member

glenn-jocher commented Jul 16, 2020

@DundieDev it's very simple. You labelled your dataset as including nc: 20, but your labels include classes>19. The error message already states this. See https://docs.ultralytics.com/yolov5/tutorials/train_custom_data

@glenn-jocher
Copy link
Member

Okay, even when I changed in yolov5m.yaml the nc: 30. The same error occurs...

In my data.yaml I still have nc set to 20. And there are 20 classes in the names section...

When I change in my data.yaml file the nc: 30, I get this output:

Traceback (most recent call last):
  File "train.py", line 405, in <module>
    train(hyp)
  File "train.py", line 72, in train
    assert len(names) == nc, '%g names found for nc=%g dataset in %s' % (len(names), nc, opt.data)  # check
AssertionError: 20 names found for nc=30 dataset in /content/yolov5/data.yaml

Any help appreciated.

As the error message states, you've declared that your dataset has 30 classes, but you've supplied 20 names. This will not fly for obvious reasons.

@poorneshwaran
Copy link

Hi,
And I have the same issue as AssertionError: Label class 7 exceeds nc=7 in ./models/yolov5l.yaml. Correct your labels or your model. I print mlc gives 7.0. but as per function condition mlc < nc .

@glenn-jocher
Copy link
Member

glenn-jocher commented Jul 19, 2020

@poorneshwaran see https://docs.ultralytics.com/yolov5/tutorials/train_custom_data#2-create-labels

As it says, class numbers are zero-indexed. 7 classes require labels 0-6.

@glenn-jocher
Copy link
Member

I've updated the assert error message to explain this more clearly.

glenn-jocher added a commit that referenced this issue Jul 19, 2020
Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>
NanoCode012 added a commit to MagicFrogSJTU/yolov5 that referenced this issue Jul 21, 2020
* update test.py --save-txt

* update test.py --save-txt

* add GH action tests

* requirements

* requirements

* requirements

* fix tests

* add badge

* lower batch-size

* weights

* args

* parallel

* rename eval

* rename eval

* paths

* rename

* lower bs

* timeout

* less xOS

* drop xOS

* git attrib

* paths

* paths

* Apply suggestions from code review

* Update eval.py

* Update eval.py

* update requirements.txt

* Update ci-testing.yml

* Update ci-testing.yml

* rename test

* revert test module to confuse users...

* update hubconf.py

* update common.py add Classify()

* Update ci-testing.yml

* Update ci-testing.yml

* Update ci-testing.yml

* Update ci-testing.yml

* update common.py Classify()

* Update ci-testing.yml

* update test.py

* update train.py ckpt loading

* update train.py class count assertion ultralytics#424

* update train.py class count assertion ultralytics#424

Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Update requirements.txt

* [WIP] Feature/ddp fixed (ultralytics#401)

* Squashed commit of the following:

commit d738487
Author: NanoCode012 <kevinvong@rocketmail.com>
Date:   Tue Jul 14 17:33:38 2020 +0700

    Adding world_size

    Reduce calls to torch.distributed. For use in create_dataloader.

commit e742dd9
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Tue Jul 14 15:38:48 2020 +0800

    Make SyncBN a choice

commit e90d400
Merge: 5bf8beb cd90360
Author: yzchen <Chenyzsjtu@gmail.com>
Date:   Tue Jul 14 15:32:10 2020 +0800

    Merge pull request #6 from NanoCode012/patch-5

    Update train.py

commit cd90360
Author: NanoCode012 <kevinvong@rocketmail.com>
Date:   Tue Jul 14 13:39:29 2020 +0700

    Update train.py

    Remove redundant `opt.` prefix.

commit 5bf8beb
Merge: c9558a9 a1c8406
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Tue Jul 14 14:09:51 2020 +0800

    Merge branch 'master' of https://github.com/ultralytics/yolov5 into feature/DDP_fixed

commit c9558a9
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Tue Jul 14 13:51:34 2020 +0800

    Add device allocation for loss compute

commit 4f08c69
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Thu Jul 9 11:16:27 2020 +0800

    Revert drop_last

commit 1dabe33
Merge: a1ce9b1 4b8450b
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Thu Jul 9 11:15:49 2020 +0800

    Merge branch 'feature/DDP_fixed' of https://github.com/MagicFrogSJTU/yolov5 into feature/DDP_fixed

commit a1ce9b1
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Thu Jul 9 11:15:21 2020 +0800

    fix lr warning

commit 4b8450b
Merge: b9a50ae 02c63ef
Author: yzchen <Chenyzsjtu@gmail.com>
Date:   Wed Jul 8 21:24:24 2020 +0800

    Merge pull request #4 from NanoCode012/patch-4

    Add drop_last for multi gpu

commit 02c63ef
Author: NanoCode012 <kevinvong@rocketmail.com>
Date:   Wed Jul 8 10:08:30 2020 +0700

    Add drop_last for multi gpu

commit b9a50ae
Merge: ec2dc6c 121d90b
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Tue Jul 7 19:48:04 2020 +0800

    Merge branch 'master' of https://github.com/ultralytics/yolov5 into feature/DDP_fixed

commit ec2dc6c
Merge: d0326e3 82a6182
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Tue Jul 7 19:34:31 2020 +0800

    Merge branch 'feature/DDP_fixed' of https://github.com/MagicFrogSJTU/yolov5 into feature/DDP_fixed

commit d0326e3
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Tue Jul 7 19:31:24 2020 +0800

    Add SyncBN

commit 82a6182
Merge: 96fa40a 050b2a5
Author: yzchen <Chenyzsjtu@gmail.com>
Date:   Tue Jul 7 19:21:01 2020 +0800

    Merge pull request #1 from NanoCode012/patch-2

    Convert BatchNorm to SyncBatchNorm

commit 050b2a5
Author: NanoCode012 <kevinvong@rocketmail.com>
Date:   Tue Jul 7 12:38:14 2020 +0700

    Add cleanup for process_group

commit 2aa3301
Author: NanoCode012 <kevinvong@rocketmail.com>
Date:   Tue Jul 7 12:07:40 2020 +0700

    Remove apex.parallel. Use torch.nn.parallel

    For future compatibility

commit 77c8e27
Author: NanoCode012 <kevinvong@rocketmail.com>
Date:   Tue Jul 7 01:54:39 2020 +0700

    Convert BatchNorm to SyncBatchNorm

commit 96fa40a
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Mon Jul 6 21:53:56 2020 +0800

    Fix the datset inconsistency problem

commit 16e7c26
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Mon Jul 6 11:34:03 2020 +0800

    Add loss multiplication to preserver the single-process performance

commit e838055
Merge: 625bb49 3bdea3f
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Fri Jul 3 20:56:30 2020 +0800

    Merge branch 'master' of https://github.com/ultralytics/yolov5 into feature/DDP_fixed

commit 625bb49
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Thu Jul 2 22:45:15 2020 +0800

    DDP established

* Squashed commit of the following:

commit 94147314e559a6bdd13cb9de62490d385c27596f
Merge: 65157e2 37acbdc
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Thu Jul 16 14:00:17 2020 +0800

    Merge branch 'master' of https://github.com/ultralytics/yolov4 into feature/DDP_fixed

commit 37acbdc
Author: Glenn Jocher <glenn.jocher@ultralytics.com>
Date:   Wed Jul 15 20:03:41 2020 -0700

    update test.py --save-txt

commit b8c2da4
Author: Glenn Jocher <glenn.jocher@ultralytics.com>
Date:   Wed Jul 15 20:00:48 2020 -0700

    update test.py --save-txt

commit 65157e2
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Wed Jul 15 16:44:13 2020 +0800

    Revert the README.md removal

commit 1c802bf
Merge: cd55b44 0f3b8bb
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Wed Jul 15 16:43:38 2020 +0800

    Merge branch 'feature/DDP_fixed' of https://github.com/MagicFrogSJTU/yolov5 into feature/DDP_fixed

commit cd55b44
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Wed Jul 15 16:42:33 2020 +0800

    fix the DDP performance deterioration bug.

commit 0f3b8bb
Author: Glenn Jocher <glenn.jocher@ultralytics.com>
Date:   Wed Jul 15 00:28:53 2020 -0700

    Delete README.md

commit f5921ba
Merge: 85ab2f3 bd3fdbb
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Wed Jul 15 11:20:17 2020 +0800

    Merge branch 'feature/DDP_fixed' of https://github.com/MagicFrogSJTU/yolov5 into feature/DDP_fixed

commit bd3fdbb
Author: Glenn Jocher <glenn.jocher@ultralytics.com>
Date:   Tue Jul 14 18:38:20 2020 -0700

    Update README.md

commit c1a97a7
Merge: 2bf86b8 f796708
Author: Glenn Jocher <glenn.jocher@ultralytics.com>
Date:   Tue Jul 14 18:36:53 2020 -0700

    Merge branch 'master' into feature/DDP_fixed

commit 2bf86b8
Author: NanoCode012 <kevinvong@rocketmail.com>
Date:   Tue Jul 14 22:18:15 2020 +0700

    Fixed world_size not found when called from test

commit 85ab2f3
Merge: 5a19011 c8357ad
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Tue Jul 14 22:19:58 2020 +0800

    Merge branch 'feature/DDP_fixed' of https://github.com/MagicFrogSJTU/yolov5 into feature/DDP_fixed

commit 5a19011
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Tue Jul 14 22:19:15 2020 +0800

    Add assertion for <=2 gpus DDP

commit c8357ad
Merge: e742dd9 787582f
Author: yzchen <Chenyzsjtu@gmail.com>
Date:   Tue Jul 14 22:10:02 2020 +0800

    Merge pull request #8 from MagicFrogSJTU/NanoCode012-patch-1

    Modify number of dataloaders' workers

commit 787582f
Author: NanoCode012 <kevinvong@rocketmail.com>
Date:   Tue Jul 14 20:38:58 2020 +0700

    Fixed issue with single gpu not having world_size

commit 6364892
Author: NanoCode012 <kevinvong@rocketmail.com>
Date:   Tue Jul 14 19:16:15 2020 +0700

    Add assert message for clarification

    Clarify why assertion was thrown to users

commit 69364d6
Author: NanoCode012 <kevinvong@rocketmail.com>
Date:   Tue Jul 14 17:36:48 2020 +0700

    Changed number of workers check

commit d738487
Author: NanoCode012 <kevinvong@rocketmail.com>
Date:   Tue Jul 14 17:33:38 2020 +0700

    Adding world_size

    Reduce calls to torch.distributed. For use in create_dataloader.

commit e742dd9
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Tue Jul 14 15:38:48 2020 +0800

    Make SyncBN a choice

commit e90d400
Merge: 5bf8beb cd90360
Author: yzchen <Chenyzsjtu@gmail.com>
Date:   Tue Jul 14 15:32:10 2020 +0800

    Merge pull request #6 from NanoCode012/patch-5

    Update train.py

commit cd90360
Author: NanoCode012 <kevinvong@rocketmail.com>
Date:   Tue Jul 14 13:39:29 2020 +0700

    Update train.py

    Remove redundant `opt.` prefix.

commit 5bf8beb
Merge: c9558a9 a1c8406
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Tue Jul 14 14:09:51 2020 +0800

    Merge branch 'master' of https://github.com/ultralytics/yolov5 into feature/DDP_fixed

commit c9558a9
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Tue Jul 14 13:51:34 2020 +0800

    Add device allocation for loss compute

commit 4f08c69
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Thu Jul 9 11:16:27 2020 +0800

    Revert drop_last

commit 1dabe33
Merge: a1ce9b1 4b8450b
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Thu Jul 9 11:15:49 2020 +0800

    Merge branch 'feature/DDP_fixed' of https://github.com/MagicFrogSJTU/yolov5 into feature/DDP_fixed

commit a1ce9b1
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Thu Jul 9 11:15:21 2020 +0800

    fix lr warning

commit 4b8450b
Merge: b9a50ae 02c63ef
Author: yzchen <Chenyzsjtu@gmail.com>
Date:   Wed Jul 8 21:24:24 2020 +0800

    Merge pull request #4 from NanoCode012/patch-4

    Add drop_last for multi gpu

commit 02c63ef
Author: NanoCode012 <kevinvong@rocketmail.com>
Date:   Wed Jul 8 10:08:30 2020 +0700

    Add drop_last for multi gpu

commit b9a50ae
Merge: ec2dc6c 121d90b
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Tue Jul 7 19:48:04 2020 +0800

    Merge branch 'master' of https://github.com/ultralytics/yolov5 into feature/DDP_fixed

commit ec2dc6c
Merge: d0326e3 82a6182
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Tue Jul 7 19:34:31 2020 +0800

    Merge branch 'feature/DDP_fixed' of https://github.com/MagicFrogSJTU/yolov5 into feature/DDP_fixed

commit d0326e3
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Tue Jul 7 19:31:24 2020 +0800

    Add SyncBN

commit 82a6182
Merge: 96fa40a 050b2a5
Author: yzchen <Chenyzsjtu@gmail.com>
Date:   Tue Jul 7 19:21:01 2020 +0800

    Merge pull request #1 from NanoCode012/patch-2

    Convert BatchNorm to SyncBatchNorm

commit 050b2a5
Author: NanoCode012 <kevinvong@rocketmail.com>
Date:   Tue Jul 7 12:38:14 2020 +0700

    Add cleanup for process_group

commit 2aa3301
Author: NanoCode012 <kevinvong@rocketmail.com>
Date:   Tue Jul 7 12:07:40 2020 +0700

    Remove apex.parallel. Use torch.nn.parallel

    For future compatibility

commit 77c8e27
Author: NanoCode012 <kevinvong@rocketmail.com>
Date:   Tue Jul 7 01:54:39 2020 +0700

    Convert BatchNorm to SyncBatchNorm

commit 96fa40a
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Mon Jul 6 21:53:56 2020 +0800

    Fix the datset inconsistency problem

commit 16e7c26
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Mon Jul 6 11:34:03 2020 +0800

    Add loss multiplication to preserver the single-process performance

commit e838055
Merge: 625bb49 3bdea3f
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Fri Jul 3 20:56:30 2020 +0800

    Merge branch 'master' of https://github.com/ultralytics/yolov5 into feature/DDP_fixed

commit 625bb49
Author: yizhi.chen <chenyzsjtu@outlook.com>
Date:   Thu Jul 2 22:45:15 2020 +0800

    DDP established

* Fixed destroy_process_group in DP mode

* Update torch_utils.py

* Update utils.py

Revert build_targets() to current master.

* Update datasets.py

* Fixed world_size attribute not found

Co-authored-by: NanoCode012 <kevinvong@rocketmail.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Update ci-testing.yml (ultralytics#445)

* Update ci-testing.yml

* Update ci-testing.yml

* Update requirements.txt

* Update requirements.txt

* Update google_utils.py

* Update test.py

* Update ci-testing.yml

* pretrained model loading bug fix (ultralytics#450)

Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>

* Update datasets.py (ultralytics#454)

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Jirka <jirka.borovec@seznam.cz>
Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
Co-authored-by: yzchen <Chenyzsjtu@gmail.com>
Co-authored-by: pritul dave <41751718+pritul2@users.noreply.github.com>
@poorneshwaran
Copy link

Hi glenn,
I've prepared the dataset labels like 0-6. but the errors are the same. so I print mlc which gives 7 and the condition follows mlc<nc.

@glenn-jocher
Copy link
Member

@poorneshwaran you have incorrect labels, you need to fix your labels. nc=7 mean s your labels can span 0-6.

@bipulneupane
Copy link

I get the similar error while trying to train for 1 class.

Traceback (most recent call last):
File "train.py", line 456, in <module>
train(hyp, opt, device, tb_writer)
File "train.py", line 171, 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=1 in ./data/traffic.yaml. Possible class labels are 0-0

My code for training: !python train.py --img 640 --batch 16 --epochs 10 --data ./data/traffic.yaml --cfg ./models/yolov5s.yaml --weights '' --nosave --cache

How first 4 lines of yolov5s.yaml looks like:
# parameters
nc: 1 # number of classes
depth_multiple: 0.33 # model depth multiple
width_multiple: 0.50 # layer channel multiple

How traffic.yaml looks like:
# train and val datasets (image directory or *.txt file with image paths)
train: ./data/image
val: ./data/val_image
test: ./data/test_image

# number of classes
nc: 1

# class names
names: ['vehicle']

I have checked all my label text files to look for errors, which i am 99.9% sure of. Is there anything else to do if i am training for 1 class? Any help would be given a millions of thanks.

@bipulneupane
Copy link

I get the similar error while trying to train for 1 class.

Traceback (most recent call last):
File "train.py", line 456, in <module>
train(hyp, opt, device, tb_writer)
File "train.py", line 171, 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=1 in ./data/traffic.yaml. Possible class labels are 0-0

My code for training: !python train.py --img 640 --batch 16 --epochs 10 --data ./data/traffic.yaml --cfg ./models/yolov5s.yaml --weights '' --nosave --cache

How first 4 lines of yolov5s.yaml looks like:
# parameters
nc: 1 # number of classes
depth_multiple: 0.33 # model depth multiple
width_multiple: 0.50 # layer channel multiple

How traffic.yaml looks like:
# train and val datasets (image directory or *.txt file with image paths)
train: ./data/image
val: ./data/val_image
test: ./data/test_image

# number of classes
nc: 1

# class names
names: ['vehicle']

I have checked all my label text files to look for errors, which i am 99.9% sure of. Is there anything else to do if i am training for 1 class? Any help would be given a millions of thanks.

I found out that some of my image were jpg and some png. I converted all images into jpg and the training ran without error.

@djalusic
Copy link

djalusic commented Nov 5, 2020

@bipulneupane, I had the same problem and noticed that there's a flag single-cls so try adding that to the command.

@mycuriosity123
Copy link

I suppose the error is bcz of mismatch in number classes in .yaml file and annotated(.txt) file classes

@THANGAKOWSALYA
Copy link

how to solve this

Capture
`

@glenn-jocher
Copy link
Member

@THANGAKOWSALYA your code may be out of date. To update:

  • Gitgit pull from within your yolov5/ directory or git clone https://github.com/ultralytics/yolov5 again
  • PyTorch Hub – Force-reload model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
  • Notebooks – View updated notebooks Open In Colab Open In Kaggle
  • Dockersudo docker pull ultralytics/yolov5:latest to update your image Docker Pulls

@X3nX3n
Copy link

X3nX3n commented Jan 11, 2022

I am using Google Colab to train and test my network. I have been working with it the past few days and following your tutorial which worked fine.

I wanted to create my own dataset. So I labeled all my images using LabelImg (I set format to Yolo). I ended up with 20 classes. So I created a data.yaml file (down below) in where I put my nc to 20. I edited the yolov5m.yaml to match those 20 classes.

But whenever I want to start the training process I get this error:

Traceback (most recent call last):
  File "train.py", line 405, in <module>
    train(hyp)
  File "train.py", line 168, in train
    assert mlc < nc, 'Label class %g exceeds nc=%g in %s. Correct your labels or your model.' % (mlc, nc, opt.cfg)
AssertionError: Label class 20 exceeds nc=20 in /content/drive/My Drive/ColabNotebooks/yolov5m.yaml. Correct your labels or your model.

I am starting it with this command: !python train.py --img 640 --batch 16 --epochs 50 --data /content/yolov5/data.yaml --cfg /content/drive/My\ Drive/ColabNotebooks/yolov5m.yaml --weights ''

My data.yaml looks like this:

train: ./train/images
val: ./valid/images

nc: 20
names: [ 'Class 1', 'Class 2', 'Class 3', 'Class 4', 'Class 5', 
'Class 6', 'Class 7', 'Class 8', 'Class 9', 'Class 10', 
'Class 11', 'Class 12', 'Class 13', 'Class 14', 'Class 15', 
'Class 16', 'Class 17', 'Class 18', 'Class 19', 'Class 20' ]

I don't have any idea what I have to do. I have been working on this problem for a day or two and still don't have any clue.

Any help appreciated!

I don't know if this will help anyone. I had the same problem. I read the code and added another class to dataset.yaml and to the list of classes in labels that I don’t use in training, it’s just there so that the "mlc <nc" condition would be satisfied and apparently it solved the problem for me ... At the moment, the network is still learning and I can't say for sure, but judging by train_batch0.png, the classes were defined correctly.

@glenn-jocher
Copy link
Member

glenn-jocher commented Jan 11, 2022

@RuslanChokawi if you have 20 classes your label indices can only be 0-19. The Train Custom Dataset tutorial covers this clearly:

YOLOv5 Tutorials

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

@X3nX3n
Copy link

X3nX3n commented Jan 11, 2022

@poorneshwaran you have incorrect labels, you need to fix your labels. nc=7 mean s your labels can span 0-6.

I'm sorry, but are you sure that the condition should be "mlc <nc" and not "mlc <= nc"? I have the same number of classes in yaml and classes.txt, and I get the same error: "Label class 25 exceeds nc = 25 in. \ Data \ mydataset.yaml. Possible class labels 0-24" However, if I add to these files one class, which will be in them but will not be marked in any image, everything seems to work. Thanks!

@glenn-jocher
Copy link
Member

@RuslanChokawi this is very simple. 25 classes are zero indexed 0-24 in your labels.txt files.

@X3nX3n
Copy link

X3nX3n commented Jan 11, 2022

@RuslanChokawi this is very simple. 25 classes are zero indexed 0-24 in your labels.txt files.

I understand this, but I don't understand why it doesn't work. I have the same number of classes, I have been doing all sorts of checks all day today, but later looking into the code, I applied this solution (probably quite wild) and it worked. Perhaps tomorrow I will try to solve it somehow more adequately. I'll go through my classes again. But everything looks as it should. I switched yesterday from yolov3, but I'm not sure if this is the case.

@X3nX3n
Copy link

X3nX3n commented Jan 11, 2022

I think I'm doing something wrong, but I can't figure out what yet.

@kuangxiaoye
Copy link

I solved this problem: Don't change labimg's class.txt arbitrarily

@mfwz247
Copy link

mfwz247 commented Apr 14, 2022

Can you please provide more information?

@kuangxiaoye
Copy link

Can you please provide more information?

This error happened to me because, when I finished to label the picture by labimg software, I modified the label files content directly in the folder generated by labimg.

To fix this error.

Before labeling, I follow the regular order set the label in labimg, and label, import YOLO, this error disappeared.

@epenaintecol
Copy link

check your classes.txt taken by LabelImg. it is possible you have 21 classes instead of 20 (20 created by you and an aditional class classes.txt had by default)

@xzkeee
Copy link

xzkeee commented Nov 26, 2022

if you change the class number, delete all the cache files in the data folder, otherwise will have this error

@saqibshinwari777
Copy link

hello! i trained yolov7 on custom dataset and have two classses it work fine .. and show the class name and confidence ... but when i try i on 5 classes dataset then only index of the class is shown ... not the class name.. every thank is perfect like yaml file ete.. so please help to show the class name and probabilty when detecting 5 class dataset

@saqibshinwari777
Copy link

hello! i trained yolov7 on custom dataset and have two classses it work fine .. and show the class name and confidence ... but when i try i on 5 classes dataset then only index of the class is shown ... not the class name.. every thank is perfect like yaml file ete.. so please help to show the class name and probabilty when detecting 5 class dataset
train_batch3 (1)

@dhp2015
Copy link

dhp2015 commented May 23, 2023

IS there any method to increase the output length? I could only get a answer of approximately 160 words (~250 tokens) right now. thanks

@glenn-jocher
Copy link
Member

@dhp2015 hello! To show the class names and probabilities when detecting a 5-class dataset, ensure the following:

  1. In your data.yaml file, set nc: 5 and provide the names for your 5 classes.

  2. Confirm that the model is using the correct yolov5.yaml configuration file with the 5 classes specified.

If everything is properly set and you are still only seeing class indices, please provide the command you are using for inference and the contents of your data.yaml and modified yolov5.yaml files for further assistance.

Thank you!

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

No branches or pull requests

16 participants