Skip to content

Commit

Permalink
Merge pull request open-mmlab#16 from opencv/reading_annotation_files…
Browse files Browse the repository at this point in the history
…_by_pattern

Reading annotation files by pattern
  • Loading branch information
Ilya-Krylov authored Apr 14, 2020
2 parents 390b7d0 + cfef91b commit a5c1ee7
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion mmdet/datasets/builder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import copy
import glob
import os

from mmdet.utils import build_from_cfg

from .dataset_wrappers import ConcatDataset, RepeatDataset
from .registry import DATASETS

Expand All @@ -11,6 +14,25 @@ def _concat_dataset(cfg, default_args=None):
seg_prefixes = cfg.get('seg_prefix', None)
proposal_files = cfg.get('proposal_file', None)

if cfg.get('img_prefix_auto', False):
del cfg['img_prefix_auto']
assert img_prefixes is None
if cfg['type'] == 'CustomCocoDataset':
# assuming following dataset structure:
# dataset_root
# ├── annotations
# │ ├── instances_train.json
# │ ├── ...
# ├── images
# ├── image_name1
# ├── image_name2
# ├── ...
# and file_name inside instances_train.json is relative to <dataset_root>/images
img_prefixes = \
[os.path.join(os.path.dirname(ann_file), '..', 'images') for ann_file in ann_files]
else:
raise NotImplementedError

datasets = []
num_dset = len(ann_files)
for i in range(num_dset):
Expand All @@ -36,6 +58,15 @@ def build_dataset(cfg, default_args=None):
elif isinstance(cfg['ann_file'], (list, tuple)):
dataset = _concat_dataset(cfg, default_args)
else:
dataset = build_from_cfg(cfg, DATASETS, default_args)
matches = glob.glob(cfg['ann_file'], recursive=True)
if not matches:
raise RuntimeError(f'Failed to find annotation files that match pattern: '
f'{cfg["ann_file"]}')
cfg['ann_file'] = matches
if len(cfg['ann_file']) == 1:
cfg['ann_file'] = cfg['ann_file'][0]
dataset = build_from_cfg(cfg, DATASETS, default_args)
else:
dataset = _concat_dataset(cfg, default_args)

return dataset

0 comments on commit a5c1ee7

Please sign in to comment.