This is a tutorial to help you setup Object Detection using TensorFlow Object Detection API
.
Author: @jerryc05
For latest setup process, please refer to the following sites:
Table of Contents:
For latest setup process, please refer to the following site:
Please refer to the official tutorials:
Run the following command in shell from folder ./
:
python -m pip install --user -r requirements.txt
For latest setup process, please refer to the official site cocodataset/cocoapi.
-
Download
cocodataset/cocoapi/PythonAPI
andcocodataset/cocoapi/common
folders. -
Extract
PyhonAPI
andcommon
folder so that the directory looks like:Hello-Object-Detection |-- cocoapi | |-- common | |-- maskApi.c | |-- ... | |-- PythonAPI | |-- setup.py | |-- ... |-- main.py |-- ...
-
IMPORTANT! Modify the file
./cocoapi/PythonAPI/setup.py
as follow:- Delete the line that contains
extra_compile_args
.
- Delete the line that contains
-
Build and install
pycocotools
:Run the following command in shell from folder
./cocoapi/PythonAPI
:python setup.py install
-
Successful output will output
Finished processing dependencies for pycocotools==...
as the last line of output.
Run the following command in shell from folder ./
:
python -m pip install pycocotools
- Download the corresponding binary zipped release from protocolbuffers/protobuf.
- Unzip the folder.
- Add
protoc
executable to Environment Variable PATH then restart terminal. (Or you will need to replace everyprotoc
call withPATH_TO_PROTOC/protoc
later.)
-
Download
tensorflow/models/research/object_detection
andtensorflow/models/research/slim
folders. -
Extract
object_detection
andslim
folders so that the directory looks like:Hello-Object-Detection |-- models | |-- research | |-- object_detection | |-- model_main.py | |-- ... | |-- slim | |-- setup.py | |-- ... |-- main.py |-- ...
-
Run the following command in shell from folder
./models/research
:protoc object_detection/protos/*.proto --python_out=.
-
Successful execution will output nothing.
The following path shall be appended to Environment Variable PYTHONPATH
:
__PWD__/models/research
__PWD__/models/research/slim
Note: change __PWD__
to the absolute path of Hello-Object-Detection
folder before touching PYTHONPATH
!
-
Run the following command in shell from folder
./models/research
:python object_detection/builders/model_builder_test.py
-
Successful execution will output
OK
orOK (skipped=...)
as the last line of output.
For latest setup process, please refer to site: tzutalin/labelImg
Run the following command in shell from folder ./
:
python -m pip install labelImg
No Bullshit here, please refer to official site tzutalin/labelImg.
-
Parse labels from
xml
tocsv
:Run the following command in shell from folder
./
:python utils/xml_to_csv.py -i ${PATH_TO_XML_FOLDER} -o ${PATH_TO_CSV_FOLDER}
Note: change the following paths before running the script:
${PATH_TO_XML_FOLDER}
<- path of xml labels created bylabelImg
.${PATH_TO_CSV_FOLDER}
<- path where csv file will be saved.
-
Parse
csv
toTFRecord
:Run the following command in shell from folder
./
:python utils/csv_to_tfrecord.py -c ${PATH_TO_CSV_FILE} -i ${PATH_TO_IMG_FOLDER} -o ${PATH_TO_TFRECORD_FILE_FOLDER}
Note: change the following paths before running the script:
${PATH_TO_CSV_FILE}
<- path of csv file created byxml_to_csv.py
.${PATH_TO_IMG_FOLDER}
<- path of image files.${PATH_TO_TFRECORD_FILE_FOLDER}
<- path of TFRecord file that the script will create.
Note: you might need to run it twice for both
train.csv
andeval.csv
respectively.
-
Select a pre-configure model config from
./models/research/object_detection/samples/configs
. -
Copy the
.config
file to somewhere else.For example, I use
./config/ssd_resnet50_v1_fpn_shared_box_predictor_640x640_coco14_sync.config
.So the directory looks like:
Hello-Object-Detection |-- training | |-- ssd_resnet50_v1_fpn_shared_box_predictor_640x640_coco14_sync.config |-- main.py |-- ...
-
Open the config file.
-
Replace the value of
num_classes
with your number of classes.If you forgot the number, check your
.pbtxt
file that contains label map. -
Replace the value of
fine_tune_checkpoint
with the path of model checkpoint file to save.For example, I use
"./config/ssd_resnet50_v1_fpn_shared_box_predictor_640x640_coco14_sync.ckpt"
.IMPORTANT! If the pre-trained model does not exist yet, comment out this line by adding a
#
at the begining. -
Replace the value of
num_steps
with the number of steps to train.For example, I use
"./config/ssd_resnet50_v1_fpn_shared_box_predictor_640x640_coco14_sync.ckpt"
.Note: If the model should be trained indefinitely, comment out this line by adding a
#
at the beginning. -
Replace the value of
input_path
under sectiontrain_input_reader
with the path of training TFRecord file.Note: wildcard char
?
can be used to match multiple file names. -
Replace the value of
label_map_path
under sectiontrain_input_reader
with the path of training label map.pbtxt
file. -
OPTIONAL: Replace the value of
num_examples
with the number of images to eval. -
Replace the value of
input_path
under sectioneval_input_reader
with the path of eval TFRecord file.Note: wildcard char
?
can be used to match multiple file names. -
Replace the value of
label_map_path
under sectioneval_input_reader
with the path of eval label map.pbtxt
file.
Run the following command in shell from folder ./
:
python models/research/object_detection/model_main.py --pipeline_config_path=${PATH_TO_CONFIG_FILE} --model_dir=${PATH_TO_CHECKPOINT_FOLDER} --alsologtostderr
Note: change the following paths before running the script:
${PATH_TO_CONFIG_FILE}
<- path of pre-configured model config file.${PATH_TO_CHECKPOINT_FOLDER}
<- path where training checkpoints and events will be saved.
Run the following command in shell from folder ./
:
python models/research/object_detection/export_inference_graph.py --pipeline_config_path ${PIPELINE_CONFIG_PATH} --output_directory ${OUTPUT_PATH} --trained_checkpoint_prefix ${CKPT_PATH_PREFIX_ONLY}
Note: change the following paths before running the script:
${PIPELINE_CONFIG_PATH}
<- path of pre-configured model config file.${OUTPUT_PATH}
<- path where the frozen model will be saved.${CKPT_PATH_PREFIX_ONLY}
<- path of the checkpoint file, discarding postfixes such as.meta
,.index
, and.data-...
.