Mask RCNN is used in Object Detection to predict instances (masks) of objects present in an image. Using TensorFlow Object Detection API and its pre-trained models, we can easily have our own object detection tool ready and setup in less time than you could ever expect.
Create a root directory with a suitable name (say, ObjectDetection_MaskRCNN).
- cd to the root directory:
cd ./ObjectDetection_MaskRCNN - Download TensorFlow code base and models
git clone https://github.com/tensorflow/models.git - After finishing download, cd to the models/research directory
cd ./models/research/ - Run these commands to set up environment and add your
pwdto Python Path. Note: you have to run these commands whenver you start a terminal and want to use TF models.
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
protoc object_detection/protos/*.proto --python_out=.
For our custom task, we try to detect Red Stamps in attested documents. We download ~70 publicly accesible attested documents from Google Images (multiple sources) and use it for the training and validation purposes.
- Here is a snapshot of the data.
- Divide the images to
JPEGImagesfor training andtestImagesfor testing - The
dataset/folder should be in the root directoryObjectDetection_MaskRCNN/and should have the following directory setup.- Annotations
- masks
- xmls
- < color masks files >
- JPEGImages
- < training image files >
- testImages
- < test image files >
- label.pbtxt
- train.record
- Annotations
- Use PixelAnnotationTool to create object masks. The instruction to mask an image can be seen here.
- You will get 3 outputs for every image while using the PixelAnnotationTool
- IMAGE_mask.png
- IMAGE_color_mask.png
- IMAGE_watershed_mask.png
-
Rename all IMAGE_watershed_mask.png to IMAGE_mask.png and place inside
.dataset/Annotations/masksfolder. -
Rename all IMAGE_color_mask.png to IMAGE.png and place inside
./dataset/Annotationsfolder -
Convert your data to TF Record format (to generate the
train.recordfile)- Place the
'create_mask_rcnn_tf_record.pyfrom the directoryImp_Filestomodels/research/object_detection/dataset_tools/directory. - To edit class name as per you requirement, edit this python script at line 57.
- From the directory
./models/research/as present working directory, run the following command
py object_detection/dataset_tools/create_mask_rcnn_tf_record.py \
--data_dir=/home/vijay/ObjectDetection_MaskRCNN/dataset \
--annotations_dir=Annotations \
--image_dir=JPEGImages \
--output_dir=/home/vijay/ObjectDetection_MaskRCNN/dataset/train.record \
--label_map_path=/home/vijay/ObjectDetection_MaskRCNN/dataset/label.pbtxt
- Place the
-
You will see a
train.record-xxxfile inside thedatasetdirectory. Rename it totrain.record
- Create 3 folders inside the root directory
IG,CPandpre_trained_models. The current project structure would look like.- ObjectDetection_MaskRCNN/
- CP/
- IG/
- dataset/
- pre_trained_models/
- ObjectDetection_MaskRCNN/
- Download any model starting with the name
mask_rcnnfrom the TensorFlow Detection Model Zoo and extract the compressed file inside thepre_trained_modelsfolder. For this project, we downloadmask_rcnn_inception_v2_coco - Copy the file
mask_rcnn_inception_v2_coco.configfile from./models/research/object_detection/samples/configsto the root directoryObjectDetection_MaskRCNN/and edit PATH_TO_BE_CONFIGURED at 5 locations inside this config file. Refer to the config file in this repo for making changes. You can further explore other configurations and adjust as per your needs. - Finally, run this command from
./models/researchas present working directory to start training.
py object_detection/legacy/train.py \
--train_dir=/home/vijay/ObjectDetection_MaskRCNN/CP \
--pipeline_config_path=/home/vijay/ObjectDetection_MaskRCNN/mask_rcnn_inception_v2_coco.config - Keep training for a few hundred (or thousand steps) and/or till the loss comes near to
0.1 or 0.2or something.
- Check the step on which a
.ckptfile has been saved. Let's say, the step is 230. Run the following command from./models/researchas present working directory to save frozen inference graph.
py object_detection/export_inference_graph.py \
--input_type=image_tensor \
--pipeline_config_path=/home/vijay/ObjectDetection_MaskRCNN/mask_rcnn_inception_v2_coco.config \
--trained_checkpoint_prefix=/home/vijay/ObjectDetection_MaskRCNN/CP/model.ckpt-230 \
--output_directory=/home/vijay/ObjectDetection_MaskRCNN/IG - Run the notebook
mask_rcnn_eval.ipynbto test on the images you have kept aside in thetestImagesfolder. Explore the notebook and make the changes as you understand. - Please note to make changes in the
visualization_utils.pyfile inside./models/research/object_detection/utilsfolder to change the way you want to visualize the bounding boxes and object masks.


