logo

How to train own object detection classifier using TensorFlow on Windows 10

avatarMUSTAFA COŞAR

clock27 Nisan 2020

view3029 Görüntülenme

4 Beğeni

Paylaş

In this tutorial,i am going to show you how to train own tensorflow object detection classifier on Windows 10.

How to train own object detection classifier using TensorFlow on Windows 10

Source Link:
https://github.com/maacofficial/tensorflow-train-object-detection-classiffier-tutorial#7-edit-label-map-and-configure-training-config

Turkish translation of this tutorial is located in the lang folder. If you would like to add new translation for this guide.You can request.

You can train your own object detection classifier on Windows 10,8 and 7. This repository written with TensorFlow 1.5.0 version. If you try with newer version of TensorFlow and different tensorflow models version, you can receive too many errors.

Also i published a YouTube video for this tutorial.




Video Link:
https://www.youtube.com/watch?v=ZuG-k__yFrc


Statement

You can use TensorFlow (GPU) instead of TensorFlow (CPU) changing "pip install --ignore-installed --upgrade tensorflow==1.5.0" to "pip install --ignore-installed --upgrade tensorflow-gpu==1.5.0" and you need to install CUDA and cuDNN.

If you use GPU for training,you should have Nvidia GPU for trainig object detection classifier with TensorFlow. Also your graphic card should meet minimum requirements. TensorFlow required minimum 3.5 Compute Capability for training own object detection classifier. Compute Capability List For Each Graphic Card

If your graphic card is medium level, it take about 3 hours. And here is a table of compatible CUDA versions with TensorFlow.

Steps

1.Install Anaconda
First of all we should download Anaconda that is a free and open-source distribution of the Python for package management.

You can follow steps from video.

2.Create TensorFlow directory
If you don't want to train another version of TensorFlow, you can download this repository directly from here. And continue from third step.

2.1. Create folder in C:/ that named "tensorflow1". This directory will include our models,object_detection and training folder. We will work in "tensorflow1" directory.

2.2. Download TensorFlow models repository from above links. We use TensorFlow 1.5 version in this guide.

TensorFlow version
GitHub Models Repository
TF v1.4
https://github.com/tensorflow/models/tree/1f34fcafc1454e0d31ab4a6cc022102a54ac0f5b
TF v1.5
https://github.com/tensorflow/models/tree/d90d5280fea4a5303affc1e28af505d8292d84b8
TF v1.6
https://github.com/tensorflow/models/tree/4c05414826e87f3b8ef0534862748e4b7fcd1ec7
TF v1.7
https://github.com/tensorflow/models/tree/adfd5a3aca41638aa9fb297c5095f33d64446d8f
TF v1.8
https://github.com/tensorflow/models/tree/abd504235f3c2eed891571d62f0a424e54a2dabc
TF v1.9
https://github.com/tensorflow/models/tree/d530ac540b0103caa194b4824af353f1b073553b
TF v1.10
https://github.com/tensorflow/models/tree/b07b494e3514553633b132178b4c448f994d59df
TF v1.11
https://github.com/tensorflow/models/tree/23b5b4227dfa1b23d7c21f0dfaf0951b16671f43
TF v1.12
https://github.com/tensorflow/models/tree/r1.12.0
TF v1.13
https://github.com/tensorflow/models/tree/r1.13.0
Latest version
https://github.com/tensorflow/models

2.3. Extract tensorflow model repository to your "tensorflow1" directory that we have created in "C:/" and rename extracted folder to "models".

2.4. Download "Faster-RCNN-Inception-V2-COCO" model from TensorFlow's model zoo from this link.

2.5. Extract downloaded "faster_rcnn_inception_v2_coco_2018_01_28.tar.gz" file to "C:\tensorflow1\models\research\object_detection\" folder.

2.6. Download "Edje Electronics repository" model from this link.

2.7. Extract downloaded "TensorFlow-Object-Detection-API.zip" file to "C:\tensorflow1\models\research\object_detection\" folder.

2.8. Finally your "object_detection" folder looks like this.

3. Create and Set up Anaconda Virtual Environment
3.1 Open your command prompt as administrator.

3.2 Than run this command: C:\>conda create -n tensorflow1 pip python=3.5

C:\>conda activate tensorflow1

3.3 Install TensorFlow 1.5 version: C:\>pip install --ignore-installed --upgrade tensorflow==1.5.0

If you want to install TensorFlow GPU 1.5 version: C:\>pip install --ignore-installed --upgrade tensorflow-gpu==1.5.0

3.4 Install TensorFlow 1.5 version: C:\>pip install --ignore-installed --upgrade tensorflow==1.5.0

3.5 Install necessary packages by running following commands;
C:\>conda install -c anaconda protobuf
C:\>pip install pillow lxml cython jupyter matplotlib pandas opencv-python

3.6 Configure environment variable:
C:\>set PYTHONPATH=C:\tensorflow1\models;C:\tensorflow1\models\research;C:\tensorflow1\models\research\slim


4. Compile protobufs and run setup.py

In command prompt go to research folder by running:C:\>cd C:\tensorflow1\models\research
Then run the following commands:

