FasterNet
repository·master·Indexed 21 days ago
https://github.com/jierunchen/fasternetA PyTorch and PyTorchLightning implementation of the 'Run, Don't Walk' paper. It introduces Partial Convolution (PConv) and a family of latency-efficient architectures designed for high FLOPS and fast neural network execution. The repository provides tools for training and evaluating models on ImageNet-1K for image classification and COCO for object detection using Mask R-CNN.
What's inside FasterNet
- To use FasterNet for object detection, you must prepare the COCO dataset following the official MMDetection v2.25.0 guidelines.
Prepare ImageNet-1K dataset
masterFasterNet requires the ImageNet-1K classification dataset. Ensure your data is structured into
trainandvaldirectories, with each directory containing subdirectories for each class containing the images./path/to/imagenet-1k/ train/ class1/ img1.jpeg class2/ img2.jpeg val/ class1/ img3.jpeg class2/ img4.jpegEvaluate FasterNet models
masterUse
train_test.pyto evaluate pre-trained models. You must specify the configuration file via-cand the checkpoint path via--checkpoint_path.Key Flags:
-c: Path to the model configuration YAML file (e.g.,cfg/fasternet_t0.yaml).--checkpoint_path: Path to the downloaded.pthmodel checkpoint.--data_dir: Path to the ImageNet dataset.--test_phase: Enables evaluation mode.-g: GPU IDs (e.g.,1or a list like0,1,2,3).-e: Batch size for evaluation on GPU.
Measuring Latency and Throughput: To measure latency on CPU/ARM or throughput on GPU, add the
--measure_latencyand--fuse_conv_bnflags.# Single GPU evaluation example python train_test.py -c cfg/fasternet_t0.yaml \ --checkpoint_path model_ckpt/fasternet_t0-epoch.281-val_acc1.71.9180.pth \ --data_dir ../../data/imagenet --test_phase -g 1 -e 125 # Latency and throughput measurement python train_test.py -c cfg/fasternet_t0.yaml \ --checkpoint_path model_ckpt/fasternet_t0-epoch.281-val_acc1.71.9180.pth \ --data_dir ../../data/imagenet --test_phase -g 1 -e 32 --measure_latency --fuse_conv_bnEvaluate FasterNet models on COCO
masterYou can evaluate FasterNet models (e.g., FasterNet-S + Mask R-CNN) on the COCO
val2017dataset. Use./dist_test.shfor multi-GPU evaluation.To evaluate on a single node with 8 GPUs, use the following command structure:
bash ./dist_test.sh configs/fasternet/mask_rcnn_fasternet_s_fpn_1x_coco.py \ ckpts/mask_rcnn_fasternet_s_fpn_1x_coco_20221111_063419.pth 8 --eval bbox segmInstall FasterNet dependencies
masterTo set up the environment for FasterNet, create a new Conda environment with Python 3.9.12, clone the repository, and install the required packages via pip.
conda create -n fasternet python=3.9.12 -y conda activate fasternet git clone https://github.com/JierunChen/FasterNet cd FasterNet/ pip install -r requirements.txtTrain FasterNet + Mask R-CNN on COCO
masterTo train FasterNet-T0 + Mask R-CNN on the COCO
train2017dataset using 8 GPUs for 12 epochs, use the./dist_train.shscript. You can specify a work directory and provide a pretrained model via--cfg-options.CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 bash ./dist_train.sh \ configs/fasternet/mask_rcnn_fasternet_s_fpn_1x_coco.py 8 \ --work-dir work_dirs/mask_rcnn_fasternet_s_fpn_1x_coco/$(date +'%Y%m%d_%H%M%S') \ --cfg-options model.pretrained=../model_ckpt/fasternet_t0-epoch=281-val_acc1=71.9180.pthTrain FasterNet models
masterTrain FasterNet variants using
train_test.py. You must provide a configuration file via--cfgand specify the training directory for checkpoints.Key Flags:
-g: GPU IDs for training (e.g.,0,1,2,3,4,5,6,7).--num_nodes: Number of nodes used for training.-n: Number of workers.-b: Training batch size.-e: Number of epochs.--data_dir: Path to the ImageNet dataset.--model_ckpt_dir: Directory where model checkpoints will be saved.--cfg: Path to the model configuration YAML file.--wandb_project_name: Project name for Weights & Biases logging.
# Example training on an 8-GPU node python train_test.py -g 0,1,2,3,4,5,6,7 --num_nodes 1 -n 4 -b 4096 -e 2000 \ --data_dir ../../data/imagenet --pin_memory --wandb_project_name fasternet \ --model_ckpt_dir ./model_ckpt/$(date +'%Y%m%d_%H%M%S') --cfg cfg/fasternet_t0.yamlMeasure FLOPs and Throughput for FasterNet
masterTo analyze the performance of FasterNet models, you can measure FLOPs and GPU throughput using the provided scripts.
Measure FLOPs:
python get_flops.py configs/fasternet/mask_rcnn_fasternet_s_fpn_1x_coco.pyMeasure Throughput: Use
benchmark.pywithtorch.distributed.launch. Note that PConv and FasterNet use the"slicing"type for faster inference, which may involve computation inconsistencies with"split_cat"in certain implementations.CUDA_VISIBLE_DEVICES=0 python -m torch.distributed.launch --nproc_per_node=1 --master_port=29500 \ benchmark.py configs/fasternet/mask_rcnn_fasternet_s_fpn_1x_coco.py \ ckpts/mask_rcnn_fasternet_s_fpn_1x_coco_20221111_063419.pth --launcher pytorch --fuse-conv-bnInstall dependencies for COCO Object Detection
masterBefore installing detection dependencies, ensure you have already set up the dependencies for Image Classification. Then, install
mmcv-fullandmmdetusing the following commands:pip install mmcv-full==1.6.0 -f https://download.openmmlab.com/mmcv/dist/cu113/torch1.11.0/index.html pip install mmdet==2.25.0Reference: FasterNet COCO Results and Configs
masterThe following table summarizes the available FasterNet configurations and pre-trained models for Mask R-CNN on COCO:
Method Backbone Pretrain Lr schd Aug box AP mask AP Config Download Mask R-CNN FasterNet-S ImageNet-1K 1x No 39.9 36.9 config log & model Mask R-CNN FasterNet-M ImageNet-1K 1x No 43.0 39.1 config log & model Mask R-CNN FasterNet-L ImageNet-1K 1x No 44.0 39.9 config log & model