[Fix] Robust mapping from image path to seg map path (#3091)

## Motivation

Suppose an image is named `jpg.jpg` and its corresponding segmap is
named `jpg.png`.
The original implementation will try to read segmap from `png.png` and
causes FileNotfoundError

## Modification

Only replace the suffix, instead of full string search and replacement. 

## BC-breaking (Optional)

Probably no. 

## Use cases (Optional)


## Checklist

1. Pre-commit or other linting tools are used to fix the potential lint
issues.
2. The modification is covered by complete unit tests. If not, please
add more unit test to ensure the correctness.
3. If the modification has potential influence on downstream projects,
this PR should be tested with downstream projects, like MMDet or
MMDet3D.
4. The documentation has been modified accordingly, like docstring or
example tutorials.

---------

Co-authored-by: 谢昕辰 <xiexinch@outlook.com>
Co-authored-by: CSH <40987381+csatsurnh@users.noreply.github.com>
This commit is contained in:
WRH 2023-06-16 11:07:17 +08:00 committed by GitHub
parent 279b89783e
commit c30d5060f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -252,6 +252,7 @@ class BaseSegDataset(BaseDataset):
data_info['seg_fields'] = []
data_list.append(data_info)
else:
_suffix_len = len(self.img_suffix)
for img in fileio.list_dir_or_file(
dir_path=img_dir,
list_dir=False,
@ -260,7 +261,7 @@ class BaseSegDataset(BaseDataset):
backend_args=self.backend_args):
data_info = dict(img_path=osp.join(img_dir, img))
if ann_dir is not None:
seg_map = img.replace(self.img_suffix, self.seg_map_suffix)
seg_map = img[:-_suffix_len] + self.seg_map_suffix
data_info['seg_map_path'] = osp.join(ann_dir, seg_map)
data_info['label_map'] = self.label_map
data_info['reduce_zero_label'] = self.reduce_zero_label