• Tensorflow Object Detection2 Collab Tutorial

    --- Let's get started with Tensorflow2 ---

    Step 1: Data Annotations

    For data annotation, we will be using Labelimg Tool
    Download the tool from the given github link:-
    https://github.com/tzutalin/labelImg

    You will see same interface as above

    Click on Open Dir and select the folder where your training/test images are. Then start labelling all images in the dataset.

    The selected image is loaded in the LabelImg Interface




    Click on Create RectBox.


    Create the Bounding Box and write the Label name corresponding to the image

    Select PascalVOC from the option. Click on the Save button and a corresponding XML file will be saved in the directory with the image.

    Step 2 : Mounting Google Drive

    To Mount your drive execute below code.

    from google.colab import drive
    drive.mount('/content/drive')

    Create a folder inside your drive (for e.g. TFOD3)

    After creating the folder change the directory to your desired directory as shown.

    %cd /content/drive/My Drive/TFOD3

    Below Code will Clone the Official Models Repository inside your desired folder

    import os
    import pathlib


    if "models" in pathlib.Path.cwd().parts:
      while "models" in pathlib.Path.cwd().parts:
        os.chdir('..')
    elif not pathlib.Path('models').exists():
      !git clone --depth 1 https://github.com/tensorflow/models

    This will clone the models repository in the TFOD3 for training and future reference of the model checkpoints. I will be keeping my complete repository and the folder structure in the TFOD3 folder.

    Some sample pictures are provided below :

    Inside models we have other folder out of which research and official are the important ones.

    Inside the research folder we have the most important folder object_detection


    Step 3 : Installing necessary libraries :


    • Installing pycocotools

    !pip install pycocotools

    • Installing tfslim

    !pip install tf_slim

    • Conversion of the protos files to the python files

    %%bash
    cd /content/drive/MyDrive/TFOD3/models/research
    pip install .

    • Installing setup.py

    %cd /content/drive/My Drive/TFOD3/models/research
    !python setup.py install

    If you are getting error in setup.py installation download the setup.py file from the below link and put it inside your research folder.
    click here  to download the file.

    Step 4 : Importing Packages:

    • Importing all the useful libraries

    import numpy as np
    import os
    import six.moves.urllib as urllib
    import sys
    import tarfile
    import tensorflow as tf
    import zipfile

    from collections import defaultdict
    from io import StringIO
    from matplotlib import pyplot as plt
    from PIL import Image
    from IPython.display import display

    • Importing all the object detection modules

    from object_detection.utils import ops as utils_ops
    from object_detection.utils import label_map_util
    from object_detection.utils import visualization_utils as vis_util

    • Configuring some patched for TF2 form TF1:

    # patch tf1 into `utils.ops`
    utils_ops.tf = tf.compat.v1

    # Patch the location of gfile
    tf.gfile = tf.io.gfile


    Step 5 : Training Setup:

    • Prepairing Dataset for Custom Training

    Let's say we have 4000 images and their 4000 corresponding annotation files. Then we will split it into 80:20 ratio like 3000 images and their 3000 corresponding annotation files in the train folder and 100 images and their 100 corresponding annotation files in the test folder.

    So the directory structure of the dataset will be like this


    • Creating The Dataset for Training

    1. Download these files from the given link and place them inside your object_detection folder.

        xml_to_csv.py link here 
        generate_tfrecord.py link here
    2. Inside the object_detection create a folder name images

    3. Inside the images folder create train and test folder where you will put the train and test images with their xml files.

    4. Creating XML to CSV:

    Execute the below code to convert XML to CSV.

    !python xml_to_csv.py
    print("Done")

    test_labels.csv and train_labels.csv files will be created inside the images folder.


    • Creating TF Records : 

    Open generate_tfrecords.py file that you have downloaded early and do these changes on it according to your classes.

    changes to do in generate_tfrecord.py file

    #TO-DO replace this with label map

    def class_text_to_int(row_label):
        if row_label == 'class1':
            return 1
        elif row_label == 'class2':
            return 2
        elif row_label == 'class3':
            return 3
        else:
            return 0 


    5. Creating test and train record files from csv files:
    Execute the blow commands to create tfrecord files.

    Creating train tf record file from csv

    !python generate_tfrecord.py --csv_input=images/train_labels.csv --image_dir=images/train 
    --output_path=train.record

    Creating test tf record file from csv

    !python generate_tfrecord.py --csv_input=images/test_labels.csv --image_dir=images/test
     --output_path=test.record


    6. Creating Labelmap.pbtxt file :

    1. Create a file Labelmap.pbtxt where we will be keeping the name of the                              classes.
    2. Class structure of Labelmap.pbtxt file



    7. Training folder
    Create a training folder inside object_detection then Place the Labelmap.pbtxt file inside the newly created training folder.

    Step 6 : Downloading the Model for Training:


    • Downloading the pretrained model efficientdet_d1 from the model zoo and untar it.
    Use below code for downloading the model file:
    !wget http://download.tensorflow.org/models/object_detection/tf2/20200711/
    efficientdet_d1_coco17_tpu-32.tar.gz

    Code  to Untar the downloaded model:
    !tar -xf efficientdet_d1_coco17_tpu-32.tar.gz


    • Changes to do in pipeline.config 

    You will find your config file inside the downloaded model folder.

    Do the following changes according to your requirements:

    batch_size = 4 (Change it according to your dataset size

    num_classes: Put your total number of classes here (e.g - 5)

    fine_tune_checkpoint: "your_model_name/checkpoint/ckpt-0"

    num_steps: 50000 (Put the steps for training)

    fine_tune_checkpoint_type: "detection"

    # Change the path of labelmap.pbtxt ,train.record and test.record

     train_input_reader {
      label_map_path: "training/labelmap.pbtxt"
      tf_record_input_reader {
        input_path: "train.record"
      }
    }
    eval_config {
      metrics_set: "coco_detection_metrics"
      use_moving_averages: false
    }
    eval_input_reader {
      label_map_path: "training/labelmap.pbtxt"
      shuffle: false
      num_epochs: 1
      tf_record_input_reader {
        input_path: "test.record"
      }

    Step 7 : Start the Training:

    • Use below code to start the training process:
    !python model_main_tf2.py --model_dir=training --pipeline_config_path=training/
    pipeline.config



    Step 8 : Exporting the Inference Graph:

    • Use below code:
    !python exporter_main_v2.py --input_type image_tensor --pipeline_config_path training/
    pipeline.config --trained_checkpoint_dir training/ --output_directory exported-models/
    my_model


    At the end you can see something similar it means your trained model is successfully saved into the exported-models folder.


    You will see something similar like this inside the exported-models folder.



    ------------------------------------- Congratz  you have trained your model -----------------------------------

    You can download the Collab notebook from this link G-Drive


    Leave any Queries in the comment section below.

    ---------------------------------Don't forget to Subscribe for future Posts--------------------------------------

    Thank you :)
  • 0 comments:

    Post a Comment