ConvNeXt V2 Documentation

repository·main·Indexed 24 days ago

https://github.com/facebookresearch/convnext-v2

Official PyTorch implementation of ConvNeXt V2, featuring a co-designed architecture with Global Response Normalization (GRN) and a Fully Convolutional Masked Autoencoder (FCMAE) framework. The library supports eight model scales: Atto, Femto, Pico, Nano, Tiny, Base, Large, and Huge. It includes instructions for installation, dataset preparation for ImageNet-1K and ImageNet-22K, and scripts for self-supervised pre-training and supervised fine-tuning using single-GPU, multi-GPU, or SLURM clusters.

Tokens
3.7K
Snippets
9
Records
14
Agent score
83%

What's inside ConvNeXt V2

  1. Overview of ConvNeXt V2

    main
    ConvNeXt V2 is a family of convolutional neural networks that incorporates a fully convolutional masked autoencoder framework (FCMAE) and a Global Response Normalization (GRN) layer to enhance inter-channel feature competition. The repository provides PyTorch implementations for 8 model scales: Atto, Femto, Pico, Nano, Tiny, Base, Large, and Huge.
  2. Install ConvNeXt V2 dependencies

    main

    To set up the environment for ConvNeXt V2 ImageNet classification experiments, create a new conda environment and install the required Python packages and system dependencies.

    1. Create and activate environment:

      conda create -n convnextv2 python=3.8 -y
      conda activate convnextv2
    2. Install PyTorch and torchvision: Ensure you install PyTorch >= 1.8.0 and torchvision >= 0.9.0. Example for CUDA 11.1:

      pip install torch==1.8.0+cu111 torchvision==0.9.0+cu111 -f https://download.pytorch.org/whl/torch_stable.html
    3. Install core packages: Clone the repository and install timm, tensorboardX, six, submitit, and openblas-devel:

      git clone https://github.com/facebookresearch/ConvNeXt-V2.git
      pip install timm==0.3.2 tensorboardX six
      pip install submitit
      conda install openblas-devel -c anaconda -y
    conda create -n convnextv2 python=3.8 -y
    conda activate convnextv2
    
    pip install torch==1.8.0+cu111 torchvision==0.9.0+cu111 -f https://download.pytorch.org/whl/torch_stable.html
    
    git clone https://github.com/facebookresearch/ConvNeXt-V2.git
    pip install timm==0.3.2 tensorboardX six
    pip install submitit
    conda install openblas-devel -c anaconda -y
  3. Prepare ImageNet datasets

    main

    The project requires specific directory structures for ImageNet datasets.

    ImageNet-1K Structure

    For classification experiments, organize the data into train and val folders, each containing subdirectories for every class:

    /path/to/imagenet-1k/
      train/
        class1/
          img1.jpeg
        class2/
          img2.jpeg
      val/
        class1/
          img3.jpeg
        class2/
          img4.jpeg

    ImageNet-22K Structure

    For pre-training, organize the data into class-named subdirectories directly under the root:

    /path/to/imagenet-22k/
      class1/
        img1.jpeg
      class2/
        img2.jpeg
      class3/
        img3.jpeg
      class4/
        img4.jpeg
  4. Evaluate ConvNeXt V2 models

    main

    Evaluation can be performed using the main_finetune.py script. You can run evaluation on a single GPU or across multiple GPUs using torch.distributed.launch.

    When evaluating different model variants, ensure you update the --model, --resume (path to checkpoint), and --input_size flags. Note that while --drop_path is required during training, it is not strictly required during evaluation as the DropPath module in timm behaves consistently.

    ### Single-GPU Evaluation
    ```bash
    python main_finetune.py \
    --model convnextv2_base \
    --eval true \
    --resume /path/to/checkpoint \
    --input_size 224 \
    --data_path /path/to/imagenet-1k \

    Multi-GPU Evaluation

    python -m torch.distributed.launch --nproc_per_node=8 main_finetune.py \
    --model convnextv2_base \
    --eval true \
    --resume /path/to/checkpoint \
    --input_size 224 \
    --data_path /path/to/imagenet-1k \
  5. Pre-train ConvNeXt V2 using FCMAE on ImageNet-1K

    main

    You can perform FCMAE pre-training on ImageNet-1K using either a multi-node SLURM cluster or a single machine with PyTorch distributed launch.

    Multi-node (SLURM): Use submitit_pretrain.py. Single-machine: Use main_pretrain.py with torch.distributed.launch.

    # Multi-node example
    python submitit_pretrain.py --nodes 8 --ngpus 8 \
    --model convnextv2_base \
    --batch_size 64 \
    --blr 1.5e-4 \
    --epochs 1600 \
    --warmup_epochs 40 \
    --data_path /path/to/imagenet-1k \
    --job_dir /path/to/save_results
    
    # Single-machine example
    python -m torch.distributed.launch --nproc_per_node=8 main_pretrain.py \
    --model convnextv2_base \
    --batch_size 64 --update_freq 8 \
    --blr 1.5e-4 \
    --epochs 1600 \
    --warmup_epochs 40 \
    --data_path /path/to/imagenet-1k \
    --output_dir /path/to/save_results
  6. Install MinkowskiEngine with custom CUDA kernel

    main

    ConvNeXt V2 requires a customized version of MinkowskiEngine that supports depth-wise convolutions. You must initialize submodules before installing.

    1. Update submodules:

      git submodule update --init --recursive
      git submodule update --recursive --remote
    2. Build and install: Navigate to the MinkowskiEngine directory and install using openblas:

      cd MinkowskiEngine
      python setup.py install --blas_include_dirs=${CONDA_PREFIX}/include --blas=openblas
    git submodule update --init --recursive
    git submodule update --recursive --remote
    cd MinkowskiEngine
    python setup.py install --blas_include_dirs=${CONDA_PREFIX}/include --blas=openblas
  7. Fine-tune ConvNeXt V2 on ImageNet-1K

    main

    Fine-tuning can be performed on a multi-node cluster using submitit_finetune.py or on a single machine using main_finetune.py.

    Key arguments for fine-tuning:

    • --finetune: Path to the checkpoint to start from.
    • --layer_decay_type: Type of layer decay (e.g., 'group' or 'single').
    • --layer_decay: The decay rate.
    • --model_ema / --model_ema_eval: Enable Exponential Moving Average for the model and evaluation.
    # Multi-node example (Base model)
    python submitit_finetune.py --nodes 4 --ngpus 8 \
    --model convnextv2_base \
    --batch_size 32 \
    --blr 6.25e-4 \
    --epochs 100 \
    --warmup_epochs 20 \
    --layer_decay_type 'group' \
    --layer_decay 0.6 \
    --weight_decay 0.05 \
    --drop_path 0.1 \
    --reprob 0.25 \
    --mixup 0.8 \
    --cutmix 1.0 \
    --smoothing 0.1 \
    --model_ema True --model_ema_eval True \
    --use_amp True \
    --finetune /path/to/checkpoint \
    --data_path /path/to/imagenet-1k \
    --job_dir /path/to/save_results
    
    # Single-machine example (Base model)
    python -m torch.distributed.launch --nproc_per_node=8 main_finetune.py \
    --model convnextv2_base \
    --batch_size 32 --update_freq 4 \
    --blr 6.25e-4 \
    --epochs 100 \
    --warmup_epochs 20 \
    --layer_decay_type 'group' \
    --layer_decay 0.6 \
    --weight_decay 0.05 \
    --drop_path 0.1 \
    --reprob 0.25 \
    --mixup 0.8 \
    --cutmix 1.0 \
    --smoothing 0.1 \
    --model_ema True --model_ema_eval True \
    --use_amp True \
    --finetune /path/to/checkpoint \
    --data_path /path/to/imagenet-1k \
    --output_dir /path/to/save_results
  8. Fine-tune ConvNeXt V2-Atto and ConvNeXt V2-Tiny

    main

    Specific training configurations are provided for the Atto and Tiny variants. Note that Atto uses 'single' layer decay and different augmentation settings (e.g., no mixup/cutmix), while Tiny uses 'single' layer decay and standard augmentations.

    # ConvNeXt V2-Atto (Multi-node)
    python submitit_finetune.py --nodes 4 --ngpus 8 \
    --model convnextv2_atto \
    --batch_size 32 \
    --blr 2e-4 \
    --epochs 600 \
    --warmup_epochs 0 \
    --layer_decay_type 'single' \
    --layer_decay 0.9 \
    --weight_decay 0.3 \
    --drop_path 0.1 \
    --reprob 0.25 \
    --mixup 0. \
    --cutmix 0. \
    --smoothing 0.2 \
    --model_ema True --model_ema_eval True \
    --use_amp True \
    --finetune /path/to/checkpoint \
    --data_path /path/to/imagenet-1k \
    --job_dir /path/to/save_results
    
    # ConvNeXt V2-Tiny (Single-machine)
    python -m torch.distributed.launch --nproc_per_node=8 main_finetune.py \
    --model convnextv2_tiny \
    --batch_size 32 --update_freq 4 \
    --blr 8e-4 \
    --epochs 300 \
    --warmup_epochs 40 \
    --layer_decay_type 'single' \
    --layer_decay 0.9 \
    --weight_decay 0.05 \
    --drop_path 0.2 \
    --reprob 0.25 \
    --mixup 0.8 \
    --cutmix 1.0 \
    --smoothing 0.1 \
    --model_ema True --model_ema_eval True \
    --finetune /path/to/checkpoint \
    --data_path /path/to/imagenet-1k \
    --output_dir /path/to/save_results