-
Notifications
You must be signed in to change notification settings - Fork 154
/
compat.py
179 lines (148 loc) · 5.67 KB
/
compat.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
import os
import yaml
from . import autodecode, cache, filters, shardlists, tariterators
from .filters import reraise_exception
from .pipeline import DataPipeline
from .pytorch import DataLoader, IterableDataset
class FluidInterface:
def batched(
self, batchsize, collation_fn=filters.default_collation_fn, partial=True
):
return self.compose(
filters.batched(batchsize, collation_fn=collation_fn, partial=partial)
)
def unbatched(self):
return self.compose(filters.unbatched())
def listed(self, batchsize, partial=True):
return self.compose(filters.batched(), batchsize=batchsize, collation_fn=None)
def unlisted(self):
return self.compose(filters.unlisted())
def log_keys(self, logfile=None):
return self.compose(filters.log_keys(logfile))
def shuffle(self, size, **kw):
if size < 1:
return self
else:
return self.compose(filters.shuffle(size, **kw))
def map(self, f, handler=reraise_exception):
return self.compose(filters.map(f, handler=handler))
def decode(
self,
*args,
pre=None,
post=None,
only=None,
partial=False,
handler=reraise_exception,
):
handlers = [
autodecode.ImageHandler(x) if isinstance(x, str) else x for x in args
]
decoder = autodecode.Decoder(
handlers, pre=pre, post=post, only=only, partial=partial
)
return self.map(decoder, handler=handler)
def map_dict(self, handler=reraise_exception, **kw):
return self.compose(filters.map_dict(handler=handler, **kw))
def select(self, predicate, **kw):
return self.compose(filters.select(predicate, **kw))
def to_tuple(self, *args, handler=reraise_exception):
return self.compose(filters.to_tuple(*args, handler=handler))
def map_tuple(self, *args, handler=reraise_exception):
return self.compose(filters.map_tuple(*args, handler=handler))
def slice(self, *args):
return self.compose(filters.slice(*args))
def rename(self, **kw):
return self.compose(filters.rename(**kw))
def rsample(self, p=0.5):
return self.compose(filters.rsample(p))
def rename_keys(self, *args, **kw):
return self.compose(filters.rename_keys(*args, **kw))
def extract_keys(self, *args, **kw):
return self.compose(filters.extract_keys(*args, **kw))
def xdecode(self, *args, **kw):
return self.compose(filters.xdecode(*args, **kw))
def mcached(self):
return self.compose(filters.Cached())
def lmdb_cached(self, *args, **kw):
return self.compose(filters.LMDBCached(*args, **kw))
class WebDataset(DataPipeline, FluidInterface):
"""Small fluid-interface wrapper for DataPipeline."""
def __init__(
self,
urls,
handler=reraise_exception,
resampled=False,
repeat=False,
shardshuffle=None,
cache_size=-1,
cache_dir=None,
url_to_name=cache.pipe_cleaner,
detshuffle=False,
nodesplitter=shardlists.single_node_only,
select_files=None,
rename_files=None,
verbose=False,
):
super().__init__()
cache_size = int(os.environ.get("WDS_CACHE_SIZE", cache_size))
cache_dir = os.environ.get("WDS_CACHE", cache_dir)
if cache_dir is not None:
cache_dir = os.path.expanduser(cache_dir)
if not os.path.exists(cache_dir):
raise ValueError(f"cache directory {cache_dir} does not exist")
if isinstance(urls, IterableDataset):
assert not resampled
self.append(urls)
elif isinstance(urls, str) and (
urls.endswith(".yaml") or urls.endswith(".yml")
):
with open(urls) as stream:
spec = yaml.safe_load(stream)
assert "datasets" in spec
self.append(shardlists.MultiShardSample(spec))
elif isinstance(urls, dict):
assert "datasets" in urls
self.append(shardlists.MultiShardSample(urls))
elif resampled:
self.append(shardlists.ResampledShards(urls))
else:
self.append(shardlists.SimpleShardList(urls))
self.append(nodesplitter)
self.append(shardlists.split_by_worker)
if shardshuffle is True:
shardshuffle = 100
if shardshuffle is not None:
if detshuffle:
self.append(filters.detshuffle(shardshuffle))
else:
self.append(filters.shuffle(shardshuffle))
if cache_dir is None or cache_size == 0:
self.append(
tariterators.tarfile_to_samples(
handler=handler,
select_files=select_files,
rename_files=rename_files,
)
)
else:
assert cache_size == -1 or cache_size > 0
self.append(
cache.cached_tarfile_to_samples(
handler=handler,
verbose=verbose,
url_to_name=url_to_name,
cache_size=cache_size,
cache_dir=cache_dir,
select_files=select_files,
rename_files=rename_files,
)
)
class FluidWrapper(DataPipeline, FluidInterface):
"""Small fluid-interface wrapper for DataPipeline."""
def __init__(self, initial):
super().__init__()
self.append(initial)
class WebLoader(DataPipeline, FluidInterface):
def __init__(self, *args, **kw):
super().__init__(DataLoader(*args, **kw))