Top Ad unit 728 × 90

Train customize object for object recognition by Tensorflow Part 1

As in the previous article (Install tensorflow and object detection sample), we learned how to use tensorflow in object recognition with built-in data. But in practice we do not need too much data object train (need more machine resources, slow program speed). In this article we will learn how to train optional objects.
          This tutorial we will train the object on Ubuntu OS (linux).
Step 1: Collect photos and label images (Annotate )
Collect a few hundred images containing your object - Minimum minimum is about 100, more ideal as 500+, but, the more you have the image, the tedious it is ...
Annotate / label the images, ideally with a program. In this tutorial we use LabelImg. This process basically draws the cells around your object (s) in an image. The auto-tagging program creates an XML file describing the objects in the image.

Install labeImage tool

sudo apt-get install pyqt5-dev-tools
sudo pip3 install lxml
git clone
cd labelImg
make qt5py3

When running this file, you will get a GUI window. From here, choose to open the folder and select the folder where you saved all your images. Now, you can start the annotation with the rectbox creation button. Draw your canvas, add a name, and hit ok. Save, hit the next photo, and repeat! You can press the w key to draw the frame and use ctrl + s to make it faster.

Once you have more than 100 images labeled, we'll split them into training and experiment groups. To do this, simply copy about 10% of your image and XML annotation files to a new folder called "test" and then copy the remaining files to a new directory called. is "train"

Once you have done all of this, you are ready to go to the next tutorial, here we will show you how to create the TFRecord files required from this data.

Step 2: TFRecord

We now need to convert these XML files into individual CSV files that can be converted to TFRecord files. To do this, we will use will use code (you can download Here)

In the xml_to_csv script, I changed:   

In xml_to_csv, we need some change to suitable with our dir structure:
def main():
    image_path = os.path.join(os.getcwd(), 'annotations')
    xml_df = xml_to_csv(image_path)
    xml_df.to_csv('raccoon_labels.csv', index=None)
    print('Successfully converted xml to csv.')
def main():
    for directory in ['train','test']:
        image_path = os.path.join(os.getcwd(), 'images/{}'.format(directory))
        xml_df = xml_to_csv(image_path)
        xml_df.to_csv('data/{}_labels.csv'.format(directory), index=None)
        print('Successfully converted xml to csv.')

This Scrpit is for separating train / test and naming files. For the program to run well, you put the folder as instructed: (folder placed on the Desktop)


Now go to The only modification you need to do here is in class class_text_to_int. You need to change this to your specific class. In our case, we have only one class. If you have multiple classes, you will need to continue to build the if statement.

# TO-DO replace this with label map
def class_text_to_int(row_label):
    if row_label == 'macncheese':
        return 1

And of course to do the train, we need to install Tensorflow.

First we need to clone tensorflow to the computer:

git clone

Then install the necessary library

pip3 install tensorflow 
sudo apt-get install protobuf-compiler python-pil python-lxml
sudo pip install jupyter
sudo pip install matplotlib

Then continue

# From tensorflow/models/research
protoc object_detection/protos/*.proto --python_out=.
If you encounter errors we need to use the latest version of protoc
Download the version for python extract and execute the command: [download]

sudo ./configure
sudo make check
sudo make install

Then try to execute the protoc command again (make sure you are running from the models directory).

# fromtensorflow/models/research
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim

Finally, we proceed to install the object_dection library:

sudo python3 install

We start to run script. We need to run two times, one to train TFRecord and the other to test TFRecord.

python3 --csv_input=data/train_labels.csv --output_path=data/train.record
python3 --csv_input=data/test_labels.csv --output_path=data/test.record

Now, in your data directory, you have got train.record and test.record.
Next, we need to set up a configuration file and then train a new model or start from a test point with a pre-trained model, which is what we'll cover in the tutorial. next post.


Train customize object for object recognition by Tensorflow Part 1 Reviewed by Jacky on December 18, 2017 Rating: 5


All Rights Reserved by JackyLe © 2018 © 2017
Edit bởi: Jacky Le | Youtube Channel: JACKY LE

Send email form


Email *

Message *

Powered by Blogger.