From 40c8fb081eb1c32f32c1a561366b4a0bd47018b0 Mon Sep 17 00:00:00 2001 From: jinwonkim93 Date: Mon, 1 Aug 2022 06:13:00 +0000 Subject: [PATCH 1/3] [Fix] Fix mmseg.api.inference inference_segmentor Motivation Fix inference_segmentor not working with multiple images path or images. List[str/ndarray] Modification - process images if instance is list --- mmseg/apis/inference.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/mmseg/apis/inference.py b/mmseg/apis/inference.py index a2a8ab0cb0..ef0b80f017 100644 --- a/mmseg/apis/inference.py +++ b/mmseg/apis/inference.py @@ -67,7 +67,7 @@ def __call__(self, results): return results -def inference_segmentor(model, img): +def inference_segmentor(model, imgs): """Inference image(s) with the segmentor. Args: @@ -84,14 +84,23 @@ def inference_segmentor(model, img): test_pipeline = [LoadImage()] + cfg.data.test.pipeline[1:] test_pipeline = Compose(test_pipeline) # prepare data - data = dict(img=img) - data = test_pipeline(data) - data = collate([data], samples_per_gpu=1) + if isinstance(imgs, list): + data = [] + for img in imgs: + img_data = dict(img=img) + img_data = test_pipeline(img_data) + data.append(img_data) + data = collate(data, samples_per_gpu=len(imgs)) + else: + data = dict(img=imgs) + data = test_pipeline(data) + data = collate([data], samples_per_gpu=1) if next(model.parameters()).is_cuda: # scatter to specified GPU data = scatter(data, [device])[0] else: data['img_metas'] = [i.data[0] for i in data['img_metas']] + breakpoint() # forward the model with torch.no_grad(): From 1cf65647bb00fc98f90d46c99da6223dcbc6f5d0 Mon Sep 17 00:00:00 2001 From: jinwonkim93 Date: Mon, 1 Aug 2022 07:03:48 +0000 Subject: [PATCH 2/3] fix typo --- mmseg/apis/inference.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mmseg/apis/inference.py b/mmseg/apis/inference.py index ef0b80f017..4321c1ac37 100644 --- a/mmseg/apis/inference.py +++ b/mmseg/apis/inference.py @@ -100,7 +100,6 @@ def inference_segmentor(model, imgs): data = scatter(data, [device])[0] else: data['img_metas'] = [i.data[0] for i in data['img_metas']] - breakpoint() # forward the model with torch.no_grad(): From 2f6c08a811824c00d4f66225b563c0592f2ec7c2 Mon Sep 17 00:00:00 2001 From: whooray Date: Fri, 12 Aug 2022 21:16:25 +0900 Subject: [PATCH 3/3] Update mmseg/apis/inference.py Co-authored-by: Hakjin Lee --- mmseg/apis/inference.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/mmseg/apis/inference.py b/mmseg/apis/inference.py index 4321c1ac37..5bbe66634e 100644 --- a/mmseg/apis/inference.py +++ b/mmseg/apis/inference.py @@ -84,17 +84,13 @@ def inference_segmentor(model, imgs): test_pipeline = [LoadImage()] + cfg.data.test.pipeline[1:] test_pipeline = Compose(test_pipeline) # prepare data - if isinstance(imgs, list): - data = [] - for img in imgs: - img_data = dict(img=img) - img_data = test_pipeline(img_data) - data.append(img_data) - data = collate(data, samples_per_gpu=len(imgs)) - else: - data = dict(img=imgs) - data = test_pipeline(data) - data = collate([data], samples_per_gpu=1) + data = [] + imgs = imgs if isinstance(imgs, list) else [imgs] + for img in imgs: + img_data = dict(img=img) + img_data = test_pipeline(img_data) + data.append(img_data) + data = collate(data, samples_per_gpu=len(imgs)) if next(model.parameters()).is_cuda: # scatter to specified GPU data = scatter(data, [device])[0]