Darknet: Error of "Too many or too few labels" while training a classification model

Created on 9 Nov 2019  Â·  4Comments  Â·  Source: pjreddie/darknet

I am trying to train a classification model (resnet18) on my custom dataset but getting the error

_Too many or too few labels: 2, C:yolodarknetbuilddarknetx64dataobj_20027_dog.png_

I have renamed the training image file as index_label.png and store the filepaths at train.list and put this train.list location to the train.data folder.

Can anyone please tell me why this error is coming and how to get rid of this error?

Most helpful comment

I had this error too and for me it was a simple (undocumented) rule

The name of the label cannot be also somewhere in the path

For me this was the problem because I was training on weather data and "rain" was triggered by the path "/home/darknet/data/myset/train/05_rain.jpg"

All 4 comments

check data file [ classes = ? ]

I had this error too and for me it was a simple (undocumented) rule

The name of the label cannot be also somewhere in the path

For me this was the problem because I was training on weather data and "rain" was triggered by the path "/home/darknet/data/myset/train/05_rain.jpg"

chrisiaut your comment solved my problem, although it was slightly different. I had a binary classification task and my two classes were 'brevis' and 'notbrevis.' Darknet couldn't grok the 'brevis' substring being in all the file names. I did a quick change to 'brevis' and 'detritus' and that fixed the problem. I need to do some research to see how small a substring will cause this error. Right now I'm thinking that if I have several species of phytoplankton all named xxxxxdinium the code will break. I need to see if there has to be absolutely no substring matching. What a pita.

I've investigated the code and had some changes in fill_truth function in src/data.c file. This allow we name root path freedom as we want.

void fill_truth(char *path, char **labels, int k, float *truth)
 {
     int i;
     char *filename = "";
     for (i = strlen(path) - 1; i >= 0; --i) {
         if (path[i] == '/' || path[i] == '\\') {
             filename = (char*)malloc(strlen(path + i) + 1);
             strcpy(filename, path + i + 1);
             break;
         }
     }
     memset(truth, 0, k*sizeof(float));
     int count = 0;
     for(i = 0; i < k; ++i){
         if(strstr(filename, labels[i])){
             truth[i] = 1;
             ++count;
             //printf("%s %s %d\n", path, labels[i], i);
         }
     }
     if(count != 1 && (k != 1 || count != 0)) printf("Too many or too few labels: %d, %s\n", count, path);
 }

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TheHidden1 picture TheHidden1  Â·  3Comments

kthordarson picture kthordarson  Â·  3Comments

arianaa30 picture arianaa30  Â·  3Comments

spaul13 picture spaul13  Â·  3Comments

sujithm picture sujithm  Â·  3Comments