open-mmlab / mmdetection

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

ValueError: class `EpochBasedTrainLoop` in mmengine/runner/loops.py: class `CocoDataset` in mmdet/datasets/coco.py: need at least one array to concatenate #10161

Open Louis24 opened 1 year ago

Louis24 commented 1 year ago

my config file

dataset_type = 'CocoDataset' data_root = 'data/coco/' backend_args = None train_pipeline = [ dict(type='LoadImageFromFile', backend_args=None), dict(type='LoadAnnotations', with_bbox=True), dict( type='RandomResize', scale=(896, 896), ratio_range=(0.1, 2.0), keep_ratio=True), dict(type='RandomCrop', crop_size=(896, 896)), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ] test_pipeline = [ dict(type='LoadImageFromFile', backend_args=None), dict(type='Resize', scale=(896, 896), keep_ratio=True), 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=0, persistent_workers=True, sampler=dict(type='DefaultSampler', shuffle=True, scope='mmdet'), batch_sampler=dict(type='AspectRatioBatchSampler', scope='mmdet'), dataset=dict( type='CocoDataset', data_root='C:/Zero/Python/Dataset/Refrigerator_COCO_Format/', ann_file='annotations/instances_train2017.json', data_prefix=dict(img='train2017/'), filter_cfg=dict(filter_empty_gt=True, min_size=32), pipeline=[ dict(type='LoadImageFromFile', backend_args=None), dict(type='LoadAnnotations', with_bbox=True), dict( type='RandomResize', scale=(896, 896), ratio_range=(0.1, 2.0), keep_ratio=True), dict(type='RandomCrop', crop_size=(896, 896)), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ], backend_args=None, scope='mmdet'), classes=('Coke', 'Sprite', 'Fanta')) val_dataloader = dict( batch_size=8, num_workers=0, persistent_workers=True, drop_last=False, sampler=dict(type='DefaultSampler', shuffle=False, scope='mmdet'), dataset=dict( type='CocoDataset', data_root='C:/Zero/Python/Dataset/Refrigerator_COCO_Format/', ann_file='annotations/instances_val2017.json', data_prefix=dict(img='val2017/'), test_mode=True, pipeline=[ dict(type='LoadImageFromFile', backend_args=None), dict(type='Resize', scale=(896, 896), keep_ratio=True), dict(type='LoadAnnotations', with_bbox=True), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ], backend_args=None, scope='mmdet'), classes=('Coke', 'Sprite', 'Fanta')) test_dataloader = dict( batch_size=8, num_workers=0, persistent_workers=True, drop_last=False, sampler=dict(type='DefaultSampler', shuffle=False, scope='mmdet'), dataset=dict( type='CocoDataset', data_root='C:/Zero/Python/Dataset/Refrigerator_COCO_Format/', ann_file='annotations/instances_val2017.json', data_prefix=dict(img='val2017/'), test_mode=True, pipeline=[ dict(type='LoadImageFromFile', backend_args=None), dict(type='Resize', scale=(896, 896), keep_ratio=True), dict(type='LoadAnnotations', with_bbox=True), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ], backend_args=None, scope='mmdet'), classes=('Coke', 'Sprite', 'Fanta')) val_evaluator = dict( type='CocoMetric', ann_file= 'C:/Zero/Python/Dataset/Refrigerator_COCO_Format/annotations/instances_val2017.json', metric='bbox', format_only=False, backend_args=None, scope='mmdet') test_evaluator = dict( type='CocoMetric', ann_file= 'C:/Zero/Python/Dataset/Refrigerator_COCO_Format/annotations/instances_val2017.json', metric='bbox', format_only=False, backend_args=None, scope='mmdet') train_cfg = dict( type='EpochBasedTrainLoop', max_epochs=10, val_interval=1, scope='mmdet') val_cfg = dict(type='ValLoop', scope='mmdet') test_cfg = dict(type='TestLoop', scope='mmdet') param_scheduler = [ dict(type='LinearLR', start_factor=0.1, by_epoch=False, begin=0, end=917), dict( type='CosineAnnealingLR', eta_min=0.0, begin=1, T_max=299, end=300, by_epoch=True, convert_to_iter_based=True) ] optim_wrapper = dict( type='OptimWrapper', optimizer=dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=4e-05), scope='mmdet', paramwise_cfg=dict( norm_decay_mult=0, bias_decay_mult=0, bypass_duplicate=True), clip_grad=dict(max_norm=10, norm_type=2)) auto_scale_lr = dict(enable=False, base_batch_size=128) default_scope = 'mmdet' default_hooks = dict( timer=dict(type='IterTimerHook', scope='mmdet'), logger=dict(type='LoggerHook', interval=-1, scope='mmdet'), param_scheduler=dict(type='ParamSchedulerHook', scope='mmdet'), checkpoint=dict(type='CheckpointHook', interval=-1, scope='mmdet'), sampler_seed=dict(type='DistSamplerSeedHook', scope='mmdet'), visualization=dict(type='DetVisualizationHook', scope='mmdet')) env_cfg = dict( cudnn_benchmark=True, mp_cfg=dict(mp_start_method='fork', opencv_num_threads=0), dist_cfg=dict(backend='nccl')) vis_backends = [ dict(type='LocalVisBackend'), dict(type='TensorboardVisBackend') ] visualizer = dict( type='DetLocalVisualizer', vis_backends=[ dict(type='LocalVisBackend'), dict(type='TensorboardVisBackend') ], name='visualizer', scope='mmdet') log_processor = dict( type='LogProcessor', window_size=50, by_epoch=True, scope='mmdet') log_level = 'INFO' load_from = None resume = False custom_imports = dict( imports=['projects.EfficientDet.efficientdet'], allow_failed_imports=False) image_size = 896 batch_augments = [dict(type='BatchFixedSizePad', size=(896, 896))] evalute_type = 'CocoMetric' norm_cfg = dict(type='SyncBN', requires_grad=True, eps=0.001, momentum=0.01) checkpoint = 'https://download.openmmlab.com/mmclassification/v0/efficientnet/efficientnet-b3_3rdparty_8xb32-aa-advprop_in1k_20220119-53b41118.pth' model = dict( type='EfficientDet', data_preprocessor=dict( type='DetDataPreprocessor', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], bgr_to_rgb=True, pad_size_divisor=896, batch_augments=[dict(type='BatchFixedSizePad', size=(896, 896))]), backbone=dict( type='EfficientNet', arch='b3', drop_path_rate=0.3, out_indices=(3, 4, 5), frozen_stages=0, conv_cfg=dict(type='Conv2dSamePadding'), norm_cfg=dict( type='SyncBN', requires_grad=True, eps=0.001, momentum=0.01), norm_eval=False, init_cfg=dict( type='Pretrained', prefix='backbone', checkpoint= 'C:/Zero/Python/Weight/efficientdet_effb3_bifpn_8xb16-crop896-300e_coco_20230223_122457-e6f7a833.pth' )), neck=dict( type='BiFPN', num_stages=6, in_channels=[48, 136, 384], out_channels=160, start_level=0, norm_cfg=dict( type='SyncBN', requires_grad=True, eps=0.001, momentum=0.01)), bbox_head=dict( type='EfficientDetSepBNHead', num_classes=3, num_ins=5, in_channels=160, feat_channels=160, stacked_convs=4, norm_cfg=dict( type='SyncBN', requires_grad=True, eps=0.001, momentum=0.01), anchor_generator=dict( type='AnchorGenerator', octave_base_scale=4, scales_per_octave=3, ratios=[1.0, 0.5, 2.0], strides=[8, 16, 32, 64, 128], center_offset=0.5), bbox_coder=dict( type='DeltaXYWHBBoxCoder', target_means=[0.0, 0.0, 0.0, 0.0], target_stds=[1.0, 1.0, 1.0, 1.0]), loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=1.5, alpha=0.25, loss_weight=1.0), loss_bbox=dict(type='HuberLoss', beta=0.1, loss_weight=50)), train_cfg=dict( assigner=dict( type='MaxIoUAssigner', pos_iou_thr=0.5, neg_iou_thr=0.5, min_pos_iou=0, ignore_iof_thr=-1), sampler=dict(type='PseudoSampler'), allowed_border=-1, pos_weight=-1, debug=False), test_cfg=dict( nms_pre=1000, min_bbox_size=0, score_thr=0.05, nms=dict( type='soft_nms', iou_threshold=0.3, sigma=0.5, min_score=0.001, method='gaussian'), max_per_img=100)) max_epochs = 300 custom_hooks = [ dict( type='EMAHook', ema_type='ExpMomentumEMA', momentum=0.0002, update_buffers=True, priority=49) ] work_dir = '../runs'

