Mask DINO Documentation

repository·main·Indexed 23 days ago

https://github.com/idea-research/maskdino

A unified transformer-based framework for object detection and various segmentation tasks, including instance, panoptic, and semantic segmentation. Built on detectron2 and the detrex toolbox, it supports major datasets such as COCO, ADE20K, and Cityscapes. The documentation covers installation requirements, dataset preparation, training and evaluation procedures, and running inference demos using pre-trained models.

Tokens
3.8K
Snippets
10
Records
20
Agent score
81%

What's inside Mask DINO

  1. Overview of Mask DINO

    main

    Mask DINO is a unified transformer-based framework designed for object detection, panoptic segmentation, instance segmentation, and semantic segmentation. It achieves task and data cooperation between detection and segmentation tasks, providing state-of-the-art performance across multiple benchmarks.

    Key features include:

    • A unified architecture for all major segmentation and detection tasks.
    • Support for major datasets: COCO, ADE20K, and Cityscapes.
    • High performance: Achieves competitive results on COCO (instance and panoptic) and ADE20K (semantic) leaderboards.

    The implementation is based on detectron2 and is also available via the detrex toolbox.

  2. Extend MaskDINO components

    main

    MaskDINO is composed of three modular components that can be replaced with custom implementations:

    1. Backbone: Define and register new backbones in maskdino/modeling/backbone (e.g., following the Swin Transformer implementation).
    2. Pixel Decoder: Located in maskdino/modeling/pixel_decoder. It functions as a multi-scale encoder. It returns:
      • mask_features: Per-pixel embeddings at 1/4 resolution used for binary masks.
      • multi_scale_features: Multi-scale inputs for the Transformer decoder.
    3. Transformer Decoder: Located in maskdino/modeling/transformer_decoder. It handles both detection and segmentation tasks, following the DINO decoder design.
  3. Getting Started with Mask DINO

    main

    To begin using Mask DINO, you can follow these primary paths:

    • Run an Inference Demo: Use pre-trained models to see the model in action by following the guide in demo/README.md.
    • Prepare Datasets: If you are training or evaluating on custom data, follow the instructions in datasets/README.md to prepare your datasets for MaskDINO.
    • Explore Results: Review the performance benchmarks in the Results section of the README.
    • Advanced Usage: For more complex workflows, see the More Usage section.
  4. Configure the DETECTRON2_DATASETS environment variable

    main

    MaskDINO uses the DETECTRON2_DATASETS environment variable to locate builtin datasets (ADEChallengeData2016, coco, cityscapes).

    • If unset, the default location is ./datasets relative to your current working directory.
    • To set a custom location, use export DETECTRON2_DATASETS=/path/to/datasets.
    export DETECTRON2_DATASETS=/path/to/datasets
  5. Install MaskDINO requirements

    main

    To install MaskDINO, ensure your system meets the following requirements:

    • OS: Linux
    • Python: $\ge$ 3.6
    • PyTorch: $\ge$ 1.9 (ensure torchvision matches your PyTorch installation and is compatible with your Detectron2 version).
    • Detectron2: Must be installed following the official instructions.
    • OpenCV: Optional, but required for running demos and visualization.

    After meeting these requirements, install the project dependencies using: pip install -r requirements.txt

  6. Compile CUDA kernel for MSDeformAttn

    main

    MaskDINO requires a compiled CUDA kernel for MSDeformAttn. Before compiling, ensure the CUDA_HOME environment variable is defined and points to your installed CUDA toolkit directory.

    Navigate to the operations directory and run the provided shell script:

    cd maskdino/modeling/pixel_decoder/ops
    sh make.sh
  7. Prepare the COCO dataset structure

    main

    To use COCO for instance, panoptic, or semantic segmentation, organize your files as follows:

    coco/
      annotations/
        instances_{train,val}2017.json
        panoptic_{train,val}2017.json
      {train,val}2017/
        # image files mentioned in the corresponding json
      panoptic_{train,val}2017/  # png annotations
      panoptic_semseg_{train,val}2017/  # generated by the script below

    Steps:

    1. Install panopticapi: pip install git+https://github.com/cocodataset/panopticapi.git
    2. Extract semantic annotations from panoptic annotations (required for evaluation) by running: python datasets/prepare_coco_semantic_annos_from_panoptic_annos.py
    pip install git+https://github.com/cocodataset/panopticapi.git
    python datasets/prepare_coco_semantic_annos_from_panoptic_annos.py
  8. Evaluate pretrained MaskDINO models

    main

    To evaluate pretrained models, download the checkpoint file and run the train_net.py script with the --eval-only flag.

    Prerequisites: If your dataset files are not located within this repository, you must either set the DETECTRON2_DATASETS environment variable or create a symbolic link to your data directory inside this repo.

    export DETECTRON2_DATASETS=/path/to/your/data

    Command Syntax:

    python train_net.py --eval-only --num-gpus <N> --config-file <config_path> MODEL.WEIGHTS <path_to_checkpoint>

    Example (Instance Segmentation):

    python train_net.py --eval-only --num-gpus 8 --config-file configs/coco/instance-segmentation/maskdino_R50_bs16_50ep_3s_dowsample1_2048.yaml MODEL.WEIGHTS /path/to/checkpoint_file
    python train_net.py --eval-only --num-gpus 8 --config-file configs/coco/instance-segmentation/maskdino_R50_bs16_50ep_3s_dowsample1_2048.yaml MODEL.WEIGHTS /path/to/checkpoint_file
  9. Prepare the ADE20k dataset structure

    main

    Organize ADE20k data as follows:

    ADEChallengeData2016/
      images/
      annotations/
      objectInfo150.txt
      annotations_instance/
      # generated by prepare_ade20k_sem_seg.py
      annotations_detectron2/
      # generated by prepare_ade20k_pan_seg.py
      ade20k_panoptic_{train,val}.json
      ade20k_panoptic_{train,val}/
      # generated by prepare_ade20k_ins_seg.py
      ade20k_instance_{train,val}.json

    Steps:

    1. Install panopticapi: pip install git+https://github.com/cocodataset/panopticapi.git
    2. Download instance annotations: wget http://sceneparsing.csail.mit.edu/data/ChallengeData2017/annotations_instance.tar
    3. Generate annotations_detectron2 (semantic segmentation): python datasets/prepare_ade20k_sem_seg.py
    4. Generate panoptic annotations: python datasets/prepare_ade20k_pan_seg.py
    5. Generate instance annotations in COCO format: python datasets/prepare_ade20k_ins_seg.py
    pip install git+https://github.com/cocodataset/panopticapi.git
    wget http://sceneparsing.csail.mit.edu/data/ChallengeData2017/annotations_instance.tar
    python datasets/prepare_ade20k_sem_seg.py
    python datasets/prepare_ade20k_pan_seg.py
    python datasets/prepare_ade20k_ins_seg.py
  10. Train MaskDINO models

    main

    To train MaskDINO from scratch or reproduce results, use the train_net.py script without the --eval-only flag.

    Training Requirements:

    • Swin Backbones: You must specify the path to the pretrained backbone using MODEL.WEIGHTS.
    • ResNet-50: Training on 8 GPUs requires approximately 15G of memory per GPU and takes about 3 days for 50 epochs.
    • Swin-L: Training on 8 GPUs requires approximately 60G of memory per GPU. If memory is insufficient, consider using 16 GPUs across two nodes.
    • Batch Size: The default total batch size is 16. If training on a single GPU, you must manually adjust SOLVER.IMS_PER_BATCH and SOLVER.BASE_LR.

    Command Syntax:

    python train_net.py --num-gpus <N> --config-file <config_path> MODEL.WEIGHTS <path_to_checkpoint>

    Example (Single GPU training):

    python train_net.py --num-gpus 1 --config-file <config_path> SOLVER.IMS_PER_BATCH <VALUE> SOLVER.BASE_LR <VALUE>
    python train_net.py --num-gpus 8 --config-file config_path MODEL.WEIGHTS /path/to/checkpoint_file
  11. Example conda environment setup for MaskDINO

    main

    The following sequence of commands provides a complete workflow for setting up a fresh Conda environment, installing PyTorch, Detectron2, necessary dataset APIs, and compiling the MaskDINO CUDA kernels.

    Note: This assumes you are starting from a clean directory and will clone the repositories manually as shown.

    conda create --name maskdino python=3.8 -y
    conda activate maskdino
    conda install pytorch==1.9.0 torchvision==0.10.0 cudatoolkit=11.1 -c pytorch -c nvidia
    pip install -U opencv-python
    
    # under your working directory
    git clone git@github.com:facebookresearch/detectron2.git
    cd detectron2
    pip install -e .
    pip install git+https://github.com/cocodataset/panopticapi.git
    pip install git+https://github.com/mcordts/cityscapesScripts.git
    
    cd ..
    git clone git@github.com:facebookresearch/MaskDINO.git
    cd MaskDINO
    pip install -r requirements.txt
    cd maskdino/modeling/pixel_decoder/ops
    sh make.sh