ArmFormer: Lightweight Transformer Architecture for Real-Time Multi-Class Weapon Segmentation and Classification
ArmFormer is an advanced semantic segmentation architecture designed for thermal and RGB weapon detection. The model integrates Convolutional Block Attention Module (CBAM) with Mix Vision Transformer (MViT) backbone and Light Harmonic Aggregation Module (LightHAM) decoder to achieve superior detection performance.
- CBAM-Enhanced MViT Backbone: Combines transformer-based hierarchical feature extraction with channel and spatial attention mechanisms
- LightHAM Decoder with Attention: Efficient multi-scale feature aggregation enhanced with CBAM attention
- Multi-Class Weapon Detection: Supports detection of handguns, rifles, revolvers, knives, and human subjects
- Optimized for Resource Efficiency: Designed for deployment on edge devices and resource-constrained platforms
- Thermal and RGB Support: Works with both thermal and RGB imagery
- Hierarchical transformer encoder with 4 stages
- CBAM attention modules after each transformer stage
- Overlap patch merging for spatial continuity
- Multi-scale feature extraction
- Lightweight Harmonic Aggregation Module (LightHAM)
- CBAM attention integration (pre-HAM and post-HAM)
- Efficient multi-scale feature fusion
- Reduced computational complexity
Step 1: Clone the Repository
git clone https://github.com/yourusername/armformer.git
cd armformerStep 2: Environment Setup
Create and activate a conda environment:
conda create -n armformer python=3.8
conda activate armformerStep 3: Install Dependencies
Install PyTorch with CUDA support:
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidiaInstall MMSegmentation and dependencies:
pip install -U openmim
mim install mmengine
mim install "mmcv>=2.0.0,<2.2.0"Important Note: After installing mmengine, manually update the MMCV version constraint:
# In mmseg/__init__.py
MMCV_MAX = '2.2.1'Install additional requirements:
pip install -r requirements.txtArmFormer is designed for thermal/RGB weapon detection datasets. Organize your dataset as follows:
data/
├── train/
│ ├── images/
│ └── mmseg_masks/
├── validation/
│ ├── images/
│ └── mmseg_masks/
└── test/
├── images/
└── mmseg_masks/
Update the data_root path in local_configs/_base_/datasets/thermal_rgb.py to point to your dataset location.
Command Line Training
python tools/train.py local_configs/armformer_config.pyJupyter Notebook Training
Navigate to the notebooks directory and open train.ipynb:
cd notebooks
jupyter notebook train.ipynbThe notebook provides an interactive interface to:
- Load and customize the configuration
- Set working directories
- Monitor training progress
- Visualize results
Command Line Evaluation
python tools/test.py local_configs/armformer_config.py /path/to/checkpoint.pth --eval mIoUJupyter Notebook Evaluation
Open test.ipynb in the notebooks directory for evaluation tasks:
cd notebooks
jupyter notebook test.ipynbThe test notebook includes cells for:
- Basic testing and evaluation
- Performance benchmarking
- FLOPs calculation
- Confusion matrix generation
The Thermal_RGBDataset supports the following classes:
- Background
- Handgun
- Human
- Knife
- Rifle
- Revolver
Key training parameters in armformer_config.py:
- Optimizer: AdamW with learning rate 0.00006
- Training iterations: 160,000
- Batch size: 2 (training), 1 (validation/test)
- Input size: 512×512
- Learning rate schedule: Linear warmup + Polynomial decay
- Validation interval: Every 8,000 iterations
Backbone (CBAM-MViT):
- Embedding dimensions: 32
- Number of stages: 4
- Number of layers per stage: [2, 2, 2, 2]
- Number of heads: [1, 2, 5, 8]
- CBAM reduction ratios: [16, 16, 16, 16]
- CBAM kernel sizes: [7, 7, 7, 7]
Decoder (CBAM-LightHAM):
- Input channels: [64, 160, 256]
- HAM channels: 256
- CBAM position: pre_ham
- Number of classes: 6
armformer/
├── local_configs/ # Configuration files
│ ├── _base_/
│ │ ├── datasets/ # Dataset configurations
│ │ └── models/ # Model configurations
│ └── armformer_config.py # Main config file
├── mmseg/ # Model implementations
│ ├── datasets/ # Custom dataset definitions
│ └── models/ # CBAM modules and architectures
├── notebooks/ # Jupyter notebooks
│ ├── train.ipynb # Training notebook
│ └── test.ipynb # Testing notebook
├── tools/ # Training and testing scripts
│ ├── train.py
│ └── test.py
├── requirements/ # Dependency specifications
├── requirements.txt # Main requirements file
├── LICENSE # MIT License
└── README.md # This file
The CBAM (Convolutional Block Attention Module) enhances feature representation through:
- Channel Attention: Uses average and max pooling to capture channel-wise relationships
- Spatial Attention: Applies convolutions on channel-pooled features to learn spatial attention
ARMFormer integrates CBAM at multiple locations:
- After each MixVisionTransformer stage in the backbone
- Before the Hamburger module in the decoder (pre_ham position)
- GPU Memory: Adjust batch size based on available GPU memory
- Multi-GPU Training: Use distributed training for faster convergence
- Data Augmentation: Enabled by default with random resizing, cropping, and flipping
- Learning Rate: Fine-tune based on dataset size and complexity
This project is licensed under the MIT License - see the LICENSE file for details.
For questions and feedback, please open an issue on GitHub or contact the authors.
