ConvNeXt V2 Documentation
repository·main·Indexed 24 days ago
https://github.com/facebookresearch/convnext-v2Official 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.
What's inside ConvNeXt V2
- 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.
Install ConvNeXt V2 dependencies
mainTo set up the environment for ConvNeXt V2 ImageNet classification experiments, create a new conda environment and install the required Python packages and system dependencies.
Create and activate environment:
conda create -n convnextv2 python=3.8 -y conda activate convnextv2Install 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.htmlInstall core packages: Clone the repository and install
timm,tensorboardX,six,submitit, andopenblas-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 -ySet up multi-node training with submitit
mainTo reproduce the results from the paper using a SLURM cluster, you can use multi-node training via the
submititlibrary. Ensure you have installedsubmititbefore running the training scripts.pip install submititPrepare ImageNet datasets
mainThe project requires specific directory structures for ImageNet datasets.
ImageNet-1K Structure
For classification experiments, organize the data into
trainandvalfolders, each containing subdirectories for every class:/path/to/imagenet-1k/ train/ class1/ img1.jpeg class2/ img2.jpeg val/ class1/ img3.jpeg class2/ img4.jpegImageNet-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.jpegEvaluate ConvNeXt V2 models
mainEvaluation can be performed using the
main_finetune.pyscript. You can run evaluation on a single GPU or across multiple GPUs usingtorch.distributed.launch.When evaluating different model variants, ensure you update the
--model,--resume(path to checkpoint), and--input_sizeflags. Note that while--drop_pathis required during training, it is not strictly required during evaluation as theDropPathmodule intimmbehaves 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 \Pre-train ConvNeXt V2 using FCMAE on ImageNet-1K
mainYou 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: Usemain_pretrain.pywithtorch.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_resultsTrain ConvNeXt V2 models
mainFor detailed instructions on both pre-training (self-supervised) and fine-tuning (supervised) ConvNeXt V2 models, refer to theTRAINING.mdfile.Install NVIDIA apex
mainInstall NVIDIA
apexfrom source with C++ and CUDA extensions enabled:git clone https://github.com/NVIDIA/apex cd apex pip install -v --disable-pip-version-check --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" ./ cd ..Install MinkowskiEngine with custom CUDA kernel
mainConvNeXt V2 requires a customized version of MinkowskiEngine that supports depth-wise convolutions. You must initialize submodules before installing.
Update submodules:
git submodule update --init --recursive git submodule update --recursive --remoteBuild and install: Navigate to the
MinkowskiEnginedirectory and install usingopenblas: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=openblasInstall ConvNeXt V2
mainRefer to theINSTALL.mdfile in the repository for detailed installation instructions.Fine-tune ConvNeXt V2 on ImageNet-1K
mainFine-tuning can be performed on a multi-node cluster using
submitit_finetune.pyor on a single machine usingmain_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_resultsFine-tune ConvNeXt V2-Atto and ConvNeXt V2-Tiny
mainSpecific 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