mmsegmentation/mmseg/apis/inference.py

185 lines
6.8 KiB
Python
Raw Normal View History

# Copyright (c) OpenMMLab. All rights reserved.
2022-08-05 20:18:55 +08:00
import warnings
from pathlib import Path
from typing import Optional, Union
2020-07-07 20:52:19 +08:00
import mmcv
import numpy as np
2020-07-07 20:52:19 +08:00
import torch
from mmengine import Config
from mmengine.registry import init_default_scope
2022-08-05 20:18:55 +08:00
from mmengine.runner import load_checkpoint
from mmengine.utils import mkdir_or_exist
2020-07-07 20:52:19 +08:00
from mmseg.models import BaseSegmentor
from mmseg.registry import MODELS
from mmseg.structures import SegDataSample
2022-08-05 20:18:55 +08:00
from mmseg.utils import SampleList, dataset_aliases, get_classes, get_palette
from mmseg.visualization import SegLocalVisualizer
from .utils import ImageType, _preprare_data
2020-07-07 20:52:19 +08:00
2022-08-05 20:18:55 +08:00
def init_model(config: Union[str, Path, Config],
checkpoint: Optional[str] = None,
device: str = 'cuda:0',
cfg_options: Optional[dict] = None):
2020-07-07 20:52:19 +08:00
"""Initialize a segmentor from config file.
Args:
2022-08-05 20:18:55 +08:00
config (str, :obj:`Path`, or :obj:`mmengine.Config`): Config file path,
:obj:`Path`, or the config object.
2020-07-07 20:52:19 +08:00
checkpoint (str, optional): Checkpoint path. If left as None, the model
will not load any weights.
device (str, optional) CPU/CUDA device option. Default 'cuda:0'.
Use 'cpu' for loading model on CPU.
2022-08-05 20:18:55 +08:00
cfg_options (dict, optional): Options to override some settings in
the used config.
2020-07-07 20:52:19 +08:00
Returns:
nn.Module: The constructed segmentor.
"""
2022-08-05 20:18:55 +08:00
if isinstance(config, (str, Path)):
config = Config.fromfile(config)
2022-08-05 20:18:55 +08:00
elif not isinstance(config, Config):
2020-07-07 20:52:19 +08:00
raise TypeError('config must be a filename or Config object, '
'but got {}'.format(type(config)))
2022-08-05 20:18:55 +08:00
if cfg_options is not None:
config.merge_from_dict(cfg_options)
elif 'init_cfg' in config.model.backbone:
config.model.backbone.init_cfg = None
2020-07-07 20:52:19 +08:00
config.model.pretrained = None
config.model.train_cfg = None
init_default_scope(config.get('default_scope', 'mmseg'))
model = MODELS.build(config.model)
2020-07-07 20:52:19 +08:00
if checkpoint is not None:
checkpoint = load_checkpoint(model, checkpoint, map_location='cpu')
2022-08-05 20:18:55 +08:00
dataset_meta = checkpoint['meta'].get('dataset_meta', None)
# save the dataset_meta in the model for convenience
if 'dataset_meta' in checkpoint.get('meta', {}):
# mmseg 1.x
model.dataset_meta = dataset_meta
elif 'CLASSES' in checkpoint.get('meta', {}):
# < mmseg 1.x
classes = checkpoint['meta']['CLASSES']
palette = checkpoint['meta']['PALETTE']
model.dataset_meta = {'classes': classes, 'palette': palette}
else:
warnings.simplefilter('once')
warnings.warn(
'dataset_meta or class names are not saved in the '
'checkpoint\'s meta data, classes and palette will be'
'set according to num_classes ')
num_classes = model.decode_head.num_classes
dataset_name = None
for name in dataset_aliases.keys():
if len(get_classes(name)) == num_classes:
dataset_name = name
break
if dataset_name is None:
warnings.warn(
'No suitable dataset found, use Cityscapes by default')
dataset_name = 'cityscapes'
model.dataset_meta = {
'classes': get_classes(dataset_name),
'palette': get_palette(dataset_name)
}
2020-07-07 20:52:19 +08:00
model.cfg = config # save the config in the model for convenience
model.to(device)
model.eval()
return model
def inference_model(model: BaseSegmentor,
img: ImageType) -> Union[SegDataSample, SampleList]:
2020-07-07 20:52:19 +08:00
"""Inference image(s) with the segmentor.
Args:
model (nn.Module): The loaded segmentor.
imgs (str/ndarray or list[str/ndarray]): Either image files or loaded
images.
Returns:
:obj:`SegDataSample` or list[:obj:`SegDataSample`]:
If imgs is a list or tuple, the same length list type results
will be returned, otherwise return the segmentation results directly.
2020-07-07 20:52:19 +08:00
"""
# prepare data
data, is_batch = _preprare_data(img, model)
2020-07-07 20:52:19 +08:00
# forward the model
with torch.no_grad():
results = model.test_step(data)
return results if is_batch else results[0]
def show_result_pyplot(model: BaseSegmentor,
img: Union[str, np.ndarray],
result: SegDataSample,
opacity: float = 0.5,
title: str = '',
draw_gt: bool = True,
draw_pred: bool = True,
wait_time: float = 0,
show: bool = True,
[CodeCamp2023-154] Add semantic label to the segmentation visualization results (#3229) Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers. ## Motivation [Add semantic label to the segmentation visualization results 分割可视化结果中加上语义信息 #154](https://github.com/open-mmlab/OpenMMLabCamp/discussions/154) corresponding issue: [跑出来结果之后怎么在结果图片上获取各个语意部分的区域信息? #2578](https://github.com/open-mmlab/mmsegmentation/issues/2578) ## Modification 1. mmseg/apis/inference.py, add withLabels in visualizer.add_datasample call, to indicate whether add semantic label 2. mmseg/visualization/local_visualizer.py, add semantic labels by opencv; modify the demo comment description 3. mmseg/utils/__init__.py, add bdd100k datasets to test local_visualizer.py **Current visualize result** <img width="637" alt="image" src="https://github.com/open-mmlab/mmsegmentation/assets/35064479/6ef6ce02-1d82-46f8-bde9-a1d69ff62df8"> **Add semantic label** <img width="637" alt="image" src="https://github.com/open-mmlab/mmsegmentation/assets/35064479/00716679-b43a-4794-8499-9bfecdb4b78b"> ## Test results **tests/test_visualization/test_local_visualizer.py** test results:(MMSegmentation/tests/data/pseudo_cityscapes_dataset/leftImg8bit/val/frankfurt/frankfurt_000000_000294_leftImg8bit.png) <img width="643" alt="image" src="https://github.com/open-mmlab/mmsegmentation/assets/35064479/6792b7d2-2512-4ea9-8500-1a7ed2d5e0dc"> **demo/inference_demo.ipynb** test results: <img width="966" alt="image" src="https://github.com/open-mmlab/mmsegmentation/assets/35064479/dfc0147e-fb1a-490a-b6ff-a8b209352d9b"> ----- ## Drawbacks config opencv thickness according to image size <img width="496" alt="image" src="https://github.com/open-mmlab/mmsegmentation/assets/35064479/0a54d72c-62b1-422c-89ae-69dc753fe0fc"> I have no idea of dealing with label overlapping for the time being
2023-08-01 14:38:33 +08:00
withLabels: Optional[bool] = True,
save_dir=None,
out_file=None):
2020-07-07 20:52:19 +08:00
"""Visualize the segmentation results on the image.
Args:
model (nn.Module): The loaded segmentor.
img (str or np.ndarray): Image filename or loaded image.
result (SegDataSample): The prediction SegDataSample result.
opacity(float): Opacity of painted segmentation map.
Default 0.5. Must be in (0, 1] range.
title (str): The title of pyplot figure.
Default is ''.
draw_gt (bool): Whether to draw GT SegDataSample. Default to True.
draw_pred (bool): Whether to draw Prediction SegDataSample.
Defaults to True.
wait_time (float): The interval of show (s). 0 is the special value
that means "forever". Defaults to 0.
show (bool): Whether to display the drawn image.
Default to True.
[CodeCamp2023-154] Add semantic label to the segmentation visualization results (#3229) Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers. ## Motivation [Add semantic label to the segmentation visualization results 分割可视化结果中加上语义信息 #154](https://github.com/open-mmlab/OpenMMLabCamp/discussions/154) corresponding issue: [跑出来结果之后怎么在结果图片上获取各个语意部分的区域信息? #2578](https://github.com/open-mmlab/mmsegmentation/issues/2578) ## Modification 1. mmseg/apis/inference.py, add withLabels in visualizer.add_datasample call, to indicate whether add semantic label 2. mmseg/visualization/local_visualizer.py, add semantic labels by opencv; modify the demo comment description 3. mmseg/utils/__init__.py, add bdd100k datasets to test local_visualizer.py **Current visualize result** <img width="637" alt="image" src="https://github.com/open-mmlab/mmsegmentation/assets/35064479/6ef6ce02-1d82-46f8-bde9-a1d69ff62df8"> **Add semantic label** <img width="637" alt="image" src="https://github.com/open-mmlab/mmsegmentation/assets/35064479/00716679-b43a-4794-8499-9bfecdb4b78b"> ## Test results **tests/test_visualization/test_local_visualizer.py** test results:(MMSegmentation/tests/data/pseudo_cityscapes_dataset/leftImg8bit/val/frankfurt/frankfurt_000000_000294_leftImg8bit.png) <img width="643" alt="image" src="https://github.com/open-mmlab/mmsegmentation/assets/35064479/6792b7d2-2512-4ea9-8500-1a7ed2d5e0dc"> **demo/inference_demo.ipynb** test results: <img width="966" alt="image" src="https://github.com/open-mmlab/mmsegmentation/assets/35064479/dfc0147e-fb1a-490a-b6ff-a8b209352d9b"> ----- ## Drawbacks config opencv thickness according to image size <img width="496" alt="image" src="https://github.com/open-mmlab/mmsegmentation/assets/35064479/0a54d72c-62b1-422c-89ae-69dc753fe0fc"> I have no idea of dealing with label overlapping for the time being
2023-08-01 14:38:33 +08:00
withLabels(bool, optional): Add semantic labels in visualization
result, Default to True.
save_dir (str, optional): Save file dir for all storage backends.
If it is None, the backend storage will not save any data.
out_file (str, optional): Path to output file. Default to None.
[CodeCamp2023-154] Add semantic label to the segmentation visualization results (#3229) Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers. ## Motivation [Add semantic label to the segmentation visualization results 分割可视化结果中加上语义信息 #154](https://github.com/open-mmlab/OpenMMLabCamp/discussions/154) corresponding issue: [跑出来结果之后怎么在结果图片上获取各个语意部分的区域信息? #2578](https://github.com/open-mmlab/mmsegmentation/issues/2578) ## Modification 1. mmseg/apis/inference.py, add withLabels in visualizer.add_datasample call, to indicate whether add semantic label 2. mmseg/visualization/local_visualizer.py, add semantic labels by opencv; modify the demo comment description 3. mmseg/utils/__init__.py, add bdd100k datasets to test local_visualizer.py **Current visualize result** <img width="637" alt="image" src="https://github.com/open-mmlab/mmsegmentation/assets/35064479/6ef6ce02-1d82-46f8-bde9-a1d69ff62df8"> **Add semantic label** <img width="637" alt="image" src="https://github.com/open-mmlab/mmsegmentation/assets/35064479/00716679-b43a-4794-8499-9bfecdb4b78b"> ## Test results **tests/test_visualization/test_local_visualizer.py** test results:(MMSegmentation/tests/data/pseudo_cityscapes_dataset/leftImg8bit/val/frankfurt/frankfurt_000000_000294_leftImg8bit.png) <img width="643" alt="image" src="https://github.com/open-mmlab/mmsegmentation/assets/35064479/6792b7d2-2512-4ea9-8500-1a7ed2d5e0dc"> **demo/inference_demo.ipynb** test results: <img width="966" alt="image" src="https://github.com/open-mmlab/mmsegmentation/assets/35064479/dfc0147e-fb1a-490a-b6ff-a8b209352d9b"> ----- ## Drawbacks config opencv thickness according to image size <img width="496" alt="image" src="https://github.com/open-mmlab/mmsegmentation/assets/35064479/0a54d72c-62b1-422c-89ae-69dc753fe0fc"> I have no idea of dealing with label overlapping for the time being
2023-08-01 14:38:33 +08:00
Returns:
np.ndarray: the drawn image which channel is RGB.
2020-07-07 20:52:19 +08:00
"""
if hasattr(model, 'module'):
model = model.module
if isinstance(img, str):
image = mmcv.imread(img, channel_order='rgb')
else:
image = img
if save_dir is not None:
mkdir_or_exist(save_dir)
# init visualizer
visualizer = SegLocalVisualizer(
vis_backends=[dict(type='LocalVisBackend')],
save_dir=save_dir,
alpha=opacity)
visualizer.dataset_meta = dict(
classes=model.dataset_meta['classes'],
palette=model.dataset_meta['palette'])
visualizer.add_datasample(
name=title,
image=image,
data_sample=result,
draw_gt=draw_gt,
draw_pred=draw_pred,
wait_time=wait_time,
out_file=out_file,
[CodeCamp2023-154] Add semantic label to the segmentation visualization results (#3229) Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers. ## Motivation [Add semantic label to the segmentation visualization results 分割可视化结果中加上语义信息 #154](https://github.com/open-mmlab/OpenMMLabCamp/discussions/154) corresponding issue: [跑出来结果之后怎么在结果图片上获取各个语意部分的区域信息? #2578](https://github.com/open-mmlab/mmsegmentation/issues/2578) ## Modification 1. mmseg/apis/inference.py, add withLabels in visualizer.add_datasample call, to indicate whether add semantic label 2. mmseg/visualization/local_visualizer.py, add semantic labels by opencv; modify the demo comment description 3. mmseg/utils/__init__.py, add bdd100k datasets to test local_visualizer.py **Current visualize result** <img width="637" alt="image" src="https://github.com/open-mmlab/mmsegmentation/assets/35064479/6ef6ce02-1d82-46f8-bde9-a1d69ff62df8"> **Add semantic label** <img width="637" alt="image" src="https://github.com/open-mmlab/mmsegmentation/assets/35064479/00716679-b43a-4794-8499-9bfecdb4b78b"> ## Test results **tests/test_visualization/test_local_visualizer.py** test results:(MMSegmentation/tests/data/pseudo_cityscapes_dataset/leftImg8bit/val/frankfurt/frankfurt_000000_000294_leftImg8bit.png) <img width="643" alt="image" src="https://github.com/open-mmlab/mmsegmentation/assets/35064479/6792b7d2-2512-4ea9-8500-1a7ed2d5e0dc"> **demo/inference_demo.ipynb** test results: <img width="966" alt="image" src="https://github.com/open-mmlab/mmsegmentation/assets/35064479/dfc0147e-fb1a-490a-b6ff-a8b209352d9b"> ----- ## Drawbacks config opencv thickness according to image size <img width="496" alt="image" src="https://github.com/open-mmlab/mmsegmentation/assets/35064479/0a54d72c-62b1-422c-89ae-69dc753fe0fc"> I have no idea of dealing with label overlapping for the time being
2023-08-01 14:38:33 +08:00
show=show,
withLabels=withLabels)
vis_img = visualizer.get_image()
return vis_img