In this tutorial I’m going to explain you one of the easiest way to train YOLO to detect a custom object even if you’re a beginner and have no experience with coding.

You will need just a simple laptop (windows, linux or mac), as the training is going to be done online, taking advantage of the free gpu offered by google colab.

We can train YOLO to detect a custom object, I choosed for example to detect a Koala, you can choose any animal/object you prefer.

Let’s start.

1. Prepare the Image dataset

An image dataset is a folder containing a lot of images (I suggest to get at least 100 of them) where there is the custom object you want to detect. For example I’m training YOLO to recognize a Koala, so I have downloaded around 350 images containing Koalas.

Having the iamges is not enough, but we also need to specify where the custom object is located on the specific image.

For this operation we will need an external software: LabelImg.
You can download it here for Windows and Linux: https://tzutalin.github.io/labelImg/
If you have the MAC, you can install it by following the instructions here: https://github.com/tzutalin/labelImg#macOS

1. Let’s set labelimg for our dataset:

  1. Once we run LabelImg let’s click on “Open Dir”.
  2. We choose the folder where the images are located
  3. Then we click on “Select folder”
  4. We then click on “Change save dir”.
  5. We select the folder where the images are located (same folder we selected on step 2).
  6. Then we click on “Select folder”.
  7. Finally make sure that we’re using the settings for YOLO.
    If pascalVOC is written, then let’s click and we will see YOLO.

2. Now we’re ready to label the images.

  1. Let’s click on “Create RectBox”
  2. Let’s select the area where our object is located (in my case I’m going to select the Koala)
  3. We add the label with the name of our object. In my case typed Koala and press Ok.
  4. We click on “Save”

We’re going to do this operation for all the images we have on the dataset.

At the end of this operation, we should see on the folder of the images, for each image a .txt file with the same name.

Inside the file will look like this:

15 0.392578 0.504395 0.566406 0.791992

3. Let’s now put all the images and txt file into a .zip archive called images.zip

Well done.
Your image dataset is now ready.

2. Train the Image dataset online

To train the image dataset we’re going to use the free server offered by google colab.

Google colab is a free service offered by google where you can run python scripts and use machine learning libraries taking advantage of their powerful hardware.
It’s for free with the only disadvantage the you can use it for 12 hours in a row, after that you’ll be disconnected and your files will be deleted.
you can restart it again but doing everything from scratch.

We can solve this problem by connecting google colab with google drive, so we won’t lose the files in case of disconnection.

Set up google drive:

  1. Go on google drive and log in. If you’ don’t have an account, create one and log in.
  2. Create a new folder called “yolov3”.
  3. Then upload the file “images.zip” you created before inside the yolov3 folder.

Set up google colab:

  1. Go on google colab and log in with the same account you used to log in on google drive.
  2. Upload this file “Train_YoloV3.ipynb”
    n.b. You can get this file by clicking on “Click here to download the Source code” at the beginning of the post.
  3. Then we need to enable the GPU. So click on “Edit”.
  4. Then we select “GPU” and click save.
  5. Now we’re ready to connect Colab with our drive.
    Run the cell where it’s written “from google.colam import drive” and then clicl on the link that appears.
  6. This links is to grant access to your google drive. Once you enter it asks you to allow the Google drive file Stream. Click on “Allow”. And then copy the code that appears.
  7. Paste the code on the notebook and press “Enter”.

Your colal is ready for the training.

Now you can start the training by simply clicking on “Runtime”, then “Run all”.

The command above is going to run all the cells.
The code will automatically install the darknet (framework used to run and train YOLO), it will make the configuration and it will run the training.

If you see an output similar to the one below, then well done, your model is training.

Leav it as long as it goes. Usually it takes a minimum of 3 hours to the entire 12 hours you have the access to the gpu.

We will see now how to use the model you’ve created.

3. Test the model we created

Each 100 iterations, our custom object detector is going to be updated and saved on our Google drive, inside the folder “yolov3”.

The file that we need is “yolov3_training_last.weights”.
You might find that other files are also saved on your drive, “yolov3_training__1000.weights”, “yolov3_training_2000.weights” and so on because the darknet makes a backup of the model each 1000 iterations.

I created a python project to test your model with Opencv.
The project is on the folder yolo_custom_detection, which contains 2 files (yolo_object_detection.py and yolov3_testing.cfg).
n.b. You can get this file by clicking on “Click here to download the Source code” at the beginning of the post.

You need to download the file yolov3_training_last.weights from Google Drive and place in on the same folder with yolo_object_detection.py and yolov3_testing.cfg.

On the file “yolo_object_detection.py” on line 11 change “koala” with the name of your object.
And on line 14 you have to put the path of a folder containing the images where there is your object, so you can test it.

# Load Yolo
net = cv2.dnn.readNet("yolov3_training_last.weights", "yolov3_testing.cfg")

# Name custom object
classes = ["Koala"]

# Images path
images_path = glob.glob(r"D:\Pysource\Youtube205) Train Yolo google cloud\dataset\*.jpg")

And finally, here is the result.
It works!