From b83a498d6b88de81120e21d2f827dfbc602f3966 Mon Sep 17 00:00:00 2001 From: haruishi <7902640+haruishi43@users.noreply.github.com> Date: Fri, 12 May 2023 17:28:30 +0900 Subject: [PATCH] [Feature] Prevent passed `ann_file` from silently failing to load (#2966) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Motivation While customizing the number of samples using `ann_file` for Cityscapes, I noticed that when the `ann_file` name is incorrect, it will silently resort to loading the dataset from the directory. I think when the user intends to load using `ann_file`, it should not silently fail, but give some sort of error message or warning. ## Modification I added assertion to check whether the `ann_file` exists instead of silently resorting to loading from the directory. Since `ann_file` is set to `''` by default and joined with `self.data_root`, I used `osp.isdir` to first check if `self.ann_dir` is a directory or text file. ## BC-breaking (Optional) Not that I am aware of. ## Use cases (Optional) If this PR introduces a new feature, it is better to list some use cases here, and update the documentation. --------- Co-authored-by: 谢昕辰 Co-authored-by: CSH <40987381+csatsurnh@users.noreply.github.com> --- mmseg/datasets/basesegdataset.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mmseg/datasets/basesegdataset.py b/mmseg/datasets/basesegdataset.py index b9c21fb03e..8cacca234c 100644 --- a/mmseg/datasets/basesegdataset.py +++ b/mmseg/datasets/basesegdataset.py @@ -235,7 +235,9 @@ def load_data_list(self) -> List[dict]: data_list = [] img_dir = self.data_prefix.get('img_path', None) ann_dir = self.data_prefix.get('seg_map_path', None) - if osp.isfile(self.ann_file): + if not osp.isdir(self.ann_file) and self.ann_file: + assert osp.isfile(self.ann_file), \ + f'Failed to load `ann_file` {self.ann_file}' lines = mmengine.list_from_file( self.ann_file, backend_args=self.backend_args) for line in lines: