1

When training a custom dataset using YOLO and darknet, suppose available data is annotated with 3 classes, voc.names

cat
dog
bird

So, the .txt files will be in the format of

1 0.587 0.576 0.361 0.415
0 0.205 0.803 0.166 0.206
0 0.181 0.597 0.166 0.206
2 0.417 0.857 0.166 0.206

which means cat is class 0, dog - class 1, bird - class 2

If I want to train the model to detect only dog(class 1) and ignore remaining classes, how to do it? Can I change the voc.names file in the following manner i.e. leave the 1st and 3rd line empty

<assume empty line>
dog
<assume empty line>

If the above process is wrong, is there any other solution?

R-R
  • 317
  • 1
  • 6
  • 18

3 Answers3

0

If you want the model to detect all classes, but only draw the dog boxes, you can just add an if condition before the drawing function. But if you want to reduce the number of classes from 3 to 1, you should take some other steps:

  • You should change the number of classes in the code everywhere that it is mentioned from 3 to 1. For example, in each of the three yolo layers. Also change number of maximum batches which is number_of_classes*2000 (divide by 3).
  • change number of filters in each of the conv layers before yolo layers. By default it is number_of_filters=(classes + 5)x3
  • change voc.names to a file with 1 line (dog) without empty lines.
  • change number of classes in voc.data

It would help if you could post related part of the code or a link.

Hadi GhahremanNezhad
  • 2,377
  • 5
  • 29
  • 58
  • "change number of classes in voc.data" - But the problem is the annotated files(.txt) has 'dog' as class 1. If we mention only dog class in voc.names, it throws an error. In darkflow, if we mention a particular class in 'labels.txt', it automatically ignores the remaining classes from the annotated xml files and we can make the necessary changes in .cfg as you mentioned above. I think there is no such functionality in darknet, we might have to edit .txt files to contain only necessary classes. Thanks for your suggestions! – R-R Aug 05 '19 at 14:58
0

The safest way is to train your network with dog class only, you can refer to this question for details.

AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52
0

I think you have to remove other classes. It could be a solution :

import numpy as np
import os

# Specific class
class_remaining = 1
# Dataset's folder name
folder = "Dataset"

# Browse the dataset
for file in os.listdir(folder):
    # Get the data from txt file as numpy array
    data = np.genfromtxt(file, delimiter=" ")
    # Filter the array
    filter = data[:, 0] == class_remaining
    # Save the filter array in txt file
    np.savetxt(file, data[filter], newline='\n', fmt='%1.3f')
tCot
  • 307
  • 2
  • 7