From 716f255a1ec003055cba9c29455f103102a6a49b Mon Sep 17 00:00:00 2001 From: liyinshuo Date: Sat, 8 May 2021 11:36:27 +0800 Subject: [PATCH 1/7] Add ReSampling --- mmedit/datasets/pipelines/__init__.py | 4 +- .../{down_sampling.py => re_sampling.py} | 57 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) rename mmedit/datasets/pipelines/{down_sampling.py => re_sampling.py} (69%) diff --git a/mmedit/datasets/pipelines/__init__.py b/mmedit/datasets/pipelines/__init__.py index 1538edca87..dddb88e2e3 100644 --- a/mmedit/datasets/pipelines/__init__.py +++ b/mmedit/datasets/pipelines/__init__.py @@ -6,7 +6,6 @@ from .compose import Compose from .crop import (Crop, CropAroundCenter, CropAroundFg, CropAroundUnknown, FixedCrop, ModCrop, PairedRandomCrop) -from .down_sampling import RandomDownSampling from .formating import (Collect, FormatTrimap, GetMaskedImage, ImageToTensor, ToTensor) from .generate_coordinate_and_cell import GenerateCoordinateAndCell @@ -17,6 +16,7 @@ GenerateTrimap, GenerateTrimapWithDistTransform, MergeFgAndBg, PerturbBg, TransformTrimap) from .normalization import Normalize, RescaleToZeroOne +from .re_sampling import RandomDownSampling, ReSampling __all__ = [ 'Collect', 'FormatTrimap', 'LoadImageFromFile', 'LoadMask', @@ -29,6 +29,6 @@ 'GenerateFrameIndices', 'GenerateFrameIndiceswithPadding', 'FixedCrop', 'LoadPairedImageFromFile', 'GenerateSoftSeg', 'GenerateSeg', 'PerturbBg', 'CropAroundFg', 'GetSpatialDiscountMask', 'RandomDownSampling', - 'GenerateTrimapWithDistTransform', 'TransformTrimap', + 'GenerateTrimapWithDistTransform', 'TransformTrimap', 'ReSampling', 'GenerateCoordinateAndCell', 'GenerateSegmentIndices', 'MirrorSequence' ] diff --git a/mmedit/datasets/pipelines/down_sampling.py b/mmedit/datasets/pipelines/re_sampling.py similarity index 69% rename from mmedit/datasets/pipelines/down_sampling.py rename to mmedit/datasets/pipelines/re_sampling.py index 346a7ff024..9885610f56 100644 --- a/mmedit/datasets/pipelines/down_sampling.py +++ b/mmedit/datasets/pipelines/re_sampling.py @@ -7,6 +7,63 @@ from ..registry import PIPELINES +@PIPELINES.register_module() +class ReSampling: + """Resize image. + + Args: + scale (float): The resampling scale. scale > 0. + scale > 1: upsampling. + scale < 1: downsampling. + input_key (str): The input key. + output_key (str): The output key. + interpolation (str): Interpolation method, accepted values are + "nearest", "bilinear", "bicubic", "area", "lanczos" for 'cv2' + backend, "nearest", "bilinear", "bicubic", "box", "lanczos", + "hamming" for 'pillow' backend. + Default: "bicubic". + backend (str | None): The image resize backend type. Options are `cv2`, + `pillow`, `None`. If backend is None, the global imread_backend + specified by ``mmcv.use_backend()`` will be used. + Default: "pillow". + """ + + def __init__(self, + scale, + input_key, + output_key, + interpolation='bicubic', + backend='pillow'): + self.scale = scale + self.input_key = input_key + self.output_key = output_key + self.interpolation = interpolation + self.backend = backend + + def __call__(self, results): + """Call function. + + Args: + results (dict): A dict containing the necessary information and + data for augmentation. self.input_key is required. + + Returns: + dict: A dict containing the processed data and information. + supplement self.output_key to keys. + """ + assert self.input_key in results, f'Cannot find {self.input_key}.' + image_in = results[self.input_key] + h_in, w_in = image_in.shape[:2] + h_out = math.floor(h_in * self.scale + 1e-9) + w_out = math.floor(w_in * self.scale + 1e-9) + image_out = resize_fn(image_in, (w_out, h_out), self.interpolation, + self.backend) + + results[self.output_key] = image_out + + return results + + @PIPELINES.register_module() class RandomDownSampling: """Generate LQ image from GT (and crop), which will randomly pick a scale. From 335ed5f1e0ebaff5249001f88badea3eed0661cd Mon Sep 17 00:00:00 2001 From: liyinshuo Date: Sat, 8 May 2021 12:03:14 +0800 Subject: [PATCH 2/7] Add ReSampling --- mmedit/datasets/pipelines/re_sampling.py | 16 +++++++- tests/test_down_sampling.py | 29 --------------- tests/test_re_sampling.py | 47 ++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 31 deletions(-) delete mode 100644 tests/test_down_sampling.py create mode 100644 tests/test_re_sampling.py diff --git a/mmedit/datasets/pipelines/re_sampling.py b/mmedit/datasets/pipelines/re_sampling.py index 9885610f56..3a105cc4e1 100644 --- a/mmedit/datasets/pipelines/re_sampling.py +++ b/mmedit/datasets/pipelines/re_sampling.py @@ -63,6 +63,16 @@ def __call__(self, results): return results + def __repr__(self): + repr_str = self.__class__.__name__ + repr_str += (f' scale={self.scale}, ' + f'input_key={self.input_key}, ' + f'output_key={self.output_key}, ' + f'interpolation={self.interpolation}, ' + f'backend={self.backend}') + + return repr_str + @PIPELINES.register_module() class RandomDownSampling: @@ -138,9 +148,11 @@ def __call__(self, results): def __repr__(self): repr_str = self.__class__.__name__ - repr_str += (f'scale_min={self.scale_min}, ' + repr_str += (f' scale_min={self.scale_min}, ' f'scale_max={self.scale_max}, ' - f'patch_size={self.patch_size}') + f'patch_size={self.patch_size}, ' + f'interpolation={self.interpolation}, ' + f'backend={self.backend}') return repr_str diff --git a/tests/test_down_sampling.py b/tests/test_down_sampling.py deleted file mode 100644 index d761f9aaa2..0000000000 --- a/tests/test_down_sampling.py +++ /dev/null @@ -1,29 +0,0 @@ -import numpy as np - -from mmedit.datasets.pipelines import RandomDownSampling - - -def test_down_sampling(): - img1 = np.uint8(np.random.randn(480, 640, 3) * 255) - inputs1 = dict(gt=img1) - down_sampling1 = RandomDownSampling( - scale_min=1, scale_max=4, patch_size=None) - results1 = down_sampling1(inputs1) - assert set(list(results1.keys())) == set(['gt', 'lq', 'scale']) - assert repr(down_sampling1) == ( - down_sampling1.__class__.__name__ + - f'scale_min={down_sampling1.scale_min}, ' + - f'scale_max={down_sampling1.scale_max}, ' + - f'patch_size={down_sampling1.patch_size}') - - img2 = np.uint8(np.random.randn(480, 640, 3) * 255) - inputs2 = dict(gt=img2) - down_sampling2 = RandomDownSampling( - scale_min=1, scale_max=4, patch_size=48) - results2 = down_sampling2(inputs2) - assert set(list(results2.keys())) == set(['gt', 'lq', 'scale']) - assert repr(down_sampling2) == ( - down_sampling2.__class__.__name__ + - f'scale_min={down_sampling2.scale_min}, ' + - f'scale_max={down_sampling2.scale_max}, ' + - f'patch_size={down_sampling2.patch_size}') diff --git a/tests/test_re_sampling.py b/tests/test_re_sampling.py new file mode 100644 index 0000000000..4334add200 --- /dev/null +++ b/tests/test_re_sampling.py @@ -0,0 +1,47 @@ +import numpy as np + +from mmedit.datasets.pipelines import RandomDownSampling, ReSampling + + +def test_random_down_sampling(): + img1 = np.uint8(np.random.randn(480, 640, 3) * 255) + inputs1 = dict(gt=img1) + down_sampling1 = RandomDownSampling( + scale_min=1, scale_max=4, patch_size=None) + results1 = down_sampling1(inputs1) + assert set(list(results1.keys())) == set(['gt', 'lq', 'scale']) + assert repr(down_sampling1) == ( + down_sampling1.__class__.__name__ + + f' scale_min={down_sampling1.scale_min}, ' + + f'scale_max={down_sampling1.scale_max}, ' + + f'patch_size={down_sampling1.patch_size}, ' + + f'interpolation={down_sampling1.interpolation}, ' + + f'backend={down_sampling1.backend}') + + img2 = np.uint8(np.random.randn(480, 640, 3) * 255) + inputs2 = dict(gt=img2) + down_sampling2 = RandomDownSampling( + scale_min=1, scale_max=4, patch_size=48) + results2 = down_sampling2(inputs2) + assert set(list(results2.keys())) == set(['gt', 'lq', 'scale']) + assert repr(down_sampling2) == ( + down_sampling2.__class__.__name__ + + f' scale_min={down_sampling2.scale_min}, ' + + f'scale_max={down_sampling2.scale_max}, ' + + f'patch_size={down_sampling2.patch_size}, ' + + f'interpolation={down_sampling2.interpolation}, ' + + f'backend={down_sampling2.backend}') + + +def test_re_sampling(): + img = np.uint8(np.random.randn(480, 640, 3) * 255) + inputs = dict(gt=img) + re_sampling = ReSampling(scale=1 / 4, input_key='gt', output_key='lq') + results = re_sampling(inputs) + assert set(list(results.keys())) == set(['gt', 'lq']) + assert repr(re_sampling) == ( + re_sampling.__class__.__name__ + f' scale={re_sampling.scale}, ' + + f'input_key={re_sampling.input_key}, ' + + f'output_key={re_sampling.output_key}, ' + + f'interpolation={re_sampling.interpolation}, ' + + f'backend={re_sampling.backend}') From a2827f4abf3f59e8506c74b0f6badb25832b6d55 Mon Sep 17 00:00:00 2001 From: liyinshuo Date: Tue, 11 May 2021 10:00:46 +0800 Subject: [PATCH 3/7] Fix docstring --- mmedit/datasets/pipelines/re_sampling.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mmedit/datasets/pipelines/re_sampling.py b/mmedit/datasets/pipelines/re_sampling.py index 3a105cc4e1..6822397d04 100644 --- a/mmedit/datasets/pipelines/re_sampling.py +++ b/mmedit/datasets/pipelines/re_sampling.py @@ -9,7 +9,10 @@ @PIPELINES.register_module() class ReSampling: - """Resize image. + """Re-Sampling image. + + The result will be saved in the same key (output_key == input_key) + or another key. Args: scale (float): The resampling scale. scale > 0. From b3f7373f6f29bc0d214f0dd3d9af2d60b0fa91d5 Mon Sep 17 00:00:00 2001 From: liyinshuo Date: Tue, 11 May 2021 10:45:55 +0800 Subject: [PATCH 4/7] fix --- mmedit/datasets/pipelines/__init__.py | 4 ++-- .../pipelines/{re_sampling.py => resize.py} | 4 ++-- tests/{test_re_sampling.py => test_resize.py} | 24 +++++++++++-------- 3 files changed, 18 insertions(+), 14 deletions(-) rename mmedit/datasets/pipelines/{re_sampling.py => resize.py} (99%) rename tests/{test_re_sampling.py => test_resize.py} (72%) diff --git a/mmedit/datasets/pipelines/__init__.py b/mmedit/datasets/pipelines/__init__.py index dddb88e2e3..ad509e4d4c 100644 --- a/mmedit/datasets/pipelines/__init__.py +++ b/mmedit/datasets/pipelines/__init__.py @@ -16,7 +16,7 @@ GenerateTrimap, GenerateTrimapWithDistTransform, MergeFgAndBg, PerturbBg, TransformTrimap) from .normalization import Normalize, RescaleToZeroOne -from .re_sampling import RandomDownSampling, ReSampling +from .resize import GenerateByResize, RandomDownSampling __all__ = [ 'Collect', 'FormatTrimap', 'LoadImageFromFile', 'LoadMask', @@ -29,6 +29,6 @@ 'GenerateFrameIndices', 'GenerateFrameIndiceswithPadding', 'FixedCrop', 'LoadPairedImageFromFile', 'GenerateSoftSeg', 'GenerateSeg', 'PerturbBg', 'CropAroundFg', 'GetSpatialDiscountMask', 'RandomDownSampling', - 'GenerateTrimapWithDistTransform', 'TransformTrimap', 'ReSampling', + 'GenerateTrimapWithDistTransform', 'TransformTrimap', 'GenerateByResize', 'GenerateCoordinateAndCell', 'GenerateSegmentIndices', 'MirrorSequence' ] diff --git a/mmedit/datasets/pipelines/re_sampling.py b/mmedit/datasets/pipelines/resize.py similarity index 99% rename from mmedit/datasets/pipelines/re_sampling.py rename to mmedit/datasets/pipelines/resize.py index 6822397d04..9ea68f7ea8 100644 --- a/mmedit/datasets/pipelines/re_sampling.py +++ b/mmedit/datasets/pipelines/resize.py @@ -8,8 +8,8 @@ @PIPELINES.register_module() -class ReSampling: - """Re-Sampling image. +class GenerateByResize: + """Resize to generate image. The result will be saved in the same key (output_key == input_key) or another key. diff --git a/tests/test_re_sampling.py b/tests/test_resize.py similarity index 72% rename from tests/test_re_sampling.py rename to tests/test_resize.py index 4334add200..2a9b8f4204 100644 --- a/tests/test_re_sampling.py +++ b/tests/test_resize.py @@ -1,6 +1,6 @@ import numpy as np -from mmedit.datasets.pipelines import RandomDownSampling, ReSampling +from mmedit.datasets.pipelines import GenerateByResize, RandomDownSampling def test_random_down_sampling(): @@ -33,15 +33,19 @@ def test_random_down_sampling(): f'backend={down_sampling2.backend}') -def test_re_sampling(): +def test_generate_by_resize(): img = np.uint8(np.random.randn(480, 640, 3) * 255) inputs = dict(gt=img) - re_sampling = ReSampling(scale=1 / 4, input_key='gt', output_key='lq') - results = re_sampling(inputs) + re_size = GenerateByResize(scale=1 / 4, input_key='gt', output_key='lq') + results = re_size(inputs) assert set(list(results.keys())) == set(['gt', 'lq']) - assert repr(re_sampling) == ( - re_sampling.__class__.__name__ + f' scale={re_sampling.scale}, ' + - f'input_key={re_sampling.input_key}, ' + - f'output_key={re_sampling.output_key}, ' + - f'interpolation={re_sampling.interpolation}, ' + - f'backend={re_sampling.backend}') + assert repr(re_size) == ( + re_size.__class__.__name__ + f' scale={re_size.scale}, ' + + f'input_key={re_size.input_key}, ' + + f'output_key={re_size.output_key}, ' + + f'interpolation={re_size.interpolation}, ' + + f'backend={re_size.backend}') + + +if __name__ == '__main__': + test_generate_by_resize() From 5c1f273b85b52507abba7b4119ecadf9d0f7bc75 Mon Sep 17 00:00:00 2001 From: liyinshuo Date: Tue, 11 May 2021 12:03:20 +0800 Subject: [PATCH 5/7] add generate-by-resize --- tests/test_resize.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/test_resize.py b/tests/test_resize.py index 2a9b8f4204..bee32fa445 100644 --- a/tests/test_resize.py +++ b/tests/test_resize.py @@ -45,7 +45,3 @@ def test_generate_by_resize(): f'output_key={re_size.output_key}, ' + f'interpolation={re_size.interpolation}, ' + f'backend={re_size.backend}') - - -if __name__ == '__main__': - test_generate_by_resize() From 1b50bf7db2b221620106911570329d863384eb17 Mon Sep 17 00:00:00 2001 From: liyinshuo Date: Wed, 12 May 2021 10:35:25 +0800 Subject: [PATCH 6/7] Rename --- mmedit/datasets/pipelines/__init__.py | 4 ++-- .../datasets/pipelines/{resize.py => sr_resize.py} | 4 ++-- tests/{test_resize.py => test_sr_resize.py} | 13 ++++++++++--- 3 files changed, 14 insertions(+), 7 deletions(-) rename mmedit/datasets/pipelines/{resize.py => sr_resize.py} (98%) rename tests/{test_resize.py => test_sr_resize.py} (80%) diff --git a/mmedit/datasets/pipelines/__init__.py b/mmedit/datasets/pipelines/__init__.py index ad509e4d4c..8f7d114208 100644 --- a/mmedit/datasets/pipelines/__init__.py +++ b/mmedit/datasets/pipelines/__init__.py @@ -16,7 +16,7 @@ GenerateTrimap, GenerateTrimapWithDistTransform, MergeFgAndBg, PerturbBg, TransformTrimap) from .normalization import Normalize, RescaleToZeroOne -from .resize import GenerateByResize, RandomDownSampling +from .sr_resize import RandomDownSampling, SRResize __all__ = [ 'Collect', 'FormatTrimap', 'LoadImageFromFile', 'LoadMask', @@ -29,6 +29,6 @@ 'GenerateFrameIndices', 'GenerateFrameIndiceswithPadding', 'FixedCrop', 'LoadPairedImageFromFile', 'GenerateSoftSeg', 'GenerateSeg', 'PerturbBg', 'CropAroundFg', 'GetSpatialDiscountMask', 'RandomDownSampling', - 'GenerateTrimapWithDistTransform', 'TransformTrimap', 'GenerateByResize', + 'GenerateTrimapWithDistTransform', 'TransformTrimap', 'SRResize', 'GenerateCoordinateAndCell', 'GenerateSegmentIndices', 'MirrorSequence' ] diff --git a/mmedit/datasets/pipelines/resize.py b/mmedit/datasets/pipelines/sr_resize.py similarity index 98% rename from mmedit/datasets/pipelines/resize.py rename to mmedit/datasets/pipelines/sr_resize.py index 9ea68f7ea8..6a691315ca 100644 --- a/mmedit/datasets/pipelines/resize.py +++ b/mmedit/datasets/pipelines/sr_resize.py @@ -8,8 +8,8 @@ @PIPELINES.register_module() -class GenerateByResize: - """Resize to generate image. +class SRResize: + """Resize image, include up sampling and down sampling. The result will be saved in the same key (output_key == input_key) or another key. diff --git a/tests/test_resize.py b/tests/test_sr_resize.py similarity index 80% rename from tests/test_resize.py rename to tests/test_sr_resize.py index bee32fa445..c8ef599bff 100644 --- a/tests/test_resize.py +++ b/tests/test_sr_resize.py @@ -1,6 +1,6 @@ import numpy as np -from mmedit.datasets.pipelines import GenerateByResize, RandomDownSampling +from mmedit.datasets.pipelines import RandomDownSampling, SRResize def test_random_down_sampling(): @@ -33,15 +33,22 @@ def test_random_down_sampling(): f'backend={down_sampling2.backend}') -def test_generate_by_resize(): +def test_sr_resize(): img = np.uint8(np.random.randn(480, 640, 3) * 255) inputs = dict(gt=img) - re_size = GenerateByResize(scale=1 / 4, input_key='gt', output_key='lq') + re_size = SRResize(scale=1 / 4, input_key='gt', output_key='lq') results = re_size(inputs) assert set(list(results.keys())) == set(['gt', 'lq']) + assert results['lq'].shape == (120, 160, 3) assert repr(re_size) == ( re_size.__class__.__name__ + f' scale={re_size.scale}, ' + f'input_key={re_size.input_key}, ' + f'output_key={re_size.output_key}, ' + f'interpolation={re_size.interpolation}, ' + f'backend={re_size.backend}') + + inputs = dict(gt=img) + re_size = SRResize(scale=2, input_key='gt', output_key='gt') + results = re_size(inputs) + assert set(list(results.keys())) == set(['gt']) + assert results['gt'].shape == (960, 1280, 3) From 740c4b75efd12b6be6b820087f7f0b5f43cac885 Mon Sep 17 00:00:00 2001 From: liyinshuo Date: Wed, 12 May 2021 14:14:53 +0800 Subject: [PATCH 7/7] update docstring --- mmedit/datasets/pipelines/sr_resize.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mmedit/datasets/pipelines/sr_resize.py b/mmedit/datasets/pipelines/sr_resize.py index 6a691315ca..5143dc04ea 100644 --- a/mmedit/datasets/pipelines/sr_resize.py +++ b/mmedit/datasets/pipelines/sr_resize.py @@ -9,10 +9,10 @@ @PIPELINES.register_module() class SRResize: - """Resize image, include up sampling and down sampling. + """Resize image by a scale, including upsampling and downsampling. - The result will be saved in the same key (output_key == input_key) - or another key. + Image will be loaded from the input_key and the result will be saved + in the specified output_key (can equal to input_key). Args: scale (float): The resampling scale. scale > 0.