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.
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.
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.
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.
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.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
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
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.
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
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.
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:
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.
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