open-mmlab / mmdetection

OpenMMLab Detection Toolbox and Benchmark
https://mmdetection.readthedocs.io
Apache License 2.0
29.63k stars 9.47k forks source link

AttributeError: class `EpochBasedTrainLoop` in mmengine/runner/loops.py: class `MultiImageMixDataset` in mmdet/datasets/dataset_wrappers.py: class `CocoDataset` in mmdet/datasets/coco.py: 'str' object has no attribute 'items' #10159

Open hsp2454 opened 1 year ago

hsp2454 commented 1 year ago

When I run 'python tools\train.py configs\yolox\yolox_x_8xb8-300e_coco.py', it reported the following error,My detection class is one, which I have set in 'mmdet\datasets\coco.py':

METAINFO = { 
        'classes':('pig',),
        'palette':[(220,60,20)]
}

But it still gives an error when I run.

Traceback (most recent call last):
  File "E:\software\Anaconda\envs\mmyolo\lib\site-packages\mmengine\registry\build_functions.py", line 121, in build_from_cfg
    obj = obj_cls(**args)  # type: ignore
  File "E:\software\Anaconda\envs\mmyolo\lib\site-packages\mmdet-3.0.0rc6-py3.9.egg\mmdet\datasets\base_det_dataset.py", line 33, in __init__
    super().__init__(*args, **kwargs)
  File "E:\software\Anaconda\envs\mmyolo\lib\site-packages\mmengine\dataset\base_dataset.py", line 244, in __init__
    self._join_prefix()
  File "E:\software\Anaconda\envs\mmyolo\lib\site-packages\mmengine\dataset\base_dataset.py", line 546, in _join_prefix
    for data_key, prefix in self.data_prefix.items():
