From f54a5bfb938ac8166efc266bc575c0b2cfe50e0b Mon Sep 17 00:00:00 2001 From: linfangjian01 Date: Thu, 24 Feb 2022 14:50:19 +0800 Subject: [PATCH 01/15] [Feature] add setr_resize --- mmseg/datasets/pipelines/transforms.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index 003a564507..c00f262fee 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -105,7 +105,9 @@ def __init__(self, img_scale=None, multiscale_mode='range', ratio_range=None, - keep_ratio=True): + keep_ratio=True, + crop_size=None, + setr_multi_scale=False): if img_scale is None: self.img_scale = None else: @@ -126,6 +128,8 @@ def __init__(self, self.multiscale_mode = multiscale_mode self.ratio_range = ratio_range self.keep_ratio = keep_ratio + self.crop_size = crop_size + self.setr_multi_scale = setr_multi_scale @staticmethod def random_select(img_scales): @@ -240,6 +244,19 @@ def _random_scale(self, results): def _resize_img(self, results): """Resize images with ``results['scale']``.""" if self.keep_ratio: + if self.setr_multi_scale: + if min(results['scale']) < self.crop_size[0]: + new_short = self.crop_size[0] + else: + new_short = min(results['scale']) + + h, w = results['img'].shape[:2] + if h > w: + new_h, new_w = new_short * h / w, new_short + else: + new_h, new_w = new_short, new_short * w / h + results['scale'] = (new_h, new_w) + img, scale_factor = mmcv.imrescale( results['img'], results['scale'], return_scale=True) # the w_scale and h_scale has minor difference From 282138be702bfcc16d0cd421b9103ae13300d1c1 Mon Sep 17 00:00:00 2001 From: linfangjian01 Date: Fri, 25 Feb 2022 12:30:54 +0800 Subject: [PATCH 02/15] fix a bug --- mmseg/datasets/pipelines/transforms.py | 12 ++++++------ tests/test_data/test_transform.py | 6 ++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index c00f262fee..d985130cb2 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -99,6 +99,7 @@ class Resize(object): Default: None keep_ratio (bool): Whether to keep the aspect ratio when resizing the image. Default: True + crop_size (int): crop size for setr resize. Default: None """ def __init__(self, @@ -106,8 +107,7 @@ def __init__(self, multiscale_mode='range', ratio_range=None, keep_ratio=True, - crop_size=None, - setr_multi_scale=False): + crop_size=None): if img_scale is None: self.img_scale = None else: @@ -129,7 +129,6 @@ def __init__(self, self.ratio_range = ratio_range self.keep_ratio = keep_ratio self.crop_size = crop_size - self.setr_multi_scale = setr_multi_scale @staticmethod def random_select(img_scales): @@ -244,9 +243,10 @@ def _random_scale(self, results): def _resize_img(self, results): """Resize images with ``results['scale']``.""" if self.keep_ratio: - if self.setr_multi_scale: - if min(results['scale']) < self.crop_size[0]: - new_short = self.crop_size[0] + if self.crop_size is not None: + # keep the shape is not less than crop_size + if min(results['scale']) < self.crop_size: + new_short = self.crop_size else: new_short = min(results['scale']) diff --git a/tests/test_data/test_transform.py b/tests/test_data/test_transform.py index e9aa1d75ae..45ff5315a4 100644 --- a/tests/test_data/test_transform.py +++ b/tests/test_data/test_transform.py @@ -123,6 +123,12 @@ def test_resize(): assert int(288 * 0.5) <= resized_results['img_shape'][0] <= 288 * 2.0 assert int(512 * 0.5) <= resized_results['img_shape'][1] <= 512 * 2.0 + # test crop_size=640 + transform = dict(type='Resize', img_scale=(2560, 640), crop_size=640) + resize_module = build_from_cfg(transform, PIPELINES) + resized_results = resize_module(results.copy()) + assert resized_results['img_shape'] == (640, 1138, 3) + def test_flip(): # test assertion for invalid prob From 93961368e649c0da6f98ef7093e708c9bb849291 Mon Sep 17 00:00:00 2001 From: linfangjian01 Date: Fri, 25 Feb 2022 13:39:09 +0800 Subject: [PATCH 03/15] fix --- mmseg/datasets/pipelines/transforms.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index d985130cb2..0b974ed843 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -99,7 +99,10 @@ class Resize(object): Default: None keep_ratio (bool): Whether to keep the aspect ratio when resizing the image. Default: True - crop_size (int): crop size for setr resize. Default: None + crop_size (int, optional): The minimum size for input and the shape + of the image and seg map will not be less than ``crop_szie``. + It always be used in ``SETR`` algorithm to keep the shape. + Default: None """ def __init__(self, From 46c10ad453a546067131bb962b4f6752352aa0f7 Mon Sep 17 00:00:00 2001 From: linfangjian01 Date: Fri, 25 Feb 2022 15:33:44 +0800 Subject: [PATCH 04/15] fix --- tests/test_data/test_transform.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/test_data/test_transform.py b/tests/test_data/test_transform.py index 45ff5315a4..0c3e054242 100644 --- a/tests/test_data/test_transform.py +++ b/tests/test_data/test_transform.py @@ -129,6 +129,28 @@ def test_resize(): resized_results = resize_module(results.copy()) assert resized_results['img_shape'] == (640, 1138, 3) + # test crop_size=640 and img_scale=(512, 640) + transform = dict(type='Resize', img_scale=(512, 640), crop_size=640) + resize_module = build_from_cfg(transform, PIPELINES) + resized_results = resize_module(results.copy()) + assert resized_results['img_shape'] == (640, 1138, 3) + + # test h > w + img = np.random.randn(512, 288, 3) + 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 + transform = dict(type='Resize', img_scale=(2560, 640), crop_size=640) + resize_module = build_from_cfg(transform, PIPELINES) + resized_results = resize_module(results.copy()) + assert resized_results['img_shape'] == (1138, 640, 3) + + +test_resize() + def test_flip(): # test assertion for invalid prob From 914762574b4cad763e8042d196e73b33b0133b5b Mon Sep 17 00:00:00 2001 From: linfangjian01 Date: Fri, 25 Feb 2022 15:49:46 +0800 Subject: [PATCH 05/15] fix --- mmseg/datasets/pipelines/transforms.py | 10 +++++----- tests/test_data/test_transform.py | 13 +++++-------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index 0b974ed843..b80f77e9ac 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -110,7 +110,7 @@ def __init__(self, multiscale_mode='range', ratio_range=None, keep_ratio=True, - crop_size=None): + min_size=None): if img_scale is None: self.img_scale = None else: @@ -131,7 +131,7 @@ def __init__(self, self.multiscale_mode = multiscale_mode self.ratio_range = ratio_range self.keep_ratio = keep_ratio - self.crop_size = crop_size + self.min_size = min_size @staticmethod def random_select(img_scales): @@ -246,10 +246,10 @@ def _random_scale(self, results): def _resize_img(self, results): """Resize images with ``results['scale']``.""" if self.keep_ratio: - if self.crop_size is not None: + if self.min_size is not None: # keep the shape is not less than crop_size - if min(results['scale']) < self.crop_size: - new_short = self.crop_size + if min(results['scale']) < self.min_size: + new_short = self.min_size else: new_short = min(results['scale']) diff --git a/tests/test_data/test_transform.py b/tests/test_data/test_transform.py index 0c3e054242..fcc46e7d02 100644 --- a/tests/test_data/test_transform.py +++ b/tests/test_data/test_transform.py @@ -123,14 +123,14 @@ def test_resize(): assert int(288 * 0.5) <= resized_results['img_shape'][0] <= 288 * 2.0 assert int(512 * 0.5) <= resized_results['img_shape'][1] <= 512 * 2.0 - # test crop_size=640 - transform = dict(type='Resize', img_scale=(2560, 640), crop_size=640) + # test min_size=640 + transform = dict(type='Resize', img_scale=(2560, 640), min_size=640) resize_module = build_from_cfg(transform, PIPELINES) resized_results = resize_module(results.copy()) assert resized_results['img_shape'] == (640, 1138, 3) - # test crop_size=640 and img_scale=(512, 640) - transform = dict(type='Resize', img_scale=(512, 640), crop_size=640) + # test min_size=640 and img_scale=(512, 640) + transform = dict(type='Resize', img_scale=(512, 640), min_size=640) resize_module = build_from_cfg(transform, PIPELINES) resized_results = resize_module(results.copy()) assert resized_results['img_shape'] == (640, 1138, 3) @@ -143,15 +143,12 @@ def test_resize(): # Set initial values for default meta_keys results['pad_shape'] = img.shape results['scale_factor'] = 1.0 - transform = dict(type='Resize', img_scale=(2560, 640), crop_size=640) + transform = dict(type='Resize', img_scale=(2560, 640), min_size=640) resize_module = build_from_cfg(transform, PIPELINES) resized_results = resize_module(results.copy()) assert resized_results['img_shape'] == (1138, 640, 3) -test_resize() - - def test_flip(): # test assertion for invalid prob with pytest.raises(AssertionError): From 97f37fa501bb71e9dba275b18ae1ed5ab91187b6 Mon Sep 17 00:00:00 2001 From: linfangjian01 Date: Fri, 25 Feb 2022 15:51:12 +0800 Subject: [PATCH 06/15] fix --- 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 b80f77e9ac..a1f044c679 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -99,7 +99,7 @@ class Resize(object): Default: None keep_ratio (bool): Whether to keep the aspect ratio when resizing the image. Default: True - crop_size (int, optional): The minimum size for input and the shape + min_size (int, optional): The minimum size for input and the shape of the image and seg map will not be less than ``crop_szie``. It always be used in ``SETR`` algorithm to keep the shape. Default: None From dd397e1743c0a38ec98ad3a9d40fe4bd09927816 Mon Sep 17 00:00:00 2001 From: linfangjian01 Date: Mon, 28 Feb 2022 11:29:57 +0800 Subject: [PATCH 07/15] fix --- 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 a1f044c679..a503008f95 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -247,7 +247,7 @@ def _resize_img(self, results): """Resize images with ``results['scale']``.""" if self.keep_ratio: if self.min_size is not None: - # keep the shape is not less than crop_size + # TODO: ..... if min(results['scale']) < self.min_size: new_short = self.min_size else: From bc11c63067d79363dce5f980711422030e49a0d8 Mon Sep 17 00:00:00 2001 From: linfangjian01 Date: Mon, 28 Feb 2022 14:17:45 +0800 Subject: [PATCH 08/15] fix --- mmseg/datasets/pipelines/transforms.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index a503008f95..1d99093915 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -100,7 +100,7 @@ class Resize(object): keep_ratio (bool): Whether to keep the aspect ratio when resizing the image. Default: True min_size (int, optional): The minimum size for input and the shape - of the image and seg map will not be less than ``crop_szie``. + of the image and seg map will not be less than ``min_size``. It always be used in ``SETR`` algorithm to keep the shape. Default: None """ @@ -247,7 +247,7 @@ def _resize_img(self, results): """Resize images with ``results['scale']``.""" if self.keep_ratio: if self.min_size is not None: - # TODO: ..... + # TODO: support H not equal to w in crop size if min(results['scale']) < self.min_size: new_short = self.min_size else: From 1f9631dd4b1f3b27caaa932542de4c70b87db22d Mon Sep 17 00:00:00 2001 From: linfangjian01 Date: Mon, 28 Feb 2022 14:34:27 +0800 Subject: [PATCH 09/15] fix --- mmseg/datasets/pipelines/transforms.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index 1d99093915..04c2103cd8 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -247,7 +247,10 @@ def _resize_img(self, results): """Resize images with ``results['scale']``.""" if self.keep_ratio: if self.min_size is not None: - # TODO: support H not equal to w in crop size + # TODO: Now 'min_size' is an 'int' which means the minimum + # shape of images is (min_size, min_size,3). 'min_size' + # with tuple type will be supported, i.e. the width and + # height are not equal. if min(results['scale']) < self.min_size: new_short = self.min_size else: From 6ff3fda03ebcd53e265796b24b040e03dbc880b9 Mon Sep 17 00:00:00 2001 From: linfangjian01 Date: Mon, 28 Feb 2022 16:45:46 +0800 Subject: [PATCH 10/15] fix --- mmseg/datasets/pipelines/transforms.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index 04c2103cd8..5e504c17ea 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -101,7 +101,9 @@ class Resize(object): image. Default: True min_size (int, optional): The minimum size for input and the shape of the image and seg map will not be less than ``min_size``. - It always be used in ``SETR`` algorithm to keep the shape. + As the shape of model input is fixed and images are resized + before crop, the image shape after resize must be bigger than the + input size. Default: None """ @@ -248,7 +250,7 @@ def _resize_img(self, results): if self.keep_ratio: if self.min_size is not None: # TODO: Now 'min_size' is an 'int' which means the minimum - # shape of images is (min_size, min_size,3). 'min_size' + # shape of images is (min_size, min_size, 3). 'min_size' # with tuple type will be supported, i.e. the width and # height are not equal. if min(results['scale']) < self.min_size: From dce4bb02db0bd099add751d0ee373cbdc6075575 Mon Sep 17 00:00:00 2001 From: linfangjian01 Date: Mon, 28 Feb 2022 16:58:18 +0800 Subject: [PATCH 11/15] fix --- mmseg/datasets/pipelines/transforms.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index 5e504c17ea..bca01714b4 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -101,10 +101,9 @@ class Resize(object): image. Default: True min_size (int, optional): The minimum size for input and the shape of the image and seg map will not be less than ``min_size``. - As the shape of model input is fixed and images are resized - before crop, the image shape after resize must be bigger than the - input size. - Default: None + As the shape of model input is fixed like 'setr' and 'beit' + and images are resized before crop, the image shape after resize + must be bigger than the input size. Default: None """ def __init__(self, From caf68aeaabc04b81e9c2c5062fc7dec5b64494ba Mon Sep 17 00:00:00 2001 From: linfangjian01 Date: Tue, 1 Mar 2022 11:51:57 +0800 Subject: [PATCH 12/15] fix --- mmseg/datasets/pipelines/transforms.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index bca01714b4..f9ad9f5d37 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -101,9 +101,9 @@ class Resize(object): image. Default: True min_size (int, optional): The minimum size for input and the shape of the image and seg map will not be less than ``min_size``. - As the shape of model input is fixed like 'setr' and 'beit' - and images are resized before crop, the image shape after resize - must be bigger than the input size. Default: None + As the shape of model input is fixed like 'setr' and 'beit'. + Following the setting in these models, resized images must be + bigger than the crop size in `slide_inference`. Default: None """ def __init__(self, @@ -272,6 +272,7 @@ def _resize_img(self, results): h, w = results['img'].shape[:2] w_scale = new_w / w h_scale = new_h / h + print(img.shape[:2]) else: img, w_scale, h_scale = mmcv.imresize( results['img'], results['scale'], return_scale=True) From d500d4221080e46785509b2d34023d0a598ae3a9 Mon Sep 17 00:00:00 2001 From: linfangjian01 Date: Tue, 1 Mar 2022 14:15:28 +0800 Subject: [PATCH 13/15] fix --- mmseg/datasets/pipelines/transforms.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index f9ad9f5d37..c47a67eb0a 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -100,8 +100,8 @@ class Resize(object): keep_ratio (bool): Whether to keep the aspect ratio when resizing the image. Default: True min_size (int, optional): The minimum size for input and the shape - of the image and seg map will not be less than ``min_size``. - As the shape of model input is fixed like 'setr' and 'beit'. + of the image and seg map will not be less than `min_size`. + As the shape of model input is fixed like 'SETR' and 'BEiT'. Following the setting in these models, resized images must be bigger than the crop size in `slide_inference`. Default: None """ From 7a9df0e0efa737e8b210302be769e39fe9917248 Mon Sep 17 00:00:00 2001 From: linfangjian01 Date: Tue, 1 Mar 2022 14:37:16 +0800 Subject: [PATCH 14/15] fix --- mmseg/datasets/pipelines/transforms.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index c47a67eb0a..d1dbe4f37b 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -100,10 +100,10 @@ class Resize(object): keep_ratio (bool): Whether to keep the aspect ratio when resizing the image. Default: True min_size (int, optional): The minimum size for input and the shape - of the image and seg map will not be less than `min_size`. + of the image and seg map will not be less than ``min_size``. As the shape of model input is fixed like 'SETR' and 'BEiT'. Following the setting in these models, resized images must be - bigger than the crop size in `slide_inference`. Default: None + bigger than the crop size in ``slide_inference``. Default: None """ def __init__(self, From ebd33e0cb7038710073c3f2445ae93ba0ff7cafb Mon Sep 17 00:00:00 2001 From: linfangjian01 Date: Tue, 1 Mar 2022 14:39:42 +0800 Subject: [PATCH 15/15] fix --- mmseg/datasets/pipelines/transforms.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mmseg/datasets/pipelines/transforms.py b/mmseg/datasets/pipelines/transforms.py index d1dbe4f37b..5673b646fa 100644 --- a/mmseg/datasets/pipelines/transforms.py +++ b/mmseg/datasets/pipelines/transforms.py @@ -272,7 +272,6 @@ def _resize_img(self, results): h, w = results['img'].shape[:2] w_scale = new_w / w h_scale = new_h / h - print(img.shape[:2]) else: img, w_scale, h_scale = mmcv.imresize( results['img'], results['scale'], return_scale=True)