Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MirrorExtend for training BasicVSR and IconVSR #253

Merged
merged 3 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions mmedit/datasets/pipelines/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .augmentation import (BinarizeImage, Flip, GenerateFrameIndices,
GenerateFrameIndiceswithPadding, Pad, RandomAffine,
RandomJitter, RandomMaskDilation, RandomTransposeHW,
Resize, TemporalReverse)
GenerateFrameIndiceswithPadding, MirrorExtend, Pad,
RandomAffine, RandomJitter, RandomMaskDilation,
RandomTransposeHW, Resize, TemporalReverse)
from .compose import Compose
from .crop import (Crop, CropAroundCenter, CropAroundFg, CropAroundUnknown,
FixedCrop, ModCrop, PairedRandomCrop)
Expand Down Expand Up @@ -29,5 +29,5 @@
'LoadPairedImageFromFile', 'GenerateSoftSeg', 'GenerateSeg', 'PerturbBg',
'CropAroundFg', 'GetSpatialDiscountMask', 'RandomDownSampling',
'GenerateTrimapWithDistTransform', 'TransformTrimap',
'GenerateCoordinateAndCell'
'GenerateCoordinateAndCell', 'MirrorExtend'
]
39 changes: 39 additions & 0 deletions mmedit/datasets/pipelines/augmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,3 +895,42 @@ def __repr__(self):
repr_str = self.__class__.__name__
repr_str += f'(keys={self.keys}, reverse_ratio={self.reverse_ratio})'
return repr_str


@PIPELINES.register_module()
class MirrorExtend:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about MirrorSequenceExtend, so that to rule out the wrong guess of spatial extention.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, will change~

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another idea: MirrorSequence

Copy link
Member Author

@ckkelvinchan ckkelvinchan Apr 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one seems more succinct. Will change. Thanks~

"""Extend short sequences (e.g. Vimeo-90K) by mirroring the sequences

Given a sequence with N frames (x1, ..., xN), extend the sequence to
(x1, ..., xN, xN, ..., x1).

Args:
keys (list[str]): The frame lists to be extended.
"""

def __init__(self, keys):
self.keys = keys

def __call__(self, results):
"""Call function.

Args:
results (dict): A dict containing the necessary information and
data for augmentation.

Returns:
dict: A dict containing the processed data and information.
"""
for key in self.keys:
if isinstance(results[key], list):
results[key] = results[key] + results[key][::-1]
else:
raise TypeError('The input must be of class list[nparray]. '
f'Got {type(results[key])}.')

return results

def __repr__(self):
repr_str = self.__class__.__name__
repr_str += (f'(keys={self.keys})')
return repr_str
33 changes: 29 additions & 4 deletions tests/test_augmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
# yapf: disable
from mmedit.datasets.pipelines import (BinarizeImage, Flip,
GenerateFrameIndices,
GenerateFrameIndiceswithPadding, Pad,
RandomAffine, RandomJitter,
RandomMaskDilation, RandomTransposeHW,
Resize, TemporalReverse)
GenerateFrameIndiceswithPadding,
MirrorExtend, Pad, RandomAffine,
RandomJitter, RandomMaskDilation,
RandomTransposeHW, Resize,
TemporalReverse)

# yapf: enable

Expand Down Expand Up @@ -622,3 +623,27 @@ def test_temporal_reverse(self):
np.testing.assert_almost_equal(results['lq'][0], img_lq1)
np.testing.assert_almost_equal(results['lq'][1], img_lq2)
np.testing.assert_almost_equal(results['gt'][0], img_gt)

def mirror_extend(self):
lqs = [np.random.rand(4, 4, 3) for _ in range(0, 5)]
gts = [np.random.rand(16, 16, 3) for _ in range(0, 5)]

target_keys = ['lq', 'gt']
mirror_extend = MirrorExtend(keys=['lq', 'gt'])
results = dict(lq=lqs, gt=gts)
results = mirror_extend(results)

assert self.check_keys_contain(results.keys(), target_keys)
for i in range(0, 5):
np.testing.assert_almost_equal(results['lq'][i],
results['lq'][-i - 1])
np.testing.assert_almost_equal(results['gt'][i],
results['gt'][-i - 1])

assert repr(mirror_extend) == mirror_extend.__class__.__name__ + (
f"(keys=['lq', 'gt'])")

# each key should contain a list of nparray
with pytest.raises(TypeError):
results = dict(lq=0, gt=gts)
mirror_extend(results)