styler00dollar commented 1 year ago

I have the same problem and I don't know what the issue is.

HongkuiYu commented 1 year ago

Me too. It makes me crazy.

i-aki-y commented 1 year ago

When you define custom classes, you need to set it in metainfo of the dataset. https://mmdetection.readthedocs.io/en/latest/advanced_guides/customize_dataset.html#modify-the-config-file-for-using-the-customized-dataset

tureckova commented 1 year ago

The classes in metainfo solved this error for me, but I faced following error: KeyError: "classEpochBasedTrainLoopin mmengine/runner/loops.py: 'sampler'"

I solved it by adding the sampler and batch_sampler into train_dataloader and val_dataloader, so it look like this:

train_dataloader = dict(
    batch_size=4,
    num_workers=4,
    persistent_workers=True,
    sampler=dict(type='DefaultSampler', shuffle=True),
    batch_sampler=dict(type='AspectRatioBatchSampler'),
    dataset=dict(
        type=dataset_type,
        metainfo=dict(classes=classes),
        data_root=data_root,
        ann_file=split_root + 'cut_2042x2042_train_coco.json',
        data_prefix=dict(img=''))
val_dataloader = dict(
    batch_size=1,
    num_workers=2,
    persistent_workers=True,
    sampler=dict(type='DefaultSampler', shuffle=True),
    dataset=dict(
        type=dataset_type,
        metainfo=dict(classes=classes),
        data_root=data_root,
        ann_file=split_root + 'cut_2042x2042_val_coco.json',
        data_prefix=dict(img=''))
mahilaMoghadami commented 1 year ago

When you define custom classes, you need to set it in metainfo of the dataset. https://mmdetection.readthedocs.io/en/latest/advanced_guides/customize_dataset.html#modify-the-config-file-for-using-the-customized-dataset

hello, I changed classes names in coco.py file but still I have this error. what should I do?

jennzhuge commented 1 year ago

When you define custom classes, you need to set it in metainfo of the dataset. https://mmdetection.readthedocs.io/en/latest/advanced_guides/customize_dataset.html#modify-the-config-file-for-using-the-customized-dataset

hello, I changed classes names in coco.py file but still I have this error. what should I do?

same, I explicitly put classes as a key into metainfo in my config, but I'm still getting the "need >=1 array" error

mahilaMoghadami commented 1 year ago

maybe your classes name are not correct. my problem solved by changing names to ids. for example in Visdrone dataset, I changed class names 'pedestrian','car' , .... with 0,1,... and then it worked.

kv-chiu commented 1 year ago

I had the same problem with the balloon dataset and I added to the config:

metainfo = {
    'classes': ('balloon',),
    'palette': [
        (220, 20, 60),
    ]
}
num_classes = 1
Traceback (most recent call last):
  File "/root/autodl-tmp/work/mmdetection-main/tools/train.py", line 133, in <module>
    main()
  File "/root/autodl-tmp/work/mmdetection-main/tools/train.py", line 129, in main
    runner.train()
  File "/root/miniconda3/envs/mmdet/lib/python3.8/site-packages/mmengine/runner/runner.py", line 1721, in train
    model = self.train_loop.run()  # type: ignore
  File "/root/miniconda3/envs/mmdet/lib/python3.8/site-packages/mmengine/runner/loops.py", line 102, in run
    self.runner.val_loop.run()
  File "/root/miniconda3/envs/mmdet/lib/python3.8/site-packages/mmengine/runner/loops.py", line 366, in run
    metrics = self.evaluator.evaluate(len(self.dataloader.dataset))
  File "/root/miniconda3/envs/mmdet/lib/python3.8/site-packages/mmengine/evaluator/evaluator.py", line 79, in evaluate
    _results = metric.evaluate(size)
  File "/root/miniconda3/envs/mmdet/lib/python3.8/site-packages/mmengine/evaluator/metric.py", line 133, in evaluate
    _metrics = self.compute_metrics(results)  # type: ignore
  File "/root/autodl-tmp/work/mmdetection-main/mmdet/evaluation/metrics/coco_metric.py", line 419, in compute_metrics
    result_files = self.results2json(preds, outfile_prefix)
  File "/root/autodl-tmp/work/mmdetection-main/mmdet/evaluation/metrics/coco_metric.py", line 239, in results2json
    data['category_id'] = self.cat_ids[label]
IndexError: list index out of range

Through pdb debugging, I found that this array out-of-bounds problem was caused by the fact that num_classes was not 1.

So I modified the num_classes of the head in the model to make it 1, which solved the error.

model = ......
    bbox_head=dict(
        type='RTMDetSepBNHead',
        num_classes=1,
        in_channels=96,
        stacked_convs=2,
        ......

@Louis24 @mahilaMoghadami @jennzhuge @HongkuiYu

aojiaode commented 1 year ago

maybe your classes name are not correct. my problem solved by changing names to ids. for example in Visdrone dataset, I changed class names 'pedestrian','car' , .... with 0,1,... and then it worked.

Hello, may I ask how the specific modifications were made

mahilaMoghadami commented 1 year ago

s

hello, based on your dataset configs. as an example for VisDrone dataset this config worked for me:

classes = ('pedestrian', 'people', 'bicycle', 'car', 'van','truck', 'tricycle', 'awning-tricycle', 'bus', 'motor')

classes = ('1', '2', '3', '4','5', '6', '7', '8', '9', '10')

METAINFO = dict(classes = classes)