Skip to content
Open

Cspnext #1980

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
339 changes: 339 additions & 0 deletions README-mmpretrain.md

Large diffs are not rendered by default.

342 changes: 5 additions & 337 deletions README.md

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions configs/cspnext/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# CSPNeXt ImageNet Pre-training

In this folder, we provide the imagenet pre-training config of RTMDet's backbone CSPNeXt.

## Requirements

To train with these configs, please install [MMClassification 1.x](https://github.com/open-mmlab/mmclassification/tree/1.x) first.

Install by MIM:

```shell
mim install mmcls>=1.0.0rc0
```

or install by pip:

```shell
pip install mmcls>=1.0.0rc0
```

## Prepare Dataset

To pre-train on ImageNet, you need to prepare the dataset first. Please refer to the [guide](https://mmclassification.readthedocs.io/en/1.x/user_guides/dataset_prepare.html#imagenet).

## How to Train

You can use the classification config in the same way as the detection config.

For single-GPU training, run:

```shell
python tools/train.py \
${CONFIG_FILE} \
[optional arguments]
```

For multi-GPU training, run:

```shell
bash ./tools/dist_train.sh \
${CONFIG_FILE} \
${GPU_NUM} \
[optional arguments]
```

More details can be found in [user guides](https://mmdetection.readthedocs.io/en/3.x/user_guides/train.html).

## Results and Models

| Model | resolution | Params(M) | Flops(G) | Top-1 (%) | Top-5 (%) | Download |
| :----------: | :--------: | :-------: | :------: | :-------: | :-------: | :-----------------------------------------------------------------------------------------------------------------: |
| CSPNeXt-tiny | 224x224 | 2.73 | 0.339 | 69.44 | 89.45 | [model](https://download.openmmlab.com/mmdetection/v3.0/rtmdet/cspnext_rsb_pretrain/cspnext-tiny_imagenet_600e.pth) |
| CSPNeXt-s | 224x224 | 4.89 | 0.664 | 74.41 | 92.23 | [model](https://download.openmmlab.com/mmdetection/v3.0/rtmdet/cspnext_rsb_pretrain/cspnext-s_imagenet_600e.pth) |
107 changes: 107 additions & 0 deletions configs/cspnext/cpsnext-tiny-600e_in100.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
_base_ = './cspnext-s_8xb256-rsb-a1-600e_in1k.py'
work_dir = "./work_dir/cspnext-tiny-600e_in100"
# act_cfg = dict(type='ReLU6', inplace=True)

model = dict(
data_preprocessor=dict(
num_classes=100, # ImageNet-100 이므로 100으로 설정
),
backbone=dict(deepen_factor=0.167, widen_factor=0.375),
head=dict(in_channels=384,
num_classes=100))

train_pipeline = [
dict(type='LoadImageFromFile'),
dict(type='RandomResizedCrop', scale=224),
dict(type='RandomFlip', prob=0.5, direction='horizontal'),
dict(type='PackInputs'), # <--- [필수]
]

test_pipeline = [
dict(type='LoadImageFromFile'),
dict(type='ResizeEdge', scale=256, edge='short'),
dict(type='CenterCrop', crop_size=224),
dict(type='PackInputs'), # <--- [필수]
]

train_dataloader = dict(
batch_size=384,
num_workers=12,
dataset=dict(
type='ImageNet',
data_root='/mnt/hdd/OpenDataLab___ImageNet-100/raw/MyImagenet',
ann_file='meta/train.txt',
data_prefix='train/',
pipeline=train_pipeline), # <--- [추가]
sampler=dict(type='RepeatAugSampler', shuffle=True)
)

val_dataloader = dict(
batch_size=384,
num_workers=12,
dataset=dict(
type='ImageNet',
data_root='/mnt/hdd/OpenDataLab___ImageNet-100/raw/MyImagenet',
ann_file='meta/val.txt',
data_prefix='val/',
pipeline=test_pipeline), # <--- [추가]
sampler=dict(type='DefaultSampler', shuffle=False)
)
val_cfg = dict()

# 2. 평가 지표 설정 (정확도 Top-1, Top-5 측정)
val_evaluator = dict(type='Accuracy', topk=(1, 5))

# test_dataloader = val_dataloader
# test_evaluator = val_evaluator

vis_backends = [
dict(type='LocalVisBackend'),
dict(
type='WandbVisBackend',
init_kwargs=dict(
project='csp', # WandB 프로젝트 이름
name='CSPNeXt-tiny-default', # (선택) 실험 이름
# entity='my-username' # (선택) 팀/유저 이름
)
)
]

visualizer = dict(
type='UniversalVisualizer', # <--- [중요] mmpretrain 전용 비주얼라이저 사용
vis_backends=vis_backends
)

default_hooks = dict(
# 기존 설정 유지
timer=dict(type='IterTimerHook'),
logger=dict(type='LoggerHook', interval=50),
param_scheduler=dict(type='ParamSchedulerHook'),
sampler_seed=dict(type='DistSamplerSeedHook'),

# [수정 1] Segmentation 전용 훅 제거 -> 일반 VisualizationHook 사용
visualization=dict(type='VisualizationHook', enable=True),

# [수정 2] CheckpointHook의 저장 기준 변경
checkpoint=dict(
type='CheckpointHook',
interval=1, # 1 Epoch마다 저장 시도
save_best='accuracy/top1', # [변경] Segmentation(mIoU) -> Classification(Top-1 Accuracy)
rule='greater', # 정확도는 높을수록 좋으므로 greater
max_keep_ckpts=3, # Best 모델 최대 3개 유지 (주석에 따라 1로 줄여도 됨)
save_last=True # 가장 마지막 Epoch 모델 별도 저장
)
)

optim_wrapper = dict(
type='OptimWrapper', # 기본 OptimWrapper 사용
optimizer=dict(
type='Lamb', # 여기에 반드시 type이 있어야 합니다.
lr=0.005,
weight_decay=0.02
),
paramwise_cfg=dict(
bias_decay_mult=0.0,
norm_decay_mult=0.0
)
)
67 changes: 67 additions & 0 deletions configs/cspnext/cspnext-s_8xb256-rsb-a1-600e_in1k.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
_base_ = [
'mmcls::_base_/datasets/imagenet_bs256_rsb_a12.py',
'mmcls::_base_/schedules/imagenet_bs2048_rsb.py',
'mmcls::_base_/default_runtime.py'
]
default_scope = 'mmpretrain'
# custom_imports = dict(
# imports=['mmdet.models', 'mmyolo.models'], allow_failed_imports=False)

model = dict(
type='ImageClassifier',
backbone=dict(
type='CSPNeXt',
arch='P5',
out_indices=(4, ),
expand_ratio=0.5,
deepen_factor=0.33,
widen_factor=0.5,
channel_attention=True,
norm_cfg=dict(type='BN'),
act_cfg=dict(type='SiLU')),
neck=dict(type='GlobalAveragePooling'),
head=dict(
type='LinearClsHead',
num_classes=1000,
in_channels=512,
loss=dict(
type='LabelSmoothLoss',
label_smooth_val=0.1,
mode='original',
loss_weight=1.0),
topk=(1, 5)),
train_cfg=dict(augments=[
dict(type='Mixup', alpha=0.2),
dict(type='CutMix', alpha=1.0)
]))

# dataset settings
train_dataloader = dict(sampler=dict(type='RepeatAugSampler', shuffle=True))

# schedule settings
optim_wrapper = dict(
optimizer=dict(weight_decay=0.01),
paramwise_cfg=dict(bias_decay_mult=0., norm_decay_mult=0.),
)

param_scheduler = [
# warm up learning rate scheduler
dict(
type='LinearLR',
start_factor=0.0001,
by_epoch=True,
begin=0,
end=5,
# update by iter
convert_to_iter_based=True),
# main learning rate scheduler
dict(
type='CosineAnnealingLR',
T_max=595,
eta_min=1.0e-6,
by_epoch=True,
begin=5,
end=600)
]

train_cfg = dict(by_epoch=True, max_epochs=600)
5 changes: 5 additions & 0 deletions configs/cspnext/cspnext-tiny_8xb256-rsb-a1-600e_in1k.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
_base_ = './cspnext-s_8xb256-rsb-a1-600e_in1k.py'

model = dict(
backbone=dict(deepen_factor=0.167, widen_factor=0.375),
head=dict(in_channels=384))
2 changes: 2 additions & 0 deletions mmpretrain/models/backbones/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
from .vit_eva02 import ViTEVA02
from .vit_sam import ViTSAM
from .xcit import XCiT
from .cspnext import CSPNeXt

__all__ = [
'LeNet5',
Expand Down Expand Up @@ -126,4 +127,5 @@
'HiViT',
'SparseResNet',
'SparseConvNeXt',
'CSPNeXt',
]
Loading