본문 바로가기
Deep learning/Computer Vision

MMpretrain custom dataset Train Base Line code _Tutorial 튜토리얼

by min0114 2024. 9. 20.

 

 

#mmpretrain

_base_ = [
    '../_base_/models/mae_vit-base-p16.py',
    '../_base_/datasets/imagenet_bs512_mae.py',
    '../_base_/default_runtime.py',
]


train_dataloader = dict(
    dataset=dict(
        type='CustomDataset',
        data_root='data/custom_dataset/',
        ann_file='',       # We assume you are using the sub-folder format without ann_file
        data_prefix='',    # The `data_root` is the data_prefix directly.
        with_label=False,
    )
)


# optimizer wrapper
optim_wrapper = dict(
    type='AmpOptimWrapper',
    loss_scale='dynamic',
    optimizer=dict(
        type='AdamW',
        lr=1.5e-4 * 4096 / 256,
        betas=(0.9, 0.95),
        weight_decay=0.05),
    paramwise_cfg=dict(
        custom_keys={
            'ln': dict(decay_mult=0.0),
            'bias': dict(decay_mult=0.0),
            'pos_embed': dict(decay_mult=0.),
            'mask_token': dict(decay_mult=0.),
            'cls_token': dict(decay_mult=0.)
        }))

# learning rate scheduler
param_scheduler = [
    dict(
        type='LinearLR',
        start_factor=0.0001,
        by_epoch=True,
        begin=0,
        end=40,
        convert_to_iter_based=True),
    dict(
        type='CosineAnnealingLR',
        T_max=260,
        by_epoch=True,
        begin=40,
        end=300,
        convert_to_iter_based=True)
]

# runtime settings
train_cfg = dict(type='EpochBasedTrainLoop', max_epochs=300)
default_hooks = dict(
    # only keeps the latest 3 checkpoints
    checkpoint=dict(type='CheckpointHook', interval=1, max_keep_ckpts=3))

randomness = dict(seed=0, diff_rank_seed=True)

# auto resume
resume = True

# NOTE: `auto_scale_lr` is for automatically scaling LR
# based on the actual training batch size.
auto_scale_lr = dict(base_batch_size=4096)

1. 상황에 따라 __base__ 써도되고 안되고 수정하기 나름

2. train pipe line 따로 설정해서 넣어도됨

self.train_pipeline = [
            dict(type='LoadImageFromFile'),
            dict(type='RandomResizedCrop', scale=192, backend='pillow', interpolation='bicubic'),
            dict(type='RandomFlip', prob=0.5, direction='horizontal'),
            dict(type='PackInputs')
        ]
        
 self.train_dataloader = dict(
            dataset=dict(
                type=self.dataset_type,
                data_root=r'D:\DATA\cl_sample',
                data_prefix='train',
                classes=['a', 'b', 'c'],
                pipeline=self.train_pipeline
            ),
            sampler=dict(type='DefaultSampler', shuffle=True),
            num_workers=2,
            batch_size=32,
            persistent_workers=True
        )

 

 

self.default_hooks = dict(
    # record the time of every iteration.
    timer=dict(type='IterTimerHook'),
    # print log every 100 iterations.
    logger=dict(type='LoggerHook', interval=100),
    # enable the parameter scheduler.
    param_scheduler=dict(type='ParamSchedulerHook'),
    # set sampler seed in distributed evrionment.
    sampler_seed=dict(type='DistSamplerSeedHook'),

    visualization=dict(type='VisualizationHook', enable=False),
    checkpoint=dict(type='CheckpointHook', interval=10, max_keep_ckpts=1, save_best='auto', rule='greater',
                    by_epoch=True),
    early_stopping=dict(type='EarlyStoppingHook', patience=10, monitor='accuracy', rule='greater')
)

3.defual_hooks-> early_stopping에서 자꾸 에러가 났는데init에 인자가 없다 에러뜸 

알고보니까 최신으로 변경되면서 인자가 바뀌어서임

monitor='accuracy'

monitor인자로 바꿔주면 해결됨

반응형

댓글