Newer
Older
Demo-Maker / modules / yolox / config / yolox_x_8x8_300e_coco-EARS-fine-tuning-test.py
# Basic Settings
default_scope = 'mmdet'
log_level = 'INFO'
load_from = 'checkpoints/yolox_x_Black_only.pth'  # Changed to YOLOX-X checkpoint
resume = False

# Environment Configuration
env_cfg = dict(
    cudnn_benchmark=False,
    dist_cfg=dict(backend='nccl'),
    mp_cfg=dict(mp_start_method='fork', opencv_num_threads=0)
)

# Dataset and Data Root Configuration
data_root = 'data/White-lined-designs-Test-COCO/'
dataset_type = 'CocoDataset'
class_name = ['stethoscope']
metainfo = dict(
    classes=class_name,
    palette=[(20, 220, 60)]
)

# Image Size Configuration
img_scale = (640, 480)
img_scales = [(640, 480)]

# Model Configuration
model = dict(
    type='YOLOX',
    data_preprocessor=dict(
        type='DetDataPreprocessor',
        pad_size_divisor=32,
        batch_augments=[
            dict(
                type='BatchSyncRandomResize',
                random_size_range=(480, 480),
                size_divisor=32,
                interval=10
            ),
        ],
    ),
    backbone=dict(
        type='CSPDarknet',
        deepen_factor=1.33,  # Increased from 1.0 for YOLOX-X
        widen_factor=1.25,   # Increased from 1.0 for YOLOX-X
        out_indices=(2, 3, 4),
        use_depthwise=False,
        spp_kernal_sizes=(5, 9, 13),
        norm_cfg=dict(type='BN', momentum=0.03, eps=0.001),
        act_cfg=dict(type='Swish'),
    ),
    neck=dict(
        type='YOLOXPAFPN',
        in_channels=[320, 640, 1280],  # Changed according to widen_factor
        out_channels=320,              # Changed according to widen_factor
        num_csp_blocks=4,             # Increased from 3 for YOLOX-X
        use_depthwise=False,
        upsample_cfg=dict(scale_factor=2, mode='nearest'),
        norm_cfg=dict(type='BN', momentum=0.03, eps=0.001),
        act_cfg=dict(type='Swish'),
    ),
    bbox_head=dict(
        type='YOLOXHead',
        num_classes=1,
        in_channels=320,             # Changed according to neck out_channels
        feat_channels=320,           # Changed according to neck out_channels
        strides=(8, 16, 32),
        stacked_convs=2,
        use_depthwise=False,
        norm_cfg=dict(type='BN', momentum=0.03, eps=0.001),
        act_cfg=dict(type='Swish'),
        loss_cls=dict(
            type='CrossEntropyLoss',
            use_sigmoid=True,
            reduction='sum',
            loss_weight=1.0
        ),
        loss_bbox=dict(
            type='IoULoss',
            mode='square',
            eps=1e-16,
            reduction='sum',
            loss_weight=5.0
        ),
        loss_obj=dict(
            type='CrossEntropyLoss',
            use_sigmoid=True,
            reduction='sum',
            loss_weight=1.0
        ),
        loss_l1=dict(type='L1Loss', reduction='sum', loss_weight=1.0),
    ),
    train_cfg=dict(assigner=dict(type='SimOTAAssigner', center_radius=2.5)),
    test_cfg=dict(score_thr=0.01, nms=dict(type='nms', iou_threshold=0.65))
)

# Training Configuration
max_epochs = 300
num_last_epochs = 15
base_lr = 0.01 * 64 / 128  # Adjusted for batch size
interval = 10

# Optimization Configuration
optim_wrapper = dict(
    type='OptimWrapper',
    optimizer=dict(
        type='SGD',
        lr=0.01 * 64 / 128,  # Adjusted for batch size
        momentum=0.9,
        weight_decay=0.0005,
        nesterov=True
    ),
    paramwise_cfg=dict(
        norm_decay_mult=0.0,
        bias_decay_mult=0.0
    )
)

# Learning Rate Schedule
param_scheduler = [
    dict(
        type='mmdet.QuadraticWarmupLR',
        by_epoch=True,
        begin=0,
        end=5,
        convert_to_iter_based=True
    ),
    dict(
        type='CosineAnnealingLR',
        T_max=285,
        eta_min=0.0005 * 64 / 128,  # Adjusted for batch size
        begin=5,
        end=285,
        by_epoch=True,
        convert_to_iter_based=True
    ),
    dict(
        type='ConstantLR',
        factor=1,
        begin=285,
        end=300,
        by_epoch=True
    ),
]

# Custom Hooks Configuration
custom_hooks = [
    dict(
        type='YOLOXModeSwitchHook',
        num_last_epochs=15,
        priority=48
    ),
    dict(
        type='SyncNormHook',
        priority=48
    ),
    dict(
        type='EMAHook',
        ema_type='ExpMomentumEMA',
        momentum=0.0001,
        update_buffers=True,
        priority=49
    ),
]

