-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use registry to manage datasets (#924)
* use registry to manage datasets * bug fix for concat dataset * update documentation to fit the new api
- Loading branch information
Showing
19 changed files
with
210 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import copy | ||
|
||
from mmdet.utils import build_from_cfg | ||
from .dataset_wrappers import ConcatDataset, RepeatDataset | ||
from .registry import DATASETS | ||
|
||
|
||
def _concat_dataset(cfg): | ||
ann_files = cfg['ann_file'] | ||
img_prefixes = cfg.get('img_prefix', None) | ||
seg_prefixes = cfg.get('seg_prefixes', None) | ||
proposal_files = cfg.get('proposal_file', None) | ||
|
||
datasets = [] | ||
num_dset = len(ann_files) | ||
for i in range(num_dset): | ||
data_cfg = copy.deepcopy(cfg) | ||
data_cfg['ann_file'] = ann_files[i] | ||
if isinstance(img_prefixes, (list, tuple)): | ||
data_cfg['img_prefix'] = img_prefixes[i] | ||
if isinstance(seg_prefixes, (list, tuple)): | ||
data_cfg['seg_prefix'] = seg_prefixes[i] | ||
if isinstance(proposal_files, (list, tuple)): | ||
data_cfg['proposal_file'] = proposal_files[i] | ||
datasets.append(build_dataset(data_cfg)) | ||
|
||
return ConcatDataset(datasets) | ||
|
||
|
||
def build_dataset(cfg): | ||
if cfg['type'] == 'RepeatDataset': | ||
dataset = RepeatDataset(build_dataset(cfg['dataset']), cfg['times']) | ||
elif isinstance(cfg['ann_file'], (list, tuple)): | ||
dataset = _concat_dataset(cfg) | ||
else: | ||
dataset = build_from_cfg(cfg, DATASETS) | ||
|
||
return dataset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import numpy as np | ||
from torch.utils.data.dataset import ConcatDataset as _ConcatDataset | ||
|
||
from .registry import DATASETS | ||
|
||
|
||
@DATASETS.register_module | ||
class ConcatDataset(_ConcatDataset): | ||
"""A wrapper of concatenated dataset. | ||
Same as :obj:`torch.utils.data.dataset.ConcatDataset`, but | ||
concat the group flag for image aspect ratio. | ||
Args: | ||
datasets (list[:obj:`Dataset`]): A list of datasets. | ||
""" | ||
|
||
def __init__(self, datasets): | ||
super(ConcatDataset, self).__init__(datasets) | ||
self.CLASSES = datasets[0].CLASSES | ||
if hasattr(datasets[0], 'flag'): | ||
flags = [] | ||
for i in range(0, len(datasets)): | ||
flags.append(datasets[i].flag) | ||
self.flag = np.concatenate(flags) | ||
|
||
|
||
@DATASETS.register_module | ||
class RepeatDataset(object): | ||
"""A wrapper of repeated dataset. | ||
The length of repeated dataset will be `times` larger than the original | ||
dataset. This is useful when the data loading time is long but the dataset | ||
is small. Using RepeatDataset can reduce the data loading time between | ||
epochs. | ||
Args: | ||
dataset (:obj:`Dataset`): The dataset to be repeated. | ||
times (int): Repeat times. | ||
""" | ||
|
||
def __init__(self, dataset, times): | ||
self.dataset = dataset | ||
self.times = times | ||
self.CLASSES = dataset.CLASSES | ||
if hasattr(self.dataset, 'flag'): | ||
self.flag = np.tile(self.dataset.flag, times) | ||
|
||
self._ori_len = len(self.dataset) | ||
|
||
def __getitem__(self, idx): | ||
return self.dataset[idx % self._ori_len] | ||
|
||
def __len__(self): | ||
return self.times * self._ori_len |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from mmdet.utils import Registry | ||
|
||
DATASETS = Registry('dataset') |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.