/TrainCustomModel

This repository will explain about how to train your custom object detection model using google colab

Primary LanguageJupyter Notebook

TrainCustomModel - Introduction

This repository will explain you the way to train your custom object detection model using tensorflow via google colab (FREE GPU). This tutorial is a combination of several tutorial below:

  1. How to train custom object detection model using Google Colab (Free GPU) Part 1
  2. How to train custom object detection model using Google Colab (Free GPU) Part 2
  3. How to train custom object detection model using Google Colab (Free GPU) Part 3
  4. How to Train a Custom Model for Object Detection (Local and Google Colab!)

Pose Estimator Analyzer

Contents

LabelImg Setup

Windows

  1. You can download labelImg here
  2. Change predefined_classes.txt inside data with your object target name. Here is the example that i use:
goalpost
ball
x_line
t_line
l_line
eros

Linux

  1. Open your terminal
  2. Type these following commands:
sudo apt-get update
sudo apt-get install python3 python3-pip pyqt5-dev-tools git
git clone https://github.com/tzutalin/labelImg.git
cd labelImg
pip3 install -r requirements/requirements-linux-python3.txt
make qt5py3
  1. Change predefined_classes.txt inside data with your object target name. Here is the example that i use:
goalpost
ball
x_line
t_line
l_line
eros
  1. To run labelImg, you can type python3 labelImg.py in your terminal inside labelImg folder

Labeling Instruction

  1. First, you need to create two folders: train and test

    create two folders
  2. Split your images into two folder

    Split your images into two folder

    note: you need to put 80% of your images to train folder and put the rest of it to test folder (i only put three images as an example). Also make sure that all of your object target is exist in both folder.

  3. Open your labelImg and make sure that your save format is PascalVOC

    make sure your save format is PascalVOC
  4. Click Open Dir and select your folder. I started it with test folder

    Click Open Dir and select your folder
  5. Click Change Save Dir and select the same folder that you opened in previous step

    Click Change Save Dir and select your folder
  6. Click view and select auto save mode

  7. Start annotate your images. Click W to create rectangle. Click D to move to the next image, and Click A to move to the previous image

  8. Select your object label

    Select your object label
  9. If you are done with all of images in test folder, you can repeat step 4 with train folder

  10. If you did it in a correct way, you will found several xml file inside your test and train folder

generate_xml

Edit Configuration

  1. There are tons of configuration in tensorflow. But i choose ssd_mobilenet_v2 due to its speed and accuracy. If you want to try another config, you can access it here

  2. Open your tensorflow config

  3. Change your number of classes based on total object that you want to detect

    Change your number of classes
  4. Set fine_tune_checkpoint based on your config. If you use ssd_mobilenet_v2, you can ignore it cause i already set it. But if you want to use another config, you can check the fine_tune_checkpoint list here

    Set fine_tune_checkpoint

Edit LabelMap

  1. Open labelmap.pbtxt

  2. change the content based on your model

     Edit LabelMap
  3. Save your labelmap.pbtxt

Edit generate_tfrecord

  1. Open generate_tfrecord.py

  2. change the content based on your model

     Edit generate_tfrecord.py
  3. Save your generate_tfrecord.py

Prepare your training folder

  1. Zip these file images, generate_tfrecord.py, labelmap.pbtxt, ssd_mobilenet_v2_coco.config (your config), and train.py

    zip your training file
  2. Rename it to my_training.zip

    Rename it to my_training.zip

Google Colab Steps

  1. Drop your my_training.zip and TrainCustomModelOnColab.ipynb to your google drive

    upload to drive
  2. Open your TrainCustomModelOnColab.ipynb with Google Colab. If you are not adding it yet, you can add it by clicking Connect more apps and search Google Colaboratory

    open with google colab
  3. Click runtime, and then click change runtime type, and select GPU

  4. Click runtime and select run all to start your Google Colab program. You can check your running update below every code

    running on google colab

    If you are worried about the process, you can run it one by one using alt + enter or click 'play' button for each step

  5. Don't forget to mount your drive with Google Colab by clicking the link that shown there or you can click here

  6. Enter your authorization code

    Enter your authorization code
  7. If your authorization and setup step succesfull, you can check your training progress

    check training process

    You can stop your training process when the loss rate is stagnant at low value (the lower your loss rate, the better result you will achieve) and your system already generate a checkpoint

    stop the training
  8. Stop your training process by clicking stop button

    click stop button
  9. Export your training to inference graph by changing ckpt-XXXX with the last checkpoint that you have (your ckpt index is like a picture on step 7)

    export to inference graph
  10. Run all step and make sure that all step is done. if you did every step in a corret way, you will find model_graph.zip and models-master.zip in your Google Drive

check your google drive

  1. Your training results is inside models-master.zip
  • models-master
    • content
      • models-master
        • research
          • object-detection
            • new_graph
              • frozen_inference_graph.pb
              • pipeline.config

Done

Your training process is over, you can check your training process by running inference that load your frozen_inference_graph.pb and pipeline.config. Good Luck!