YOLO

From Interaction Station Wiki
Jump to navigation Jump to search

Initial notes

  • This guide if for using YOLOv3 as part of Darknet.
  • The configuration file has been changed for making use of Nvidia GPU and of OpenCV (needed for running YOLOv3 in a video file).
  • Tested on Ubuntu 16.04

Installation

  • Go to the directory and download the YOLO weights:
cd darknet
wget https://pjreddie.com/media/files/yolov3.weights

Using YOLO

Using YOLO with an image

  • Go to the directory and run the detector for images:
cd darknet
./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg

Using YOLO with a video file

  • Go to the directory and run the detector for videos:
cd darknet
./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights data/ny720p2.mp4

Using YOLO with the live feed from a webcam

  • Go to the directory and run the detector for a webcam:
./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights

Training your own Dataset

  • Download the convolutional weights from the darknet53 model that are pre-trained on Imagenet and place them in the Darknet folder:
wget https://pjreddie.com/media/files/darknet53.conv.74

Step 1: Dataset

Step 2: Data Annotation

  • Download the YOLO annotation tool:
https://github.com/ManivannanMurugavel/Yolo-Annotation-Tool-New-
  • Modify the classes.txt file. Add the name of your categories, each one of them in a new line. Example:
Teletubby
Espinete
  • Enter the directory with the terminal:
cd Downloads
cd Yolo-Annotation-Tool-New-
python main.py
  • Note: If _tkinter is not installed, type in the terminal
sudo apt-get install python-tk
  • Note: If PIL is not installed, type in the terminal
pip install Pillow
  • Write the name of the folder/path where you have placed the images of the first category of your dataset and click the button "Load"
  • Select the category of those images in the combo box next that "Choose Class".
  • Create the bounding boxes in each one of the images in this category by first, clicking on the image and then clicking on the button Next
  • When you are done with all the images of that category, write the path of the next category and press the Load button.
  • Select the category of those images in the combo box next that "Choose Class".
  • Create the bounding boxes for all the images of this category.
  • Do the same for the rest of categories.
  • Whenever you are done, close the tool
  • The tool should have created one text file that contains the objects coordinates for each one of the images of your dataset.
  • Now we need to split the images of the dataset into two sets: train and test.
  • We will do that, by typing in the terminal:
python process.py
  • By default this is done in a 90%/10% ratio, but it can be changed in the script.
  • This script will generate the train.txt and test.txt files that we need to copy into the Darknet folder.
  • Copy the folder with the images and text files with the coordinates into the data folder of Darknet

Step 3: We need to create 3 files:


- File 1 : myDataset-obj.names
- This text file should only contain the names of the categories. For example if we have the categories cat and dog, the content be:
    Teletubby
    Espinete
- Copy this file into the Darknet directory
- File 2: myDataset.data
- This file contains the number of categories, the name of the .names file, the name of the train and validation set files, and the folder where you want to store the yolo weights file.
    classes= 2 
    train  = myDataset-train.txt  
    valid  = myDataset-test.txt  
    names = myDataset-obj.names  
    backup = backup/
- File 3: myDataset.cfg
- This file contains some parameters for the training. We can choose to start from the default yolov3.cfg config file or from yolov3-tiny.cfg (faster but less precise). For now we will work with the tiny version of yolov3.
- Duplicate and the file yolov3-tiny.cfg located in Darknet/cfg and rename it as myDataset-yolov3-tiny.cfg and make the following changes:
    Line 3: set batch=24, this means we will be using 24 images for every training step
    Line 4: set subdivisions=8, the batch will be divided by 8 to decrease GPU VRAM requirements.
    Line 127: set filters=(classes + 5)*3 in our case filters=21
    Line 135: set classes=2, the number of categories we want to detect
    Line 171: set filters=(classes + 5)*3 in our case filters=21
    Line 177: set classes=2, the number of categories we want to detect

Step 4: Train your Dataset!

./darknet detector train cfg/myDataset.data cfg/myDataset-yolov3-tiny.cfg darknet19_448.conv.23
- This step will take several hours...
- Note: When completed 100 iteration it will automatically store weights file and kill the process once the average loss is less than 0.06 to get good a accuracy.

Step 5: Test it!

- For testing your new model, go to your backup folder and rename the file generated as the result of the training process to myDataset.weights. Copy the file into the Darknet folder, and run this line:
./darknet detect cfg/yolov3.cfg myDataset.weights data/myImage.jpg
- Being myImage.jpg the one in which you would like to try to find the new categories that were in your dataset.

Sources

More information