Train your own tiny YOLO v3 on Google colaboratory with the custom dataset

Rafi
4 min readJul 24, 2019

Training custom data for object detection requires a lot of challenges, but with google colaboratory, we can leverage the power of free GPU for training our dataset quite easily.

The following are the steps you can follow to train your dataset in google colaboratory. Let’s walk through an example of custom helmet detection using custom data, which will showcase the process. The following are the steps you should follow to train your custom Yolo model:

1. Create dataset compatible with the YOLO format.

2. Divide the dataset into train-test format.

3. Calculate the anchors according to the data.

4. Modify the cfg file.

5. Prepare the data into a zip according to the google colaboratory.

6. Load the prepared data into the drive account of google.

7. Train the model

8. Finally, test the model.

Download the below Github repository for the reference and follow the steps together.

Step 1: First go to the data conversion folder and put your custom images to the input folder and change the class_list.txt to name the class i.e. how much class label you have for instance I have helmet class so only one class is there, you can put n-number of classes to this text file. Now, run the python main.py into your terminal or command prompt. This will prompt a GUI for creating a dataset. Prepare your data by making a bounding box to the object you want to detect and by pressing D you will load next image, A for previous and w, s for the changing the class label. After you have gone through all the images, the dataset will be ready and prepared. Now, goto to the output folder where you can find all the text files with the marked bounding box.

Finally, create one folder and put all the input images and text files together. The data is set to go for training. It’s time to split the data into train test.

Step 2: Goto to the train-test-conversion folder and run the process.py into your command prompt or terminal. It will create two text file name train and test. you have to change the path in the process.py file to the location of your own data.

Step 3: Move on to the anchor-calculation folder and run the anchor.py file into your terminal and before doing that edit the path according to the location of your train.txt file. It will create the anchors and gives the values of anchors which is calculated by the k-means clustering algorithm. Copy the anchors and this will be replaced with the anchors in the cfg file.

Step 4: Goto the data_for_colab folder. Replace the data folder with your own data folder you have created in step 1. Open yolov3-tiny-obj.cfg and replace the anchors in line 134 and 176 with the anchors calculated in step 3. Also, edit the class in line 135 and 177 to how many class you want to detect, in my case it was 1. Change the filters 127 and 171 to 18 if you have a single class. The formula is (n+1)*3 where n-no of class. Also, edit the obj.data and obj.names accordingly.

Step 5: Copy the train and test file you have calculated in step 2 to data_for_colab folder and then replace the path in the text file with the path of the google colaboratory which is: for instance if your path is:

/user/rafi/data/test.png — — to — —

/content/darknet/data_for_colab/data/test.png

Step 5: Zip the data_for_colab folder and upload the folder to google drive. Then, open then upload the helmet.ipynb file to google drive and open it and set the runtime environment to GPU it is set.

In the first cell, you have to mount your google drive so that the data you have uploaded to drive can be accessed here in the google colaboratory environment.
drive.mount(‘/content/drive’)
this will create a link which you have to give access and paste the key of your account.

Lastly at one of the cell you have to upload the train and test text file to the drive which you put into the data_for_colab drive.

That’s it run all the cell and training will starts and finally after 100 and then after every 1000 epoch you will get the trained weights in the backup folder which you can download by double click. You can see the folder structure at the right hand side of the window of google colaboratory.

Thanks and have a nice day.!!!!

Check out the Helmet detection which I have trained in google colaboratory.

Helmet Detection using YOLO

--

--

Rafi

Deep Learning, Computer-Vision, Object Detection, CNN architecture, Jetson Nano, Android-AI