FasterNet

repository·master·Indexed 21 days ago

https://github.com/jierunchen/fasternet

A 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.

Tokens
2K
Snippets
7
Records
10
Agent score
24%

What's inside FasterNet

  1. Prepare ImageNet-1K dataset

    master

    FasterNet requires the ImageNet-1K classification dataset. Ensure your data is structured into train and val directories, 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.jpeg
  2. Evaluate FasterNet models

    master

    Use train_test.py to evaluate pre-trained models. You must specify the configuration file via -c and 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 .pth model checkpoint.
    • --data_dir: Path to the ImageNet dataset.
    • --test_phase: Enables evaluation mode.
    • -g: GPU IDs (e.g., 1 or a list like 0,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_latency and --fuse_conv_bn flags.

    # 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_bn
  3. Evaluate FasterNet models on COCO

    master

    You can evaluate FasterNet models (e.g., FasterNet-S + Mask R-CNN) on the COCO val2017 dataset. Use ./dist_test.sh for 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 segm
  4. Install FasterNet dependencies

    master

    To 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.txt
  5. Train FasterNet + Mask R-CNN on COCO

    master

    To train FasterNet-T0 + Mask R-CNN on the COCO train2017 dataset using 8 GPUs for 12 epochs, use the ./dist_train.sh script. 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.pth 
  6. Train FasterNet models

    master

    Train FasterNet variants using train_test.py. You must provide a configuration file via --cfg and 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.yaml
  7. Measure FLOPs and Throughput for FasterNet

    master

    To 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.py

    Measure Throughput: Use benchmark.py with torch.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-bn