[Feature] Prevent passed ann_file from silently failing to load (#2966)

## 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: 谢昕辰 <xiexinch@outlook.com>
Co-authored-by: CSH <40987381+csatsurnh@users.noreply.github.com>
This commit is contained in:
haruishi 2023-05-12 17:28:30 +09:00 committed by GitHub
parent b89c2c4cb7
commit b83a498d6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -235,7 +235,9 @@ class BaseSegDataset(BaseDataset):
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: