Robotics URJC

Logo

Personal webpage for TFM Students.

View the Project on GitHub RoboticsLabURJC/2018-tfm-Jessica-Fernandez

Darknet Training

To train a model with my dataset I use (Darknet). I adapted it so I could install it on my computer and use DarknetApi on smart-traffic-sensor. I have this adaptation in [https://github.com/RoboticsURJC-students/2018-tfm-Jessica-Fernandez]

If you have to use OpenCV, Cuda and GPU, you have to edit the Makefile:

  GPU=0
  CUDNN=0
  OPENCV=0

To train you can execute make for generating the executable.

  cd darknet
  make -j4

Here are the steps to follow to train a model with your dataset:

1- We need to generate the label files that Darknet uses. Darknet wants a .txt file for each image with a line for each ground truth object in the image that looks like:

  <object-class> <x> <y> <width> <height>

Where x, y, width, and height are relative to the image’s width and height. In my case I have xml label files because I used labelImg and I saved in this format.To generate these file(.txt) we will run the voc_label.py script in Darknet’s scripts/ directory. To execute this script you need an annotations folder (folder with xml labels), a .txt with the annotations names file (annotations_file.txt) and an output folder.

Next it shows that it should contain the annotations_file.txt:

0006-00000001.xml
0006-00000002.xml
0006-00000003.xml
0006-00000005.xml
0006-00000006.xml
0006-00000007.xml
0006-00000008.xml
...

You have to have the next files in the directory darknet/scripts:

Directory

Now you can generate the .txt files (you get this files in output folder):

   python voc_label.py -xml annotations/ -xml_files annotations_file.txt -out labels/

You have to copy the labels folder in the darknet/data folder. In the darknet/folder you should have the images and labels folders.

2- Darknet needs one text file with all of the images you want to train on and other with all of the images you want to test. Below is an example of what you should put in both files:

   /home/docker/Jessi/Darknet_training/darknet/data/images/0081-00000055.jpg
   /home/docker/Jessi/Darknet_training/darknet/data/images/0081-00000056.jpg
   /home/docker/Jessi/Darknet_training/darknet/data/images/0081-00000057.jpg
   /home/docker/Jessi/Darknet_training/darknet/data/images/0081-00000058.jpg
   /home/docker/Jessi/Darknet_training/darknet/data/images/0081-00000059.jpg
   /home/docker/Jessi/Darknet_training/darknet/data/images/0081-00000060.jpg

In my case I have these files in scripts folder.

3- We have to change the cfg/voc.data config file to point to your data:

   classes= 8
   train  = /home/docker/Jessi/Darknet_training/darknet/scripts/train.txt
   valid  = /home/docker/Jessi/Darknet_training/darknet/scripts/test.txt
   names = /home/docker/Jessi/Darknet_training/darknet/data/voc.names
   backup = /home/docker/Jessi/Darknet_training/darknet/backup

voc.names is a file where the names of the classes that we want to train are indicated. backup is a folder where all the results will be saved. The weights file are saved very 100 iterations during the first 1000 iterations and then every 1000 iterations. If you need change this you have to edit the line 138 of examples/detector.c (if(i%1000==0 || (i < 1000 && i%100 == 0))).

My voc.names is the next:

   None
   motorcycle
   car
   van
   bus
   truck
   small-truck
   tank-truck

4- For training we use convolutional weights that are pre-trained on Imagenet. We use weights from the darknet53 model. You can just download the weights for the convolutional layers here (76 MB).

5- You have to edit the .cfg file. In my case I used yolov3-voc.cfg. You must modify the following:

So if classes=1 then should be filters=18. If classes=2 then write filters=21.

6- Now we can train! Run the command:

./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg darknet53.conv.74

During training, you will see varying indicators of error, and you should stop when no longer decreases 0.XXXXXXX avg:

Region Avg IOU: 0.798363, Class: 0.893232, Obj: 0.700808, No Obj: 0.004567, Avg Recall: 1.000000, count: 8 Region Avg IOU: 0.800677, Class: 0.892181, Obj: 0.701590, No Obj: 0.004574, Avg Recall: 1.000000, count: 8

9002: 0.211667, 0.060730 avg, 0.001000 rate, 3.868000 seconds, 576128 images Loaded: 0.000000 seconds

When you see that average loss 0.xxxxxx avg no longer decreases at many iterations then you should stop training.