open-mmlab / mmdetection

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

Getting error when using albumentations with Mask R-CNN #2857

Closed fabmeyer closed 4 years ago

fabmeyer commented 4 years ago

In my config.py I have: albu_train_transforms = [ dict( type='ShiftScaleRotate', shift_limit=0.075, scale_limit=0.25, rotate_limit=180, interpolation=1, p=0.75), dict( type='RandomBrightnessContrast', brightness_limit=0.1, contrast_limit=0.1, p=0.25), dict( type='OneOf', transforms=[ dict( type='RGBShift', r_shift_limit=10, g_shift_limit=10, b_shift_limit=10, p=1.0), dict( type='HueSaturationValue', hue_shift_limit=20, sat_shift_limit=20, val_shift_limit=20, p=1.0) ], p=0.1), dict( type='CenterCrop', height=40, width=40, p=0.25), dict( type='HorizontalFlip', p=0.25), dict( type='RandomCrop', height=200, width=200, p=0.25), dict( type='Rotate', limit=180, interpolation=1, p=0.25), ]

and in my train_pipeline I have a dict with: dict( type='Albu', transforms=albu_train_transforms, bbox_params=dict( type='BboxParams', format='CocoDataset', label_fields=['gt_labels'], min_visibility=0.0, filter_lost_elements=True), keymap={ 'img': 'image', 'gt_masks': 'masks', 'gt_bboxes': 'bboxes' }, update_pad_shape=False, skip_img_without_anno=True),

I am getting the following error:

Traceback (most recent call last): File "/content/mmdetection/tools/train.py", line 142, in <module> main() File "/content/mmdetection/tools/train.py", line 138, in main meta=meta) File "/content/mmdetection/mmdet/apis/train.py", line 111, in train_detector meta=meta) File "/content/mmdetection/mmdet/apis/train.py", line 305, in _non_dist_train runner.run(data_loaders, cfg.workflow, cfg.total_epochs) File "/usr/local/lib/python3.6/dist-packages/mmcv/runner/runner.py", line 384, in run epoch_runner(data_loaders[i], **kwargs) File "/usr/local/lib/python3.6/dist-packages/mmcv/runner/runner.py", line 279, in train for i, data_batch in enumerate(data_loader): File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py", line 582, in __next__ return self._process_next_batch(batch) File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py", line 608, in _process_next_batch raise batch.exc_type(batch.exc_msg) TypeError: Traceback (most recent call last): File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/worker.py", line 99, in _worker_loop samples = collate_fn([dataset[i] for i in batch_indices]) File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/worker.py", line 99, in <listcomp> samples = collate_fn([dataset[i] for i in batch_indices]) File "/content/mmdetection/mmdet/datasets/custom.py", line 132, in __getitem__ data = self.prepare_train_img(idx) File "/content/mmdetection/mmdet/datasets/custom.py", line 145, in prepare_train_img return self.pipeline(results) File "/content/mmdetection/mmdet/datasets/pipelines/compose.py", line 24, in __call__ data = t(data) File "/content/mmdetection/mmdet/datasets/pipelines/transforms.py", line 833, in __call__ results['idx_mapper'] = np.arange(len(results['bboxes'])) TypeError: object of type 'DataContainer' has no len()

I am working with a custom COCO-dataset by the way. How to specify this dataset in format in bbox_params?

yhcao6 commented 4 years ago

The correct order of the train pipeline should be as the following, I guess you put DefaultFormatBundle after Albu so that in Albu it meet DataContainer.

train_pipeline = [
    dict(type='LoadImageFromFile'),
    dict(type='LoadAnnotations', with_bbox=True, with_mask=True),
    dict(type='Resize', img_scale=(1333, 800), keep_ratio=True),
    dict(type='Pad', size_divisor=32),
    dict(
        type='Albu',
        transforms=albu_train_transforms,
        bbox_params=dict(
            type='BboxParams',
            format='pascal_voc',
            label_fields=['gt_labels'],
            min_visibility=0.0,
            filter_lost_elements=True),
        keymap={
            'img': 'image',
            'gt_masks': 'masks',
            'gt_bboxes': 'bboxes'
        },
        update_pad_shape=False,
        skip_img_without_anno=True),
    dict(type='Normalize', **img_norm_cfg),
    dict(type='DefaultFormatBundle'),
    dict(
        type='Collect',
        keys=['img', 'gt_bboxes', 'gt_labels', 'gt_masks'],
        meta_keys=('filename', 'ori_shape', 'img_shape', 'img_norm_cfg',
                   'pad_shape', 'scale_factor'))
]