AttributeError: 'str' object has no attribute 'items'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:\software\Anaconda\envs\mmyolo\lib\site-packages\mmengine\registry\build_functions.py", line 121, in build_from_cfg
    obj = obj_cls(**args)  # type: ignore
  File "E:\software\Anaconda\envs\mmyolo\lib\site-packages\mmdet-3.0.0rc6-py3.9.egg\mmdet\datasets\dataset_wrappers.py", line 62, in __init__
    self.dataset = DATASETS.build(dataset)
  File "E:\software\Anaconda\envs\mmyolo\lib\site-packages\mmengine\registry\registry.py", line 545, in build
    return self.build_func(cfg, *args, **kwargs, registry=self)
  File "E:\software\Anaconda\envs\mmyolo\lib\site-packages\mmengine\registry\build_functions.py", line 135, in build_from_cfg
    raise type(e)(
AttributeError: class `CocoDataset` in mmdet/datasets/coco.py: 'str' object has no attribute 'items'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:\software\Anaconda\envs\mmyolo\lib\site-packages\mmengine\registry\build_functions.py", line 121, in build_from_cfg
    obj = obj_cls(**args)  # type: ignore
  File "E:\software\Anaconda\envs\mmyolo\lib\site-packages\mmengine\runner\loops.py", line 44, in __init__
    super().__init__(runner, dataloader)
  File "E:\software\Anaconda\envs\mmyolo\lib\site-packages\mmengine\runner\base_loop.py", line 26, in __init__
    self.dataloader = runner.build_dataloader(
  File "E:\software\Anaconda\envs\mmyolo\lib\site-packages\mmengine\runner\runner.py", line 1346, in build_dataloader
    dataset = DATASETS.build(dataset_cfg)
  File "E:\software\Anaconda\envs\mmyolo\lib\site-packages\mmengine\registry\registry.py", line 545, in build
    return self.build_func(cfg, *args, **kwargs, registry=self)
  File "E:\software\Anaconda\envs\mmyolo\lib\site-packages\mmengine\registry\build_functions.py", line 135, in build_from_cfg
    raise type(e)(
AttributeError: class `MultiImageMixDataset` in mmdet/datasets/dataset_wrappers.py: class `CocoDataset` in mmdet/datasets/coco.py: 'str' object has no attribute 'items'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Code\mmdetection-3.0.0rc6\tools\train.py", line 124, in <module>
    main()
  File "D:\Code\mmdetection-3.0.0rc6\tools\train.py", line 120, in main
    runner.train()
  File "E:\software\Anaconda\envs\mmyolo\lib\site-packages\mmengine\runner\runner.py", line 1672, in train
    self._train_loop = self.build_train_loop(
  File "E:\software\Anaconda\envs\mmyolo\lib\site-packages\mmengine\runner\runner.py", line 1464, in build_train_loop
    loop = LOOPS.build(
  File "E:\software\Anaconda\envs\mmyolo\lib\site-packages\mmengine\registry\registry.py", line 545, in build
    return self.build_func(cfg, *args, **kwargs, registry=self)
  File "E:\software\Anaconda\envs\mmyolo\lib\site-packages\mmengine\registry\build_functions.py", line 135, in build_from_cfg
    raise type(e)(
AttributeError: class `EpochBasedTrainLoop` in mmengine/runner/loops.py: class `MultiImageMixDataset` in mmdet/datasets/dataset_wrappers.py: class `CocoDataset` in mmdet/datasets/coco.py: 'str' object has no attribute 'items'

Below is my configuration file, my version of mmdet is 3.0.0rc6.

Config:
train_cfg = dict(type='EpochBasedTrainLoop', max_epochs=300, val_interval=10)
val_cfg = dict(type='ValLoop')
test_cfg = dict(type='TestLoop')
param_scheduler = [
    dict(
        type='mmdet.QuadraticWarmupLR',
        by_epoch=True,
        begin=0,
        end=5,
        convert_to_iter_based=True),
    dict(
        type='CosineAnnealingLR',
        eta_min=0.0005,
        begin=5,
        T_max=285,
        end=285,
        by_epoch=True,
        convert_to_iter_based=True),
    dict(type='ConstantLR', by_epoch=True, factor=1, begin=285, end=300)
]
optim_wrapper = dict(
    type='OptimWrapper',
    optimizer=dict(
        type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0005, nesterov=True),
    paramwise_cfg=dict(norm_decay_mult=0.0, bias_decay_mult=0.0))
auto_scale_lr = dict(enable=False, base_batch_size=64)
default_scope = 'mmdet'
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'))
env_cfg = dict(
    cudnn_benchmark=False,
    mp_cfg=dict(mp_start_method='fork', opencv_num_threads=0),
    dist_cfg=dict(backend='nccl'))
vis_backends = [dict(type='LocalVisBackend')]
visualizer = dict(
    type='DetLocalVisualizer',
    vis_backends=[dict(type='LocalVisBackend')],
    name='visualizer')
log_processor = dict(type='LogProcessor', window_size=50, by_epoch=True)
log_level = 'INFO'
load_from = None
resume = False
tta_model = dict(
    type='DetTTAModel',
    tta_cfg=dict(nms=dict(type='nms', iou_threshold=0.65), max_per_img=100))
img_scales = [(640, 640), (320, 320), (960, 960)]
tta_pipeline = [
    dict(type='LoadImageFromFile', file_client_args=dict(backend='disk')),
    dict(
        type='TestTimeAug',
        transforms=[[{
            'type': 'Resize',
            'scale': (640, 640),
            'keep_ratio': True
        }, {
            'type': 'Resize',
            'scale': (320, 320),
            'keep_ratio': True
        }, {
            'type': 'Resize',
            'scale': (960, 960),
            'keep_ratio': True
        }],
                    [{
                        'type': 'RandomFlip',
                        'prob': 1.0
                    }, {
                        'type': 'RandomFlip',
                        'prob': 0.0
                    }],
                    [{
                        'type': 'Pad',
                        'pad_to_square': True,
                        'pad_val': {
                            'img': (114.0, 114.0, 114.0)
                        }
                    }], [{
                        'type': 'LoadAnnotations',
                        'with_bbox': True
                    }],
                    [{
                        'type':
                        'PackDetInputs',
                        'meta_keys':
                        ('img_id', 'img_path', 'ori_shape', 'img_shape',
                         'scale_factor', 'flip', 'flip_direction')
                    }]])
]
img_scale = (640, 640)
metainfo = dict(classes=('pig', ), palette=[(220, 20, 60)])
model = dict(
    type='YOLOX',
    data_preprocessor=dict(
        type='DetDataPreprocessor',
        pad_size_divisor=32,
        batch_augments=[
            dict(
                type='BatchSyncRandomResize',
                random_size_range=(480, 800),
                size_divisor=32,
                interval=10)
        ]),
    backbone=dict(
        type='CSPDarknet',
        deepen_factor=1.33,
        widen_factor=1.25,
        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],
        out_channels=320,
        num_csp_blocks=4,
        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,
        feat_channels=320,
        stacked_convs=2,
        strides=(8, 16, 32),
        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)))
data_root = 'data/coco/'
dataset_type = 'CocoDataset'
file_client_args = dict(backend='disk')
train_pipeline = [
    dict(type='Mosaic', img_scale=(640, 640), pad_val=114.0),
    dict(
        type='RandomAffine', scaling_ratio_range=(0.1, 2),
        border=(-320, -320)),
    dict(
        type='MixUp',
        img_scale=(640, 640),
        ratio_range=(0.8, 1.6),
        pad_val=114.0),
    dict(type='YOLOXHSVRandomAug'),
    dict(type='RandomFlip', prob=0.5),
    dict(type='Resize', scale=(640, 640), 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')
]
train_dataset = dict(
    type='MultiImageMixDataset',
    dataset=dict(
        metainfo=dict(classes=('pig', ), palette=[(220, 20, 60)]),
        type='CocoDataset',
        data_root='data/coco/',
        ann_file='annotations/instances_train2017.json',
        data_prefix='data/coco/images/train2017',
        pipeline=[
            dict(
                type='LoadImageFromFile',
                file_client_args=dict(backend='disk')),
            dict(type='LoadAnnotations', with_bbox=True)
        ],
        filter_cfg=dict(filter_empty_gt=False, min_size=32)),
    pipeline=[
        dict(type='Mosaic', img_scale=(640, 640), pad_val=114.0),
        dict(
            type='RandomAffine',
            scaling_ratio_range=(0.1, 2),
            border=(-320, -320)),
        dict(
            type='MixUp',
            img_scale=(640, 640),
            ratio_range=(0.8, 1.6),
            pad_val=114.0),
        dict(type='YOLOXHSVRandomAug'),
        dict(type='RandomFlip', prob=0.5),
        dict(type='Resize', scale=(640, 640), 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')
    ])
test_pipeline = [
    dict(type='LoadImageFromFile', file_client_args=dict(backend='disk')),
    dict(type='Resize', scale=(640, 640), 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'))
]
train_dataloader = dict(
    batch_size=8,
    num_workers=4,
    persistent_workers=True,
    sampler=dict(type='DefaultSampler', shuffle=True),
    dataset=dict(
        type='MultiImageMixDataset',
        dataset=dict(
            metainfo=dict(classes=('pig', ), palette=[(220, 20, 60)]),
            type='CocoDataset',
            data_root='data/coco/',
            ann_file='annotations/instances_train2017.json',
            data_prefix='data/coco/images/train2017',
            pipeline=[
                dict(
                    type='LoadImageFromFile',
                    file_client_args=dict(backend='disk')),
                dict(type='LoadAnnotations', with_bbox=True)
            ],
            filter_cfg=dict(filter_empty_gt=False, min_size=32)),
        pipeline=[
            dict(type='Mosaic', img_scale=(640, 640), pad_val=114.0),
            dict(
                type='RandomAffine',
                scaling_ratio_range=(0.1, 2),
                border=(-320, -320)),
            dict(
                type='MixUp',
                img_scale=(640, 640),
                ratio_range=(0.8, 1.6),
                pad_val=114.0),
            dict(type='YOLOXHSVRandomAug'),
            dict(type='RandomFlip', prob=0.5),
            dict(type='Resize', scale=(640, 640), 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')
        ]))
val_dataloader = dict(
    batch_size=8,
    num_workers=4,
    persistent_workers=True,
    drop_last=False,
    sampler=dict(type='DefaultSampler', shuffle=False),
    dataset=dict(
        metainfo=dict(classes=('pig', ), palette=[(220, 20, 60)]),
        type='CocoDataset',
        data_root='data/coco/',
        ann_file='annotations/instances_val2017.json',
        data_prefix='data/coco/images/val2017',
        test_mode=True,
        pipeline=[
            dict(
                type='LoadImageFromFile',
                file_client_args=dict(backend='disk')),
            dict(type='Resize', scale=(640, 640), 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_dataloader = dict(
    batch_size=8,
    num_workers=4,
    persistent_workers=True,
    drop_last=False,
    sampler=dict(type='DefaultSampler', shuffle=False),
    dataset=dict(
        metainfo=dict(classes=('pig', ), palette=[(220, 20, 60)]),
        type='CocoDataset',
        data_root='data/coco/',
        ann_file='annotations/instances_test2017.json',
        data_prefix='data/coco/images/test2017',
        test_mode=True,
        pipeline=[
            dict(
                type='LoadImageFromFile',
                file_client_args=dict(backend='disk')),
            dict(type='Resize', scale=(640, 640), 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'))
        ]))
val_evaluator = dict(
    type='CocoMetric',
    ann_file='data/coco/annotations/instances_val2017.json',
    metric='bbox')
test_evaluator = dict(
    type='CocoMetric',
    ann_file='data/coco/annotations/instances_test2017.json',
    metric='bbox')
max_epochs = 300
num_last_epochs = 15
interval = 10
base_lr = 0.01
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)
]
hsp2454 commented 1 year ago

can someone answer please

Lixinsuccess commented 1 year ago

还有一个calss_name..py需要改一下吧

i-aki-y commented 1 year ago

@hsp2454 The error looks to say something wrong in your data_prefix defined in *_dataloader. It should be dict but you set a string

*_dataloader = dict(
    ...
    dataset=dict(
        ...
        data_prefix='data/coco/images/train2017',  # <---- Here.  it should be like: data_prefix=dict(img='val2017/'),  
        ...
    )
hsp2454 commented 1 year ago

好的,谢谢