Skip to content

Instantly share code, notes, and snippets.

@harshilpatel312
Last active June 27, 2018 13:25
Show Gist options
  • Save harshilpatel312/c14152690fa25d709c2bc40fcbff1b13 to your computer and use it in GitHub Desktop.
Save harshilpatel312/c14152690fa25d709c2bc40fcbff1b13 to your computer and use it in GitHub Desktop.
Train YOLO detector to perform object detection on test images/videos

Prerequisites

  1. Install tensorflow-gpu by running conda install -c anaconda tensorflow-gpu
  2. Install NVIDIA dependencies for using GPU https://gist.github.com/msis/108a74d08f55eed48d8521fa968851ea

Data Preparation

  1. Clone the repo
git clone https://github.com/experiencor/basic-yolo-keras
  1. Make a dataset folder and organize your dataset within it into 4 folders:
  • train_image_folder
  • train_annot_folder : contains labels in VOC format (.xml)
  • valid_image_folder
  • valid_annot_folder : contains labels in VOC format (.xml)
  1. Edit the following fields within config.json:
  • "labels": put all the labels that exist in your dataset as a list (e.g. ["boat", "person", "bird"])
  • add the path to your dataset under "train/valid_image_folder" and "train/valid_annot_folder"
  • "saved_weights_name": give a nice name
  • (Optional) "architecture": select from tiny yolo, full yolo, squeezenet, mobilenet, and inceptionV3
  1. Download pretrained weights for backend (tiny yolo, full yolo, squeezenet, mobilenet, and inceptionV3) at: https://1drv.ms/f/s!ApLdDEW3ut5fec2OzK4S4RpT-SU and put the weights in the project's root. Keep the "pretrained_weights" field within config.json EMPTY.

  2. Generate anchors for your dataset:

python gen_anchors.py -c config.json

Copy the generated anchors printed on the terminal to the "anchors" setting in config.json.

Training

  1. Warm up the network
  • Set warmup_epochs in config.json to 3.
  • python train.py -c config.json

This process saves the trained weights to the file specified in saved_weights_name setting.

[Note: if there is a ResourceExhaustedError or MemoryError, try decreasing the "batch_size"]

  1. Actual network training
  • Set pretrained_weights setting in config.json to the warmup weights (whatever in saved_weights_name).
  • Set warmup_epochs in config.json to 0.
  • python train.py -c config.json

By the end of this process, the code will write the weights of the best model to file best_weights.h5 (or whatever name specified in the setting "saved_weights_name" in the config.json file). The training process stops when the loss on the validation set is not improved in 3 consecutive epochs.

Test

python predict.py -c config.json -w /path/to/best_weights.h5 -i /path/to/image/or/video

It carries out detection on the image and write the image with detected bounding boxes to the same folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment