GithubHelp home page GithubHelp logo

soumyaiitkgp / custom_maskrcnn Goto Github PK

View Code? Open in Web Editor NEW
66.0 4.0 47.0 78.84 MB

Custom Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow

License: Other

Python 100.00%
maskrcnn computer-vision deep-learning implementation-of-research-paper

custom_maskrcnn's Introduction

MaskRCNN custom dataset module

This is a repository based on Matterport Maskrcnn repository for the application of custom MaskRCNN to custom datasets/application like weed detection, day to day object detection etc. Find the module in the folder 'samples/custom'. If you like this work please consider giving it a star. It helps me to track down the progress.

Here is the detailed step by step approach guide.

For citation:

@misc{Soumya_Maskrcnn_2020,
  title={Mask R-CNN for custom object detection and instance segmentation on Keras and TensorFlow},
  author={Soumya Yadav},
  year={2020},
  publisher={Github},
  journal={GitHub repository},
  howpublished={\url{https://github.com/soumyaiitkgp/Mask_RCNN/}},
}

If you like to know more about MaskRCNN, please consider visiting Matterport's MaskRCNN repository. Below are the details:

Mask R-CNN for Object Detection and Segmentation

This is an implementation of Mask R-CNN on Python 3, Keras, and TensorFlow. The model generates bounding boxes and segmentation masks for each instance of an object in the image. It's based on Feature Pyramid Network (FPN) and a ResNet101 backbone.

Instance Segmentation Sample

The repository includes:

  • Source code of Mask R-CNN built on FPN and ResNet101.
  • Training code for MS COCO
  • Pre-trained weights for MS COCO
  • Jupyter notebooks to visualize the detection pipeline at every step
  • ParallelModel class for multi-GPU training
  • Evaluation on MS COCO metrics (AP)
  • Example of training on your own dataset

The code is documented and designed to be easy to extend. If you use it in your research, please consider citing this repository (bibtex below). If you work on 3D vision, you might find our recently released Matterport3D dataset useful as well. This dataset was created from 3D-reconstructed spaces captured by our customers who agreed to make them publicly available for academic use. You can see more examples here.

Getting Started

  • demo.ipynb Is the easiest way to start. It shows an example of using a model pre-trained on MS COCO to segment objects in your own images. It includes code to run object detection and instance segmentation on arbitrary images.

  • train_shapes.ipynb shows how to train Mask R-CNN on your own dataset. This notebook introduces a toy dataset (Shapes) to demonstrate training on a new dataset.

  • (model.py, utils.py, config.py): These files contain the main Mask RCNN implementation.

  • inspect_data.ipynb. This notebook visualizes the different pre-processing steps to prepare the training data.

  • inspect_model.ipynb This notebook goes in depth into the steps performed to detect and segment objects. It provides visualizations of every step of the pipeline.

  • inspect_weights.ipynb This notebooks inspects the weights of a trained model and looks for anomalies and odd patterns.

Step by Step Detection

To help with debugging and understanding the model, there are 3 notebooks (inspect_data.ipynb, inspect_model.ipynb, inspect_weights.ipynb) that provide a lot of visualizations and allow running the model step by step to inspect the output at each point. Here are a few examples:

1. Anchor sorting and filtering

Visualizes every step of the first stage Region Proposal Network and displays positive and negative anchors along with anchor box refinement.

2. Bounding Box Refinement

This is an example of final detection boxes (dotted lines) and the refinement applied to them (solid lines) in the second stage.

3. Mask Generation

Examples of generated masks. These then get scaled and placed on the image in the right location.

4.Layer activations

Often it's useful to inspect the activations at different layers to look for signs of trouble (all zeros or random noise).

5. Weight Histograms

Another useful debugging tool is to inspect the weight histograms. These are included in the inspect_weights.ipynb notebook.

6. Logging to TensorBoard

TensorBoard is another great debugging and visualization tool. The model is configured to log losses and save weights at the end of every epoch.

6. Composing the different pieces into a final result

Training on MS COCO

We're providing pre-trained weights for MS COCO to make it easier to start. You can use those weights as a starting point to train your own variation on the network. Training and evaluation code is in samples/coco/coco.py. You can import this module in Jupyter notebook (see the provided notebooks for examples) or you can run it directly from the command line as such:

# Train a new model starting from pre-trained COCO weights
python3 samples/coco/coco.py train --dataset=/path/to/coco/ --model=coco

# Train a new model starting from ImageNet weights
python3 samples/coco/coco.py train --dataset=/path/to/coco/ --model=imagenet

# Continue training a model that you had trained earlier
python3 samples/coco/coco.py train --dataset=/path/to/coco/ --model=/path/to/weights.h5

# Continue training the last model you trained. This will find
# the last trained weights in the model directory.
python3 samples/coco/coco.py train --dataset=/path/to/coco/ --model=last

You can also run the COCO evaluation code with:

# Run COCO evaluation on the last trained model
python3 samples/coco/coco.py evaluate --dataset=/path/to/coco/ --model=last

The training schedule, learning rate, and other parameters should be set in samples/coco/coco.py.

Training on Your Own Dataset

Start by reading this blog post about the balloon color splash sample. It covers the process starting from annotating images to training to using the results in a sample application.

In summary, to train the model on your own dataset you'll need to extend two classes:

Config This class contains the default configuration. Subclass it and modify the attributes you need to change.

Dataset This class provides a consistent way to work with any dataset. It allows you to use new datasets for training without having to change the code of the model. It also supports loading multiple datasets at the same time, which is useful if the objects you want to detect are not all available in one dataset.

See examples in samples/shapes/train_shapes.ipynb, samples/coco/coco.py, samples/balloon/balloon.py, samples/nucleus/nucleus.py and samples/custom/custom.py.

Differences from the Official Paper

This implementation follows the Mask RCNN paper for the most part, but there are a few cases where we deviated in favor of code simplicity and generalization. These are some of the differences we're aware of. If you encounter other differences, please do let us know.

  • Image Resizing: To support training multiple images per batch we resize all images to the same size. For example, 1024x1024px on MS COCO. We preserve the aspect ratio, so if an image is not square we pad it with zeros. In the paper the resizing is done such that the smallest side is 800px and the largest is trimmed at 1000px.

  • Bounding Boxes: Some datasets provide bounding boxes and some provide masks only. To support training on multiple datasets we opted to ignore the bounding boxes that come with the dataset and generate them on the fly instead. We pick the smallest box that encapsulates all the pixels of the mask as the bounding box. This simplifies the implementation and also makes it easy to apply image augmentations that would otherwise be harder to apply to bounding boxes, such as image rotation.

    To validate this approach, we compared our computed bounding boxes to those provided by the COCO dataset. We found that ~2% of bounding boxes differed by 1px or more, ~0.05% differed by 5px or more, and only 0.01% differed by 10px or more.

  • Learning Rate: The paper uses a learning rate of 0.02, but we found that to be too high, and often causes the weights to explode, especially when using a small batch size. It might be related to differences between how Caffe and TensorFlow compute gradients (sum vs mean across batches and GPUs). Or, maybe the official model uses gradient clipping to avoid this issue. We do use gradient clipping, but don't set it too aggressively. We found that smaller learning rates converge faster anyway so we go with that.

Citation

Use this bibtex to cite this repository:

@misc{matterport_maskrcnn_2017,
  title={Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow},
  author={Waleed Abdulla},
  year={2017},
  publisher={Github},
  journal={GitHub repository},
  howpublished={\url{https://github.com/matterport/Mask_RCNN}},
}

Contributing

Contributions to this repository are welcome. Examples of things you can contribute:

  • Speed Improvements. Like re-writing some Python code in TensorFlow or Cython.
  • Training on other datasets.
  • Accuracy Improvements.
  • Visualizations and examples.

You can also join our team and help us build even more projects like this one.

Requirements

Python 3.4, TensorFlow 1.3, Keras 2.0.8 and other common packages listed in requirements.txt.

MS COCO Requirements:

To train or test on MS COCO, you'll also need:

If you use Docker, the code has been verified to work on this Docker container.

Installation

  1. Clone this repository

  2. Install dependencies

    pip3 install -r requirements.txt
  3. Run setup from the repository root directory

    python3 setup.py install
  4. Download pre-trained COCO weights (mask_rcnn_coco.h5) from the releases page.

  5. (Optional) To train or test on MS COCO install pycocotools from one of these repos. They are forks of the original pycocotools with fixes for Python3 and Windows (the official repo doesn't seem to be active anymore).

Projects Using this Model

If you extend this model to other datasets or build projects that use it, we'd love to hear from you.

4K Video Demo by Karol Majek.

Mask RCNN on 4K Video

Images to OSM: Improve OpenStreetMap by adding baseball, soccer, tennis, football, and basketball fields.

Identify sport fields in satellite images

Splash of Color. A blog post explaining how to train this model from scratch and use it to implement a color splash effect.

Balloon Color Splash

Code is in the samples/nucleus directory.

Nucleus Segmentation

Detection and Segmentation for Surgery Robots by the NUS Control & Mechatronics Lab.

Surgery Robot Detection and Segmentation

A proof of concept project by Esri, in collaboration with Nvidia and Miami-Dade County. Along with a great write up and code by Dmitry Kudinov, Daniel Hedges, and Omar Maher. 3D Building Reconstruction

A project from Japan to automatically track cells in a microfluidics platform. Paper is pending, but the source code is released.

Research project to understand the complex processes between degradations in the Arctic and climate change. By Weixing Zhang, Chandi Witharana, Anna Liljedahl, and Mikhail Kanevskiy. image

A computer vision class project by HU Shiyu to apply the color pop effect on people with beautiful results.

Mapping Challenge: Convert satellite imagery to maps for use by humanitarian organisations.

Mapping Challenge

GRASS GIS Addon to generate vector masks from geospatial imagery. Based on a Master's thesis by Ondřej Pešek.

GRASS GIS Image

custom_maskrcnn's People

Contributors

bill2239 avatar borda avatar cclauss avatar concerttttt avatar coreyhu avatar cpruce avatar jmtatsch avatar jningwei avatar keineahnung2345 avatar llltttppp avatar manasbedmutha98 avatar marluca avatar maxfrei750 avatar moorage avatar mowoe avatar np-csu avatar philferriere avatar ps48 avatar robaleman avatar rymalia avatar skt7 avatar soumyaiitkgp avatar stevenhickson avatar veemon avatar viredery avatar waleedka avatar xelmirage avatar yanfengliu avatar yqnlp avatar ziyigogogo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

custom_maskrcnn's Issues

How to calculate TP , TN, FP and FN ?

@soumyaiitkgp I am using my own dataset which consists of 1235 images in total. I have divided 864 images into a training set and 371 images in the test set. After the training process is done, I am trying to make predictions on each image in my test set. I know that there is a "compute_ap" function which returns the mAp value after sending "gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks']" as parameters. But I want to know the values of TP, TN, FP, and FN of the entire test set? Can you please help me in finding these values? Is there any predefined function available in the model which returns these values?

Detection scores is always >0.9 even for wrong detections

Hi Soumya,

This is not related to usage maskrcnn from your repo.

I am using matterport implementation of maskrcnn and train my custom data. While detection the scores for each instance in images is always greater than 0.9 even when the detection is incorrect. Say the instance is ball and is being detected as knife but the detection score is 0.97. Can you help me with the pausible reasons or where should i look into.

I have tried looking into the model.py and util and config.

Issue while trying to train model

So I have followed the Medium article and am trying to train the model on a custom dataset of floorplans. After working around some issues I found I am finally ready to train.

However, now I run into a new problem which I can not seem to fix myself. When trying to train on a custom dataset, I get the following error:

Traceback (most recent call last): File "custom.py", line 391, in <module> train(model) File "custom.py", line 222, in train layers='heads') File "C:\...\Custom_MaskRCNN-master\mrcnn\model.py", line 2356, in train self.compile(learning_rate, self.config.LEARNING_MOMENTUM) File "C:\...\Custom_MaskRCNN-master\mrcnn\model.py", line 2201, in compile self.keras_model.add_metric(loss, name) AttributeError: 'Model' object has no attribute 'add_metric'

This comes right after the model should start training, stating that it is starting at epoch 0.
We are kind of at a loss, as we have been struggling to use this model for a couple of hours now.

If anybody knows a solution that would be very much appreciated.

Building the model for the first time with custom dataset

Steps followed as per your link https://medium.com/analytics-vidhya/a-simple-guide-to-maskrcnn-custom-dataset-implementation-27f7eab381f2:

  • Step 1 - cloned your repo

  • Step 2: Prepare the data (90:10)

  • Step 3: Prepare the model.- for this I made the necessary changes to add my custom class (in my case just one) - Running under GPU compute instance in Azure ML workspace
    -- line 91 and 140
    -- and also in the CustomConfig class line 71 in my case NUM_CLASSES = 1 + 1

  • ran the script
    python custom.py train --dataset="Users/pankaja_us/Custom_MaskRCNN/samples/custom/dataset/train" --weights=coco

  • getting the following error -->
    2020-12-18 21:52:27.922509: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1241] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 10812 MB memory) -> physical GPU (device: 0, name: Tesla K80, pci bus id: 0001:00:00.0, compute capability: 3.7)
    Traceback (most recent call last):
    File "custom.py", line 375, in
    model_dir=args.logs)
    File "/mnt/resource/batch/tasks/shared/LS_root/mounts/clusters/pankaja-gpu1/code/users/pankaja_us/Custom_MaskRCNN/mrcnn/model.py", line 1837, in init
    self.keras_model = self.build(mode=mode, config=config)
    File "/mnt/resource/batch/tasks/shared/LS_root/mounts/clusters/pankaja-gpu1/code/users/pankaja_us/Custom_MaskRCNN/mrcnn/model.py", line 1990, in build
    target_rois, input_gt_class_ids, gt_boxes, input_gt_masks])
    File "/anaconda/envs/azureml_py36/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 75, in symbolic_fn_wrapper
    return func(*args, **kwargs)
    File "/anaconda/envs/azureml_py36/lib/python3.6/site-packages/keras/engine/base_layer.py", line 489, in call
    output = self.call(inputs, **kwargs)
    File "/mnt/resource/batch/tasks/shared/LS_root/mounts/clusters/pankaja-gpu1/code/users/pankaja_us/Custom_MaskRCNN/mrcnn/model.py", line 664, in call
    self.config.IMAGES_PER_GPU, names=names)
    File "/mnt/resource/batch/tasks/shared/LS_root/mounts/clusters/pankaja-gpu1/code/users/pankaja_us/Custom_MaskRCNN/mrcnn/utils.py", line 820, in batch_slice
    output_slice = graph_fn(*inputs_slice)
    File "/mnt/resource/batch/tasks/shared/LS_root/mounts/clusters/pankaja-gpu1/code/users/pankaja_us/Custom_MaskRCNN/mrcnn/model.py", line 663, in
    w, x, y, z, self.config),
    File "/mnt/resource/batch/tasks/shared/LS_root/mounts/clusters/pankaja-gpu1/code/users/pankaja_us/Custom_MaskRCNN/mrcnn/model.py", line 553, in detection_targets_graph
    positive_indices = tf.random_shuffle(positive_indices)[:positive_count]
    AttributeError: module 'tensorflow' has no attribute 'random_shuffle'
    azureuser@pankaja-gpu1:/mnt/resource/batch/tasks/shared/LS_root/mounts/clusters/pankaja-gpu1/code/users/pankaja_us/Custom_MaskRCNN/samples/custom$