protoc --python_out=. .\object_detection\protos\anchor_generator.proto .\object_detection\protos\argmax_matcher.proto .\object_detection\protos\bipartite_matcher.proto .\object_detection\protos\box_coder.proto .\object_detection\protos\box_predictor.proto .\object_detection\protos\eval.proto .\object_detection\protos\faster_rcnn.proto .\object_detection\protos\faster_rcnn_box_coder.proto .\object_detection\protos\grid_anchor_generator.proto .\object_detection\protos\hyperparams.proto .\object_detection\protos\image_resizer.proto .\object_detection\protos\input_reader.proto .\object_detection\protos\losses.proto .\object_detection\protos\matcher.proto .\object_detection\protos\mean_stddev_box_coder.proto .\object_detection\protos\model.proto .\object_detection\protos\optimizer.proto .\object_detection\protos\pipeline.proto .\object_detection\protos\post_processing.proto .\object_detection\protos\preprocessor.proto .\object_detection\protos\region_similarity_calculator.proto .\object_detection\protos\square_box_coder.proto .\object_detection\protos\ssd.proto .\object_detection\protos\ssd_anchor_generator.proto .\object_detection\protos\string_int_label_map.proto .\object_detection\protos\train.proto .\object_detection\protos\keypoint_box_coder.proto

(tensorflow1) C:\tensorflow1\models\research>python setup.py build
(tensorflow1) C:\tensorflow1\models\research>python setup.py install

5. Take and label images

You can use your phone to take images. But they should be less then 200KB and their resolution should be less then 720x1280. After you take images you should copy %20 of them to "C:\tensorflow1\models\research\object_detection\images\test\" folder and %80 of them to "C:\tensorflow1\models\research\object_detection\images\train\" folder. Finally care to variety of pictures in folders.

In this step we should download "LabelImg" tool to label images from here.

6. Generate training record

First we should convert images xml data to csv. To do this:

(tensorflow1) C:\tensorflow1\models\research\object_detection>python xml_to_csv.py
After that, edit generate_tfrecord.py with a text editor. Change label map with your own label map.

# TO-DO replace this with label map
def class_text_to_int(row_label):
if row_label == '200TL':
return 1
elif row_label == '5TL':
return 2
else:
None

TO:

# TO-DO replace this with label map
def class_text_to_int(row_label):
if row_label == 'cat':
return 1
elif row_label == 'dog':
return 2
elif row_label == 'bird':
return 3
elif row_label == 'person':
return 4
else:
None

Then,generate data files by running following commands in "C:\tensorflow1\models\research\object_detection" folder:

python generate_tfrecord.py --csv_input=images\train_labels.csv --image_dir=images\train --output_path=train.record
python generate_tfrecord.py --csv_input=images\test_labels.csv --image_dir=images\test --output_path=test.record


7. Edit label map and configure training config

Edit "C:\tensorflow1\models\research\object_detection\training\labelmap.pbtxt" with a text editor.

item {
id: 1
name: '200TL'
}

item {
id: 2
name: '5TL'
}

To:

item {
id: 1
name: 'cat'
}

item {
id: 2
name: 'dog'
}
item {
id: 3
name: 'bird'
}
item {
id: 4
name: 'person'
}

After that edit "C:\tensorflow1\models\research\object_detection\training\faster_rcnn_inception_v2_pets.config" file with a text editor to configure training.

a. Line 9 - Change num_classes to the number of objects you want to detect.For example; cat,dog,bird and person "num_classes:4"

b. Line 110 - Change fine_tune_checkpoint:


fine_tune_checkpoint: "C:/tensorflow1/models/research/object_detection/faster_rcnn_inception_v2_coco_2018_01_28/model.ckpt"
 

c. Line 126-128 - Change some variables in the train_input_reader section:


train_input_reader: {
tf_record_input_reader {
input_path: "C:/tensorflow1/models/research/object_detection/train.record"
}
label_map_path: "C:/tensorflow1/models/research/object_detection/training/labelmap.pbtxt"
}

 

d. Line 140-142 - Change some variables in the eval_input_reader section:


tf_record_input_reader {
input_path: "C:/tensorflow1/models/research/object_detection/test.record"
}
label_map_path: "C:/tensorflow1/models/research/object_detection/training/labelmap.pbtxt"
shuffle: false
num_readers: 1
}

e. Line 132 - Change num_examples to the number of images you have in the "C:\tensorflow1\models\research\object_detection\images\test" folder: num_examples: 89

f. Save your file and exit.

8. Run the train.py

If you use a newer version of TensorFlow ,train.py file is located in C:\tensorflow1\models\research\object_detection\legacy" folder. You can copy it to object_detection folder.

Let's run train.py! Run the following command from "C:\tensorflow1\models\research\object_detection" folder:

python train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/faster_rcnn_inception_v2_pets.config

When training begins, it will look like this:




9. Export inference graph

When training is completed,we generate inference graph file(.pb).Change "XXXX" with your highest number in your "C:\tensorflow1\models\research\object_detection\training" folder. Please run the following command to generate file from "C:\tensorflow1\models\research\object_detection" folder:

python export_inference_graph.py --input_type image_tensor --pipeline_config_path training/faster_rcnn_inception_v2_pets.config --trained_checkpoint_prefix training/model.ckpt-XXXX --output_directory inference_graph

Generated file is located in "C:\tensorflow1\models\research\object_detection\inference_graph" folder.

10. Using own object detection classifier

If you want to detect with webcam run "Object_detection_webcam.py" that is located in "C:\tensorflow1\models\research\object_detection" folder.Before running "Object_detection_webcam.py" file ,edit "NUM_CLASSES = 2" section. Finally run following command:

python Object_detection_webcam.py

Dream until your dreams come true!!!

Etiketler

kendi modelimizi eğitme

tensorflow nesne algılama

tensorflow

object detection

nesne algılama

traing own object model

tensorflow classifier

tensorflow object detection

makine öğrenimi

machine learning