mmsegmentation icon indicating copy to clipboard operation
mmsegmentation copied to clipboard

Allow multiple RandomFlip operations in training pipeline

Open SBCV opened this issue 1 year ago • 4 comments

Allow multiple RandomFlip operations in the training pipeline. The current implementation is limited to a single RandomFlip using a single direction.

Motivation

I've tried to add an additional RandomFlip step in the training pipeline, which results in something like this:

train_pipeline = [
    dict(type='LoadImageFromFile'),
    dict(type='LoadAnnotations', reduce_zero_label=True),
    ...
    dict(type='RandomFlip', prob=0.5, direction="vertical"),
    dict(type='RandomFlip', prob=0.5, direction="horizontal"),
    ...
    dict(type='DefaultFormatBundle'),
    dict(type='Collect', keys=['img', 'gt_semantic_seg']),
]

However, in this case mmsegmentation does not apply any flips at all (neither vertical nor horizontal). If I remove one of the two lines, it works as expected.

I could provide a PR for this. However, in this case I need to know what kind of solution is preferable.

Possible approach 1:

dict(type='RandomFlip', prob=0.5, direction="vertical"),
dict(type='RandomFlip', prob=0.5, direction="horizontal"),

Possible approach 2: dict(type='RandomFlip', prob=(0.5, 0.5), direction=("vertical". "horizontal"))

SBCV avatar Jul 15 '22 08:07 SBCV

Hi @SBCV, Thanks for your bug report, you might modify the following code to solve this problem simply. And if you have a better solution, welcome to create a PR. Personally, I think the second one is probably better :)

https://github.com/open-mmlab/mmsegmentation/blob/336435b80b26fb1273b5a4b5d597d1d65e2ff644/mmseg/datasets/pipelines/transforms.py#L361

if 'flip_direction' not in results or results['flip_direction'] != self.direction:
    results['flip_direction'] = self.direction

xiexinch avatar Jul 15 '22 11:07 xiexinch

Hi @xiexinch,

thank you for answer. I think the adjustment you have proposed does not properly address the corresponding meta data created by two consecutive flip operations. I'll push an appropriate PR tomorrow.

Bests

SBCV avatar Jul 20 '22 10:07 SBCV

I prefer approach 1, as the RandomFlip api is much easier to use. #1812 might increase complexity.

MeowZheng avatar Jul 26 '22 15:07 MeowZheng

@MeowZheng Added an alternative implementation according to your suggestion

SBCV avatar Aug 15 '22 15:08 SBCV