From e9023280153555eba21b3e39a548b37c5564e900 Mon Sep 17 00:00:00 2001 From: yamengxi <854341266@qq.com> Date: Thu, 5 Nov 2020 14:37:45 +0800 Subject: [PATCH 01/11] add AdjustGamma transform --- .pre-commit-config.yaml | 10 +++---- mmseg/datasets/pipelines/transforms.py | 38 +++++++++++++++++++++++++ tests/test_data/test_transform.py | 39 ++++++++++++++++++++++++++ 3 files changed, 82 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..b33602e74d 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -1,3 +1,4 @@ +import cv2 import mmcv import numpy as np from numpy import random @@ -390,6 +391,43 @@ def __repr__(self): return repr_str +@PIPELINES.register_module() +class AdjustGamma(object): + """Using gamma correction to process the image. + + Args: + gamma (float or int): Gamma value used in gamma correction. + Default: 1.0. + """ + + def __init__(self, gamma=1.0): + assert isinstance(gamma, float) or isinstance(gamma, int) + assert gamma > 0 + self.gamma = gamma + inv_gamma = 1.0 / gamma + self.table = np.array([(i / 255.0)**inv_gamma * 255 + for i in np.arange(0, 256)]).astype('uint8') + + def __call__(self, results): + """Call function to process the image with gamma correction. + + Args: + results (dict): Result dict from loading pipeline. + + Returns: + dict: Processed results. + """ + + for i in range(results['img'].shape[2]): + results['img'][:, :, i] = cv2.LUT( + np.array(results['img'][:, :, i], dtype=np.uint8), self.table) + + return results + + def __repr__(self): + return self.__class__.__name__ + f'(gamma={self.gamma})' + + @PIPELINES.register_module() class RandomCrop(object): """Random crop the image & seg. diff --git a/tests/test_data/test_transform.py b/tests/test_data/test_transform.py index 7a1ca0dde3..5346340d01 100644 --- a/tests/test_data/test_transform.py +++ b/tests/test_data/test_transform.py @@ -1,6 +1,7 @@ import copy import os.path as osp +import cv2 import mmcv import numpy as np import pytest @@ -223,6 +224,44 @@ def test_normalize(): assert np.allclose(results['img'], converted_img) +def test_adjust_gamma(): + # test assertion if gamma <= 0 + with pytest.raises(AssertionError): + transform = dict(type='AdjustGamma', gamma=0) + build_from_cfg(transform, PIPELINES) + + # test assertion if gamma is list + with pytest.raises(AssertionError): + transform = dict(type='AdjustGamma', gamma=[1.2]) + build_from_cfg(transform, PIPELINES) + + # test with gamma = 1.2 + transform = dict(type='AdjustGamma', gamma=1.2) + transform = build_from_cfg(transform, PIPELINES) + results = dict() + img = mmcv.imread( + osp.join(osp.dirname(__file__), '../data/color.jpg'), 'color') + original_img = copy.deepcopy(img) + results['img'] = img + 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) + + inv_gamma = 1.0 / 1.2 + table = np.array([((i / 255.0)**inv_gamma) * 255 + for i in np.arange(0, 256)]).astype('uint8') + converted_img = np.empty(original_img.shape) + for i in range(original_img.shape[2]): + converted_img[:, :, i] = cv2.LUT( + np.array(original_img[:, :, i], dtype=np.uint8), table) + assert np.allclose(results['img'], converted_img) + assert str(transform) == f'AdjustGamma(gamma={1.2})' + + def test_seg_rescale(): results = dict() seg = np.array( From 25308e0c6cb583944f2593cdc81da9701ce52505 Mon Sep 17 00:00:00 2001 From: yamengxi <854341266@qq.com> Date: Thu, 5 Nov 2020 14:38:37 +0800 Subject: [PATCH 02/11] 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 90e4397dda208bdfca4a160288656cb1f3ca4058 Mon Sep 17 00:00:00 2001 From: yamengxi <854341266@qq.com> Date: Thu, 26 Nov 2020 19:53:20 +0800 Subject: [PATCH 03/11] change cv2 to mmcv --- mmseg/datasets/pipelines/transforms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index 8069ba432a..51b9b4aaac 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -678,7 +678,7 @@ def __call__(self, results): """ for i in range(results['img'].shape[2]): - results['img'][:, :, i] = cv2.LUT( + results['img'][:, :, i] = mmcv.lut_transform( np.array(results['img'][:, :, i], dtype=np.uint8), self.table) return results From b136557209b4b0ae5a7676ebc32b13559f3b7daf Mon Sep 17 00:00:00 2001 From: yamengxi <854341266@qq.com> Date: Thu, 26 Nov 2020 20:04:22 +0800 Subject: [PATCH 04/11] simplify AdjustGamma --- mmseg/datasets/pipelines/transforms.py | 5 ++--- tests/test_data/test_transform.py | 5 +---- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index 51b9b4aaac..cf784a7c3a 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -677,9 +677,8 @@ def __call__(self, results): dict: Processed results. """ - for i in range(results['img'].shape[2]): - results['img'][:, :, i] = mmcv.lut_transform( - np.array(results['img'][:, :, i], dtype=np.uint8), self.table) + results['img'] = mmcv.lut_transform( + np.array(results['img'], dtype=np.uint8), self.table) return results diff --git a/tests/test_data/test_transform.py b/tests/test_data/test_transform.py index a20adc9e63..bf9fcdf7c3 100644 --- a/tests/test_data/test_transform.py +++ b/tests/test_data/test_transform.py @@ -360,10 +360,7 @@ def test_adjust_gamma(): inv_gamma = 1.0 / 1.2 table = np.array([((i / 255.0)**inv_gamma) * 255 for i in np.arange(0, 256)]).astype('uint8') - converted_img = np.empty(original_img.shape) - for i in range(original_img.shape[2]): - converted_img[:, :, i] = cv2.LUT( - np.array(original_img[:, :, i], dtype=np.uint8), table) + converted_img = mmcv.LUT(np.array(original_img, dtype=np.uint8), table) assert np.allclose(results['img'], converted_img) assert str(transform) == f'AdjustGamma(gamma={1.2})' From 85602a7a3b74f4266b9d31e89dedee2bbd2118c6 Mon Sep 17 00:00:00 2001 From: yamengxi <854341266@qq.com> Date: Thu, 26 Nov 2020 20:10:48 +0800 Subject: [PATCH 05/11] fix syntax error --- mmseg/datasets/pipelines/transforms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index cf784a7c3a..5e079864d9 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -683,7 +683,7 @@ def __call__(self, results): return results def __repr__(self): - return self.__class__.__name__ + f'(gamma={self.gamma})' + return self.__class__.__name__ + f'(gamma={self.gamma})' @PIPELINES.register_module() From 70fa8b05e3709f06566e16422f906d97ecfa29d2 Mon Sep 17 00:00:00 2001 From: yamengxi <854341266@qq.com> Date: Thu, 26 Nov 2020 20:14:06 +0800 Subject: [PATCH 06/11] modify --- .pre-commit-config.yaml | 10 +++++----- mmseg/datasets/pipelines/transforms.py | 2 +- 2 files changed, 6 insertions(+), 6 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 5e079864d9..cf784a7c3a 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -683,7 +683,7 @@ def __call__(self, results): return results def __repr__(self): - return self.__class__.__name__ + f'(gamma={self.gamma})' + return self.__class__.__name__ + f'(gamma={self.gamma})' @PIPELINES.register_module() From 2cb4a2b4e5141352617720851ec40a9ffbd35cec Mon Sep 17 00:00:00 2001 From: yamengxi <854341266@qq.com> Date: Thu, 26 Nov 2020 20:15:47 +0800 Subject: [PATCH 07/11] fix syntax error --- .pre-commit-config.yaml | 10 +++++----- mmseg/datasets/pipelines/transforms.py | 4 ++-- 2 files changed, 7 insertions(+), 7 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: diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index cf784a7c3a..de25b63553 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -678,12 +678,12 @@ def __call__(self, results): """ results['img'] = mmcv.lut_transform( - np.array(results['img'], dtype=np.uint8), self.table) + np.array(results['img'], dtype=np.uint8), self.table) return results def __repr__(self): - return self.__class__.__name__ + f'(gamma={self.gamma})' + return self.__class__.__name__ + f'(gamma={self.gamma})' @PIPELINES.register_module() From 9367b91b5190792f65f1c385dc24b51362d42e4a Mon Sep 17 00:00:00 2001 From: yamengxi <854341266@qq.com> Date: Mon, 30 Nov 2020 14:53:02 +0800 Subject: [PATCH 08/11] change mmcv version to 1.3.0 --- mmseg/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmseg/__init__.py b/mmseg/__init__.py index ffc848a934..f301a5dc34 100644 --- a/mmseg/__init__.py +++ b/mmseg/__init__.py @@ -3,7 +3,7 @@ from .version import __version__, version_info MMCV_MIN = '1.1.4' -MMCV_MAX = '1.2.0' +MMCV_MAX = '1.3.0' def digit_version(version_str): From 30a051a4375ac1c8c1a4e5a8c7dab11ddc48cc08 Mon Sep 17 00:00:00 2001 From: yamengxi <854341266@qq.com> Date: Wed, 2 Dec 2020 11:13:47 +0800 Subject: [PATCH 09/11] fix lut function name error --- tests/test_data/test_transform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_data/test_transform.py b/tests/test_data/test_transform.py index bf9fcdf7c3..5a4e1c61db 100644 --- a/tests/test_data/test_transform.py +++ b/tests/test_data/test_transform.py @@ -360,7 +360,7 @@ def test_adjust_gamma(): inv_gamma = 1.0 / 1.2 table = np.array([((i / 255.0)**inv_gamma) * 255 for i in np.arange(0, 256)]).astype('uint8') - converted_img = mmcv.LUT(np.array(original_img, dtype=np.uint8), table) + converted_img = mmcv.lut_transform(np.array(original_img, dtype=np.uint8), table) assert np.allclose(results['img'], converted_img) assert str(transform) == f'AdjustGamma(gamma={1.2})' From ced80422ee9b388b7882fc0d0704339aad22b4ce Mon Sep 17 00:00:00 2001 From: yamengxi <854341266@qq.com> Date: Wed, 2 Dec 2020 11:26:24 +0800 Subject: [PATCH 10/11] fix syntax error --- tests/test_data/test_transform.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_data/test_transform.py b/tests/test_data/test_transform.py index 5a4e1c61db..19cf6d5337 100644 --- a/tests/test_data/test_transform.py +++ b/tests/test_data/test_transform.py @@ -360,7 +360,8 @@ def test_adjust_gamma(): inv_gamma = 1.0 / 1.2 table = np.array([((i / 255.0)**inv_gamma) * 255 for i in np.arange(0, 256)]).astype('uint8') - converted_img = mmcv.lut_transform(np.array(original_img, dtype=np.uint8), table) + converted_img = mmcv.lut_transform( + np.array(original_img, dtype=np.uint8), table) assert np.allclose(results['img'], converted_img) assert str(transform) == f'AdjustGamma(gamma={1.2})' From d6ea3c3e190e5cebc7de755e622ad87c5ed375ce Mon Sep 17 00:00:00 2001 From: yamengxi <854341266@qq.com> Date: Wed, 2 Dec 2020 11:36:51 +0800 Subject: [PATCH 11/11] fix range --- mmseg/datasets/pipelines/transforms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index de25b63553..c138b21c20 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -665,7 +665,7 @@ def __init__(self, gamma=1.0): self.gamma = gamma inv_gamma = 1.0 / gamma self.table = np.array([(i / 255.0)**inv_gamma * 255 - for i in np.arange(0, 256)]).astype('uint8') + for i in np.arange(256)]).astype('uint8') def __call__(self, results): """Call function to process the image with gamma correction.