Skip to content

Commit

Permalink
[Feature] Prevent passed ann_file from silently failing to load (#2966
Browse files Browse the repository at this point in the history
)

## 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: 谢昕辰 <[email protected]>
Co-authored-by: CSH <[email protected]>
  • Loading branch information
3 people authored May 12, 2023
1 parent b89c2c4 commit b83a498
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mmseg/datasets/basesegdataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit b83a498

Please sign in to comment.