-
Notifications
You must be signed in to change notification settings - Fork 154
/
test_xcompat.py
592 lines (449 loc) · 16.3 KB
/
test_xcompat.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import io
import os
import numpy as np
import PIL
import pytest
import torch
import pickle
import yaml
from io import StringIO
import webdataset as wds
local_data = "testdata/imagenet-000000.tgz"
compressed = "testdata/compressed.tar"
remote_loc = "http://storage.googleapis.com/nvdata-openimages/"
remote_shards = "openimages-train-0000{00..99}.tar"
remote_shard = "openimages-train-000321.tar"
remote_pattern = "openimages-train-{}.tar"
def identity(x):
return x
def count_samples_tuple(source, *args, n=10000):
count = 0
for i, sample in enumerate(iter(source)):
if i >= n:
break
assert isinstance(sample, (tuple, dict, list)), (type(sample), sample)
for f in args:
assert f(sample)
count += 1
return count
def count_samples(source, *args, n=1000):
count = 0
for i, sample in enumerate(iter(source)):
if i >= n:
break
for f in args:
assert f(sample)
count += 1
return count
def test_dataset():
ds = wds.WebDataset(local_data)
assert count_samples_tuple(ds) == 47
def test_dataset_resampled():
ds = wds.WebDataset(local_data, resampled=True)
assert count_samples_tuple(ds, n=100) == 100
shardspec = """
datasets:
- name: CDIP
perepoch: 10
buckets:
- ./gs/nvdata-ocropus/words/
shards:
- cdipsub-{000000..000092}.tar
- name: Google 1000 Books
perepoch: 20
buckets:
- ./gs/nvdata-ocropus/words/
shards:
- gsub-{000000..000167}.tar
- name: Internet Archive Sample
perepoch: 30
buckets:
- ./gs/nvdata-ocropus/words/
shards:
- ia1-{000000..000033}.tar
"""
yaml3_data = """
prefix: pipe:curl -s -L http://storage.googleapis.com/
buckets: nvdata-ocropus-words
datasets:
- shards: uw3-word-0000{00..21}.tar
- shards: ia1-{000000..000033}.tar
- shards: gsub-{000000..000167}.tar
- shards: cdipsub-{000000..000092}.tar
"""
def test_yaml3():
spec = yaml.safe_load(StringIO(yaml3_data))
ds = wds.WebDataset(spec)
next(iter(ds))
def test_length():
ds = wds.WebDataset(local_data)
with pytest.raises(TypeError):
len(ds)
dsl = ds.with_length(1793)
assert len(dsl) == 1793
dsl2 = ds.repeat(17)
dsl3 = dsl2.with_length(19)
assert len(dsl3) == 19
def test_mock():
ds = wds.MockDataset((True, True), 193)
assert count_samples_tuple(ds) == 193
def IGNORE_test_ddp_equalize():
ds = wds.WebDataset(local_data).ddp_equalize(733)
assert count_samples_tuple(ds) == 733
def test_dataset_shuffle_extract():
ds = wds.WebDataset(local_data).shuffle(5).to_tuple("png;jpg cls")
assert count_samples_tuple(ds) == 47
def test_dataset_pipe_cat():
ds = wds.WebDataset(f"pipe:cat {local_data}").shuffle(5).to_tuple("png;jpg cls")
assert count_samples_tuple(ds) == 47
def test_slice():
ds = wds.WebDataset(local_data).slice(10)
assert count_samples_tuple(ds) == 10
def test_dataset_eof():
import tarfile
with pytest.raises(tarfile.ReadError):
ds = wds.WebDataset(f"pipe:dd if={local_data} bs=1024 count=10").shuffle(5)
assert count_samples(ds) == 47
def test_dataset_eof_handler():
ds = wds.WebDataset(f"pipe:dd if={local_data} bs=1024 count=10", handler=wds.ignore_and_stop)
assert count_samples(ds) < 47
def test_dataset_decode_nohandler():
count = [0]
def faulty_decoder(key, data):
if count[0] % 2 == 0:
raise ValueError("nothing")
else:
return data
count[0] += 1
with pytest.raises(ValueError):
ds = wds.WebDataset(local_data).decode(faulty_decoder)
count_samples_tuple(ds)
def test_dataset_missing_totuple_raises():
with pytest.raises(ValueError):
ds = wds.WebDataset(local_data).to_tuple("foo", "bar")
count_samples_tuple(ds)
def test_dataset_missing_rename_raises():
with pytest.raises(ValueError):
ds = wds.WebDataset(local_data).rename(x="foo", y="bar")
count_samples_tuple(ds)
def getkeys(sample):
return set(x for x in sample.keys() if not x.startswith("_"))
def test_dataset_rename_keep():
ds = wds.WebDataset(local_data).rename(image="png", keep=False)
sample = next(iter(ds))
assert getkeys(sample) == set(["image"]), getkeys(sample)
ds = wds.WebDataset(local_data).rename(image="png")
sample = next(iter(ds))
assert getkeys(sample) == set("cls image wnid xml".split()), getkeys(sample)
def test_dataset_rsample():
ds = wds.WebDataset(local_data).rsample(1.0)
assert count_samples_tuple(ds) == 47
ds = wds.WebDataset(local_data).rsample(0.5)
result = [count_samples_tuple(ds) for _ in range(300)]
assert np.mean(result) >= 0.3 * 47 and np.mean(result) <= 0.7 * 47, np.mean(result)
def test_dataset_decode_handler():
count = [0]
good = [0]
def faulty_decoder(key, data):
if "png" not in key:
return data
count[0] += 1
if count[0] % 2 == 0:
raise ValueError("nothing")
else:
good[0] += 1
return data
ds = wds.WebDataset(local_data).decode(faulty_decoder, handler=wds.ignore_and_continue)
result = count_samples_tuple(ds)
assert count[0] == 47
assert good[0] == 24
assert result == 24
def test_dataset_rename_handler():
ds = wds.WebDataset(local_data).rename(image="png;jpg", cls="cls")
count_samples_tuple(ds)
with pytest.raises(ValueError):
ds = wds.WebDataset(local_data).rename(image="missing", cls="cls")
count_samples_tuple(ds)
def test_dataset_map_handler():
def f(x):
assert isinstance(x, dict)
return x
def g(x):
raise ValueError()
ds = wds.WebDataset(local_data).map(f)
count_samples_tuple(ds)
with pytest.raises(ValueError):
ds = wds.WebDataset(local_data).map(g)
count_samples_tuple(ds)
def test_dataset_map_dict_handler():
ds = wds.WebDataset(local_data).map_dict(png=identity, cls=identity)
count_samples_tuple(ds)
with pytest.raises(KeyError):
ds = wds.WebDataset(local_data).map_dict(png=identity, cls2=identity)
count_samples_tuple(ds)
def g(x):
raise ValueError()
with pytest.raises(ValueError):
ds = wds.WebDataset(local_data).map_dict(png=g, cls=identity)
count_samples_tuple(ds)
def test_dataset_shuffle_decode_rename_extract():
ds = (
wds.WebDataset(local_data)
.shuffle(5)
.decode("rgb")
.rename(image="png;jpg", cls="cls")
.to_tuple("image", "cls")
)
assert count_samples_tuple(ds) == 47
image, cls = next(iter(ds))
assert isinstance(image, np.ndarray), image
assert isinstance(cls, int), type(cls)
def test_rgb8():
ds = wds.WebDataset(local_data).decode("rgb8").to_tuple("png;jpg", "cls")
assert count_samples_tuple(ds) == 47
image, cls = next(iter(ds))
assert isinstance(image, np.ndarray), type(image)
assert image.dtype == np.uint8, image.dtype
assert isinstance(cls, int), type(cls)
def test_pil():
ds = wds.WebDataset(local_data).decode("pil").to_tuple("jpg;png", "cls")
assert count_samples_tuple(ds) == 47
image, cls = next(iter(ds))
assert isinstance(image, PIL.Image.Image)
def test_raw():
ds = wds.WebDataset(local_data).to_tuple("jpg;png", "cls")
assert count_samples_tuple(ds) == 47
image, cls = next(iter(ds))
assert isinstance(image, bytes)
assert isinstance(cls, bytes)
def test_only1():
ds = wds.WebDataset(local_data).decode(only="cls").to_tuple("jpg;png", "cls")
assert count_samples_tuple(ds) == 47
image, cls = next(iter(ds))
assert isinstance(image, bytes)
assert isinstance(cls, int)
ds = wds.WebDataset(local_data).decode("l", only=["jpg", "png"]).to_tuple("jpg;png", "cls")
assert count_samples_tuple(ds) == 47
image, cls = next(iter(ds))
assert isinstance(image, np.ndarray)
assert isinstance(cls, bytes)
def test_gz():
ds = wds.WebDataset(compressed).decode()
sample = next(iter(ds))
print(sample)
assert sample["txt.gz"] == "hello\n", sample
@pytest.mark.skip(reason="need to figure out unraisableexceptionwarning")
def test_rgb8_np_vs_torch():
import warnings
warnings.filterwarnings("error")
ds = wds.WebDataset(local_data).decode("rgb8").to_tuple("png;jpg", "cls")
image, cls = next(iter(ds))
assert isinstance(image, np.ndarray), type(image)
assert isinstance(cls, int), type(cls)
ds = wds.WebDataset(local_data).decode("torchrgb8").to_tuple("png;jpg", "cls")
image2, cls2 = next(iter(ds))
assert isinstance(image2, torch.Tensor), type(image2)
assert isinstance(cls, int), type(cls)
assert (image == image2.permute(1, 2, 0).numpy()).all, (image.shape, image2.shape)
assert cls == cls2
def test_float_np_vs_torch():
ds = wds.WebDataset(local_data).decode("rgb").to_tuple("png;jpg", "cls")
image, cls = next(iter(ds))
ds = wds.WebDataset(local_data).decode("torchrgb").to_tuple("png;jpg", "cls")
image2, cls2 = next(iter(ds))
assert (image == image2.permute(1, 2, 0).numpy()).all(), (image.shape, image2.shape)
assert cls == cls2
# def test_associate():
# with open("testdata/imagenet-extra.json") as stream:
# extra_data = simplejson.load(stream)
# def associate(key):
# return dict(MY_EXTRA_DATA=extra_data[key])
# ds = wds.WebDataset(local_data).associate(associate)
# for sample in ds:
# assert "MY_EXTRA_DATA" in sample.keys()
# break
def test_tenbin():
from webdataset import tenbin
for d0 in [0, 1, 2, 10, 100, 1777]:
for d1 in [0, 1, 2, 10, 100, 345]:
for t in [np.uint8, np.float16, np.float32, np.float64]:
a = np.random.normal(size=(d0, d1)).astype(t)
a_encoded = tenbin.encode_buffer([a])
(a_decoded,) = tenbin.decode_buffer(a_encoded)
print(a.shape, a_decoded.shape)
assert a.shape == a_decoded.shape
assert a.dtype == a_decoded.dtype
assert (a == a_decoded).all()
def test_tenbin_dec():
ds = wds.WebDataset("testdata/tendata.tar").decode().to_tuple("ten")
assert count_samples_tuple(ds) == 100
for sample in ds:
xs, ys = sample[0]
assert xs.dtype == np.float64
assert ys.dtype == np.float64
assert xs.shape == (28, 28)
assert ys.shape == (28, 28)
# def test_container_mp():
# ds = wds.WebDataset("testdata/mpdata.tar", container="mp", decoder=None)
# assert count_samples_tuple(ds) == 100
# for sample in ds:
# assert isinstance(sample, dict)
# assert set(sample.keys()) == set("__key__ x y".split()), sample
# def test_container_ten():
# ds = wds.WebDataset("testdata/tendata.tar", container="ten", decoder=None)
# assert count_samples_tuple(ds) == 100
# for xs, ys in ds:
# assert xs.dtype == np.float64
# assert ys.dtype == np.float64
# assert xs.shape == (28, 28)
# assert ys.shape == (28, 28)
def test_dataloader():
import torch
ds = wds.WebDataset(remote_loc + remote_shards)
dl = torch.utils.data.DataLoader(ds, num_workers=4)
assert count_samples_tuple(dl, n=100) == 100
def test_handlers():
def mydecoder(data):
return PIL.Image.open(io.BytesIO(data)).resize((128, 128))
ds = (
wds.WebDataset(remote_loc + remote_shard)
.decode(
wds.handle_extension("jpg", mydecoder),
wds.handle_extension("png", mydecoder),
)
.to_tuple("jpg;png", "json")
)
for sample in ds:
assert isinstance(sample[0], PIL.Image.Image)
break
def test_decoder():
def mydecoder(key, sample):
return len(sample)
ds = wds.WebDataset(remote_loc + remote_shard).decode(mydecoder).to_tuple("jpg;png", "json")
for sample in ds:
assert isinstance(sample[0], int)
break
def test_shard_syntax():
print(remote_loc, remote_shards)
ds = wds.WebDataset(remote_loc + remote_shards).decode().to_tuple("jpg;png", "json")
assert count_samples_tuple(ds, n=10) == 10
# def test_opener():
# def opener(url):
# print(url, file=sys.stderr)
# cmd = "curl -s '{}{}'".format(remote_loc, remote_pattern.format(url))
# print(cmd, file=sys.stderr)
# return subprocess.Popen(
# cmd, bufsize=1000000, shell=True, stdout=subprocess.PIPE
# ).stdout
#
# ds = (
# wds.WebDataset("{000000..000099}", open_fn=opener)
# .shuffle(100)
# .to_tuple("jpg;png", "json")
# )
# assert count_samples_tuple(ds, n=10) == 10
def test_pipe():
ds = (
wds.WebDataset(f"pipe:curl -s '{remote_loc}{remote_shards}'").shuffle(100).to_tuple("jpg;png", "json")
)
assert count_samples_tuple(ds, n=10) == 10
def test_torchvision():
import torch
from torchvision import transforms
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
preproc = transforms.Compose(
[
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
]
)
ds = (
wds.WebDataset(remote_loc + remote_shards)
.decode("pil")
.to_tuple("jpg;png", "json")
.map_tuple(preproc, identity)
)
for sample in ds:
assert isinstance(sample[0], torch.Tensor), type(sample[0])
assert tuple(sample[0].size()) == (3, 224, 224), sample[0].size()
assert isinstance(sample[1], list), type(sample[1])
break
def test_batched():
import torch
from torchvision import transforms
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
preproc = transforms.Compose(
[
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
]
)
raw = wds.WebDataset(remote_loc + remote_shards)
ds = raw.decode("pil").to_tuple("jpg;png", "json").map_tuple(preproc, identity).batched(7)
for sample in ds:
assert isinstance(sample[0], torch.Tensor), type(sample[0])
assert tuple(sample[0].size()) == (7, 3, 224, 224), sample[0].size()
assert isinstance(sample[1], list), type(sample[1])
break
pickle.dumps(ds)
def test_unbatched():
import torch
from torchvision import transforms
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
preproc = transforms.Compose(
[
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
]
)
ds = (
wds.WebDataset(remote_loc + remote_shards)
.decode("pil")
.to_tuple("jpg;png", "json")
.map_tuple(preproc, identity)
.batched(7)
.unbatched()
)
for sample in ds:
assert isinstance(sample[0], torch.Tensor), type(sample[0])
assert tuple(sample[0].size()) == (3, 224, 224), sample[0].size()
assert isinstance(sample[1], list), type(sample[1])
break
pickle.dumps(ds)
def test_with_epoch():
ds = wds.WebDataset(local_data)
for _ in range(10):
assert count_samples_tuple(ds) == 47
be = ds.with_epoch(193)
for _ in range(10):
assert count_samples_tuple(be) == 193
be = ds.with_epoch(2)
for _ in range(10):
assert count_samples_tuple(be) == 2
def test_repeat():
ds = wds.WebDataset(local_data)
assert count_samples_tuple(ds.repeat(nepochs=2)) == 47 * 2
def test_repeat2():
ds = wds.WebDataset(local_data).to_tuple("png", "cls").batched(2)
assert count_samples_tuple(ds.repeat(nbatches=20)) == 20
def test_webloader():
ds = wds.WebDataset(local_data)
dl = wds.WebLoader(ds, num_workers=4, batch_size=3)
nsamples = count_samples_tuple(dl)
assert nsamples == (47 + 2) // 3, nsamples
def test_webloader_repeat():
ds = wds.WebDataset(local_data)
dl = wds.WebLoader(ds, num_workers=4, batch_size=3).repeat(nepochs=2)
nsamples = count_samples_tuple(dl)
assert nsamples == 2 * (47 + 2) // 3, nsamples
def test_webloader_unbatched():
ds = wds.WebDataset(local_data).to_tuple("png", "cls")
dl = wds.WebLoader(ds, num_workers=4, batch_size=3).unbatched()
nsamples = count_samples_tuple(dl)
assert nsamples == 47, nsamples