- Install
tensorflow-gpu
by running
conda install -c anaconda tensorflow-gpu
-
Install NVIDIA dependencies for using GPU from
https//gist.github.com/msis/108a74d08f55eed48d8521fa968851ea
-
You need the following libraries
sudo apt-get install protobuf-compiler python-pil python-lxml python-tk
sudo pip install jupyter
sudo pip install matplotlib
- Make sure your annotations are in PASCAL VOC format.
- Get the Tensorflow Object Detection API
git clone https://github.com/tensorflow/models.git
- The Tensorflow Object Detection API uses Protobufs to configure model and training parameters; compile Protobuf libraries
cd models/research
protoc object_detection/protos/*.proto --python_out=.
- Add Libraries to PYTHONPATH
# From models/research/
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
- Test Installation
python object_detection/builders/model_builder_test.py
'''
tf_object_detection
|
├── images
│ └── train
│ | └── IMG1.jpg
│ | └── IMG1.xml
│ | └── IMG2.jpg
│ | └── IMG2.xml
│ └── test
│ | └── IMG_A.jpg
│ | └── IMG_A.xml
│ | └── IMG_B.jpg
│ | └── IMG_B.xml
|
├── data
│ └── train_labels.csv
│ └── train.record
│ └── test_labels.csv
│ └── test.record
│ └── tf_object_detection.pbtxt
|
├── training
│ └── ssd_mobilenet_v1_coco.config
|
├── ssd_mobilenet_v1_coco_2017_11_17
│ └── checkpoint
│ └── frozen_inference_graph.pb
│ └── ...
|
├── generate_tfrecord.py
├── xml_to_csv.py
'''
- Create necessary folders
mkdir tf_object_detection
cd tf_object_detection/
mkdir images data
- Split the data into train and test sets
# from tf_object_detection/images/
python train_test_splitter.py --annotations=Annotations/ --images=JPEGImages/ --testsize=0.1 --outputdir=.
- Convert annotations from xml to csv
cd tf_object_detection
# get the following script for conversion
wget https://raw.githubusercontent.com/datitran/raccoon_dataset/master/xml_to_csv.py
Make the following changes to xml_to_csv.py
...
def main():
for directory in ['train', 'test']:
image_path = '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.')
...
and run..
python xml_to_csv.py
- Convert dataset to TFRecords
cd tf_object_detection
# get the following script for conversion
wget https://raw.githubusercontent.com/datitran/raccoon_dataset/master/generate_tfrecord.py
Make the following changes to generate_tfrecord.py
# NOTE: If you have multiple categories..
..
def class_text_to_int(row_label):
if row_label == 'boat':
return 1
elif row_label == 'buoy':
return 2
elif row_label == 'other':
return 3
else:
None
...
def main(_):
writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
examples = pd.read_csv(FLAGS.csv_input)
grouped = split(examples, 'filename')
for group in grouped:
try:
try:
path = os.getcwd() + '/images/train'
tf_example = create_tf_example(group, path)
except:
path = os.getcwd() + '/images/test'
tf_example = create_tf_example(group, path)
except:
print('Check the path to {}'.format(group.filename))
writer.write(tf_example.SerializeToString())
writer.close()
output_path = os.path.join(os.getcwd(), FLAGS.output_path)
print('Successfully created the TFRecords: {}'.format(output_path))
and run..
python generate_tfrecord.py --csv_input=data/train_labels.csv --output_path=data/train.record
python generate_tfrecord.py --csv_input=data/test_labels.csv --output_path=data/test.record
-
Download the model (.tar.gz) from the table here and extract it to
tf_object_detection/
-
Download the model config file (.config) from here
- change
num_classes
to the number of objects in your dataset - edit the following chunk:
...
fine_tune_checkpoint: "ssd_mobilenet_v1_coco_2017_11_17/model.ckpt" # NOTE: enter your model name
...
train_input_reader: {
tf_record_input_reader {
input_path: "data/train.record" # NOTE: path to train.record
}
label_map_path: "data/tf_object_detection.pbtxt" # NOTE: path to label map
}
...
eval_input_reader: {
tf_record_input_reader {
input_path: "data/test.record" # NOTE: path to test.record
}
label_map_path: "data/tf_object_detection.pbtxt" # NOTE: path to label map
shuffle: false
num_readers: 1
}
- Create labelmap
# from tf_object_detection/data/
nano tf_object_detection.pbtxt
# add the following to tf_object_detection.pbtxt
item {
id: 1
name: 'boat'
}
item {
id: 2
name: 'land'
}
item {
id: 3
name: 'miscellaneous'
}
- Training preparation
# from tf_object_detection
cp -r data/ models/research/object_detection/.
cp -r images/ models/research/object_detection/.
cp -r ssd_mobilenet_v1_coco_2017_11_17/ models/research/object_detection/.
cp ssd_mobilenet_v1_coco.config models/research/object_detection/.
- Start training!
# from models/research/object_detection/
mkdir training/
python train.py --logtostderr --train_dir=training/ --pipeline_config_path=ssd_mobilenet_v1_coco.config --num_clones=2 --ps_tasks=1
-
Stop Training
Stop training once there is no significant decrease in error over the past hour or so. Or addEarlyStopping
-
Export inference graph
# For trained_checkpoint_prefix, get the latest checkpoint file that has all three files (meta, index, ckpt)
# Change the output_directory name as you wish
python export_inference_graph.py --input_type image_tensor --pipeline_config_path training/ssd_mobilenet_v1_coco.config --trained_checkpoint_prefix training/model.ckpt-9858 --output_directory boat_detection_graph
- Test model
# get script for evaluation
wget https://gist.github.com/harshilpatel312/521ebe35f0346a5b87aa49a7db7efecf
python object_detection_tutorial.py -i PATH_TO_INPUT_IMAGE_VIDEO -n NUMBER_OF_CLASSES -m MODEL_NAME -l NAME_OF_LABELMAP -o OUTPUT_VIDEO_NAME
# example
# python object_detection_tutorial.py -i GOPR0293.MP4 -n 7 -m detection_graph_rfcn_resnet101 -l tf_object_detection.pbtxt -o out.MP4
- For later use
Move theevents.out*
(files used by tensorboard),model.ckpt.*
(checkpoint) files, andcheckpoint
todetection_graph_$XYZ
to save it for later use.
mv training/model.ckpt* detection_graph_$XYZ/.
mv training/events.out* detection_graph_$XYZ/.
mv training/checkpoint detection_graph_$XYZ/.
-
Extras
- check GPU usage:
watch -n 0.5 nvidia-smi
- check TensorBoard
cd models/research/object_detection/ tensorboard --logdir=training/ # go to the link shown
- check GPU usage:
-
You can continue training by modifying step 13 by changing the
train_dir
todetection_graph_$XYZ
python train.py --logtostderr --train_dir=detection_graph_rfcn_resnet101/ --pipeline_config_path=training/rfcn_resnet101_coco.config
To check TensorBoard:
cd models/research/object_detection/
tensorboard --logdir=detection_graph_$XYZ/
# go to the link shown
- If you have got new data, generate the .csv and .record files, repeat steps 2, 4, 5 (edit
def class_text_to_int
), 6, 10 (changenum_classes
), 11 and 12 from above. - If you want to try new model, repeat steps 7, 8, 9, 11 (change
fine_tune_checkpoint
), 12 (copy just the model and .config file intraining
tomodels/research/object_detection/
), clean upmodels/research/object_detection/training/
so that it only contains.config
files, and 13 (start training with your new config file). - Make sure you have different name for output_directory in step 16 for every new dataset/model
MemoryError
orResourceExhaustedError
: change the batch size to something smallerImportError: No module named 'deployment'
orNo module named 'nets'
; Fix:cd models/research/ export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
ValueError: not enough values to unpack
: change thebatch_size
in the .config file to match thenum_clones
ValueError: Tried to convert 't' to a tensor and failed.
:
- In
models/research/object_detection/utils/learning_schedules.py
, changeto..rate_index = tf.reduce_max(tf.where(tf.greater_equal(global_step, boundaries), range(num_boundaries), [0] * num_boundaries))
rate_index = tf.reduce_max(tf.where(tf.greater_equal(global_step, boundaries), list(range(num_boundaries)), [0] * num_boundaries))
- Verify data (xml, csv, tfrecords) thoroughly for malformed dataset