# 需要修改的参数
img_size = 480
class_name = ['fuqi','ok']
num_classes = len(class_name)
data_root = '/home/apulis-test/teamdata/yz_dataset/fuqi'
max_epochs=300
val_interval=1
train_batch_size = 16
val_batch_size = 1
load_from = "swin_tiny_224_b16x64_300e_imagenet_20210616_090925-66df6be6.pth"
dataset_type = 'CustomDataset'
#############################auto_scale_lr = dict(base_batch_size=1024)
bgr_mean = [103.53,116.28,123.675,
]
bgr_std = [57.375,57.12,58.395,
]
data_preprocessor = dict(mean=[123.675,116.28,103.53,],num_classes=num_classes,std=[58.395,57.12,57.375,],to_rgb=True)
default_hooks = dict(checkpoint=dict(interval=1, type='CheckpointHook'),logger=dict(interval=1, type='LoggerHook'),param_scheduler=dict(type='ParamSchedulerHook'),sampler_seed=dict(type='DistSamplerSeedHook'),timer=dict(type='IterTimerHook'),visualization=dict(enable=False, type='VisualizationHook'))
default_scope = 'mmpretrain'
env_cfg = dict(cudnn_benchmark=False,dist_cfg=dict(backend='nccl'),mp_cfg=dict(mp_start_method='fork', opencv_num_threads=0))
launcher = 'none'
log_level = 'INFO'model = dict(backbone=dict(arch='tiny', drop_path_rate=0.2, img_size=img_size, type='SwinTransformer'),head=dict(cal_acc=False,in_channels=768,init_cfg=None,loss=dict(label_smooth_val=0.1, mode='original', type='LabelSmoothLoss'),num_classes=num_classes,type='LinearClsHead'),init_cfg=[dict(bias=0.0, layer='Linear', std=0.02, type='TruncNormal'),dict(bias=0.0, layer='LayerNorm', type='Constant', val=1.0),],neck=dict(type='GlobalAveragePooling'),train_cfg=dict(augments=[dict(alpha=0.8, type='Mixup'),dict(alpha=1.0, type='CutMix'),]),type='ImageClassifier')
optim_wrapper = dict(clip_grad=dict(max_norm=5.0),optimizer=dict(betas=(0.9,0.999,),eps=1e-08,lr=0.001,type='AdamW',weight_decay=0.05),paramwise_cfg=dict(bias_decay_mult=0.0,custom_keys=dict({'.absolute_pos_embed': dict(decay_mult=0.0),'.relative_position_bias_table': dict(decay_mult=0.0)}),flat_decay_mult=0.0,norm_decay_mult=0.0))
param_scheduler = [dict(by_epoch=True,convert_to_iter_based=True,end=20,start_factor=0.001,type='LinearLR'),dict(begin=20, by_epoch=True, eta_min=1e-05, type='CosineAnnealingLR'),
]
randomness = dict(deterministic=False, seed=None)
resume = False
test_cfg = dict()
test_dataloader = dict(batch_size=val_batch_size,collate_fn=dict(type='default_collate'),dataset=dict(data_root=data_root,data_prefix = 'val',with_label = True,classes = class_name,pipeline=[dict(type='LoadImageFromFile'),dict(backend='pillow',edge='short',interpolation='bicubic',scale=256,type='ResizeEdge'),dict(crop_size=img_size, type='CenterCrop'),dict(type='PackInputs'),],type=dataset_type),num_workers=5,persistent_workers=True,pin_memory=True,sampler=dict(shuffle=False, type='DefaultSampler'))
test_evaluator = dict(topk=(1,num_classes,), type='Accuracy')
test_pipeline = [dict(type='LoadImageFromFile'),dict(backend='pillow',edge='short',interpolation='bicubic',scale=256,type='ResizeEdge'),dict(crop_size=img_size, type='CenterCrop'),dict(type='PackInputs'),
]
train_cfg = dict(by_epoch=True, max_epochs=max_epochs, val_interval=val_interval)
train_dataloader = dict(batch_size=train_batch_size,collate_fn=dict(type='default_collate'),dataset=dict(data_root=data_root,data_prefix = 'train',with_label = True,classes = class_name,pipeline=[dict(type='LoadImageFromFile'),dict(backend='pillow',interpolation='bicubic',scale=img_size,type='RandomResizedCrop'),dict(direction='horizontal', prob=0.5, type='RandomFlip'),dict(hparams=dict(interpolation='bicubic', pad_val=[104,116,124,]),magnitude_level=9,magnitude_std=0.5,num_policies=2,policies='timm_increasing',total_level=10,type='RandAugment'),dict(erase_prob=0.25,fill_color=[103.53,116.28,123.675,],fill_std=[57.375,57.12,58.395,],max_area_ratio=0.3333333333333333,min_area_ratio=0.02,mode='rand',type='RandomErasing'),dict(type='PackInputs'),],type=dataset_type),num_workers=5,persistent_workers=True,pin_memory=True,sampler=dict(shuffle=True, type='DefaultSampler'))
train_pipeline = [dict(type='LoadImageFromFile'),dict(backend='pillow',interpolation='bicubic',scale=img_size,type='RandomResizedCrop'),dict(direction='horizontal', prob=0.5, type='RandomFlip'),dict(hparams=dict(interpolation='bicubic', pad_val=[104,116,124,]),magnitude_level=9,magnitude_std=0.5,num_policies=2,policies='timm_increasing',total_level=10,type='RandAugment'),dict(erase_prob=0.25,fill_color=[103.53,116.28,123.675,],fill_std=[57.375,57.12,58.395,],max_area_ratio=0.3333333333333333,min_area_ratio=0.02,mode='rand',type='RandomErasing'),dict(type='PackInputs'),
]
val_cfg = dict()
val_dataloader = dict(batch_size=val_batch_size,collate_fn=dict(type='default_collate'),dataset=dict(data_root=data_root,data_prefix = 'val',with_label = True,classes = class_name,pipeline=[dict(type='LoadImageFromFile'),dict(backend='pillow',edge='short',interpolation='bicubic',scale=256,type='ResizeEdge'),dict(crop_size=img_size, type='CenterCrop'),dict(type='PackInputs'),],type=dataset_type),num_workers=5,persistent_workers=True,pin_memory=True,sampler=dict(shuffle=False, type='DefaultSampler'))
val_evaluator = dict(topk=(1,num_classes,), type='Accuracy')
vis_backends = [dict(type='LocalVisBackend'),
]
visualizer = dict(type='UniversalVisualizer', vis_backends=[dict(type='LocalVisBackend'),])
需要注意的问题如下:
# 安装:
conda create --name openmmlab python=3.8 -y
conda activate openmmlab
conda install pytorch torchvision -c pytorch
git clone https://github.com/open-mmlab/mmpretrain.git
cd mmpretrain
pip install -U openmim && mim install -e .Package Version Location
---------------------- --------------- ------------------------------------------------------------
addict 2.4.0
albumentations 1.1.0
aliyun-python-sdk-core 2.16.0
aliyun-python-sdk-kms 2.16.5
certifi 2021.5.30
cffi 1.17.1
charset-normalizer 3.4.0
click 8.1.7
colorama 0.4.6
crcmod 1.7
cryptography 43.0.3
cycler 0.10.0
Cython 0.29.24
efficientnet-pytorch 0.7.1
einops 0.8.0
filelock 3.14.0
flatbuffers 2.0
idna 3.10
imageio 2.13.1
importlib-metadata 8.5.0
instaboostfast 0.1.2
jmespath 0.10.0
joblib 1.1.0
kiwisolver 1.3.2
Markdown 3.7
markdown-it-py 3.0.0
mat4py 0.6.0
matplotlib 3.4.3
mdurl 0.1.2
mkl-fft 1.3.0
mkl-random 1.2.2
mkl-service 2.4.0
mmcv 2.1.0
mmcv-full 1.4.0
mmengine 0.10.5
mmpretrain 1.2.0 /home/apulis-test/userdata/code/mycode/mmclassification-main
model-index 0.1.11
modelindex 0.0.2
networkx 2.6.3
numpy 1.22.3
olefile 0.46
onnx 1.11.0
onnxruntime-gpu 1.11.1
opencv-python 4.5.3.56
opencv-python-headless 4.5.4.60
opendatalab 0.0.10
openmim 0.3.9
openxlab 0.1.2
ordered-set 4.1.0
oss2 2.17.0
packaging 24.2
pandas 2.0.3
Pillow 8.4.0
pip 21.0.1
protobuf 3.20.1
pycocotools 2.0.3
pycparser 2.22
pycryptodome 3.21.0
pygments 2.18.0
pyparsing 2.4.7
python-dateutil 2.8.2
pytz 2023.4
PyWavelets 1.2.0
PyYAML 6.0
qudida 0.0.4
requests 2.28.2
rich 13.4.2
scikit-image 0.18.3
scikit-learn 1.0.1
scipy 1.7.3
setuptools 60.2.0
six 1.16.0
some-package 0.1
tabulate 0.9.0
termcolor 2.4.0
terminaltables 3.1.0
threadpoolctl 3.0.0
tifffile 2021.11.2
timm 0.4.12
torch 1.8.0
torchaudio 0.8.0a0+a751e1d
torchvision 0.9.0
tqdm 4.65.2
typing-extensions 4.12.2
tzdata 2024.2
urllib3 1.26.20
wheel 0.37.0
yacs 0.1.8
yapf 0.31.0
zipp 3.20.2# 验证是否安装成功
python demo/image_demo.py demo/demo.JPEG resnet18_8xb32_in1k --device cpu# 训练:
python tools/train.py configs/swin_transformer/yz.py
# 多卡训练
bash ./tools/dist_train.sh configs/swin_transformer/yz.py# 评估
python tools/test.py configs/swin_transformer/yz.py work_dirs/yz/epoch_300.pth# 推理:
python demo/image_demo.py configs/swin_transformer/yz.py work_dirs/yz/epoch_300.pth