Skip to content

MMDetection (instance segmentation)

MMDetection is a comprehensive and flexible framework not only for object detection, but also for instance segmentation. Custom docker images with additional tools are available from here:

https://github.com/waikato-datamining/mmdetection

Prerequisites#

Make sure you have the directory structure created as outlined in the Prerequisites.

Data#

In this example, we will use the Oxford Pets dataset, which consists of 37 different categories of cats and dogs.

Download the dataset from the following URL into the data directory and extract it:

https://datasets.cms.waikato.ac.nz/ufdl/data/oxford-pets/oxford-pets-adams.zip

Rename the adams directory to pets-adams.

To speed up training, we only use two labels: cat:abyssinian and dog:yorkshire_terrier. The label filtering and splitting it into train, validation and test subsets is done using image-dataset-converter:

docker run --rm -u $(id -u):$(id -g) \
  -v `pwd`:/workspace \
  -t waikatodatamining/image-dataset-converter:0.0.4 \
  idc-convert \
    -l INFO \
    from-adams-od \
      -i "/workspace/data/pets-adams/*.report" \
    filter-labels \
      --labels cat:abyssinian dog:yorkshire_terrier \
    discard-negatives \
    coerce-mask \
    to-coco-od \
      -o /workspace/data/pets2-coco-split \
      --categories cat:abyssinian dog:yorkshire_terrier \
      --category_output_file labels.txt \
      --split_names train val test \
      --split_ratios 70 15 15

Training#

For training, we will use the following docker image:

waikatodatamining/mmdetection:2.27.0_cuda11.1

The training script is called mmdet_train, for which we can invoke the help screen as follows (unfortunately, we need to set the MMDET_CLASSES environment variable to avoid an exception):

docker run --rm \
  -e MMDET_CLASSES= \
  -t waikatodatamining/mmdetection:2.27.0_cuda11.1 \
  mmdet_train --help 

It is good practice creating a separate sub-directory for each training run, with a directory name that hints at what dataset and model were used. So for our first training run, which will use mainly default parameters, we will create the following directory in the output folder:

pets2-mmdet-maskrcnn

Before we can train, we will need to obtain and customize a config file. Within the container, you can find example configurations for various architectures in the following directory:

/mmdetection/configs

Using the mmdet_config command, we can expand and dump one of these configurations for our own purposes:

docker run --rm \
  -u $(id -u):$(id -g) \
  --gpus=all \
  -v `pwd`:/workspace \
  -v `pwd`/cache:/.cache \
  -v `pwd`/cache/torch:/.cache/torch \
  -t waikatodatamining/mmdetection:2.27.0_cuda11.1 \
  mmdet_config \
  /mmdetection/configs/mask_rcnn/mask_rcnn_r50_fpn_1x_coco.py \
  > output/pets2-mmdet-maskrcnn/mask_rcnn_r50_fpn_1x_coco.py

Open the mask_rcnn_r50_fpn_1x_coco.py file in a text editor and perform the following operations:

  • remove any lines before model = dict(
  • change all occurrences of num_classes to 2
  • change dataset_type to Dataset and any occurrences of type in the train, test, val sections of the data dictionary
  • change data_root occurrences to /workspace/data/pets2-coco-split (the directory above the train and val directories)
  • change img_prefix occurrences to img_prefix=data_root+'/DIR', with DIR being the appropriate train, val or test
  • change ann_file occurrences to ann_file=data_root+'/DIR/annotations.json', with DIR being the appropriate train, val or test
  • change max_epochs in runner to an appropriate value, e.g., 50
  • change interval in checkpoint_config to a higher value, e.g., 5
  • change lr (learning rate) in optimizer to 0.002 to avoid NaNs with a learning rate that is too high

Kick off the training with the following command:

docker run --rm \
  -u $(id -u):$(id -g) \
  --shm-size 8G \
  --gpus=all \
  -v `pwd`:/workspace \
  -v `pwd`/cache:/.cache \
  -v `pwd`/cache/torch:/.cache/torch \
  -e MMDET_CLASSES=/workspace/data/pets2-coco-split/train/labels.txt \
  -t waikatodatamining/mmdetection:2.27.0_cuda11.1 \
  mmdet_train \
  /workspace/output/pets2-mmdet-maskrcnn/mask_rcnn_r50_fpn_1x_coco.py \
  --work-dir /workspace/output/pets2-mmdet-maskrcnn/runs

Predicting#

Using the mmdet_predict script, we can batch-process images placed in the predictions/in directory as follows (e.g., from our test subset):

docker run --rm \
  -u $(id -u):$(id -g) \
  --shm-size 8G \
  --gpus=all \
  -v `pwd`:/workspace \
  -v `pwd`/cache:/.cache \
  -v `pwd`/cache/torch:/.cache/torch \
  -e MMDET_CLASSES=/workspace/data/pets2-coco-split/train/labels.txt \
  -t waikatodatamining/mmdetection:2.27.0_cuda11.1 \
  mmdet_predict \
  --checkpoint /workspace/output/pets2-mmdet-maskrcnn/runs/latest.pth \
  --config /workspace/output/pets2-mmdet-maskrcnn/mask_rcnn_r50_fpn_1x_coco.py \
  --prediction_in /workspace/predictions/in \
  --prediction_out /workspace/predictions/out

Notes

  • By default, the predictions get output in ROI CSV format. But you can also output them in the OPEX JSON format by adding --prediction_format opex --prediction_suffix .json to the command.

  • You can view the predictions with the ADAMS Preview browser:

Example prediction

Screenshot Screenshot