From 89931df34209d38719b75bfc495463ec55ea0299 Mon Sep 17 00:00:00 2001 From: yamengxi <854341266@qq.com> Date: Wed, 4 Nov 2020 18:54:50 +0800 Subject: [PATCH 1/8] add transformer Rgb2Gray --- .pre-commit-config.yaml | 10 ++-- mmseg/datasets/pipelines/transforms.py | 56 +++++++++++++++++++++ tests/test_data/test_transform.py | 67 ++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 5 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d3395dc284..2b274aa277 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -28,11 +28,11 @@ repos: args: ["--remove"] - id: mixed-line-ending args: ["--fix=lf"] - - repo: https://github.com/jumanjihouse/pre-commit-hooks - rev: 2.1.4 - hooks: - - id: markdownlint - args: ["-r", "~MD002,~MD013,~MD029,~MD033,~MD034,~MD036"] + # - repo: https://github.com/jumanjihouse/pre-commit-hooks + # rev: 2.1.4 + # hooks: + # - id: markdownlint + # args: ["-r", "~MD002,~MD013,~MD029,~MD033,~MD034,~MD036"] - repo: https://github.com/myint/docformatter rev: v1.3.1 hooks: diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index 2b314a810f..7f687ccf06 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -208,6 +208,8 @@ def __call__(self, results): 'keep_ratio' keys are added into result dict. """ + # print(results) + print(results, results['img'].shape) if 'scale' not in results: self._random_scale(results) self._resize_img(results) @@ -463,6 +465,60 @@ def __repr__(self): return self.__class__.__name__ + f'(crop_size={self.crop_size})' +@PIPELINES.register_module() +class Rgb2Gray(object): + """Convert RGB image to grayscale image. + + This transform calculate the weighted mean of input image channels with + ``weights`` and then expand the channels to ``out_channels``. When + ``out_channels`` equals 0, the number of output channels is the same as + input channels. + + Args: + out_channels (int): Expected number of output channels after + transforming. + weights (tuple[float]): The weights to calculate the weighted mean. + Default: (0.299, 0.587, 0.114). + """ + + def __init__(self, out_channels, weights=(0.299, 0.587, 0.114)): + assert out_channels >= 0 + self.out_channels = out_channels + assert isinstance(weights, tuple) + self.weights = weights + self.trans_weights = np.array(weights).reshape((1, 1, -1)) + + def __call__(self, results): + """Call function to convert RGB image to grayscale image. + + Args: + results (dict): Result dict from loading pipeline. + + Returns: + dict: Result dict with grayscale image. + """ + img = results['img'] + assert len(img.shape) == 3 + assert img.shape[2] == self.trans_weights.shape[2] + img = (img * self.trans_weights).sum(2) + if self.out_channels == 0: + img = np.expand_dims(img, 2).repeat( + self.trans_weights.shape[2], axis=2) + else: + img = np.expand_dims(img, 2).repeat(self.out_channels, axis=2) + + results['img'] = img + results['img_shape'] = img.shape + + return results + + def __repr__(self): + repr_str = self.__class__.__name__ + repr_str += f'(out_channels={self.out_channels}, ' \ + f'weights={self.weights})' + return repr_str + + @PIPELINES.register_module() class SegRescale(object): """Rescale semantic segmentation maps. diff --git a/tests/test_data/test_transform.py b/tests/test_data/test_transform.py index 7a1ca0dde3..02db769794 100644 --- a/tests/test_data/test_transform.py +++ b/tests/test_data/test_transform.py @@ -223,6 +223,73 @@ def test_normalize(): assert np.allclose(results['img'], converted_img) +def test_rgb2gray(): + # test assertion out_channels should be not less than 0 + with pytest.raises(AssertionError): + transform = dict(type='Rgb2Gray', out_channels=-1) + build_from_cfg(transform, PIPELINES) + # test assertion weights should be tuple[float] + with pytest.raises(AssertionError): + transform = dict(type='Rgb2Gray', out_channels=1, weights=1.1) + build_from_cfg(transform, PIPELINES) + + # test out_channels = 0 + transform = dict(type='Rgb2Gray', out_channels=0) + transform = build_from_cfg(transform, PIPELINES) + + assert str(transform) == f'Rgb2Gray(' \ + f'out_channels={0}, ' \ + f'weights={(0.299, 0.587, 0.114)})' + + results = dict() + img = mmcv.imread( + osp.join(osp.dirname(__file__), '../data/color.jpg'), 'color') + h, w, c = img.shape + seg = np.array( + Image.open(osp.join(osp.dirname(__file__), '../data/seg.png'))) + results['img'] = img + results['gt_semantic_seg'] = seg + results['seg_fields'] = ['gt_semantic_seg'] + results['img_shape'] = img.shape + results['ori_shape'] = img.shape + # Set initial values for default meta_keys + results['pad_shape'] = img.shape + results['scale_factor'] = 1.0 + + results = transform(results) + assert results['img'].shape == (h, w, c) + assert results['img_shape'] == (h, w, c) + assert results['ori_shape'] == (h, w, c) + + # test out_channels = 2 + transform = dict(type='Rgb2Gray', out_channels=2) + transform = build_from_cfg(transform, PIPELINES) + + assert str(transform) == f'Rgb2Gray(' \ + f'out_channels={2}, ' \ + f'weights={(0.299, 0.587, 0.114)})' + + results = dict() + img = mmcv.imread( + osp.join(osp.dirname(__file__), '../data/color.jpg'), 'color') + h, w, c = img.shape + seg = np.array( + Image.open(osp.join(osp.dirname(__file__), '../data/seg.png'))) + results['img'] = img + results['gt_semantic_seg'] = seg + results['seg_fields'] = ['gt_semantic_seg'] + results['img_shape'] = img.shape + results['ori_shape'] = img.shape + # Set initial values for default meta_keys + results['pad_shape'] = img.shape + results['scale_factor'] = 1.0 + + results = transform(results) + assert results['img'].shape == (h, w, 2) + assert results['img_shape'] == (h, w, 2) + assert results['ori_shape'] == (h, w, c) + + def test_seg_rescale(): results = dict() seg = np.array( From 7346b02921bd0fd34151b66fb1c336700e64c50a Mon Sep 17 00:00:00 2001 From: yamengxi <854341266@qq.com> Date: Wed, 4 Nov 2020 18:59:48 +0800 Subject: [PATCH 2/8] restore --- .pre-commit-config.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2b274aa277..d3395dc284 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -28,11 +28,11 @@ repos: args: ["--remove"] - id: mixed-line-ending args: ["--fix=lf"] - # - repo: https://github.com/jumanjihouse/pre-commit-hooks - # rev: 2.1.4 - # hooks: - # - id: markdownlint - # args: ["-r", "~MD002,~MD013,~MD029,~MD033,~MD034,~MD036"] + - repo: https://github.com/jumanjihouse/pre-commit-hooks + rev: 2.1.4 + hooks: + - id: markdownlint + args: ["-r", "~MD002,~MD013,~MD029,~MD033,~MD034,~MD036"] - repo: https://github.com/myint/docformatter rev: v1.3.1 hooks: From ee3e12d22052454489df380672cc0ba4124f0496 Mon Sep 17 00:00:00 2001 From: yamengxi <854341266@qq.com> Date: Thu, 5 Nov 2020 15:28:26 +0800 Subject: [PATCH 3/8] fix self.weights --- .pre-commit-config.yaml | 10 +++++----- mmseg/datasets/pipelines/transforms.py | 11 ++++++----- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d3395dc284..2b274aa277 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -28,11 +28,11 @@ repos: args: ["--remove"] - id: mixed-line-ending args: ["--fix=lf"] - - repo: https://github.com/jumanjihouse/pre-commit-hooks - rev: 2.1.4 - hooks: - - id: markdownlint - args: ["-r", "~MD002,~MD013,~MD029,~MD033,~MD034,~MD036"] + # - repo: https://github.com/jumanjihouse/pre-commit-hooks + # rev: 2.1.4 + # hooks: + # - id: markdownlint + # args: ["-r", "~MD002,~MD013,~MD029,~MD033,~MD034,~MD036"] - repo: https://github.com/myint/docformatter rev: v1.3.1 hooks: diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index 7f687ccf06..f9d7afaf52 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -485,8 +485,9 @@ def __init__(self, out_channels, weights=(0.299, 0.587, 0.114)): assert out_channels >= 0 self.out_channels = out_channels assert isinstance(weights, tuple) + for item in weights: + assert isinstance(item, (float, int)) self.weights = weights - self.trans_weights = np.array(weights).reshape((1, 1, -1)) def __call__(self, results): """Call function to convert RGB image to grayscale image. @@ -499,11 +500,11 @@ def __call__(self, results): """ img = results['img'] assert len(img.shape) == 3 - assert img.shape[2] == self.trans_weights.shape[2] - img = (img * self.trans_weights).sum(2) + assert img.shape[2] == len(self.weights) + weights = np.array(self.weights).reshape((1, 1, -1)) + img = (img * weights).sum(2) if self.out_channels == 0: - img = np.expand_dims(img, 2).repeat( - self.trans_weights.shape[2], axis=2) + img = np.expand_dims(img, 2).repeat(weights.shape[2], axis=2) else: img = np.expand_dims(img, 2).repeat(self.out_channels, axis=2) From b8e09869c79f6979bbb2014f8f18ef9d26d0b6c0 Mon Sep 17 00:00:00 2001 From: yamengxi <854341266@qq.com> Date: Thu, 5 Nov 2020 15:29:16 +0800 Subject: [PATCH 4/8] restore --- .pre-commit-config.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2b274aa277..d3395dc284 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -28,11 +28,11 @@ repos: args: ["--remove"] - id: mixed-line-ending args: ["--fix=lf"] - # - repo: https://github.com/jumanjihouse/pre-commit-hooks - # rev: 2.1.4 - # hooks: - # - id: markdownlint - # args: ["-r", "~MD002,~MD013,~MD029,~MD033,~MD034,~MD036"] + - repo: https://github.com/jumanjihouse/pre-commit-hooks + rev: 2.1.4 + hooks: + - id: markdownlint + args: ["-r", "~MD002,~MD013,~MD029,~MD033,~MD034,~MD036"] - repo: https://github.com/myint/docformatter rev: v1.3.1 hooks: From 8618b1dc2481f7a691895442fbf49b13455d2012 Mon Sep 17 00:00:00 2001 From: yamengxi <854341266@qq.com> Date: Mon, 9 Nov 2020 14:56:13 +0800 Subject: [PATCH 5/8] fix code --- .pre-commit-config.yaml | 10 +++++----- mmseg/datasets/pipelines/transforms.py | 20 +++++++++----------- tests/test_data/test_transform.py | 18 +++++++++--------- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d3395dc284..2b274aa277 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -28,11 +28,11 @@ repos: args: ["--remove"] - id: mixed-line-ending args: ["--fix=lf"] - - repo: https://github.com/jumanjihouse/pre-commit-hooks - rev: 2.1.4 - hooks: - - id: markdownlint - args: ["-r", "~MD002,~MD013,~MD029,~MD033,~MD034,~MD036"] + # - repo: https://github.com/jumanjihouse/pre-commit-hooks + # rev: 2.1.4 + # hooks: + # - id: markdownlint + # args: ["-r", "~MD002,~MD013,~MD029,~MD033,~MD034,~MD036"] - repo: https://github.com/myint/docformatter rev: v1.3.1 hooks: diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index f9d7afaf52..e5ee6fb605 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -208,8 +208,6 @@ def __call__(self, results): 'keep_ratio' keys are added into result dict. """ - # print(results) - print(results, results['img'].shape) if 'scale' not in results: self._random_scale(results) self._resize_img(results) @@ -466,23 +464,23 @@ def __repr__(self): @PIPELINES.register_module() -class Rgb2Gray(object): +class RGB2Gray(object): """Convert RGB image to grayscale image. This transform calculate the weighted mean of input image channels with ``weights`` and then expand the channels to ``out_channels``. When - ``out_channels`` equals 0, the number of output channels is the same as + ``out_channels`` is None, the number of output channels is the same as input channels. Args: out_channels (int): Expected number of output channels after - transforming. + transforming. Default: None. weights (tuple[float]): The weights to calculate the weighted mean. Default: (0.299, 0.587, 0.114). """ - def __init__(self, out_channels, weights=(0.299, 0.587, 0.114)): - assert out_channels >= 0 + def __init__(self, out_channels=None, weights=(0.299, 0.587, 0.114)): + assert out_channels is None or out_channels > 0 self.out_channels = out_channels assert isinstance(weights, tuple) for item in weights: @@ -502,11 +500,11 @@ def __call__(self, results): assert len(img.shape) == 3 assert img.shape[2] == len(self.weights) weights = np.array(self.weights).reshape((1, 1, -1)) - img = (img * weights).sum(2) - if self.out_channels == 0: - img = np.expand_dims(img, 2).repeat(weights.shape[2], axis=2) + img = (img * weights).sum(2, keepdims=True) + if self.out_channels is None: + img = img.repeat(weights.shape[2], axis=2) else: - img = np.expand_dims(img, 2).repeat(self.out_channels, axis=2) + img = img.repeat(self.out_channels, axis=2) results['img'] = img results['img_shape'] = img.shape diff --git a/tests/test_data/test_transform.py b/tests/test_data/test_transform.py index 02db769794..9640e15fd5 100644 --- a/tests/test_data/test_transform.py +++ b/tests/test_data/test_transform.py @@ -224,21 +224,21 @@ def test_normalize(): def test_rgb2gray(): - # test assertion out_channels should be not less than 0 + # test assertion out_channels should be greater than 0 with pytest.raises(AssertionError): - transform = dict(type='Rgb2Gray', out_channels=-1) + transform = dict(type='RGB2Gray', out_channels=-1) build_from_cfg(transform, PIPELINES) # test assertion weights should be tuple[float] with pytest.raises(AssertionError): - transform = dict(type='Rgb2Gray', out_channels=1, weights=1.1) + transform = dict(type='RGB2Gray', out_channels=1, weights=1.1) build_from_cfg(transform, PIPELINES) - # test out_channels = 0 - transform = dict(type='Rgb2Gray', out_channels=0) + # test out_channels is None + transform = dict(type='RGB2Gray') transform = build_from_cfg(transform, PIPELINES) - assert str(transform) == f'Rgb2Gray(' \ - f'out_channels={0}, ' \ + assert str(transform) == f'RGB2Gray(' \ + f'out_channels={None}, ' \ f'weights={(0.299, 0.587, 0.114)})' results = dict() @@ -262,10 +262,10 @@ def test_rgb2gray(): assert results['ori_shape'] == (h, w, c) # test out_channels = 2 - transform = dict(type='Rgb2Gray', out_channels=2) + transform = dict(type='RGB2Gray', out_channels=2) transform = build_from_cfg(transform, PIPELINES) - assert str(transform) == f'Rgb2Gray(' \ + assert str(transform) == f'RGB2Gray(' \ f'out_channels={2}, ' \ f'weights={(0.299, 0.587, 0.114)})' From db8e65eca77d9c08a64d9f41b5bba896b9fd4a19 Mon Sep 17 00:00:00 2001 From: yamengxi <854341266@qq.com> Date: Mon, 9 Nov 2020 14:57:01 +0800 Subject: [PATCH 6/8] restore --- .pre-commit-config.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2b274aa277..d3395dc284 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -28,11 +28,11 @@ repos: args: ["--remove"] - id: mixed-line-ending args: ["--fix=lf"] - # - repo: https://github.com/jumanjihouse/pre-commit-hooks - # rev: 2.1.4 - # hooks: - # - id: markdownlint - # args: ["-r", "~MD002,~MD013,~MD029,~MD033,~MD034,~MD036"] + - repo: https://github.com/jumanjihouse/pre-commit-hooks + rev: 2.1.4 + hooks: + - id: markdownlint + args: ["-r", "~MD002,~MD013,~MD029,~MD033,~MD034,~MD036"] - repo: https://github.com/myint/docformatter rev: v1.3.1 hooks: From b3331e71ef1c14c921af1107e7825e75ca2c7157 Mon Sep 17 00:00:00 2001 From: yamengxi <854341266@qq.com> Date: Mon, 9 Nov 2020 15:07:29 +0800 Subject: [PATCH 7/8] fix syntax error --- .pre-commit-config.yaml | 10 +++++----- mmseg/datasets/pipelines/transforms.py | 3 +++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d3395dc284..2b274aa277 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -28,11 +28,11 @@ repos: args: ["--remove"] - id: mixed-line-ending args: ["--fix=lf"] - - repo: https://github.com/jumanjihouse/pre-commit-hooks - rev: 2.1.4 - hooks: - - id: markdownlint - args: ["-r", "~MD002,~MD013,~MD029,~MD033,~MD034,~MD036"] + # - repo: https://github.com/jumanjihouse/pre-commit-hooks + # rev: 2.1.4 + # hooks: + # - id: markdownlint + # args: ["-r", "~MD002,~MD013,~MD029,~MD033,~MD034,~MD036"] - repo: https://github.com/myint/docformatter rev: v1.3.1 hooks: diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index 3a69b1e65c..5d38fa27aa 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -468,6 +468,7 @@ def __repr__(self): @PIPELINES.register_module() class RandomRotate(object): """Rotate the image & seg. + Args: prob (float): The rotation probability. degree (float, tuple[float]): Range of degrees to select from. If @@ -506,8 +507,10 @@ def __init__(self, def __call__(self, results): """Call function to rotate image, semantic segmentation maps. + Args: results (dict): Result dict from loading pipeline. + Returns: dict: Rotated results. """ From cc46163a333336d49179ba15016316fe7ab5956e Mon Sep 17 00:00:00 2001 From: yamengxi <854341266@qq.com> Date: Mon, 9 Nov 2020 15:08:13 +0800 Subject: [PATCH 8/8] restore --- .pre-commit-config.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2b274aa277..d3395dc284 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -28,11 +28,11 @@ repos: args: ["--remove"] - id: mixed-line-ending args: ["--fix=lf"] - # - repo: https://github.com/jumanjihouse/pre-commit-hooks - # rev: 2.1.4 - # hooks: - # - id: markdownlint - # args: ["-r", "~MD002,~MD013,~MD029,~MD033,~MD034,~MD036"] + - repo: https://github.com/jumanjihouse/pre-commit-hooks + rev: 2.1.4 + hooks: + - id: markdownlint + args: ["-r", "~MD002,~MD013,~MD029,~MD033,~MD034,~MD036"] - repo: https://github.com/myint/docformatter rev: v1.3.1 hooks: