Install MCTformer dependencies
mainMCTformer requires Ubuntu 18.04 and Python 3.6. Install the necessary Python dependencies using pip from the provided requirements.txt file.
pip install -r requirements.txtrepository·main·Indexed 41 days ago
https://github.com/xulianuwa/mctformerA Multi-class Token Transformer designed for Weakly Supervised Semantic Segmentation (CVPR 2022). It provides tools for training models to generate class-specific localization maps and pseudo ground-truth masks for segmentation, with support for PASCAL VOC 2012 and MS COCO 2014 datasets.
MCTformer requires Ubuntu 18.04 and Python 3.6. Install the necessary Python dependencies using pip from the provided requirements.txt file.
pip install -r requirements.txtTo train MCTformer and generate class-specific localization maps on the MS COCO 2014 dataset, run:
bash run_coco.shNote: You may need to download the class label numpy file and the trained MCTformer-V2 model from the links provided in the documentation.
Download the MS COCO 2014 training and validation zip files using wget.
wget http://images.cocodataset.org/zips/train2014.zip
wget http://images.cocodataset.org/zips/val2014.zipThe segmentation pipeline for PASCAL VOC 2012 consists of three steps:
run.sh to train MCTformer and generate class-specific localization maps.run_psa.sh to use PSA to post-process seeds into pseudo ground-truth segmentation masks. This step uses pre-trained classification weights for initialization.run_seg.sh to train and test the segmentation model. When training on VOC, the model is initialized with pre-trained classification weights on VOC.bash run.sh
bash run_psa.sh
bash run_seg.shTo train the MCTformer+ model, run the provided shell script:
bash run_mct_plus.shFor standard MCTformer training, visualization, and evaluation of class-specific localization maps, use:
bash run.shTo use the PASCAL VOC 2012 dataset, follow these steps:
SegmentationClassAug.zip from the SBD dataset via this link.VOCdevkit/
└── VOC2012
├── Annotations
├── ImageSets
├── JPEGImages
├── SegmentationClass
├── SegmentationClassAug
└── SegmentationObjectwget http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar
tar –xvf VOCtrainval_11-May-2012.tarThe main.py script serves as the primary entrypoint for training MCTformer models or performing evaluation and attention map generation. It uses argparse to handle a wide range of hyperparameters for model architecture, optimization, augmentation, and dataset configuration.
To use the script, you must provide arguments via the command line. The script supports three primary modes:
--epochs and saves checkpoints to --output_dir.--eval flag. It performs evaluation on the validation dataset using the model loaded from --resume.--gen_attention_maps flag. It uses a checkpoint from --resume to generate and save attention maps.Note: When finetuning, the script includes logic to automatically interpolate position embeddings and handle class token mismatches if the pretrained checkpoint has a different number of classes or input resolution.
# Example: Training a model
python main.py --model deit_small_MCTformerPlus --batch-size 32 --epochs 45 --data-path /path/to/VOC2012 --output_dir ./saved_model
# Example: Evaluating a trained model
python main.py --eval --resume ./saved_model/checkpoint.pth --data-path /path/to/VOC2012
# Example: Generating attention maps
python main.py --gen_attention_maps --resume ./saved_model/checkpoint.pth --data-path /path/to/VOC2012The following arguments control the model architecture and the optimization process in main.py.
# Model parameters
--model MODEL # Name of model to train (e.g., 'deit_small_MCTformerPlus')
--input-size INT # Images input size
--drop PCT # Dropout rate
--drop-path PCT # Drop path rate
# Optimizer parameters
--opt OPTIMIZER # Optimizer (default: 'adamw')
--opt-eps EPSILON # Optimizer Epsilon
--opt-betas BETA # Optimizer Betas (nargs='+')
--clip-grad NORM # Clip gradient norm
--momentum M # SGD momentum
--weight-decay FLOAT # Weight decay
# Learning rate schedule parameters
--sched SCHEDULER # LR scheduler (default: 'cosine')
--lr LR # Learning rate
--lr-noise PCT PCT # Learning rate noise on/off epoch percentages (nargs='+')
--lr-noise-pct PERCENT # Learning rate noise limit percent
--lr-noise-std STDDEV # Learning rate noise std-dev
--warmup-lr LR # Warmup learning rate
--min-lr LR # Lower lr bound for cyclic schedulers
--decay-epochs N # Epoch interval to decay LR
--warmup-epochs N # Epochs to warmup LR
--cooldown-epochs N # Epochs to cooldown LR
--patience-epochs N # Patience epochs for Plateau LR scheduler
--decay-rate RATE # LR decay rateThe following arguments control data augmentation strategies and dataset loading in main.py.
# Augmentation parameters
--color-jitter PCT # Color jitter factor
--aa NAME # AutoAugment policy (e.g., 'v0', 'original', or 'rand-m9-mstd0.5-inc1')
--smoothing FLOAT # Label smoothing
--train-interpolation STR # Training interpolation (random, bilinear, bicubic)
--repeated-aug # Enable repeated augmentation
--no-repeated-aug # Disable repeated augmentation
--reprob PCT # Random erase prob
--remode STR # Random erase mode (e.g., 'pixel')
--recount INT # Random erase count
--resplit # Do not random erase first (clean) augmentation split
# Dataset parameters
--data-path STR # Dataset path
--img-list STR # Image list path
--data-set STR # Dataset name (e.g., 'VOC12')
--finetune STR # Finetune from checkpoint (URL or local path)
--resume STR # Resume from checkpoint
--start_epoch N # Start epoch
--eval # Perform evaluation only
--num_workers INT # Number of workers for DataLoader
--pin-mem # Pin CPU memory in DataLoader
--no-pin-mem # Do not pin CPU memoryThe following arguments are used specifically when --gen_attention_maps is enabled to control how attention maps are extracted and saved.
--gen_attention_maps # Enable attention map generation
--patch-size INT # Patch size
--attention-dir STR # Directory to save attention maps
--layer-index INT # Layer index to extract attention maps from
--patch-attn-refine BOOL # Whether to refine patch attention
--visualize-cls-attn BOOL # Whether to visualize CLS attention
--gt-dir STR # Ground truth directory
--cam-npy-dir STR # Directory for CAM npy files
--scales FLOAT # Scales for attention maps (nargs='+')
--label-file-path STR # Path to label file
--attention-type STR # Type of attention (e.g., 'fused')