Custom Object Detection using TensorFlow — (From Scratch)
By: Harshil Patel
Tags: Model, Python, Machine Learning, TensorFlow, Object Detection

Image by Author (Harshil, 2020)

In this tutorial, we’re going to create and train our own face mask detector using a pre-trained SSD MobileNet V2 model.

First we will create our own image dataset and later we will see how to train a Custom Model for Object Detection (Local and Google Colab!)

1. Installation

1.1 Tensorflow

Install Tensorflow using the following command:

$ pip install tensorflow or pip install tensorflow==1.15

If you have a GPU that you can use with Tensorflow:

$ pip install tensorflow-gpu

1.2 Other dependencies

$ pip install pillow Cython lxml jupyter matplotlib

Install protobuf using Homebrew (you can learn more about Homebrew here)

$ brew install protobuf

For protobuf installation on other OS, follow the instructions here.

1.3 Clone the Tensorflow models repository

In this tutorial, we’re going to use resources in the Tensorflow models repository. Since it does not come with the Tensorflow installation, we need to clone it from their Github repo:

First change into the Tensorflow directory:

# For example: ~/anaconda/envs/<your_env_name>/lib/python3.6/site-packages/tensorflow$ cd <path_to_your_tensorflow_installation>

Clone the Tensorflow models repository:

$ git clone https://github.com/tensorflow/models.git

From this point on, this directory will be referred to as the models directory

1.4 Setting up the environment

Every time you start a new terminal window to work with the pre-trained models, it is important to compile Protobuf and change your PYTHONPATH .

Run the following from your terminal:

$ cd <path_to_your_tensorflow_installation>/models/research/$ protoc object_detection/protos/*.proto --python_out=.$ export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim

Run a quick test to confirm that the Object Detection API is working properly:

$ python object_detection/builders/model_builder_test.py

2. Images

For Dataset images I am using face masks Images, You can use your own dataset images or download it from google. For that enable fatkun batch download extension in google. Download bulk images and remove unwanted images from downloaded folder.

Image by Author (Harshil, 2020)

Now go to tensorflow object_Detection directory and delete the data folder. Create a new empty data folder, ‘training’ folder, ‘images’ folder.

Now in images folder create 2 new empty folder ‘test, ‘train’. It will look something like this. Copy the images you downloaded and paste into models>research>object_detection>images

object_detection

├── data

|

├── training

|

├── images

| ├── test

| ├── train

| ├── ...

2.1 Image Labeling

Now to label images clone — https://github.com/tzutalin/labelImg repo, go to the anaconda cmd prompt and cd to labelImg path

C:\Users\path\labelImg> python labelImg.py

if you are getting any error in running the cmd, pip install lxml. Once the labelimg window is opened, select open dir and save dir to images folder in object_detection.

Screenshot by Author (Harshil, 2020)

Now use W to crop image and label your image, and save it with the same name of img into images folder. Same way crop and label all images. If you have more than 1 label make sure you can name it differently. Make sure you get img and xml file with same name(no space).

Now that you have label all the images, copy 10% of images and xml files into test and rest into train.

object_detection

├── images

├── test

| ├── cmimage1.jpg

| ├── cmimage1.xml

├── cmimage2.jpg

| ├── cmimage2.xml

├── train

| ├── ...

xml to csv python by author (Harshil, 2020)

now make xml_to_csv.py file and paste above code into it. Make file path in models>research>object_detection. Now make a new file generatetfrecord and paste below code into it.

Tfreorcrd python by Author(Harshil, 2020)

Now replace label and num on obj for your model. Now open conda cmd and cd to models/research/object_detection and run-

path/object_detection> python xml_to_csv.py

Now 2 files will be created into your data folder. Open both the files and check the labels and number of images into it.

2.2 Generate_tfrecord

Now that you have generated csv files, now cd/ to dir.

Commands by Author(Harshil, 2020)

If you got any error check the file names and dir for the images. Once you successfully run both cmds you will get 2 .record file into data folder. That's it for part one, if you have generated .record file you are doing great. Make sure you use proper version, file name, and dir. path for the project.

I have added some imp files in this repo — https://github.com/its-harshil/tensorflow-custom-objectdetection