Please advise. What other changes do I need to make?
TIA
Pankaja

custom demo.ipynd needs an update

The current demo.ipynb only supports coco model/weights. Need to update the notebook to use custom config, and to use custom model weights.

TL;DR:

These needs an update:

# Import COCO config
sys.path.append(os.path.join(ROOT_DIR, "samples/coco/"))  # To find local version
import coco

replace with

# Import Custom config
sys.path.append(os.path.join(ROOT_DIR, "samples/custom/"))  # To find local version
import custom
# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
    utils.download_trained_weights(COCO_MODEL_PATH)

replace with

# Local path to trained weights file
CUSTOM_MODEL_PATH = os.path.join(MODEL_DIR, "mask_rcnn_custom.h5")
class InferenceConfig(coco.CocoConfig):
    # Set batch size to 1 since we'll be running inference on
    # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1

replace with

class InferenceConfig(custom.CustomConfig):
    # Set batch size to 1 since we'll be running inference on
    # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1
# Load weights trained on MS-COCO
model.load_weights(COCO_MODEL_PATH, by_name=True)

replace with

# Load weights trained on custom
model.load_weights(CUSTOM_MODEL_PATH, by_name=True)

Only display one class

I can only display one class, I think there may be a problem with custom.py.
num_ids=[]
#Add the classes according to the requirement
for n in custom:
try:
if n['label']=='object1':
num_ids.append(1)
elif n['label']=='object2':
num_ids.append(2)
except:
pass
In custom.py ,num_ids is not being used.I read another article.https://github.com/matterport/Mask_RCNN/issues/372#issuecomment-377010489
Maybe this code should be changed.
self.add_image( "custom", image_id=a['filename'], # use file name as a unique image id path=image_path, width=width, height=height, polygons=polygons)
# Return mask, and array of class IDs of each instance. Since we have # one class ID only, we return an array of 1s return mask.astype(np.bool), np.ones([mask.shape[-1]], dtype=np.int32)

Applying the maskRCNN on custom dataset

Premise:

  • The project that I am dealing is weed detection in the environment of carrot saplings.
  • The dataset for our project consists of image files and json file for the category_ids, image file ids etc.,
  • Results -- predicting the maximum weeds so they can be rid of
    -- visualize by masking the saplings

Followed Soumi's Read the guide and trying to apply the class_id, issue is that the annotations that I have extracted from my project's dataset has no class_id. Sample of annotations.json is as follows
"annotations": [ { "area": 13026.0, "bbox": [ 1773.0023578923756, 1194.7026978561953, 222.80523106412875, 194.66877198054863 ], "category_id": 1, "id": 6155, "image_id": 362, "iscrowd": 0, "segmentation": [ [ 1981.767578125,

So at the most I can add only category_id

-Next in your custom.py, you have a file called (Not sure where that file is, although you are referring it as containing annotations... ?) not sure how to reconcile that...
custom_rncnn_err2

I am not sure how to apply your code for weed detection, even though you just mention about being able to apply, in your readme.
Please respond.

No module named 'coco'

Nice project.I tried to run 'demo.ipynb', but it gave me an error!
sys.path.append(os.path.join(ROOT_DIR, "samples\\custom\\")) # To find local version
import coco


Using TensorFlow backend.


ModuleNotFoundError Traceback (most recent call last)
in ()
19 #sys.path.append(C:\ProgramData\Anaconda3\Lib\site-packages\pycocotools)
20 sys.path.append(os.path.join(ROOT_DIR, "samples\custom\")) # To find local version
---> 21 import coco
22 #from samples.coco import coco
23 get_ipython().run_line_magic('matplotlib', 'inline')

ModuleNotFoundError: No module named 'coco'


I tried using my local version and it didn't work.
sys.path.append(C:\ProgramData\Anaconda3\Lib\site-packages\pycocotools)

python :3.6.5
tensorflow :1.15.0
pycocotools:2.0.2
keras:2.0.8

Issue while loading weights in the inspect_custom_model.ipynb

When I have to load the newly generated custom model, I get the str doesn't have a decode attribute error

`

Or, load the last model you trained

weights_path = model.find_last()

Load weights

print("Loading weights ", weights_path)
model.load_weights(weights_path, by_name=True)
`
err_1
err_2

Please advice.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.