All Projects → TannerGilbert → Tensorflow-Object-Detection-API-train-custom-Mask-R-CNN-model

TannerGilbert / Tensorflow-Object-Detection-API-train-custom-Mask-R-CNN-model

Licence: other
Train a Mask R-CNN model with the Tensorflow Object Detection API

Programming Languages

Jupyter Notebook
11667 projects
python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Tensorflow-Object-Detection-API-train-custom-Mask-R-CNN-model

Mask rcnn
Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow
Stars: ✭ 21,044 (+35567.8%)
Mutual labels:  instance-segmentation, mask-rcnn
Custom-Mask-RCNN-Using-Tensorfow-Object-Detection-API
A sample project to build a custom Mask RCNN model using Tensorflow object detection API
Stars: ✭ 70 (+18.64%)
Mutual labels:  instance-segmentation, mask-rcnn
mask-rcnn-edge-agreement-loss
Reference implementation of "Faster Training of Mask R-CNN by Focusing on Instance Boundaries"
Stars: ✭ 40 (-32.2%)
Mutual labels:  instance-segmentation, mask-rcnn
Tensorflow-Lite-Object-Detection-with-the-Tensorflow-Object-Detection-API
Run object detection on edge devices using Tensorflow Lite
Stars: ✭ 23 (-61.02%)
Mutual labels:  tensorflow-object-detection-api, tensorflow-object-detection
instance-segmentation
No description or website provided.
Stars: ✭ 40 (-32.2%)
Mutual labels:  instance-segmentation, mask-rcnn
Paddledetection
Object Detection toolkit based on PaddlePaddle. It supports object detection, instance segmentation, multiple object tracking and real-time multi-person keypoint detection.
Stars: ✭ 5,799 (+9728.81%)
Mutual labels:  instance-segmentation, mask-rcnn
rt-mrcnn
Real time instance segmentation with Mask R-CNN, live from webcam feed.
Stars: ✭ 47 (-20.34%)
Mutual labels:  instance-segmentation, mask-rcnn
Mmdetection
OpenMMLab Detection Toolbox and Benchmark
Stars: ✭ 17,646 (+29808.47%)
Mutual labels:  instance-segmentation, mask-rcnn
image-segmentation
Mask R-CNN, FPN, LinkNet, PSPNet and UNet with multiple backbone architectures support readily available
Stars: ✭ 62 (+5.08%)
Mutual labels:  instance-segmentation, mask-rcnn
celldetection
Cell Detection with PyTorch.
Stars: ✭ 44 (-25.42%)
Mutual labels:  instance-segmentation, mask-rcnn
uoais
Codes of paper "Unseen Object Amodal Instance Segmentation via Hierarchical Occlusion Modeling", ICRA 2022
Stars: ✭ 77 (+30.51%)
Mutual labels:  instance-segmentation
mrcnn serving ready
🛠 Mask R-CNN Keras to Tensorflow and TFX models + Serving models using TFX GRPC & RESTAPI
Stars: ✭ 96 (+62.71%)
Mutual labels:  mask-rcnn
max-deeplab
Unofficial implementation of MaX-DeepLab for Instance Segmentation
Stars: ✭ 84 (+42.37%)
Mutual labels:  instance-segmentation
COCO-dataset-explorer
Streamlit tool to explore coco datasets
Stars: ✭ 66 (+11.86%)
Mutual labels:  instance-segmentation
Mask-YOLO
Inspired from Mask R-CNN to build a multi-task learning, two-branch architecture: one branch based on YOLOv2 for object detection, the other branch for instance segmentation. Simply tested on Rice and Shapes. MobileNet supported.
Stars: ✭ 100 (+69.49%)
Mutual labels:  mask-rcnn
Swin-Transformer
This is an official implementation for "Swin Transformer: Hierarchical Vision Transformer using Shifted Windows".
Stars: ✭ 8,046 (+13537.29%)
Mutual labels:  mask-rcnn
pytorch-polygon-rnn
Pytorch implementation of Polygon-RNN(http://www.cs.toronto.edu/polyrnn/poly_cvpr17/)
Stars: ✭ 102 (+72.88%)
Mutual labels:  instance-segmentation
prostate segmentation
NCI-ISBI 2013 Challenge - Automated Segmentation of Prostate Structures
Stars: ✭ 28 (-52.54%)
Mutual labels:  instance-segmentation
SOLQ
"SOLQ: Segmenting Objects by Learning Queries", SOLQ is an end-to-end instance segmentation framework with Transformer.
Stars: ✭ 159 (+169.49%)
Mutual labels:  instance-segmentation
3DSemanticMapping JINT 2020
Repository for the paper "Extending Maps with Semantic and Contextual Object Information for Robot Navigation: a Learning-Based Framework using Visual and Depth Cues"
Stars: ✭ 38 (-35.59%)
Mutual labels:  instance-segmentation

Train a Mask R-CNN model with the Tensorflow Object Detection API

TensorFlow 2.5

Mask R-CNN prediction

1. Installation

You can install the TensorFlow Object Detection API either with Python Package Installer (pip) or Docker, an open-source platform for deploying and managing containerized applications. For running the Tensorflow Object Detection API locally, Docker is recommended. If you aren't familiar with Docker though, it might be easier to install it using pip.

First clone the master branch of the Tensorflow Models repository:

git clone https://github.com/tensorflow/models.git

Docker Installation

# From the root of the git repository (inside the models directory)
docker build -f research/object_detection/dockerfiles/tf2/Dockerfile -t od .
docker run -it od

Python Package Installation

cd models/research
# Compile protos.
protoc object_detection/protos/*.proto --python_out=.
# Install TensorFlow Object Detection API.
cp object_detection/packages/tf2/setup.py .
python -m pip install .

Note: The *.proto designating all files does not work protobuf version 3.5 and higher. If you are using version 3.5, you have to go through each file individually. To make this easier, I created a python script that loops through a directory and converts all proto files one at a time.

import os
import sys
args = sys.argv
directory = args[1]
protoc_path = args[2]
for file in os.listdir(directory):
    if file.endswith(".proto"):
        os.system(protoc_path+" "+directory+"/"+file+" --python_out=.")
python use_protobuf.py <path to directory> <path to protoc file>

To test the installation run:

# Test the installation.
python object_detection/builders/model_builder_tf2_test.py

If everything installed correctly you should see something like:

...
[       OK ] ModelBuilderTF2Test.test_create_ssd_models_from_config
[ RUN      ] ModelBuilderTF2Test.test_invalid_faster_rcnn_batchnorm_update
[       OK ] ModelBuilderTF2Test.test_invalid_faster_rcnn_batchnorm_update
[ RUN      ] ModelBuilderTF2Test.test_invalid_first_stage_nms_iou_threshold
[       OK ] ModelBuilderTF2Test.test_invalid_first_stage_nms_iou_threshold
[ RUN      ] ModelBuilderTF2Test.test_invalid_model_config_proto
[       OK ] ModelBuilderTF2Test.test_invalid_model_config_proto
[ RUN      ] ModelBuilderTF2Test.test_invalid_second_stage_batch_size
[       OK ] ModelBuilderTF2Test.test_invalid_second_stage_batch_size
[ RUN      ] ModelBuilderTF2Test.test_session
[  SKIPPED ] ModelBuilderTF2Test.test_session
[ RUN      ] ModelBuilderTF2Test.test_unknown_faster_rcnn_feature_extractor
[       OK ] ModelBuilderTF2Test.test_unknown_faster_rcnn_feature_extractor
[ RUN      ] ModelBuilderTF2Test.test_unknown_meta_architecture
[       OK ] ModelBuilderTF2Test.test_unknown_meta_architecture
[ RUN      ] ModelBuilderTF2Test.test_unknown_ssd_feature_extractor
[       OK ] ModelBuilderTF2Test.test_unknown_ssd_feature_extractor
----------------------------------------------------------------------
Ran 20 tests in 91.767s

OK (skipped=1)

2. Gathering data

Now that the Tensorflow Object Detection API is ready to go, we need to gather the images needed for training.

To train a robust model, the pictures should be as diverse as possible. So they should have different backgrounds, varying lighting conditions, and unrelated random objects in them.

You can either take pictures yourself, or you can download pictures from the internet. For my microcontroller detector, I took about 25 pictures of each individual microcontroller and 25 pictures containing multiple microcontrollers.

You can use the resize_images script to resize the image to the wanted resolutions.

python resize_images.py -d images/ -s 800 600

After you have all the images, move about 80% to the object_detection/images/train directory and the other 20% to the object_detection/images/test directory. Make sure that the images in both directories have a good variety of classes.

3. Labeling data

After you have gathered enough images, it's time to label them, so your model knows what to learn. In order to label the data, you will need to use some kind of labeling software.

For object detection, we used LabelImg, an excellent image annotation tool supporting both PascalVOC and Yolo format. For Image Segmentation / Instance Segmentation there are multiple great annotations tools available. Including, VGG Image Annotation Tool, labelme, and PixelAnnotationTool. I chose labelme, because of its simplicity to both install and use.

4. Generating Training data

With the images labeled, we need to create TFRecords that can be served as input data for the training of the model. Before we create the TFRecord files, we'll convert the labelme labels into COCO format. This can be done with the labelme2coco.py script.

python labelme2coco.py train train.json
python labelme2coco.py test test.json

Now we can create the TFRecord files using the create_coco_tf_record.py script.

python create_coco_tf_record.py --logtostderr --train_image_dir=images/train --test_image_dir=images/test --train_annotations_file=images/train.json --test_annotations_file=images/test.json --output_dir=./

After executing this command, you should have a train.record and test.record file inside your object detection folder.

5. Getting ready for training

The last thing we need to do before training is to create a label map and a training configuration file.

5.1 Creating a label map

The label map maps an id to a name. We will put it in a folder called training, which is located in the object_detection directory. The labelmap for my detector can be seen below.

item {
    id: 1
    name: 'Arduino'
}
item {
    id: 2
    name: 'ESP8266'
}
item {
    id: 3
    name: 'Heltec'
}
item {
    id: 4
    name: 'Raspberry'
}

The id number of each item should match the ids inside the train.json and test.json files.

"categories": [
    {
        "supercategory": "Arduino",
        "id": 0,
        "name": "Arduino"
    },
    {
        "supercategory": "ESP8266",
        "id": 1,
        "name": "ESP8266"
    },
    {
        "supercategory": "Heltec",
        "id": 2,
        "name": "Heltec"
    },
    {
        "supercategory": "Raspberry",
        "id": 3,
        "name": "Raspberry"
    }
],

5.2 Creating the training configuration

Lastly, we need to create a training configuration file. At the moment only one Mask-RCNN model is supported with Tensorflow 2.

From the Tensorflow Model Zoo:

Model name Speed (ms) COCO mAP Outputs
Mask R-CNN Inception ResNet V2 1024x1024 301 39.0/34.6 Boxes/Masks

The base config for the model can be found inside the configs/tf2 folder.

Copy the config file to the training directory. Then open it inside a text editor and make the following changes:

  • Line 12: change the number of classes to number of objects you want to detect (4 in my case)

  • Line 125: change fine_tune_checkpoint to the path of the model.ckpt file:

    • fine_tune_checkpoint: "<path>/mask_rcnn_inception_resnet_v2_1024x1024_coco17_gpu-8/checkpoint/ckpt-0"
  • Line 126: Change fine_tune_checkpoint_type to detection

  • Line 136: change input_path to the path of the train.record file:

    • input_path: "<path>/train.record"
  • Line 156: change input_path to the path of the test.record file:

    • input_path: "<path>/test.record"
  • Line 134 and 152: change label_map_path to the path of the label map:

    • label_map_path: "<path>/labelmap.pbtxt"
  • Line 107 and 147: change batch_size to a number appropriate for your hardware, like 4, 8, or 16.

6. Training the model

To train the model execute the following command in the command line:

python model_main_tf2.py --pipeline_config_path=training/mask_rcnn_inception_resnet_v2_1024x1024_coco17_gpu-8.config --model_dir=training --alsologtostderr

If everything was setup correctly, the training should begin shortly, and you should see something like the following:

training the model

Every few minutes, the current loss gets logged to Tensorboard. Open Tensorboard by opening a second command line, navigating to the object_detection folder and typing:

tensorboard --logdir=training/train

This will open a webpage at localhost:6006.

monitor training

The training script saves checkpoints about every five minutes. Train the model until it reaches a satisfying loss, then you can terminate the training process by pressing Ctrl+C.

7. Exporting the inference graph

Now that we have a trained model, we need to generate an inference graph that can be used to run the model.

python /content/models/research/object_detection/exporter_main_v2.py \
    --trained_checkpoint_dir training \
    --output_directory inference_graph \
    --pipeline_config_path training/mask_rcnn_inception_resnet_v2_1024x1024_coco17_gpu-8.config

Author

Gilbert Tanner

Support me

Buy Me A Coffee

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].