### Install Mask R-CNN Project Locally Source: https://github.com/matterport/mask_rcnn/blob/master/README.md Installs the Mask R-CNN project locally after cloning the repository and installing dependencies. This command should be run from the repository root directory. ```python python3 setup.py install ``` -------------------------------- ### Mask R-CNN Model Training and Setup Source: https://context7.com/matterport/mask_rcnn/llms.txt This snippet demonstrates the initialization, weight loading, and training configuration for a Mask R-CNN model. It handles multi-GPU training setup and dataset preparation for both training and validation. ```python import os from mrcnn import utils from mrcnn import config from mrcnn import model as modellib from mrcnn.parallel import ParallelModel # Define a custom dataset class (assuming it's defined elsewhere) class CustomDataset(utils.Dataset): def load_dataset(self, dataset_dir, subset): # Replace with actual dataset loading logic # Example: load image files and their annotations pass def prepare(self): # Replace with actual preparation logic pass # Define a configuration class (assuming it's defined elsewhere) class MyConfig(config.Config): NAME = "my_config" GPU_COUNT = 1 # Set to the number of GPUs available IMAGES_PER_GPU = 2 NUM_CLASSES = 1 + 3 # Background + 3 object classes LEARNING_RATE = 0.001 config = MyConfig() config.display() # Display configuration values # Create model model = modellib.MaskRCNN(mode="training", config=config, model_dir="./logs") # Load pre-trained weights (e.g., from COCO dataset) # Ensure mask_rcnn_coco.h5 is in the correct path weights_path = "mask_rcnn_coco.h5" if os.path.exists(weights_path): model.load_weights(weights_path, by_name=True, exclude=["mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"]) else: print(f"Warning: Pre-trained weights not found at {weights_path}. Training from scratch.") # Create parallel model for multi-GPU training if config.GPU_COUNT > 1: parallel_model = ParallelModel(model.keras_model, config.GPU_COUNT) else: parallel_model = model.keras_model # Load datasets dataset_train = CustomDataset() dataset_train.load_dataset('path/to/dataset', 'train') dataset_train.prepare() dataset_val = CustomDataset() dataset_val.load_dataset('path/to/dataset', 'val') dataset_val.prepare() # Train with multi-GPU support print("Starting training...") model.train(dataset_train, dataset_val, learning_rate=config.LEARNING_RATE, epochs=40, layers='heads') # Train only the heads initially print("Training finished.") ``` -------------------------------- ### Setup Environment and Import Mask RCNN Source: https://github.com/matterport/mask_rcnn/blob/master/samples/shapes/train_shapes.ipynb Initializes the project environment by setting the root directory, appending it to the system path, and importing necessary Mask R-CNN libraries. It also handles downloading pre-trained COCO weights if they are not found locally. ```python import os import sys import random import math import re import time import numpy as np import cv2 import matplotlib import matplotlib.pyplot as plt # Root directory of the project ROOT_DIR = os.path.abspath("../../") # Import Mask RCNN sys.path.append(ROOT_DIR) # To find local version of the library from mrcnn.config import Config from mrcnn import utils import mrcnn.model as modellib from mrcnn import visualize from mrcnn.model import log %matplotlib inline # Directory to save logs and trained model MODEL_DIR = os.path.join(ROOT_DIR, "logs") # 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) ``` -------------------------------- ### Install Python Dependencies for Mask R-CNN Source: https://github.com/matterport/mask_rcnn/blob/master/README.md Installs the required Python packages for the Mask R-CNN project using pip. Ensure you have Python 3 and pip installed. ```bash pip3 install -r requirements.txt ``` -------------------------------- ### Setup Mask R-CNN Environment and Load Weights Source: https://github.com/matterport/mask_rcnn/blob/master/samples/coco/inspect_model.ipynb Initializes the Mask R-CNN project environment by setting the root directory and importing necessary libraries from the Mask R-CNN framework. It also handles the download of pre-trained COCO weights if they are not found locally. ```python import os import sys import random import math import re import time import numpy as np import tensorflow as tf import matplotlib import matplotlib.pyplot as plt import matplotlib.patches as patches # Root directory of the project ROOT_DIR = os.path.abspath("../../") # Import Mask RCNN sys.path.append(ROOT_DIR) # To find local version of the library from mrcnn import utils from mrcnn import visualize from mrcnn.visualize import display_images import mrcnn.model as modellib from mrcnn.model import log %matplotlib inline # Directory to save logs and trained model MODEL_DIR = os.path.join(ROOT_DIR, "logs") # 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) # Path to Shapes trained weights SHAPES_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_shapes.h5") ``` -------------------------------- ### Import Mask R-CNN Libraries and Setup Environment (Python) Source: https://github.com/matterport/mask_rcnn/blob/master/samples/demo.ipynb Imports necessary libraries for Mask R-CNN, sets up the root directory, and defines paths for logs, models, and images. It also includes logic to download pre-trained COCO weights if they are not found locally. ```python import os import sys import random import math import numpy as np import skimage.io import matplotlib import matplotlib.pyplot as plt # Root directory of the project ROOT_DIR = os.path.abspath("../") # Import Mask RCNN sys.path.append(ROOT_DIR) # To find local version of the library from mrcnn import utils import mrcnn.model as modellib from mrcnn import visualize # Import COCO config sys.path.append(os.path.join(ROOT_DIR, "samples/coco/")) # To find local version import coco %matplotlib inline # Directory to save logs and trained model MODEL_DIR = os.path.join(ROOT_DIR, "logs") # 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) # Directory of images to run detection on IMAGE_DIR = os.path.join(ROOT_DIR, "images") ``` -------------------------------- ### Import Libraries and Setup Project Root - Python Source: https://github.com/matterport/mask_rcnn/blob/master/samples/nucleus/inspect_nucleus_data.ipynb Imports necessary Python libraries and sets the ROOT_DIR to the project's root directory. It adjusts the path if the script is run from a specific subdirectory. ```python import os import sys import itertools import math import logging import json import re import random import time import concurrent.futures import numpy as np import matplotlib import matplotlib.pyplot as plt import matplotlib.patches as patches import matplotlib.lines as lines from matplotlib.patches import Polygon import imgaug from imgaug import augmenters as iaa # Root directory of the project ROOT_DIR = os.getcwd() if ROOT_DIR.endswith("samples/nucleus"): # Go up two levels to the repo root ROOT_DIR = os.path.dirname(os.path.dirname(ROOT_DIR)) # Import Mask RCNN sys.path.append(ROOT_DIR) from mrcnn import utils from mrcnn import visualize from mrcnn.visualize import display_images from mrcnn import model as modellib from mrcnn.model import log import nucleus %matplotlib inline ``` -------------------------------- ### Visualize Mask R-CNN Detection Pipeline with demo.ipynb Source: https://github.com/matterport/mask_rcnn/blob/master/README.md Jupyter notebook for demonstrating the Mask R-CNN model's capabilities. It shows an example of using a pre-trained MS COCO model to segment objects in custom images, covering object detection and instance segmentation. ```python # samples/demo.ipynb ``` -------------------------------- ### Train Mask R-CNN on MS COCO Dataset Source: https://context7.com/matterport/mask_rcnn/llms.txt Demonstrates how to train a Mask R-CNN model on the MS COCO dataset. This includes options for starting from pre-trained weights (COCO or ImageNet), continuing a previous training session, and running evaluation. It utilizes command-line arguments for configuration. ```bash # 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 python3 samples/coco/coco.py train --dataset=/path/to/coco/ --model=last # Run COCO evaluation on the last trained model python3 samples/coco/coco.py evaluate --dataset=/path/to/coco/ --model=last ``` -------------------------------- ### Custom Configuration Setup for Mask R-CNN Source: https://context7.com/matterport/mask_rcnn/llms.txt Defines a custom configuration by subclassing the base Config class and overriding properties. This allows for specific settings related to training, detection, and image parameters. It is useful for adapting the Mask R-CNN model to different datasets and use cases. ```python from mrcnn.config import Config class MyConfig(Config): # Give the configuration a recognizable name NAME = "my_dataset" # Train on 1 GPU with 2 images per GPU (batch size = 2) GPU_COUNT = 1 IMAGES_PER_GPU = 2 # Number of classes (including background) NUM_CLASSES = 1 + 3 # Background + 3 object classes # Training steps per epoch STEPS_PER_EPOCH = 100 # Skip detections with confidence < 0.9 DETECTION_MIN_CONFIDENCE = 0.9 # Image input size IMAGE_MIN_DIM = 800 IMAGE_MAX_DIM = 1024 # Learning rate LEARNING_RATE = 0.001 # Initialize and display configuration config = MyConfig() config.display() ``` -------------------------------- ### Train Mask R-CNN Balloon Model Source: https://github.com/matterport/mask_rcnn/blob/master/samples/balloon/README.md Trains a Mask R-CNN model for balloon detection. Supports starting training from pre-trained COCO weights, ImageNet weights, or resuming from the last checkpoint. Requires a dataset path. Training parameters like steps and batch size can be adjusted. ```bash python3 balloon.py train --dataset=/path/to/balloon/dataset --weights=coco ``` ```bash python3 balloon.py train --dataset=/path/to/balloon/dataset --weights=last ``` ```bash python3 balloon.py train --dataset=/path/to/balloon/dataset --weights=imagenet ``` -------------------------------- ### Load Mask R-CNN Configurations (Python) Source: https://github.com/matterport/mask_rcnn/blob/master/samples/coco/inspect_data.ipynb Loads and applies configurations for the Mask R-CNN model. It provides examples for both the 'shapes' toy dataset and the MS COCO dataset, allowing users to select and initialize the appropriate configuration. Users need to specify the COCO dataset directory if using it. ```python # Run one of the code blocks # Shapes toy dataset # import shapes # config = shapes.ShapesConfig() # MS COCO Dataset import coco config = coco.CocoConfig() COCO_DIR = "path to COCO dataset" # TODO: enter value here ``` -------------------------------- ### Run Mask R-CNN Training on MS COCO Dataset Source: https://github.com/matterport/mask_rcnn/blob/master/README.md Command-line interface for training a Mask R-CNN model on the MS COCO dataset. Supports starting from pre-trained COCO weights, ImageNet weights, or continuing previous training. Requires specifying the dataset path and model weights. ```python python3 samples/coco/coco.py train --dataset=/path/to/coco/ --model=coco python3 samples/coco/coco.py train --dataset=/path/to/coco/ --model=imagenet python3 samples/coco/coco.py train --dataset=/path/to/coco/ --model=/path/to/weights.h5 python3 samples/coco/coco.py train --dataset=/path/to/coco/ --model=last ``` -------------------------------- ### MS COCO Dataset Training with Python Source: https://context7.com/matterport/mask_rcnn/llms.txt Provides a Python script to configure and train a Mask R-CNN model on the MS COCO dataset. It sets up a custom configuration, loads the dataset, initializes the model, loads pre-trained weights, and starts the training process. Dependencies include pycocotools and the mrcnn library. ```python import os import sys from pycocotools.coco import COCO from pycocotools.cocoeval import COCOeval # Root directory ROOT_DIR = os.path.abspath("./") sys.path.append(ROOT_DIR) from mrcnn.config import Config from mrcnn import model as modellib, utils from samples.coco import coco # COCO Configuration class CocoConfig(Config): NAME = "coco" GPU_COUNT = 1 IMAGES_PER_GPU = 2 NUM_CLASSES = 1 + 80 # COCO has 80 classes config = CocoConfig() # Load COCO dataset dataset_train = coco.CocoDataset() dataset_train.load_coco("/path/to/coco", "train", year="2017") dataset_train.prepare() dataset_val = coco.CocoDataset() dataset_val.load_coco("/path/to/coco", "val", year="2017") dataset_val.prepare() print(f"Training images: {len(dataset_train.image_ids)}") print(f"Validation images: {len(dataset_val.image_ids)}") print(f"Classes: {dataset_train.class_names}") # Initialize model model = modellib.MaskRCNN(mode="training", config=config, model_dir="./logs") # Load COCO weights COCO_MODEL_PATH = "mask_rcnn_coco.h5" if not os.path.exists(COCO_MODEL_PATH): utils.download_trained_weights(COCO_MODEL_PATH) model.load_weights(COCO_MODEL_PATH, by_name=True, exclude=["mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"]) # Train model.train(dataset_train, dataset_val, learning_rate=config.LEARNING_RATE, epochs=40, layers='heads') ``` -------------------------------- ### Apply Color Splash Effect with Mask R-CNN Source: https://github.com/matterport/mask_rcnn/blob/master/samples/balloon/README.md Applies a color splash effect to an image or video using pre-trained Mask R-CNN weights. Requires specifying the weights file and the input image/video path. For videos, OpenCV 3.2+ is a dependency. ```bash python3 balloon.py splash --weights=/path/to/mask_rcnn/mask_rcnn_balloon.h5 --image= ``` ```bash python3 balloon.py splash --weights=/path/to/mask_rcnn/mask_rcnn_balloon.h5 --video= ``` -------------------------------- ### Create Matplotlib Axes for Visualization (Python) Source: https://github.com/matterport/mask_rcnn/blob/master/samples/nucleus/inspect_nucleus_model.ipynb A helper function to create Matplotlib Axes objects for plotting. It allows control over the number of rows and columns, and the overall size of the plot, centralizing visualization setup. ```python def get_ax(rows=1, cols=1, size=16): """Return a Matplotlib Axes array to be used in all visualizations in the notebook. Provide a central point to control graph sizes. Adjust the size attribute to control how big to render images """ fig, ax = plt.subplots(rows, cols, figsize=(size*cols, size*rows)) fig.tight_layout() return ax ``` -------------------------------- ### Load and Detect Objects in an Image using Mask R-CNN Source: https://github.com/matterport/mask_rcnn/blob/master/samples/demo.ipynb This Python code snippet demonstrates how to load a random image from a specified directory, run object detection using a pre-trained Mask R-CNN model, and then visualize the detected instances. It requires libraries such as `os`, `skimage.io`, `random`, and the Mask R-CNN library for model inference and visualization. ```python # Load a random image from the images folder file_names = next(os.walk(IMAGE_DIR))[2] image = skimage.io.imread(os.path.join(IMAGE_DIR, random.choice(file_names))) # Run detection results = model.detect([image], verbose=1) # Visualize results r = results[0] visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores']) ``` -------------------------------- ### Run Mask Head for Detection and Mask Prediction in Python Source: https://github.com/matterport/mask_rcnn/blob/master/samples/coco/inspect_model.ipynb Executes the Mask R-CNN model's graph to get detections and segmentation masks. It retrieves outputs from the 'mrcnn_detection' and 'mrcnn_mask' layers and processes the detection class IDs. ```python # Get predictions of mask head mrcnn = model.run_graph([image], [ ("detections", model.keras_model.get_layer("mrcnn_detection").output), ("masks", model.keras_model.get_layer("mrcnn_mask").output), ]) # Get detection class IDs. Trim zero padding. det_class_ids = mrcnn['detections'][0, :, 4].astype(np.int32) det_count = np.where(det_class_ids == 0)[0][0] det_class_ids = det_class_ids[:det_count] print("{} detections: {}".format( det_count, np.array(dataset.class_names)[det_class_ids])) ``` -------------------------------- ### Configure and Train Mask R-CNN Model Source: https://context7.com/matterport/mask_rcnn/llms.txt Sets up a Mask R-CNN model for training, loads pre-trained weights, prepares custom datasets, and performs staged training (heads only, then all layers). Assumes CustomDataset class is defined and data is available at 'path/to/dataset'. ```python class TrainingConfig(Config): NAME = "training" GPU_COUNT = 1 IMAGES_PER_GPU = 2 NUM_CLASSES = 1 + 3 STEPS_PER_EPOCH = 100 LEARNING_RATE = 0.001 config = TrainingConfig() # Create model in training mode model = modellib.MaskRCNN(mode="training", config=config, model_dir="./logs") # Load pre-trained weights (COCO, ImageNet, or previous checkpoint) # Option 1: Start from COCO weights model.load_weights("mask_rcnn_coco.h5", by_name=True, exclude=["mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"]) # Option 2: Resume from last checkpoint # model.load_weights(model.find_last(), by_name=True) # Load datasets (assuming CustomDataset defined above) dataset_train = CustomDataset() dataset_train.load_dataset('path/to/dataset', 'train') dataset_train.prepare() dataset_val = CustomDataset() dataset_val.load_dataset('path/to/dataset', 'val') dataset_val.prepare() # Train the model # Stage 1: Train heads only print("Training network heads") model.train(dataset_train, dataset_val, learning_rate=config.LEARNING_RATE, epochs=40, layers='heads') # Stage 2: Fine-tune all layers print("Fine tuning all layers") model.train(dataset_train, dataset_val, learning_rate=config.LEARNING_RATE / 10, epochs=80, layers='all') # Save final weights model_path = os.path.join(model.model_dir, "mask_rcnn_final.h5") model.keras_model.save_weights(model_path) ``` -------------------------------- ### Inspect Mask R-CNN Model Detection Steps with inspect_model.ipynb Source: https://github.com/matterport/mask_rcnn/blob/master/README.md Jupyter notebook providing an in-depth look at the Mask R-CNN detection and segmentation pipeline. It visualizes each step and allows for step-by-step execution to inspect outputs. ```python # samples/coco/inspect_model.ipynb ``` -------------------------------- ### Matplotlib Axes Setup Function - Python Source: https://github.com/matterport/mask_rcnn/blob/master/samples/nucleus/inspect_nucleus_data.ipynb Defines a utility function `get_ax` to return a Matplotlib Axes array for consistent graph sizing in visualizations. It allows control over the number of rows, columns, and overall figure size. ```python def get_ax(rows=1, cols=1, size=16): """Return a Matplotlib Axes array to be used in all visualizations in the notebook. Provide a central point to control graph sizes. Adjust the size attribute to control how big to render images """ _, ax = plt.subplots(rows, cols, figsize=(size*cols, size*rows)) return ax ``` -------------------------------- ### Mask R-CNN Model Training Output Example Source: https://github.com/matterport/mask_rcnn/blob/master/samples/shapes/train_shapes.ipynb This represents a typical training epoch output for the Mask R-CNN model. It details the loss values for the overall model and its components (RPN class/bbox loss, MRCNN class/bbox/mask loss) for both training and validation sets. ```text Epoch 1/1 100/100 [==============================] - 86s - loss: 11.4006 - rpn_class_loss: 0.0184 - rpn_bbox_loss: 0.8409 - mrcnn_class_loss: 0.1576 - mrcnn_bbox_loss: 0.0902 - mrcnn_mask_loss: 0.1977 - val_loss: 11.4376 - val_rpn_class_loss: 0.0220 - val_rpn_bbox_loss: 1.0068 - val_mrcnn_class_loss: 0.1172 - val_mrcnn_bbox_loss: 0.0683 - val_mrcnn_mask_loss: 0.1278 ``` -------------------------------- ### Reduce Mask RCNN Memory: Use Smaller Images Source: https://github.com/matterport/mask_rcnn/wiki/Home Decrease memory requirements and training/inference time by resizing input images to smaller dimensions. Adjust IMAGE_MIN_DIM and IMAGE_MAX_DIM in the Config class. Example uses 800x1024. ```python IMAGE_MIN_DIM = 800 IMAGE_MAX_DIM = 1024 ``` -------------------------------- ### Mask R-CNN Core Implementation Files Source: https://github.com/matterport/mask_rcnn/blob/master/README.md Core Python files for the Mask R-CNN implementation. Includes the main model logic, utility functions, and configuration settings. These files form the backbone of the detection and segmentation pipeline. ```python # mrcnn/model.py # mrcnn/utils.py # mrcnn/config.py ``` -------------------------------- ### Get Layer Activations from Mask R-CNN Model Source: https://github.com/matterport/mask_rcnn/blob/master/samples/coco/inspect_model.ipynb This snippet demonstrates how to retrieve the output tensors for specific layers from a Mask R-CNN model. It takes an input image and a list of layer names to extract activations from. The output is a dictionary mapping layer names to their activation tensors. ```python activations = model.run_graph([image], [ ("input_image", tf.identity(model.keras_model.get_layer("input_image").output)), ("res4w_out", model.keras_model.get_layer("res4w_out").output), # for resnet100 ("rpn_bbox", model.keras_model.get_layer("rpn_bbox").output), ("roi", model.keras_model.get_layer("ROI").output), ]) ``` -------------------------------- ### Configure Inference for Nucleus Dataset (Python) Source: https://github.com/matterport/mask_rcnn/blob/master/samples/nucleus/inspect_nucleus_model.ipynb Sets up the dataset directory and initializes the inference configuration for the Nucleus dataset. The `config.display()` method shows the parameters used for inference. ```python # Dataset directory DATASET_DIR = os.path.join(ROOT_DIR, "datasets/nucleus") # Inference Configuration config = nucleus.NucleusInferenceConfig() config.display() ``` -------------------------------- ### Inspect Mask R-CNN Data Pre-processing with inspect_data.ipynb Source: https://github.com/matterport/mask_rcnn/blob/master/README.md Jupyter notebook for visualizing the pre-processing steps involved in preparing training data for Mask R-CNN. Helps understand how raw data is transformed for model training. ```python # samples/coco/inspect_data.ipynb ``` -------------------------------- ### Initialize and Prepare ShapesDataset in Python Source: https://github.com/matterport/mask_rcnn/blob/master/samples/shapes/train_shapes.ipynb This snippet demonstrates how to initialize and prepare a ShapesDataset for both training and validation. It loads a specified number of shapes and prepares the dataset for use by the Mask R-CNN model. Dependencies include ShapesDataset and config. ```python # Training dataset dataset_train = ShapesDataset() dataset_train.load_shapes(500, config.IMAGE_SHAPE[0], config.IMAGE_SHAPE[1]) dataset_train.prepare() # Validation dataset dataset_val = ShapesDataset() dataset_val.load_shapes(50, config.IMAGE_SHAPE[0], config.IMAGE_SHAPE[1]) dataset_val.prepare() ``` -------------------------------- ### Extract Mask R-CNN Layer Activations (TensorFlow) Source: https://github.com/matterport/mask_rcnn/blob/master/samples/balloon/inspect_balloon_model.ipynb This snippet shows how to get the output activations from specified layers of a Mask R-CNN Keras model. It uses `model.run_graph` to execute a subset of the model and retrieve the tensors corresponding to layer outputs. The activations can then be used for debugging or visualization. ```python activations = model.run_graph([image], [ ("input_image", tf.identity(model.keras_model.get_layer("input_image").output)), ("res2c_out", model.keras_model.get_layer("res2c_out").output), ("res3c_out", model.keras_model.get_layer("res3c_out").output), ("res4w_out", model.keras_model.get_layer("res4w_out").output), # for resnet100 ("rpn_bbox", model.keras_model.get_layer("rpn_bbox").output), ("roi", model.keras_model.get_layer("ROI").output), ]) ``` -------------------------------- ### Load and Prepare Balloon Dataset (Python) Source: https://github.com/matterport/mask_rcnn/blob/master/samples/balloon/inspect_balloon_model.ipynb Loads the balloon validation dataset using the `balloon.BalloonDataset` class and prepares it for use by calling the `prepare()` method. It then prints the number of images and class names available in the dataset. ```python # Load validation dataset dataset = balloon.BalloonDataset() dataset.load_balloon(BALLOON_DIR, "val") # Must call before using the dataset dataset.prepare() print("Images: {}\\nClasses: {}".format(len(dataset.image_ids), dataset.class_names)) ``` -------------------------------- ### Create Mask R-CNN Model and Load Weights (Python) Source: https://github.com/matterport/mask_rcnn/blob/master/samples/demo.ipynb Initializes a Mask R-CNN model in inference mode using the specified configuration and model directory. It then loads pre-trained weights from a COCO model file, ensuring compatibility by loading weights by name. ```python # Create model object in inference mode. model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) # Load weights trained on MS-COCO model.load_weights(COCO_MODEL_PATH, by_name=True) ``` -------------------------------- ### Create Mask R-CNN Model in Training Mode Source: https://github.com/matterport/mask_rcnn/blob/master/samples/shapes/train_shapes.ipynb Instantiates a Mask R-CNN model configured for training. It requires a configuration object and a directory to save models. Dependencies include modellib and config. ```python # Create model in training mode model = modellib.MaskRCNN(mode="training", config=config, model_dir=MODEL_DIR) ``` -------------------------------- ### Get Mask Head Predictions - Python Source: https://github.com/matterport/mask_rcnn/blob/master/samples/balloon/inspect_balloon_model.ipynb This code retrieves predictions from the mask head of the Mask R-CNN model. It runs the model's graph to obtain 'detections' and 'masks'. It then extracts and processes the detection class IDs, determining the number of valid detections by looking for zero padding. ```python # Get predictions of mask head mrcnn = model.run_graph([image], [ ("detections", model.keras_model.get_layer("mrcnn_detection").output), ("masks", model.keras_model.get_layer("mrcnn_mask").output), ]) # Get detection class IDs. Trim zero padding. det_class_ids = mrcnn['detections'][0, :, 4].astype(np.int32) det_count = np.where(det_class_ids == 0)[0][0] det_class_ids = det_class_ids[:det_count] print("{} detections: {}".format( det_count, np.array(dataset.class_names)[det_class_ids])) ``` -------------------------------- ### Initialize Mask R-CNN Model for Inference (Python) Source: https://github.com/matterport/mask_rcnn/blob/master/samples/nucleus/inspect_nucleus_model.ipynb Creates an instance of the Mask R-CNN model in inference mode, specifying the log directory and the inference configuration. The model is loaded onto the designated device (CPU or GPU). ```python # Create model in inference mode with tf.device(DEVICE): model = modellib.MaskRCNN(mode="inference", model_dir=LOGS_DIR, config=config) ``` -------------------------------- ### Get Next Batch from Data Generator Source: https://github.com/matterport/mask_rcnn/blob/master/samples/balloon/inspect_balloon_data.ipynb Retrieves the next batch of data from the configured data generator. The output structure depends on whether `random_rois` was enabled during generator creation. It provides normalized images, image metadata, RPN matches and bounding boxes, ground truth information, and ROIs for training the Mask R-CNN model. ```python # Get Next Image if random_rois: [normalized_images, image_meta, rpn_match, rpn_bbox, gt_class_ids, gt_boxes, gt_masks, rpn_rois, rois], [mrcnn_class_ids, mrcnn_bbox, mrcnn_mask] = next(g) log("rois", rois) log("mrcnn_class_ids", mrcnn_class_ids) log("mrcnn_bbox", mrcnn_bbox) log("mrcnn_mask", mrcnn_mask) else: [normalized_images, image_meta, rpn_match, rpn_bbox, gt_boxes, gt_masks], _ = next(g) log("gt_class_ids", gt_class_ids) log("gt_boxes", gt_boxes) log("gt_masks", gt_masks) log("rpn_match", rpn_match, ) log("rpn_bbox", rpn_bbox) image_id = modellib.parse_image_meta(image_meta)("image_id")[0] print("image_id: ", image_id, dataset.image_reference(image_id)) # Remove the last dim in mrcnn_class_ids. It's only added ``` -------------------------------- ### Get Next Image Batch from Data Generator Source: https://github.com/matterport/mask_rcnn/blob/master/samples/coco/inspect_data.ipynb This Python code snippet shows how to retrieve the next batch of data from a Mask R-CNN data generator. It handles two cases: when `random_rois` is enabled and when it is not. The retrieved data includes normalized images, image metadata, RPN matches and bounding boxes, ground truth classes, boxes, masks, RPN proposals, and final proposals (ROIs). It also logs key information and prints the image ID and its reference. This is crucial for feeding data into the model during training. ```python # Get Next Image if random_rois: [normalized_images, image_meta, rpn_match, rpn_bbox, gt_class_ids, gt_boxes, gt_masks, rpn_rois, rois], [mrcnn_class_ids, mrcnn_bbox, mrcnn_mask] = next(g) log("rois", rois) log("mrcnn_class_ids", mrcnn_class_ids) log("mrcnn_bbox", mrcnn_bbox) log("mrcnn_mask", mrcnn_mask) else: [normalized_images, image_meta, rpn_match, rpn_bbox, gt_boxes, gt_masks], _ = next(g) log("gt_class_ids", gt_class_ids) log("gt_boxes", gt_boxes) log("gt_masks", gt_masks) log("rpn_match", rpn_match, ) log("rpn_bbox", rpn_bbox) image_id = modellib.parse_image_meta(image_meta)["image_id"][0] print("image_id: ", image_id, dataset.image_reference(image_id)) ``` -------------------------------- ### Custom Dataset Creation and Loading for Mask R-CNN Source: https://context7.com/matterport/mask_rcnn/llms.txt Extends the Dataset class to manage custom datasets by defining how to load annotations and masks. It includes methods for adding classes, loading image information from JSON annotations, generating masks from polygons, and providing image references. This is essential for training Mask R-CNN on user-defined data. ```python import os import json import numpy as np import skimage.draw from mrcnn import utils class CustomDataset(utils.Dataset): def load_dataset(self, dataset_dir, subset): """Load a subset of the dataset. dataset_dir: Root directory of the dataset subset: Subset to load (train or val) """ # Add classes self.add_class("dataset", 1, "class_name_1") self.add_class("dataset", 2, "class_name_2") # Load annotations from JSON annotations_path = os.path.join(dataset_dir, subset, "annotations.json") with open(annotations_path) as f: annotations = json.load(f) # Add images for img_data in annotations: image_path = os.path.join(dataset_dir, subset, img_data['filename']) self.add_image( "dataset", image_id=img_data['id'], path=image_path, width=img_data['width'], height=img_data['height'], polygons=img_data['regions'] ) def load_mask(self, image_id): """Generate instance masks for an image. Returns: masks: A bool array of shape [height, width, instance count] class_ids: a 1D array of class IDs of the instance masks """ info = self.image_info[image_id] # Convert polygons to bitmap masks mask = np.zeros([info["height"], info["width"], len(info["polygons"])], dtype=np.uint8) class_ids = [] for i, p in enumerate(info["polygons"]): # Get polygon coordinates rr, cc = skimage.draw.polygon(p['all_points_y'], p['all_points_x']) mask[rr, cc, i] = 1 class_ids.append(p['class_id']) return mask.astype(np.bool), np.array(class_ids, dtype=np.int32) def image_reference(self, image_id): """Return the path of the image.""" info = self.image_info[image_id] return info["path"] # Load and prepare dataset dataset_train = CustomDataset() dataset_train.load_dataset('path/to/dataset', 'train') dataset_train.prepare() dataset_val = CustomDataset() dataset_val.load_dataset('path/to/dataset', 'val') dataset_val.prepare() print(f"Training images: {len(dataset_train.image_ids)}") print(f"Validation images: {len(dataset_val.image_ids)}") print(f"Classes: {dataset_train.class_names}") ``` -------------------------------- ### Visualize Mask R-CNN Detection Results Source: https://context7.com/matterport/mask_rcnn/llms.txt Visualizes detection results from a Mask R-CNN model on an image. Displays bounding boxes, masks, and class labels using matplotlib. Also includes functionality to display multiple images in a grid. Requires a trained model, image(s), and class names. ```python import matplotlib.pyplot as plt import skimage.io from mrcnn import visualize from mrcnn import model as modellib from mrcnn.config import Config # Load model and perform detection (from previous example) config = InferenceConfig() # Assuming InferenceConfig is defined elsewhere or copied model = modellib.MaskRCNN(mode="inference", config=config, model_dir="./logs") model.load_weights("path/to/weights.h5", by_name=True) # Load image image = skimage.io.imread("path/to/image.jpg") # Detect objects results = model.detect([image], verbose=1) r = results[0] # Define class names class_names = ['BG', 'class_1', 'class_2', 'class_3'] # Visualize results visualize.display_instances( image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'], title="Detection Results", figsize=(16, 16) ) # Save visualization to file plt.savefig("detection_output.png", bbox_inches='tight', dpi=150) plt.show() # Display multiple images in grid images = [skimage.io.imread(f"image_{i}.jpg") for i in range(4)] titles = [f"Image {i+1}" for i in range(4)] visualize.display_images(images, titles=titles, cols=2) ``` -------------------------------- ### Configure and Load Nucleus Dataset - Python Source: https://github.com/matterport/mask_rcnn/blob/master/samples/nucleus/inspect_nucleus_data.ipynb Sets up the configuration for the Nucleus dataset, disabling image resizing to view original dimensions. It then loads the specified subset ('train') of the dataset and prepares it for use, printing the image and class counts. ```python # Dataset directory DATASET_DIR = os.path.join(ROOT_DIR, "datasets/nucleus") # Use configuation from nucleus.py, but override # image resizing so we see the real sizes here class NoResizeConfig(nucleus.NucleusConfig): IMAGE_RESIZE_MODE = "none" config = NoResizeConfig() # Load dataset dataset = nucleus.NucleusDataset() # The subset is the name of the sub-directory, such as stage1_train, # stage1_test, ...etc. You can also use these special values: # train: loads stage1_train but excludes validation images # val: loads validation images from stage1_train. For a list # of validation images see nucleus.py dataset.load_nucleus(DATASET_DIR, subset="train") # Must call before using the dataset dataset.prepare() print("Image Count: {}".format(len(dataset.image_ids))) print("Class Count: {}".format(dataset.num_classes)) for i, info in enumerate(dataset.class_info): print("{:3}. {:50}".format(i, info['name'])) ``` -------------------------------- ### Train Mask R-CNN Heads in Python Source: https://github.com/matterport/mask_rcnn/blob/master/samples/shapes/train_shapes.ipynb This snippet illustrates how to initiate training for the head layers of a Mask R-CNN model. By freezing the backbone layers and training only the head, it allows for initial adaptation of the classifier and regressor. This is the first stage of a two-stage training process. Dependencies include model and train(). ```python # Train the head branches # Passing layers="heads" freezes all layers except the head # layers. You can also pass a regular expression to select ``` -------------------------------- ### Print Anchor Summary Source: https://github.com/matterport/mask_rcnn/blob/master/samples/coco/inspect_data.ipynb This Python script prints a summary of anchor configurations, including counts, scales, ratios, and anchors per level. It helps in understanding the anchor generation process in object detection models like Mask R-CNN. No external dependencies beyond standard Python libraries are explicitly mentioned for this snippet. ```python # Print summary of anchors num_levels = len(backbone_shapes) anchors_per_cell = len(config.RPN_ANCHOR_RATIOS) print("Count: ", anchors.shape[0]) print("Scales: ", config.RPN_ANCHOR_SCALES) print("ratios: ", config.RPN_ANCHOR_RATIOS) print("Anchors per Cell: ", anchors_per_cell) print("Levels: ", num_levels) anchors_per_level = [] for l in range(num_levels): num_cells = backbone_shapes[l][0] * backbone_shapes[l][1] anchors_per_level.append(anchors_per_cell * num_cells // config.RPN_ANCHOR_STRIDE**2) print("Anchors in Level {}: {}".format(l, anchors_per_level[l])) ``` -------------------------------- ### Visualize Mask R-CNN Detection on a Random Image Source: https://github.com/matterport/mask_rcnn/blob/master/samples/shapes/train_shapes.ipynb Tests the Mask R-CNN model on a randomly selected image from the validation dataset. It loads the ground truth data, visualizes the instances using `visualize.display_instances`, and logs relevant information such as bounding boxes and masks. ```python # Test on a random image image_id = random.choice(dataset_val.image_ids) original_image, image_meta, gt_class_id, gt_bbox, gt_mask =\ modellib.load_image_gt(dataset_val, inference_config, image_id, use_mini_mask=False) log("original_image", original_image) log("image_meta", image_meta) log("gt_class_id", gt_class_id) log("gt_bbox", gt_bbox) log("gt_mask", gt_mask) visualize.display_instances(original_image, gt_bbox, gt_mask, gt_class_id, dataset_train.class_names, figsize=(8, 8)) ``` -------------------------------- ### Visualize Anchors with Refinement and Clipping (Python) Source: https://github.com/matterport/mask_rcnn/blob/master/samples/balloon/inspect_balloon_model.ipynb Compares anchors before refinement, after refinement, and after clipping to image boundaries. This visualization aids in understanding the refinement process and how anchors are constrained by the image dimensions. Requires matplotlib and a utility function 'utils.denorm_boxes'. ```python # Show top anchors with refinement. Then with clipping to image boundaries limit = 50 ax = get_ax(1, 2) pre_nms_anchors = utils.denorm_boxes(rpn["pre_nms_anchors"][0], image.shape[:2]) refined_anchors = utils.denorm_boxes(rpn["refined_anchors"][0], image.shape[:2]) refined_anchors_clipped = utils.denorm_boxes(rpn["refined_anchors_clipped"][0], image.shape[:2]) visualize.draw_boxes(image, boxes=pre_nms_anchors[:limit], refined_boxes=refined_anchors[:limit], ax=ax[0]) visualize.draw_boxes(image, refined_boxes=refined_anchors_clipped[:limit], ax=ax[1]) ``` -------------------------------- ### Inspect Mask R-CNN Model Weights with inspect_weights.ipynb Source: https://github.com/matterport/mask_rcnn/blob/master/README.md Jupyter notebook for inspecting the weights of a trained Mask R-CNN model. Useful for identifying anomalies and understanding weight distributions. ```python # samples/coco/inspect_weights.ipynb ``` -------------------------------- ### Import Mask R-CNN Libraries and Utilities (Python) Source: https://github.com/matterport/mask_rcnn/blob/master/samples/coco/inspect_data.ipynb Imports essential Python libraries and modules required for the Mask R-CNN project, including utilities for image visualization and model implementation. It sets the root directory and adds it to the system path to ensure local library imports function correctly. ```python import os import sys import itertools import math import logging import json import re import random from collections import OrderedDict import numpy as np import matplotlib import matplotlib.pyplot as plt import matplotlib.patches as patches import matplotlib.lines as lines from matplotlib.patches import Polygon # Root directory of the project ROOT_DIR = os.path.abspath("../../") # Import Mask RCNN sys.path.append(ROOT_DIR) # To find local version of the library from mrcnn import utils from mrcnn import visualize from mrcnn.visualize import display_images import mrcnn.model as modellib from mrcnn.model import log %matplotlib inline ``` -------------------------------- ### Create Mask R-CNN Model for Inference (Python) Source: https://github.com/matterport/mask_rcnn/blob/master/samples/balloon/inspect_balloon_model.ipynb Initializes the Mask R-CNN model in inference mode, specifying the model directory and configuration. The model is loaded onto the designated device (CPU or GPU) using TensorFlow. ```python # Create model in inference mode with tf.device(DEVICE): model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) ``` -------------------------------- ### Prepare and Inspect Mask R-CNN Dataset (Python) Source: https://github.com/matterport/mask_rcnn/blob/master/samples/coco/inspect_data.ipynb This snippet shows how to initialize the dataset, retrieve the total number of images, and display the class count and names. It's essential for understanding the dataset's composition before model training. No external libraries are explicitly imported in this snippet, assuming 'dataset' is a pre-defined object. ```python dataset.prepare() print("Image Count: {}".format(len(dataset.image_ids))) print("Class Count: {}".format(dataset.num_classes)) for i, info in enumerate(dataset.class_info): print("{:3}. {:50}".format(i, info['name'])) ``` -------------------------------- ### Configure Mask R-CNN for Multi-GPU Training Source: https://context7.com/matterport/mask_rcnn/llms.txt Sets up a configuration for training a Mask R-CNN model across multiple GPUs. It defines the number of GPUs, images per GPU, and adjusts the total batch size accordingly. This configuration is intended to be used with the ParallelModel class for distributed training. ```python import os from mrcnn import model as modellib from mrcnn.parallel_model import ParallelModel from mrcnn.config import Config # Multi-GPU configuration class MultiGPUConfig(Config): NAME = "multi_gpu" GPU_COUNT = 4 # Number of GPUs available IMAGES_PER_GPU = 2 # Batch size per GPU NUM_CLASSES = 1 + 80 STEPS_PER_EPOCH = 1000 LEARNING_RATE = 0.001 config = MultiGPUConfig() print(f"Total batch size: {config.BATCH_SIZE}") # GPU_COUNT * IMAGES_PER_GPU ``` -------------------------------- ### Display Sample ROIs and Masks in Python Source: https://github.com/matterport/mask_rcnn/blob/master/samples/balloon/inspect_balloon_data.ipynb This snippet displays a selection of 8 ROIs, along with their refined bounding boxes and masks. It uses a helper function 'display_images' to arrange the visualizations in a grid, showing both the bounding boxes and the segmented masks for each selected ROI. ```python if random_rois: # Dispalay ROIs and corresponding masks and bounding boxes ids = random.sample(range(rois.shape[1]), 8) images = [] titles = [] for i in ids: image = visualize.draw_box(sample_image.copy(), rois[b,i,:4].astype(np.int32), [255, 0, 0]) image = visualize.draw_box(image, refined_rois[i].astype(np.int64), [0, 255, 0]) images.append(image) titles.append("ROI {}".format(i)) images.append(mask_specific[i] * 255) titles.append(dataset.class_names[mrcnn_class_ids[b,i]][:20]) display_images(images, titles, cols=4, cmap="Blues", interpolation="none") ```