How to train multiple objects in YOLOv2 using your own Dataset

If you have not seen my previous post for training single object in yolo i recommend you read it. Please click here.

My Configuration:

OS: Ubuntu 16.04
Python Version: 2.7
GPU: 4gb

Now we are trying to train multiple objects in YOLO with cat and dog dataset.
Now i am using two objects to train in yolo.
Please Download the dataset from here
Extract the downloded dataset, It contains cat and dog images.

Data Preparation

Please follow the New Yolo Annotation Tool(latest update) for data preparation and skip the two steps(step 1 and step 2) and go to step 3 in this blog if your used new tool for data preparation. I have explained full description on New Yolo Annotation Tool blog. Please find the new tool link.

Step 1:
Download my git repository here
Run to generate the dataset
Please follow the video to create the bounding box to our dataset

Step 2:
To create the final txt file to all images where the object position on image.
Run to create the txt file
Please follow the video to create the final txt file with two object

Step 3:
Get training(80%) and testing(20%) dataset from this dataset
Please follow the video to get train and test dataset.

Preparing YOLOv2 configuration files

YOLOv2 needs certain specific files to know how and what to train. We’ll be creating these three files. I am using 4GB GPU. So i am used yolo.cfg:

Create file inside cfg folder and paste below code
Note: The two-object is where to store the final weights file

classes= 2 
train = train.txt
valid = test.txt
names = obj.names
backup = two-object/

Create obj.names file inside cfg folder and paste below code


just duplicated the yolo.cfg file, and made the following edits:

To start training, YOLOv2 requires a set of convolutional weights.
This conv.23 file can be downloaded (76Mb) from the official YOLOv2 website and provides an excellent starting point. We’ll need this file for the next step.


Time for the fun part! Enter the following command into your terminal and watch your GPU do what it does best (copy your train.txt and test.txt to yolo_darknet folder):

manivannan@manivannan-whirldatascience:~/YoloExample/darknet$ ./darknet detector train cfg/ cfg/yolo-obj.cfg darknet19_448.conv.23

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. But my average loss is 0.02


We should now have a .weights file that represents our trained model. Let’s use this on some images to see how well it can detect the cat and dog. This command unleashes YOLOv2 on an image of our choosing.If you want my trained weights file you try download here.

manivannan@manivannan-whirldatascience:~/YoloExample/darknet$ ./darknet detector test cfg/ cfg/yolo-obj.cfg Cat-Dog-Yolo.weights data/manivannan.jpg

Input Image

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

Output Image

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

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

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