def inference_segmentor(model, img): """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: (list[Tensor]): The segmentation result. """ cfg = model.cfg device = next(model.parameters()).device # model device # build the data pipeline 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 next(model.parameters()).is_cuda: # scatter to specified GPU data = scatter(data, [device])[0] else: data['img_metas'] = data['img_metas'][0].data # forward the model with torch.no_grad(): result = model(return_loss=False, rescale=True, **data) return result
def _prepare_input_img(img_path, test_pipeline, shape=None): # build the data pipeline if shape is not None: test_pipeline[1]['img_scale'] = shape test_pipeline[1]['transforms'][0]['keep_ratio'] = False test_pipeline = [LoadImage()] + test_pipeline[1:] test_pipeline = Compose(test_pipeline) # prepare data data = dict(img=img_path) data = test_pipeline(data) imgs = data['img'] img_metas = [i.data for i in data['img_metas']] mm_inputs = {'imgs': imgs, 'img_metas': img_metas} return mm_inputs
def __init__( self, mosaic_ratio, pipeline, img_dir, img_suffix='.jpg', ann_dir=None, seg_map_suffix='.png', split=None, data_root=None, test_mode=False, ignore_index=255, reduce_zero_label=False, ): self.pipeline = Compose(pipeline) self.img_dir = img_dir self.img_suffix = img_suffix self.ann_dir = ann_dir self.seg_map_suffix = seg_map_suffix self.split = split self.data_root = data_root self.test_mode = test_mode self.ignore_index = ignore_index self.reduce_zero_label = reduce_zero_label self.mosaic_ratio = mosaic_ratio # join paths if data_root is specified if self.data_root is not None: if not osp.isabs(self.img_dir): self.img_dir = osp.join(self.data_root, self.img_dir) if not (self.ann_dir is None or osp.isabs(self.ann_dir)): self.ann_dir = osp.join(self.data_root, self.ann_dir) if not (self.split is None or osp.isabs(self.split)): self.split = osp.join(self.data_root, self.split) # load annotations self.img_infos = self.load_annotations(self.img_dir, self.img_suffix, self.ann_dir, self.seg_map_suffix, self.split)
def _prepare_input_img(img_path: str, test_pipeline: Iterable[dict], shape: Optional[Iterable] = None, rescale_shape: Optional[Iterable] = None) -> dict: # build the data pipeline if shape is not None: test_pipeline[1]['img_scale'] = (shape[1], shape[0]) test_pipeline[1]['transforms'][0]['keep_ratio'] = False test_pipeline = [LoadImage()] + test_pipeline[1:] test_pipeline = Compose(test_pipeline) # prepare data data = dict(img=img_path) data = test_pipeline(data) imgs = data['img'] img_metas = [i.data for i in data['img_metas']] if rescale_shape is not None: for img_meta in img_metas: img_meta['ori_shape'] = tuple(rescale_shape) + (3, ) mm_inputs = {'imgs': imgs, 'img_metas': img_metas} return mm_inputs
def test_config_data_pipeline(): """Test whether the data pipeline is valid and can process corner cases. CommandLine: xdoctest -m tests/test_config.py test_config_build_data_pipeline """ from mmcv import Config from mmseg.datasets.pipelines import Compose import numpy as np config_dpath = _get_config_directory() print('Found config_dpath = {!r}'.format(config_dpath)) import glob config_fpaths = list(glob.glob(join(config_dpath, '**', '*.py'))) config_fpaths = [p for p in config_fpaths if p.find('_base_') == -1] config_names = [relpath(p, config_dpath) for p in config_fpaths] print('Using {} config files'.format(len(config_names))) for config_fname in config_names: config_fpath = join(config_dpath, config_fname) print( 'Building data pipeline, config_fpath = {!r}'.format(config_fpath)) config_mod = Config.fromfile(config_fpath) # remove loading pipeline load_img_pipeline = config_mod.train_pipeline.pop(0) to_float32 = load_img_pipeline.get('to_float32', False) config_mod.train_pipeline.pop(0) config_mod.test_pipeline.pop(0) train_pipeline = Compose(config_mod.train_pipeline) test_pipeline = Compose(config_mod.test_pipeline) img = np.random.randint(0, 255, size=(1024, 2048, 3), dtype=np.uint8) if to_float32: img = img.astype(np.float32) seg = np.random.randint(0, 255, size=(1024, 2048, 1), dtype=np.uint8) results = dict(filename='test_img.png', ori_filename='test_img.png', img=img, img_shape=img.shape, ori_shape=img.shape, gt_semantic_seg=seg) results['seg_fields'] = ['gt_semantic_seg'] print('Test training data pipeline: \n{!r}'.format(train_pipeline)) output_results = train_pipeline(results) assert output_results is not None results = dict( filename='test_img.png', ori_filename='test_img.png', img=img, img_shape=img.shape, ori_shape=img.shape, ) print('Test testing data pipeline: \n{!r}'.format(test_pipeline)) output_results = test_pipeline(results) assert output_results is not None