This repository has been archived by the owner on Jul 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 304
/
transform_dataset.py
56 lines (44 loc) · 1.83 KB
/
transform_dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from chainercv.chainer_experimental.datasets.sliceable import GetterDataset
from chainercv.chainer_experimental.datasets.sliceable.sliceable_dataset \
import _is_iterable
class TransformDataset(GetterDataset):
"""A sliceable version of :class:`chainer.datasets.TransformDataset`.
Note that it requires :obj:`keys` to determine the names of returned
values.
Here is an example.
>>> def transfrom(in_data):
>>> img, bbox, label = in_data
>>> ...
>>> return new_img, new_label
>>>
>>> dataset = TramsformDataset(dataset, ('img', 'label'), transform)
>>> dataset.keys # ('img', 'label')
Args:
dataset: The underlying dataset.
This dataset should have :meth:`__len__` and :meth:`__getitem__`.
keys (string or tuple of strings): The name(s) of data
that the transform function returns.
If this parameter is omitted, :meth:`__init__` fetches a sample
from the underlying dataset to determine the number of data.
transform (callable): A function that is called to transform values
returned by the underlying dataset's :meth:`__getitem__`.
"""
def __init__(self, dataset, keys, transform=None):
if transform is None:
keys, transform = None, keys
super(TransformDataset, self).__init__()
self._dataset = dataset
self._transform = transform
if keys is None:
sample = self._get(0)
if isinstance(sample, tuple):
keys = (None,) * len(sample)
else:
keys = None
self.add_getter(keys, self._get)
if not _is_iterable(keys):
self.keys = 0
def __len__(self):
return len(self._dataset)
def _get(self, index):
return self._transform(self._dataset[index])