From 41ccbdc5c92f9e240da6ba8009a110ff9f1bb69d Mon Sep 17 00:00:00 2001 From: YouqingXiaozhua <843213558@qq.com> Date: Tue, 24 Nov 2020 20:55:13 +0800 Subject: [PATCH 1/5] add inference test --- tests/test_inference.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 tests/test_inference.py diff --git a/tests/test_inference.py b/tests/test_inference.py new file mode 100644 index 00000000000..457c80d8d53 --- /dev/null +++ b/tests/test_inference.py @@ -0,0 +1,25 @@ +import os.path as osp + +from mmseg.apis import inference_segmentor, init_segmentor +import mmcv + + +def test_test_time_augmentation_on_cpu(): + config_file = 'configs/pspnet/pspnet_r50-d8_512x1024_40k_cityscapes.py' + config = mmcv.Config.fromfile(config_file) + + # Replace SyncBN with BN to inference on CPU + norm_cfg = dict(type='BN', requires_grad=True) + config.model.backbone.norm_cfg = norm_cfg + config.model.decode_head.norm_cfg = norm_cfg + config.model.auxiliary_head.norm_cfg = norm_cfg + + # Enable test time augmentation + config.test_pipeline[1].flip = True + + checkpoint_file = None + model = init_segmentor(config, checkpoint_file, device='cpu') + + img = mmcv.imread(osp.join(osp.dirname(__file__), 'data/color.jpg'), 'color') + result = inference_segmentor(model, img) + assert result[0].shape == (288, 512) From ae835a218d69c10eff412113fd70693370941779 Mon Sep 17 00:00:00 2001 From: YouqingXiaozhua <843213558@qq.com> Date: Tue, 24 Nov 2020 21:08:21 +0800 Subject: [PATCH 2/5] fix E501 line too long (81 > 79 characters --- tests/test_inference.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_inference.py b/tests/test_inference.py index 457c80d8d53..241d62c9f4f 100644 --- a/tests/test_inference.py +++ b/tests/test_inference.py @@ -1,8 +1,9 @@ import os.path as osp -from mmseg.apis import inference_segmentor, init_segmentor import mmcv +from mmseg.apis import inference_segmentor, init_segmentor + def test_test_time_augmentation_on_cpu(): config_file = 'configs/pspnet/pspnet_r50-d8_512x1024_40k_cityscapes.py' @@ -20,6 +21,7 @@ def test_test_time_augmentation_on_cpu(): checkpoint_file = None model = init_segmentor(config, checkpoint_file, device='cpu') - img = mmcv.imread(osp.join(osp.dirname(__file__), 'data/color.jpg'), 'color') + img = mmcv.imread( + osp.join(osp.dirname(__file__), 'data/color.jpg'), 'color') result = inference_segmentor(model, img) assert result[0].shape == (288, 512) From 639db7f11492d1c489548a027b79ac4584a3c128 Mon Sep 17 00:00:00 2001 From: YouqingXiaozhua <843213558@qq.com> Date: Tue, 24 Nov 2020 21:53:24 +0800 Subject: [PATCH 3/5] fix wrong config path --- tests/test_inference.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_inference.py b/tests/test_inference.py index 241d62c9f4f..0d8bcb88ef8 100644 --- a/tests/test_inference.py +++ b/tests/test_inference.py @@ -16,7 +16,7 @@ def test_test_time_augmentation_on_cpu(): config.model.auxiliary_head.norm_cfg = norm_cfg # Enable test time augmentation - config.test_pipeline[1].flip = True + config.data.test.pipeline[1].flip = True checkpoint_file = None model = init_segmentor(config, checkpoint_file, device='cpu') From 999a0c531bcfd4c92a7fa6cd55852fa65032e964 Mon Sep 17 00:00:00 2001 From: YouqingXiaozhua <843213558@qq.com> Date: Tue, 24 Nov 2020 22:06:32 +0800 Subject: [PATCH 4/5] fix num of augmentations (2) != num of image meta (1) --- mmseg/apis/inference.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmseg/apis/inference.py b/mmseg/apis/inference.py index 6fa7e3b3430..7cebac986d9 100644 --- a/mmseg/apis/inference.py +++ b/mmseg/apis/inference.py @@ -89,7 +89,7 @@ def inference_segmentor(model, img): # scatter to specified GPU data = scatter(data, [device])[0] else: - data['img_metas'] = data['img_metas'][0].data + data['img_metas'] = [i.data[0] for i in data['img_metas']] # forward the model with torch.no_grad(): From ad71d79ffc5b402a17ab8b978c93e6a01c36b3e5 Mon Sep 17 00:00:00 2001 From: Jerry Jiarui XU Date: Sun, 13 Dec 2020 23:22:58 -0800 Subject: [PATCH 5/5] Update test_inference.py --- tests/test_inference.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_inference.py b/tests/test_inference.py index 0d8bcb88ef8..046e036281d 100644 --- a/tests/test_inference.py +++ b/tests/test_inference.py @@ -9,6 +9,8 @@ def test_test_time_augmentation_on_cpu(): config_file = 'configs/pspnet/pspnet_r50-d8_512x1024_40k_cityscapes.py' config = mmcv.Config.fromfile(config_file) + # Remove pretrain model download for testing + config.model.pretrained = None # Replace SyncBN with BN to inference on CPU norm_cfg = dict(type='BN', requires_grad=True) config.model.backbone.norm_cfg = norm_cfg