EfficientVMamba

repository·main·Indexed 42 days ago

https://github.com/terrypei/efficientvmamba

A lightweight visual state space model (SSM) utilizing an atrous-based selective scan approach to balance global and local representational features with linear complexity. The project includes implementations for ImageNet classification, object detection, and instance segmentation, with support for various backbones including ConvNeXt, Swin Transformer, and ResNet via OpenMMLab dependencies.

Tokens
13.6K
Snippets
36
Records
52
Agent score
52%

What's inside EfficientVMamba

  1. Overview of Mask R-CNN Models and Results

    main

    Mask R-CNN is a framework for object instance segmentation that extends Faster R-CNN by adding a branch for predicting object masks in parallel with bounding box recognition. This repository provides configurations and pre-trained models for various Mask R-CNN architectures, including ResNet-50 (R-50), ResNet-101 (R-101), and ResNeXt (X-101) backbones with Feature Pyramid Networks (FPN).

    Key performance metrics available in the results table include:

    • box AP: Average Precision for bounding boxes.
    • mask AP: Average Precision for segmentation masks.
    • Inf time (fps): Inference speed in frames per second.
    • Mem (GB): Memory usage in Gigabytes.

    Models are available in both caffe and pytorch styles, with different learning rate schedules (1x, 2x) and precision modes (e.g., FP16).

  2. Download pre-trained Mask R-CNN models

    main

    The project provides several pre-trained Mask R-CNN models trained with longer schedules and multi-scale training. These models can be used as a starting point for fine-tuning on downstream tasks. The available models vary by backbone (e.g., R-50, R-101, X-101), training style (caffe or pytorch), and learning rate schedule (1x, 2x, or 3x).

    The following models are available for download:
    
    | Backbone | Style | Lr schd | Mem (GB) | box AP | mask AP | Config | Download |
    | :--- | :--- | :--- | :--- | :--- | :--- | :--- | :--- |
    | R-50-FPN | caffe | 2x | 4.3 | 40.3 | 36.5 | [config](./mask-rcnn_r50-caffe_fpn_ms-poly-2x_coco.py) | [model](https://download.openmmlab.com/mmdetection/v2.0/mask_rcnn/mask_rcnn_r50_caffe_fpn_mstrain-poly_2x_coco/mask_rcnn_r50_caffe_fpn_mstrain-poly_2x_coco_bbox_mAP-0.403__segm_mAP-0.365_20200504_231822-a75c98ce.pth) | [log](...) |
    | R-50-FPN | pytorch | 3x | 4.1 | 40.9 | 37.1 | [config](./mask-rcnn_r50_fpn_ms-poly-3x_coco.py) | [model](https://download.openmmlab.com/mmdetection/v2.0/mask_rcnn/mask_rcnn_r50_fpn_mstrain-poly_3x_coco/mask_rcnn_r50_fpn_mstrain-poly_3x_coco_20210524_201154-21b550bb.pth) | [log](...) |
    | X-101-64x4d-FPN | pytorch | 3x | 10.4 | 44.5 | 39.7 | [config](./mask-rcnn_x101-64x4d_fpn_ms-poly_3x_coco.py) | [model](https://download.openmmlab.com/mmdetection/v2.0/mask_rcnn/mask_rcnn_x101_64x4d_fpn_mstrain-poly_3x_coco/mask_rcnn_x101_64x4d_fpn_mstrain-poly_3x_coco_20210526_120447-c376f129.pth) | [log](...) |
  3. Train EfficientVMamba for Detection and Segmentation

    main

    For detection and segmentation tasks, use the training command structure similar to classification, but select the appropriate configuration files from the configs/vssm directory. Ensure you adjust the following parameters:

    • --cfg: Path to the task-specific config file.
    • --data-path: Path to your specific dataset.
    • --output: Desired output location for logs and checkpoints.
  4. Install optional dependencies for Detection and Segmentation

    main

    If you intend to use EfficientVMamba for detection or segmentation tasks, you must install the following OpenMMLab-related dependencies:

    pip install mmengine==0.10.1 mmcv==2.1.0 opencv-python-headless ftfy
    pip install mmdet==3.3.0 mmsegmentation==1.2.2 mmpretrain==1.2.0
  5. Install EfficientVMamba

    main

    Follow these steps to set up the EfficientVMamba environment. The installation process follows the same steps as the original VMamba repository.

    1. Clone the repository
    2. Create a Conda environment
    3. Install Python dependencies
    4. Install selective_scan and its dependencies (Required)
    # Step 1: Clone the repository
    git clone https://github.com/TerryPei/EfficientVMamba.git
    cd EfficientVMamba
    
    # Step 2: Create and activate a new conda environment
    conda create -n vmamba
    conda activate vmamba
    
    # Step 3: Install Dependencies
    pip install -r requirements.txt
    
    # Step 4: Install selective_scan and its dependencies
    cd selective_scan && pip install . && pytest
  6. Train EfficientVMamba for ImageNet Classification

    main

    To train the models for classification on ImageNet, use python -m torch.distributed.launch. You must specify the configuration file (--cfg), batch size (--batch-size), dataset path (--data-path), and output directory (--output).

    Available configurations in configs/vssm/:

    • Tiny: vssm_efficient_tiny.yaml
    • Small: vssm_efficient_small.yaml
    • Base: vssm_efficient_base.yaml
    # For Tiny
    python -m torch.distributed.launch --nnodes=1 --node_rank=0 --nproc_per_node=8 --master_addr="127.0.0.1" --master_port=29501 main.py --cfg configs/vssm/vssm_efficient_tiny.yaml --batch-size 128 --data-path /dataset/ImageNet2012 --output /tmp
    
    # For Small
    python -m torch.distributed.launch --nnodes=1 --node_rank=0 --nproc_per_node=8 --master_addr="127.0.0.1" --master_port=29501 main.py --cfg configs/vssm/vssm_efficient_small.yaml --batch-size 128 --data-path /dataset/ImageNet2012 --output /tmp
    
    # For Base
    python -m torch.distributed.launch --nnodes=1 --node_rank=0 --nproc_per_node=8 --master_addr="127.0.0.1" --master_port=29501 main.py --cfg configs/vssm/vssm_efficient_base.yaml --batch-size 128 --data-path /dataset/ImageNet2012 --output /tmp
  7. Install selective_scan and its CUDA core

    main

    To install the selective_scan package along with its CUDA core components, clear any existing build artifacts and install the current directory using pip. After installation, you can verify the installation by running the provided test suite.

    rm -r dist/ build/; pip install .
    pytest test_selective_scan.py # expected all pass
  8. Configure Swin Transformer with one-stage detectors like RetinaNet

    main

    When combining a Swin Transformer backbone with a one-stage detector (e.g., RetinaNet), you must account for the Layer Normalization layer at the output of the Swin Transformer.

    To ensure correct feature extraction in the Feature Pyramid Network (FPN), you must:

    1. Set start_level to 0 in the FPN configuration.
    2. Set the out_indices of the backbone to [1, 2, 3].

    Refer to the retinanet_swin-t-p4-w7_fpn_1x_coco.py configuration file as a template for this setup.

  9. How SS2D and cross_selective_scan work together

    main

    The SS2D module utilizes cross_selective_scan to perform efficient spatial scanning. Instead of a standard 1D scan, it uses EfficientScan and EfficientMerge (from efficient_scan.py) to process spatial patches.

    1. EfficientScan: Reduces the spatial resolution by a step_size factor, creating a multi-scale representation.
    2. Cross-Selective Scan: Performs the SSM operation on these reduced representations using projected weights (x_proj_weight, dt_projs_weight).
    3. EfficientMerge: Reconstructs the original spatial resolution from the scanned outputs.

    This approach allows the model to capture long-range dependencies across the 2D plane more efficiently than standard 1D scans applied to flattened images.

  10. How EMA (Exponential Moving Average) works in training

    main

    The training pipeline supports Exponential Moving Average (EMA) to improve model generalization.

    1. Initialization: If --model_ema is enabled, an EMA model is created after the base model is moved to the GPU but before DDP wrapping.
    2. Update: During train_one_epoch, after every ACCUMULATION_STEPS gradient updates, the EMA model is updated using the current model weights: model_ema.update(model).
    3. Evaluation: During validation, the script evaluates both the standard model and the EMA model, logging Acc@1 and Acc@5 for both to track which version performs better.
  11. How Mixup target generation works

    main

    When applying Mixup, the targets are not just swapped; they are interpolated based on the mixing coefficient $\lambda$ (lam).

    If you use Mixup or mixup_target, the resulting target is a soft label (one-hot encoded) where:

    • The original class has a weight of lam * (1 - smoothing) + smoothing/num_classes.
    • The mixed class has a weight of (1 - lam) * (1 - smoothing) + smoothing/num_classes.

    This ensures that the labels reflect the proportion of each image present in the mixed sample, while also incorporating label smoothing.