diff --git a/mmseg/datasets/chase_db1.py b/mmseg/datasets/chase_db1.py index 7f14b2da0e..5cdc8d8d41 100644 --- a/mmseg/datasets/chase_db1.py +++ b/mmseg/datasets/chase_db1.py @@ -1,5 +1,4 @@ # Copyright (c) OpenMMLab. All rights reserved. -import os.path as osp from .builder import DATASETS from .custom import CustomDataset @@ -25,4 +24,4 @@ def __init__(self, **kwargs): seg_map_suffix='_1stHO.png', reduce_zero_label=False, **kwargs) - assert osp.exists(self.img_dir) + assert self.file_client.exists(self.img_dir) diff --git a/mmseg/datasets/custom.py b/mmseg/datasets/custom.py index fe29dcf9c0..2649a58147 100644 --- a/mmseg/datasets/custom.py +++ b/mmseg/datasets/custom.py @@ -68,6 +68,9 @@ class CustomDataset(Dataset): Default: None gt_seg_map_loader_cfg (dict, optional): build LoadAnnotations to load gt for evaluation, load from disk by default. Default: None. + file_client_args (dict): Arguments to instantiate a FileClient. + See :class:`mmcv.fileio.FileClient` for details. + Defaults to ``dict(backend='disk')``. """ CLASSES = None @@ -87,7 +90,8 @@ def __init__(self, reduce_zero_label=False, classes=None, palette=None, - gt_seg_map_loader_cfg=None): + gt_seg_map_loader_cfg=None, + file_client_args=dict(backend='disk')): self.pipeline = Compose(pipeline) self.img_dir = img_dir self.img_suffix = img_suffix @@ -105,6 +109,9 @@ def __init__(self, ) if gt_seg_map_loader_cfg is None else LoadAnnotations( **gt_seg_map_loader_cfg) + self.file_client_args = file_client_args + self.file_client = mmcv.FileClient.infer_client(self.file_client_args) + if test_mode: assert self.CLASSES is not None, \ '`cls.CLASSES` or `classes` should be specified when testing' @@ -146,16 +153,21 @@ def load_annotations(self, img_dir, img_suffix, ann_dir, seg_map_suffix, img_infos = [] if split is not None: - with open(split) as f: - for line in f: - img_name = line.strip() - img_info = dict(filename=img_name + img_suffix) - if ann_dir is not None: - seg_map = img_name + seg_map_suffix - img_info['ann'] = dict(seg_map=seg_map) - img_infos.append(img_info) + lines = mmcv.list_from_file( + split, file_client_args=self.file_client_args) + for line in lines: + img_name = line.strip() + img_info = dict(filename=img_name + img_suffix) + if ann_dir is not None: + seg_map = img_name + seg_map_suffix + img_info['ann'] = dict(seg_map=seg_map) + img_infos.append(img_info) else: - for img in mmcv.scandir(img_dir, img_suffix, recursive=True): + for img in self.file_client.list_dir_or_file( + dir_path=img_dir, + list_dir=False, + suffix=img_suffix, + recursive=True): img_info = dict(filename=img) if ann_dir is not None: seg_map = img.replace(img_suffix, seg_map_suffix) diff --git a/mmseg/datasets/drive.py b/mmseg/datasets/drive.py index 650991147c..d44fb0da71 100644 --- a/mmseg/datasets/drive.py +++ b/mmseg/datasets/drive.py @@ -1,5 +1,4 @@ # Copyright (c) OpenMMLab. All rights reserved. -import os.path as osp from .builder import DATASETS from .custom import CustomDataset @@ -25,4 +24,4 @@ def __init__(self, **kwargs): seg_map_suffix='_manual1.png', reduce_zero_label=False, **kwargs) - assert osp.exists(self.img_dir) + assert self.file_client.exists(self.img_dir) diff --git a/mmseg/datasets/hrf.py b/mmseg/datasets/hrf.py index e4e10aeafd..cf3ea8d79c 100644 --- a/mmseg/datasets/hrf.py +++ b/mmseg/datasets/hrf.py @@ -1,5 +1,4 @@ # Copyright (c) OpenMMLab. All rights reserved. -import os.path as osp from .builder import DATASETS from .custom import CustomDataset @@ -25,4 +24,4 @@ def __init__(self, **kwargs): seg_map_suffix='.png', reduce_zero_label=False, **kwargs) - assert osp.exists(self.img_dir) + assert self.file_client.exists(self.img_dir) diff --git a/mmseg/datasets/isaid.py b/mmseg/datasets/isaid.py index d6108b280b..db24f93765 100644 --- a/mmseg/datasets/isaid.py +++ b/mmseg/datasets/isaid.py @@ -1,5 +1,4 @@ # Copyright (c) OpenMMLab. All rights reserved. -import os.path as osp import mmcv from mmcv.utils import print_log @@ -35,7 +34,7 @@ def __init__(self, **kwargs): seg_map_suffix='.png', ignore_index=255, **kwargs) - assert osp.exists(self.img_dir) + assert self.file_client.exists(self.img_dir) def load_annotations(self, img_dir, diff --git a/mmseg/datasets/pascal_context.py b/mmseg/datasets/pascal_context.py index 1e7a09d723..efacee0f3f 100644 --- a/mmseg/datasets/pascal_context.py +++ b/mmseg/datasets/pascal_context.py @@ -1,5 +1,4 @@ # Copyright (c) OpenMMLab. All rights reserved. -import os.path as osp from .builder import DATASETS from .custom import CustomDataset @@ -52,7 +51,7 @@ def __init__(self, split, **kwargs): split=split, reduce_zero_label=False, **kwargs) - assert osp.exists(self.img_dir) and self.split is not None + assert self.file_client.exists(self.img_dir) and self.split is not None @DATASETS.register_module() @@ -101,4 +100,4 @@ def __init__(self, split, **kwargs): split=split, reduce_zero_label=True, **kwargs) - assert osp.exists(self.img_dir) and self.split is not None + assert self.file_client.exists(self.img_dir) and self.split is not None