-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdataset.py
256 lines (221 loc) · 9.26 KB
/
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import torch
from torch.utils.data import DataLoader
import numpy as np
from omegaconf import DictConfig, ListConfig
from typing import Dict, List, Optional, Union, Callable
import webdataset as wds
from .filters import AbstractFilter
from .mappers import AbstractMapper
from .datapipeline import StableDataPipeline, instantiate, make_callable, time_measure
@time_measure("Collator")
def dict_collation_fn(
samples: List, combine_tensors: bool = True, combine_scalars: bool = True
) -> Dict:
"""Take a list of samples (as dictionary) and create a batch, preserving the keys.
If `tensors` is True, `ndarray` objects are combined into
tensor batches.
:param dict samples: list of samples
:param bool tensors: whether to turn lists of ndarrays into a single ndarray
:returns: single sample consisting of a batch
:rtype: dict
"""
keys = set.intersection(*[set(sample.keys()) for sample in samples])
batched = {key: [] for key in keys}
for s in samples:
[batched[key].append(s[key]) for key in batched]
result = {}
for key in batched:
if isinstance(batched[key][0], (int, float)):
if combine_scalars:
result[key] = np.array(list(batched[key]))
elif isinstance(batched[key][0], torch.Tensor):
if combine_tensors:
result[key] = torch.stack(list(batched[key]))
elif isinstance(batched[key][0], np.ndarray):
if combine_tensors:
result[key] = np.array(list(batched[key]))
else:
result[key] = list(batched[key])
del samples
del batched
return result
def create_loader(
datapipeline: Union[wds.DataPipeline, Union[DictConfig, Dict]],
batch_size: int,
num_workers: int,
partial: bool = False,
collation_fn: Optional[Union[Callable, Dict, DictConfig]] = None,
batched_transforms: Optional[ListConfig] = None,
loader_kwargs: Optional[Union[Dict, DictConfig]] = None,
error_handler: Optional[Union[Callable, Dict, DictConfig]] = None,
) -> torch.utils.data.DataLoader:
if not loader_kwargs:
loader_kwargs = {}
if not batched_transforms:
batched_transforms = []
if not collation_fn:
collation_fn = dict_collation_fn
if isinstance(collation_fn, (Dict, DictConfig)):
collation_fn = make_callable(collation_fn)
if not error_handler:
error_handler = {"target": "sdata.warn_and_continue"}
if isinstance(error_handler, (Dict, DictConfig)):
error_handler = make_callable(error_handler)
print("#" * 100)
print("Building dataloader with the following parameters")
print(f"batch_size: {batch_size}")
print(f"num_workers: {num_workers}")
for key in loader_kwargs:
print(key, ": ", loader_kwargs[key])
print("#" * 100)
# create datapipeline from dict if not already instantiated
if isinstance(datapipeline, (DictConfig, Dict)):
datapipeline = instantiate(datapipeline)
# batching
datapipeline = datapipeline.batched(
batch_size, partial=partial, collation_fn=collation_fn
)
# apply transforms which act on batched samples
for i, trf in enumerate(batched_transforms):
trf = instantiate(trf)
if isinstance(trf, AbstractFilter):
print(
f"Adding filter {trf.__class__.__name__} as batched transform #{i} "
f"to the datapipeline"
)
datapipeline = datapipeline.select(trf)
elif isinstance(trf, AbstractMapper):
print(
f"Adding mapper {trf.__class__.__name__} as batched transform #{i} "
f"to the datapipeline"
)
datapipeline = datapipeline.map(trf, handler=error_handler)
else:
raise TypeError(
"chosen batched transform should be either a subclass of "
"sdata.AbstractMapper or one of sdata.AbstractFilter"
"but is none of both"
)
# create loader
loader = torch.utils.data.DataLoader(
datapipeline, batch_size=None, num_workers=num_workers, **loader_kwargs
)
return loader
def create_dataset(
urls: Union[List, ListConfig, str],
pipeline_config: Optional[Union[DictConfig, Dict]] = None,
decoders: Optional[Union[ListConfig, str]] = "pil",
additional_decoder_kwargs: Optional[DictConfig] = None,
preprocessors: Optional[ListConfig] = None,
postprocessors: Optional[ListConfig] = None,
error_handler: Optional[Union[Callable, Dict]] = None,
) -> wds.DataPipeline:
"""
Create a dataset from several (partly optional) configs and urls defining paths to shards in webdataset/torchdata format
The shards should be located in the local filesystem and can be specified as directories or in braceexpand notation
:param urls: the urls as paths to the shards
:param pipeline_config: additional parameters for configuring the main datapipeline, for a list of
available parameters, see sdata.
:param decoders:
:param additional_decoder_kwargs: Additional keyword args for the decoder. This can be e.g. used to define passthrough keys, which shall be
decoded although not having a known decoder key
:param preprocessors:
:param postprocessors:
:param error_handler: The error handler defining a strategy for handling errors in the stages in the pipeline
:return:
"""
if isinstance(urls, str):
urls = [urls]
if not pipeline_config:
pipeline_config = {}
if not error_handler:
error_handler = {"target": "sdata.warn_and_continue"}
pipeline_config.pop("handler", None)
pipeline_config["handler"] = error_handler
# default for all processors
if not preprocessors:
preprocessors = []
if not postprocessors:
postprocessors = []
if not additional_decoder_kwargs:
additional_decoder_kwargs = {}
if decoders and not isinstance(decoders, (List, ListConfig)):
# default case is assuming image decoding
decoders = [decoders]
elif not decoders:
decoders = []
datapipeline = StableDataPipeline(urls=urls, **pipeline_config)
if isinstance(error_handler, Dict):
error_handler = make_callable(error_handler)
# instantiate all preprocessors
for i, prepro_config in enumerate(preprocessors):
prepro = instantiate(prepro_config)
if isinstance(prepro, AbstractFilter):
print(
f"Adding filter {prepro.__class__.__name__} as preprocessor #{i} "
f"to the datapipeline"
)
datapipeline = datapipeline.select(prepro)
elif isinstance(prepro, AbstractMapper):
print(
f"Adding mapper {prepro.__class__.__name__} as preprocessor #{i} "
f"to the datapipeline"
)
datapipeline = datapipeline.map(prepro, handler=error_handler)
else:
raise TypeError(
f"chosen preprocessor {prepro.__class__.__name__} should be either a subclass of "
"sdata.mappers.AbstractMapper or one of sdata.filters.AbstractFilter"
"but is none of both"
)
# do decoding
prepared_decoders = []
for decoder_spec in decoders:
if isinstance(decoder_spec, (Dict, DictConfig)):
decoder = instantiate(decoder_spec)
print(f"Adding decoder {decoder.__class__.__name__} to decoders.")
prepared_decoders.append(decoder)
elif isinstance(decoder_spec, str):
assert (
decoder_spec in wds.autodecode.imagespecs
or decoder_spec in wds.autodecode.decoders
), (
"when decoder is specified via a string, then it has to be a a "
"decoder known to webdataset"
)
print(f"Adding decoder {decoder_spec} to decoders.")
prepared_decoders.append(decoder_spec)
else:
raise TypeError(f"{decoder_spec} not a thing for decoders.")
if decoders:
# default behavior is setting partial to 'True' in decode
partial = additional_decoder_kwargs.pop("partial", True)
# add instantiated decoders to the datapipeline
datapipeline = datapipeline.decode(
*prepared_decoders,
partial=partial,
handler=error_handler,
**additional_decoder_kwargs,
)
# instantiate all postprocessors
for i, postro_config in enumerate(postprocessors):
postpro = instantiate(postro_config)
if isinstance(postpro, AbstractFilter):
print(
f"Adding filter {postpro.__class__.__name__} as postprocessor #{i} "
f"to the datapipeline"
)
datapipeline = datapipeline.select(postpro)
elif isinstance(postpro, AbstractMapper):
print(
f"Adding mapper {postpro.__class__.__name__} as postprocessor #{i} "
f"to the datapipeline"
)
datapipeline = datapipeline.map(postpro, handler=error_handler)
else:
raise TypeError(
"chosen postprocessor should be either a subclass of "
"sdata.AbstractMapper or one of sdata.AbstractFilter"
"but is none of both"
)
return datapipeline