# Default Hooks Configuration
default_hooks = dict(
    timer=dict(type='IterTimerHook'),
    logger=dict(type='LoggerHook', interval=50),
    param_scheduler=dict(type='ParamSchedulerHook'),
    checkpoint=dict(type='CheckpointHook', interval=10, max_keep_ckpts=3),
    sampler_seed=dict(type='DistSamplerSeedHook'),
    visualization=dict(type='DetVisualizationHook')
)

# Data Loading Configuration
train_dataloader = dict(
    batch_size=8,  # Might need to reduce depending on GPU memory
    num_workers=4,
    persistent_workers=True,
    sampler=dict(type='DefaultSampler', shuffle=True),
    dataset=dict(
        type='MultiImageMixDataset',
        dataset=dict(
            type='CocoDataset',
            data_root=data_root,
            ann_file='train/annotations/instances_default.json',
            data_prefix=dict(img='train/images/'),
            metainfo=metainfo,
            filter_cfg=dict(filter_empty_gt=False, min_size=32),
            pipeline=[
                dict(type='LoadImageFromFile', backend_args=None),
                dict(type='LoadAnnotations', with_bbox=True),
            ],
            backend_args=None
        ),
        pipeline=[
            dict(type='Mosaic', img_scale=(640, 480), pad_val=114.0),
            dict(
                type='RandomAffine',
                scaling_ratio_range=(0.1, 2),
                border=(-320, -240)
            ),
            dict(
                type='MixUp',
                img_scale=(640, 480),
                ratio_range=(0.8, 1.6),
                pad_val=114.0
            ),
            dict(type='YOLOXHSVRandomAug'),
            dict(type='RandomFlip', prob=0.5),
            dict(type='Resize', scale=(640, 480), keep_ratio=True),
            dict(
                type='Pad',
                pad_to_square=True,
                pad_val=dict(img=(114.0, 114.0, 114.0))
            ),
            dict(
                type='FilterAnnotations',
                min_gt_bbox_wh=(1, 1),
                keep_empty=False
            ),
            dict(type='PackDetInputs')
        ]
    )
)

# Validation Configuration
val_dataloader = dict(
    batch_size=8,  # Might need to reduce depending on GPU memory
    num_workers=4,
    persistent_workers=True,
    drop_last=False,
    sampler=dict(type='DefaultSampler', shuffle=False),
    dataset=dict(
        type='CocoDataset',
        data_root=data_root,
        ann_file='valid/annotations/instances_default.json',
        data_prefix=dict(img='valid/images/'),
        test_mode=True,
        metainfo=metainfo,
        pipeline=[
            dict(type='LoadImageFromFile', backend_args=None),
            dict(type='Resize', scale=(640, 480), keep_ratio=True),
            dict(
                type='Pad',
                pad_to_square=True,
                pad_val=dict(img=(114.0, 114.0, 114.0))
            ),
            dict(type='LoadAnnotations', with_bbox=True),
            dict(
                type='PackDetInputs',
                meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')
            )
        ]
    )
)

# Test Configuration
test_dataloader = val_dataloader

# Metrics and Evaluation Configuration
val_evaluator = dict(
    type='CocoMetric',
    ann_file=data_root + 'valid/annotations/instances_default.json',
    metric='bbox',
    backend_args=None
)
test_evaluator = val_evaluator

# Training and Testing Loops Configuration
train_cfg = dict(
    type='EpochBasedTrainLoop',
    max_epochs=max_epochs,
    val_interval=10
)
val_cfg = dict(type='ValLoop')
test_cfg = dict(type='TestLoop')

# Test Time Augmentation Configuration
tta_model = dict(
    type='DetTTAModel',
    tta_cfg=dict(nms=dict(type='nms', iou_threshold=0.65), max_per_img=100)
)

tta_pipeline = [
    dict(type='LoadImageFromFile', backend_args=None),
    dict(
        type='TestTimeAug',
        transforms=[
            [
                dict(type='Resize', scale=(640, 480), keep_ratio=True),
                dict(type='Resize', scale=(320, 240), keep_ratio=True),
                dict(type='Resize', scale=(960, 720), keep_ratio=True),
            ],
            [
                dict(type='RandomFlip', prob=1.0),
                dict(type='RandomFlip', prob=0.0),
            ],
            [
                dict(type='Pad', pad_to_square=True, pad_val=dict(img=(114.0, 114.0, 114.0))),
            ],
            [
                dict(type='LoadAnnotations', with_bbox=True),
            ],
            [
                dict(
                    type='PackDetInputs',
                    meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor', 'flip', 'flip_direction')
                ),
            ]
        ]
    )
]

# Visualization Configuration
vis_backends = [dict(type='LocalVisBackend')]
visualizer = dict(
    type='DetLocalVisualizer',
    vis_backends=vis_backends,
    name='visualizer'
)

# Auto Scale Learning Rate Configuration
auto_scale_lr = dict(enable=False, base_batch_size=64)
log_processor = dict(type='LogProcessor', window_size=50, by_epoch=True)