YOLO

From Interaction Station Wiki
Jump to navigation Jump to search

What is YOLO?

YOLO (You Only Look Once) is a state-of-the-art (2019) technique to detect objects within images. One of its advantages is that it's extremely fast compared to other techniques, which makes it suitable for using it with video feeds at high frame rates (with a fast Nvidia GPU).

YOLO applies a single neural network to the full image. This network divides the image into regions and predicts bounding boxes and probabilities for each region. These bounding boxes are weighted by the predicted probabilities.

Initial notes

  • This guide was tested with 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

Changing The Detection Threshold

  • By default, YOLO only displays objects detected with a confidence of .25 or higher. You can change this by passing the -thresh <val> flag
./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg -thresh .1

Tiny YOLOv3

  • Tiny YOLOv3 is a smaller model as well for constrained environments. To use this model, first download the weights:
cd darknet
wget https://pjreddie.com/media/files/yolov3-tiny.weights
  • Then run the detector with the tiny config file and weights:
./darknet detect cfg/yolov3-tiny.cfg yolov3-tiny.weights data/dog.jpg


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.
  • Rename the folder that contains all the images with the name of your dataset and copy it to 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 your dataset directory in Darknet.
- 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/myDataset-obj.names  
    backup = backup/
- Copy this file into your dataset directory in Darknet.
- 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
- Copy this file into your dataset directory in Darknet.

Step 4: Train your Dataset!

./darknet detector train myDataset/myDataset.data myDataset/myDataset-yolov3-tiny.cfg darknet53.conv.74
- 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.
-*https://medium.com/techfeeds/yolo-single-image-train-single-object-training-9ba830076776
-Note: If getting "annot load image" errors might be the encoding/settings of the train.txt and test.txt text files. Open them to the Ubuntu text editor, click File-Save as and select
Character encoding: Current Locale (UTF-8)
Line Ending: Unix/Linux

Click Save

-Note: If you would like to use YOLOv3 instead of YOLOv3-tiny, duplicate the yolov3.cfg in the previous step instead of yolov3-tiny.cfg
-More info:
-https://github.com/pjreddie/darknet/issues/611

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