Image for post
Image for post

Training AlekseyAB YOLOv3 on own dataset in Google Colab

In my recent post I have presented a guide on training YOLOv3 darknet model on own dataset. In this post I will explain how to train YOLOv3 darknet model from AlekseyAB on own dataset in Goolge Colab.

I will omit preparing training data as it is covered in my previous post.

I my previous post I told about labelMe tool for labeling training samples. You can use also labeImg img. Install labelImg following official guide. If you face problem similar to mine just run this command:

To mount Google Drive in Google Colab use drive API

And add your working directory to sys.path in Python

You can access file system of your Google Drive from Colab. Click on Folder icon on left panel and select folder gdrive

Image for post
Image for post
Image for post
Image for post

To open file double click on it. If it opening popup for saving file navigate the file in Google Drive in browser and edit it using Text Editor app.

Now we need to clone repository with darknet YOLOv3 model to some folder in your Google Drive:

Edit Makefile to enable OPENCV and GPU:

Then compile model

And finally start training

here I redirect output to file log.txt. We can use output in log.txt file later to build loss plot.

Training on Google Colab with GPU can take hours.

If you set parameter jitter larger than 0.5 you can get error “Calloc error — possibly out of CPU RAM”. Optimal for me was jitter=.4.

When run training for the first time we can get error messages “Can’t open label file … images/…/…jpg”. We can see that training script looking for labels in directory with images. The problem is in source code of model on C. To fix that uncomment line 270 in file src/utils.c (source):

Show plot of training loss. Clone darknet_scripts repo and run plot_yolo_log.py

This script will create file loss_plot.jpg. Show loss_plot.jpg in Colab

Result

Image for post
Image for post

We can also calculate Mean Average Precision (mAP), precision, recall and another metrics during training using flag -map when start training

This will add following lines to log.txt

We can also create plots of key metrics during training using flag -dont_show

Plot will be saved in file chart.png. Here is my example cropped (original size)

Image for post
Image for post

mAP is calculated after every 100 iteration.

From previous experiments it was seen that optimal input size for image is 416x416.

First we try run training with best config from last experiment on darknet YOLOv3 model (batch=64, subdivisions=16, learning_rate=0.001, momentum=0.9). This time we use jitter=.3 as jitter large than 0.5 leads to memory shortage (out of CPU memory error).

Result loss plots for jitter=.3 and .4.

Image for post
Image for post
Loss plot for jitter=.3
Image for post
Image for post
Loss plot for jitter=.4

Then we tune parameters momentum and decay. Experiments have shown that most optimal values for these parameters are default ones. Loss plot for

decay=0.0002 (0.0005 default)

Image for post
Image for post

Then we tune burn_in. Results obtained

Image for post
Image for post
Loss plot for burn_in=500
Image for post
Image for post
Loss plot for burn_in=100

The best loss curve I get for burn_in=100. I used image size (416x416), burn_in=100, decay=0.0005 (default) and momentum=0.9 (default). Loss rapidly decreasing from the very start has very smooth curve and starting approaching 0 after 60 batches.

When training model the best indicator of training performance is validation data. There is a ‘map’ flag in darknet script which allows to calculate metrics like mAP, recall etc on validation data.

To evaluate model on validation data choose a weights file to evaluate performance on and use command:

If you get error “couldn’t open file: data/obj.names” copy files obj.names, train.txt and test.txt to folder darknet.

Output will be similar to that

Here we can see that model has Average precision (AP) 92.71% and mAP 92.26 %.

That’s all. Good luck in training your YOLOv3 detector.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store