builtin_dataset_mocks.py 53.4 KB
Newer Older
Lezwon Castelino's avatar
Lezwon Castelino committed
1
import bz2
2
3
import collections.abc
import csv
4
5
import functools
import gzip
6
import io
7
import itertools
Philip Meier's avatar
Philip Meier committed
8
import json
9
10
11
import lzma
import pathlib
import pickle
12
import random
13
import shutil
14
import unittest.mock
15
import xml.etree.ElementTree as ET
16
from collections import Counter, defaultdict
17
18
19
20

import numpy as np
import pytest
import torch
21
22
from common_utils import combinations_grid
from datasets_utils import create_image_file, create_image_folder, make_tar, make_zip
23
from torch.nn.functional import one_hot
24
from torch.testing import make_tensor as _make_tensor
25
from torchvision.prototype import datasets
Philip Meier's avatar
Philip Meier committed
26

27
make_tensor = functools.partial(_make_tensor, device="cpu")
Philip Meier's avatar
Philip Meier committed
28
make_scalar = functools.partial(make_tensor, ())
29
30


31
__all__ = ["DATASET_MOCKS", "parametrize_dataset_mocks"]
32
33


34
class DatasetMock:
35
36
37
    def __init__(self, name, *, mock_data_fn, configs):
        # FIXME: error handling for unknown names
        self.name = name
38
        self.mock_data_fn = mock_data_fn
39
        self.configs = configs
40

41
42
    def _parse_mock_info(self, mock_info):
        if mock_info is None:
43
44
45
46
            raise pytest.UsageError(
                f"The mock data function for dataset '{self.name}' returned nothing. It needs to at least return an "
                f"integer indicating the number of samples for the current `config`."
            )
47
48
49
        elif isinstance(mock_info, int):
            mock_info = dict(num_samples=mock_info)
        elif not isinstance(mock_info, dict):
50
            raise pytest.UsageError(
51
52
53
54
55
56
57
58
59
                f"The mock data function for dataset '{self.name}' returned a {type(mock_info)}. The returned object "
                f"should be a dictionary containing at least the number of samples for the key `'num_samples'`. If no "
                f"additional information is required for specific tests, the number of samples can also be returned as "
                f"an integer."
            )
        elif "num_samples" not in mock_info:
            raise pytest.UsageError(
                f"The dictionary returned by the mock data function for dataset '{self.name}' has to contain a "
                f"`'num_samples'` entry indicating the number of samples."
60
            )
61

62
        return mock_info
63

64
    def load(self, config):
65
66
67
        # `datasets.home()` is patched to a temporary directory through the autouse fixture `test_home` in
        # test/test_prototype_builtin_datasets.py
        root = pathlib.Path(datasets.home()) / self.name
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
        # We cannot place the mock data upfront in `root`. Loading a dataset calls `OnlineResource.load`. In turn,
        # this will only download **and** preprocess if the file is not present. In other words, if we already place
        # the file in `root` before the resource is loaded, we are effectively skipping the preprocessing.
        # To avoid that we first place the mock data in a temporary directory and patch the download logic to move it to
        # `root` only when it is requested.
        tmp_mock_data_folder = root / "__mock__"
        tmp_mock_data_folder.mkdir(parents=True)

        mock_info = self._parse_mock_info(self.mock_data_fn(tmp_mock_data_folder, config))

        def patched_download(resource, root, **kwargs):
            src = tmp_mock_data_folder / resource.file_name
            if not src.exists():
                raise pytest.UsageError(
                    f"Dataset '{self.name}' requires the file {resource.file_name} for {config}"
                    f"but it was not created by the mock data function."
                )
85

86
87
            dst = root / resource.file_name
            shutil.move(str(src), str(root))
88

89
90
91
92
93
94
95
96
97
            return dst

        with unittest.mock.patch(
            "torchvision.prototype.datasets.utils._resource.OnlineResource.download", new=patched_download
        ):
            dataset = datasets.load(self.name, **config)

        extra_files = list(tmp_mock_data_folder.glob("**/*"))
        if extra_files:
98
            raise pytest.UsageError(
99
100
101
102
103
                (
                    f"Dataset '{self.name}' created the following files for {config} in the mock data function, "
                    f"but they were not loaded:\n\n"
                )
                + "\n".join(str(file.relative_to(tmp_mock_data_folder)) for file in extra_files)
104
            )
105

106
107
108
        tmp_mock_data_folder.rmdir()

        return dataset, mock_info
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
def config_id(name, config):
    parts = [name]
    for name, value in config.items():
        if isinstance(value, bool):
            part = ("" if value else "no_") + name
        else:
            part = str(value)
        parts.append(part)
    return "-".join(parts)


def parametrize_dataset_mocks(*dataset_mocks, marks=None):
    mocks = {}
    for mock in dataset_mocks:
        if isinstance(mock, DatasetMock):
            mocks[mock.name] = mock
        elif isinstance(mock, collections.abc.Mapping):
            mocks.update(mock)
        else:
            raise pytest.UsageError(
                f"The positional arguments passed to `parametrize_dataset_mocks` can either be a `DatasetMock`, "
                f"a sequence of `DatasetMock`'s, or a mapping of names to `DatasetMock`'s, "
                f"but got {mock} instead."
            )
    dataset_mocks = mocks

    if marks is None:
        marks = {}
    elif not isinstance(marks, collections.abc.Mapping):
        raise pytest.UsageError()

    return pytest.mark.parametrize(
        ("dataset_mock", "config"),
        [
            pytest.param(dataset_mock, config, id=config_id(name, config), marks=marks.get(name, ()))
            for name, dataset_mock in dataset_mocks.items()
            for config in dataset_mock.configs
        ],
    )


152
DATASET_MOCKS = {}
153

154

155
156
157
158
159
160
161
162
163
164
def register_mock(name=None, *, configs):
    def wrapper(mock_data_fn):
        nonlocal name
        if name is None:
            name = mock_data_fn.__name__
        DATASET_MOCKS[name] = DatasetMock(name, mock_data_fn=mock_data_fn, configs=configs)

        return mock_data_fn

    return wrapper
165

166
167

class MNISTMockData:
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
    _DTYPES_ID = {
        torch.uint8: 8,
        torch.int8: 9,
        torch.int16: 11,
        torch.int32: 12,
        torch.float32: 13,
        torch.float64: 14,
    }

    @classmethod
    def _magic(cls, dtype, ndim):
        return cls._DTYPES_ID[dtype] * 256 + ndim + 1

    @staticmethod
    def _encode(t):
        return torch.tensor(t, dtype=torch.int32).numpy().tobytes()[::-1]

    @staticmethod
    def _big_endian_dtype(dtype):
        np_dtype = getattr(np, str(dtype).replace("torch.", ""))().dtype
        return np.dtype(f">{np_dtype.kind}{np_dtype.itemsize}")

    @classmethod
    def _create_binary_file(cls, root, filename, *, num_samples, shape, dtype, compressor, low=0, high):
        with compressor(root / filename, "wb") as fh:
            for meta in (cls._magic(dtype, len(shape)), num_samples, *shape):
                fh.write(cls._encode(meta))

            data = make_tensor((num_samples, *shape), dtype=dtype, low=low, high=high)

            fh.write(data.numpy().astype(cls._big_endian_dtype(dtype)).tobytes())

    @classmethod
    def generate(
        cls,
        root,
        *,
        num_categories,
        num_samples=None,
        images_file,
        labels_file,
        image_size=(28, 28),
        image_dtype=torch.uint8,
        label_size=(),
        label_dtype=torch.uint8,
        compressor=None,
    ):
        if num_samples is None:
            num_samples = num_categories
        if compressor is None:
            compressor = gzip.open

        cls._create_binary_file(
            root,
            images_file,
            num_samples=num_samples,
            shape=image_size,
            dtype=image_dtype,
            compressor=compressor,
            high=float("inf"),
        )
        cls._create_binary_file(
            root,
            labels_file,
            num_samples=num_samples,
            shape=label_size,
            dtype=label_dtype,
            compressor=compressor,
            high=num_categories,
        )

        return num_samples


242
243
def mnist(root, config):
    prefix = "train" if config["split"] == "train" else "t10k"
244
    return MNISTMockData.generate(
245
        root,
246
247
248
        num_categories=10,
        images_file=f"{prefix}-images-idx3-ubyte.gz",
        labels_file=f"{prefix}-labels-idx1-ubyte.gz",
249
250
251
    )


252
253
254
255
256
257
DATASET_MOCKS.update(
    {
        name: DatasetMock(name, mock_data_fn=mnist, configs=combinations_grid(split=("train", "test")))
        for name in ["mnist", "fashionmnist", "kmnist"]
    }
)
258
259


260
261
262
263
@register_mock(
    configs=combinations_grid(
        split=("train", "test"),
        image_set=("Balanced", "By_Merge", "By_Class", "Letters", "Digits", "MNIST"),
264
    )
265
266
)
def emnist(root, config):
267
    num_samples_map = {}
268
    file_names = set()
269
270
271
272
273
    for split, image_set in itertools.product(
        ("train", "test"),
        ("Balanced", "By_Merge", "By_Class", "Letters", "Digits", "MNIST"),
    ):
        prefix = f"emnist-{image_set.replace('_', '').lower()}-{split}"
274
275
276
        images_file = f"{prefix}-images-idx3-ubyte.gz"
        labels_file = f"{prefix}-labels-idx1-ubyte.gz"
        file_names.update({images_file, labels_file})
277
        num_samples_map[(split, image_set)] = MNISTMockData.generate(
278
            root,
279
280
281
            # The image sets that merge some lower case letters in their respective upper case variant, still use dense
            # labels in the data files. Thus, num_categories != len(categories) there.
            num_categories=47 if config["image_set"] in ("Balanced", "By_Merge") else 62,
282
283
            images_file=images_file,
            labels_file=labels_file,
284
285
286
287
        )

    make_zip(root, "emnist-gzip.zip", *file_names)

288
    return num_samples_map[(config["split"], config["image_set"])]
289
290


291
292
293
294
@register_mock(configs=combinations_grid(split=("train", "test", "test10k", "test50k", "nist")))
def qmnist(root, config):
    num_categories = 10
    if config["split"] == "train":
295
296
297
298
        num_samples = num_samples_gen = num_categories + 2
        prefix = "qmnist-train"
        suffix = ".gz"
        compressor = gzip.open
299
    elif config["split"].startswith("test"):
300
301
302
        # The split 'test50k' is defined as the last 50k images beginning at index 10000. Thus, we need to create
        # more than 10000 images for the dataset to not be empty.
        num_samples_gen = 10001
303
304
305
306
        num_samples = {
            "test": num_samples_gen,
            "test10k": min(num_samples_gen, 10_000),
            "test50k": num_samples_gen - 10_000,
307
        }[config["split"]]
308
309
310
        prefix = "qmnist-test"
        suffix = ".gz"
        compressor = gzip.open
311
    else:  # config["split"] == "nist"
312
313
314
315
316
        num_samples = num_samples_gen = num_categories + 3
        prefix = "xnist"
        suffix = ".xz"
        compressor = lzma.open

317
    MNISTMockData.generate(
318
319
320
321
322
323
324
325
326
        root,
        num_categories=num_categories,
        num_samples=num_samples_gen,
        images_file=f"{prefix}-images-idx3-ubyte{suffix}",
        labels_file=f"{prefix}-labels-idx2-int{suffix}",
        label_size=(8,),
        label_dtype=torch.int32,
        compressor=compressor,
    )
327
    return num_samples
328
329


330
class CIFARMockData:
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
    NUM_PIXELS = 32 * 32 * 3

    @classmethod
    def _create_batch_file(cls, root, name, *, num_categories, labels_key, num_samples=1):
        content = {
            "data": make_tensor((num_samples, cls.NUM_PIXELS), dtype=torch.uint8).numpy(),
            labels_key: torch.randint(0, num_categories, size=(num_samples,)).tolist(),
        }
        with open(pathlib.Path(root) / name, "wb") as fh:
            pickle.dump(content, fh)

    @classmethod
    def generate(
        cls,
        root,
        name,
        *,
        folder,
        train_files,
        test_files,
        num_categories,
        labels_key,
    ):
        folder = root / folder
        folder.mkdir()
        files = (*train_files, *test_files)
        for file in files:
            cls._create_batch_file(
                folder,
                file,
                num_categories=num_categories,
                labels_key=labels_key,
            )

        make_tar(root, name, folder, compression="gz")


368
369
@register_mock(configs=combinations_grid(split=("train", "test")))
def cifar10(root, config):
370
371
372
    train_files = [f"data_batch_{idx}" for idx in range(1, 6)]
    test_files = ["test_batch"]

373
    CIFARMockData.generate(
374
375
376
377
378
379
380
381
382
        root=root,
        name="cifar-10-python.tar.gz",
        folder=pathlib.Path("cifar-10-batches-py"),
        train_files=train_files,
        test_files=test_files,
        num_categories=10,
        labels_key="labels",
    )

383
    return len(train_files if config["split"] == "train" else test_files)
384
385


386
387
@register_mock(configs=combinations_grid(split=("train", "test")))
def cifar100(root, config):
388
389
390
    train_files = ["train"]
    test_files = ["test"]

391
    CIFARMockData.generate(
392
393
394
395
396
397
398
399
400
        root=root,
        name="cifar-100-python.tar.gz",
        folder=pathlib.Path("cifar-100-python"),
        train_files=train_files,
        test_files=test_files,
        num_categories=100,
        labels_key="fine_labels",
    )

401
    return len(train_files if config["split"] == "train" else test_files)
402
403


404
405
@register_mock(configs=[dict()])
def caltech101(root, config):
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
    def create_ann_file(root, name):
        import scipy.io

        box_coord = make_tensor((1, 4), dtype=torch.int32, low=0).numpy().astype(np.uint16)
        obj_contour = make_tensor((2, int(torch.randint(3, 6, size=()))), dtype=torch.float64, low=0).numpy()

        scipy.io.savemat(str(pathlib.Path(root) / name), dict(box_coord=box_coord, obj_contour=obj_contour))

    def create_ann_folder(root, name, file_name_fn, num_examples):
        root = pathlib.Path(root) / name
        root.mkdir(parents=True)

        for idx in range(num_examples):
            create_ann_file(root, file_name_fn(idx))

    images_root = root / "101_ObjectCategories"
    anns_root = root / "Annotations"

424
425
426
427
428
    image_category_map = {
        "Faces": "Faces_2",
        "Faces_easy": "Faces_3",
        "Motorbikes": "Motorbikes_16",
        "airplanes": "Airplanes_Side_2",
429
430
    }

431
432
    categories = ["Faces", "Faces_easy", "Motorbikes", "airplanes", "yin_yang"]

433
    num_images_per_category = 2
434
    for category in categories:
435
436
437
438
439
440
441
442
        create_image_folder(
            root=images_root,
            name=category,
            file_name_fn=lambda idx: f"image_{idx + 1:04d}.jpg",
            num_examples=num_images_per_category,
        )
        create_ann_folder(
            root=anns_root,
443
            name=image_category_map.get(category, category),
444
445
446
447
448
449
450
451
452
            file_name_fn=lambda idx: f"annotation_{idx + 1:04d}.mat",
            num_examples=num_images_per_category,
        )

    (images_root / "BACKGROUND_Goodle").mkdir()
    make_tar(root, f"{images_root.name}.tar.gz", images_root, compression="gz")

    make_tar(root, f"{anns_root.name}.tar", anns_root)

453
    return num_images_per_category * len(categories)
454
455


456
457
@register_mock(configs=[dict()])
def caltech256(root, config):
458
459
460
    dir = root / "256_ObjectCategories"
    num_images_per_category = 2

461
462
463
464
465
466
467
468
    categories = [
        (1, "ak47"),
        (127, "laptop-101"),
        (198, "spider"),
        (257, "clutter"),
    ]

    for category_idx, category in categories:
469
470
        files = create_image_folder(
            dir,
471
472
            name=f"{category_idx:03d}.{category}",
            file_name_fn=lambda image_idx: f"{category_idx:03d}_{image_idx + 1:04d}.jpg",
473
474
475
476
477
478
479
            num_examples=num_images_per_category,
        )
        if category == "spider":
            open(files[0].parent / "RENAME2", "w").close()

    make_tar(root, f"{dir.name}.tar", dir)

480
    return num_images_per_category * len(categories)
481
482


483
484
@register_mock(configs=combinations_grid(split=("train", "val", "test")))
def imagenet(root, config):
485
    from scipy.io import savemat
486

487
488
489
490
    info = datasets.info("imagenet")

    if config["split"] == "train":
        num_samples = len(info["wnids"])
491
        archive_name = "ILSVRC2012_img_train.tar"
492

493
        files = []
494
        for wnid in info["wnids"]:
495
496
            create_image_folder(
                root=root,
497
498
499
500
                name=wnid,
                file_name_fn=lambda image_idx: f"{wnid}_{image_idx:04d}.JPEG",
                num_examples=1,
            )
501
            files.append(make_tar(root, f"{wnid}.tar"))
502
    elif config["split"] == "val":
503
        num_samples = 3
504
505
        archive_name = "ILSVRC2012_img_val.tar"
        files = [create_image_file(root, f"ILSVRC2012_val_{idx + 1:08d}.JPEG") for idx in range(num_samples)]
506

507
508
509
        devkit_root = root / "ILSVRC2012_devkit_t12"
        data_root = devkit_root / "data"
        data_root.mkdir(parents=True)
510

511
        with open(data_root / "ILSVRC2012_validation_ground_truth.txt", "w") as file:
512
            for label in torch.randint(0, len(info["wnids"]), (num_samples,)).tolist():
513
                file.write(f"{label}\n")
514

515
516
517
        num_children = 0
        synsets = [
            (idx, wnid, category, "", num_children, [], 0, 0)
518
            for idx, (category, wnid) in enumerate(zip(info["categories"], info["wnids"]), 1)
519
520
521
        ]
        num_children = 1
        synsets.extend((0, "", "", "", num_children, [], 0, 0) for _ in range(5))
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
        synsets = np.array(
            synsets,
            dtype=np.dtype(
                [
                    ("ILSVRC2012_ID", "O"),
                    ("WNID", "O"),
                    ("words", "O"),
                    ("gloss", "O"),
                    ("num_children", "O"),
                    ("children", "O"),
                    ("wordnet_height", "O"),
                    ("num_train_images", "O"),
                ]
            ),
        )
        savemat(data_root / "meta.mat", dict(synsets=synsets))
538
539

        make_tar(root, devkit_root.with_suffix(".tar.gz").name, compression="gz")
540
    else:  # config["split"] == "test"
541
542
543
544
545
        num_samples = 5
        archive_name = "ILSVRC2012_img_test_v10102019.tar"
        files = [create_image_file(root, f"ILSVRC2012_test_{idx + 1:08d}.JPEG") for idx in range(num_samples)]

    make_tar(root, archive_name, *files)
546
547

    return num_samples
Philip Meier's avatar
Philip Meier committed
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617


class CocoMockData:
    @classmethod
    def _make_annotations_json(
        cls,
        root,
        name,
        *,
        images_meta,
        fn,
    ):
        num_anns_per_image = torch.randint(1, 5, (len(images_meta),))
        num_anns_total = int(num_anns_per_image.sum())
        ann_ids_iter = iter(torch.arange(num_anns_total)[torch.randperm(num_anns_total)])

        anns_meta = []
        for image_meta, num_anns in zip(images_meta, num_anns_per_image):
            for _ in range(num_anns):
                ann_id = int(next(ann_ids_iter))
                anns_meta.append(dict(fn(ann_id, image_meta), id=ann_id, image_id=image_meta["id"]))
        anns_meta.sort(key=lambda ann: ann["id"])

        with open(root / name, "w") as file:
            json.dump(dict(images=images_meta, annotations=anns_meta), file)

        return num_anns_per_image

    @staticmethod
    def _make_instances_data(ann_id, image_meta):
        def make_rle_segmentation():
            height, width = image_meta["height"], image_meta["width"]
            numel = height * width
            counts = []
            while sum(counts) <= numel:
                counts.append(int(torch.randint(5, 8, ())))
            if sum(counts) > numel:
                counts[-1] -= sum(counts) - numel
            return dict(counts=counts, size=[height, width])

        return dict(
            segmentation=make_rle_segmentation(),
            bbox=make_tensor((4,), dtype=torch.float32, low=0).tolist(),
            iscrowd=True,
            area=float(make_scalar(dtype=torch.float32)),
            category_id=int(make_scalar(dtype=torch.int64)),
        )

    @staticmethod
    def _make_captions_data(ann_id, image_meta):
        return dict(caption=f"Caption {ann_id} describing image {image_meta['id']}.")

    @classmethod
    def _make_annotations(cls, root, name, *, images_meta):
        num_anns_per_image = torch.zeros((len(images_meta),), dtype=torch.int64)
        for annotations, fn in (
            ("instances", cls._make_instances_data),
            ("captions", cls._make_captions_data),
        ):
            num_anns_per_image += cls._make_annotations_json(
                root, f"{annotations}_{name}.json", images_meta=images_meta, fn=fn
            )

        return int(num_anns_per_image.sum())

    @classmethod
    def generate(
        cls,
        root,
        *,
618
        split,
Philip Meier's avatar
Philip Meier committed
619
620
621
622
623
624
        year,
        num_samples,
    ):
        annotations_dir = root / "annotations"
        annotations_dir.mkdir()

625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
        for split_ in ("train", "val"):
            config_name = f"{split_}{year}"

            images_meta = [
                dict(
                    file_name=f"{idx:012d}.jpg",
                    id=idx,
                    width=width,
                    height=height,
                )
                for idx, (height, width) in enumerate(
                    torch.randint(3, 11, size=(num_samples, 2), dtype=torch.int).tolist()
                )
            ]

            if split_ == split:
                create_image_folder(
                    root,
                    config_name,
                    file_name_fn=lambda idx: images_meta[idx]["file_name"],
                    num_examples=num_samples,
                    size=lambda idx: (3, images_meta[idx]["height"], images_meta[idx]["width"]),
                )
                make_zip(root, f"{config_name}.zip")
Philip Meier's avatar
Philip Meier committed
649
650
651
652
653
654
655
656
657
658
659
660

            cls._make_annotations(
                annotations_dir,
                config_name,
                images_meta=images_meta,
            )

        make_zip(root, f"annotations_trainval{year}.zip", annotations_dir)

        return num_samples


661
662
663
664
665
666
667
668
@register_mock(
    configs=combinations_grid(
        split=("train", "val"),
        year=("2017", "2014"),
        annotations=("instances", "captions", None),
    )
)
def coco(root, config):
669
    return CocoMockData.generate(root, split=config["split"], year=config["year"], num_samples=5)
670
671


672
673
674
675
class SBDMockData:
    _NUM_CATEGORIES = 20

    @classmethod
676
677
678
679
680
681
682
683
684
    def _make_split_files(cls, root_map, *, split):
        splits_and_idcs = [
            ("train", [0, 1, 2]),
            ("val", [3]),
        ]
        if split == "train_noval":
            splits_and_idcs.append(("train_noval", [0, 2]))

        ids_map = {split: [f"2008_{idx:06d}" for idx in idcs] for split, idcs in splits_and_idcs}
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724

        for split, ids in ids_map.items():
            with open(root_map[split] / f"{split}.txt", "w") as fh:
                fh.writelines(f"{id}\n" for id in ids)

        return sorted(set(itertools.chain(*ids_map.values()))), {split: len(ids) for split, ids in ids_map.items()}

    @classmethod
    def _make_anns_folder(cls, root, name, ids):
        from scipy.io import savemat

        anns_folder = root / name
        anns_folder.mkdir()

        sizes = torch.randint(1, 9, size=(len(ids), 2)).tolist()
        for id, size in zip(ids, sizes):
            savemat(
                anns_folder / f"{id}.mat",
                {
                    "GTcls": {
                        "Boundaries": cls._make_boundaries(size),
                        "Segmentation": cls._make_segmentation(size),
                    }
                },
            )
        return sizes

    @classmethod
    def _make_boundaries(cls, size):
        from scipy.sparse import csc_matrix

        return [
            [csc_matrix(torch.randint(0, 2, size=size, dtype=torch.uint8).numpy())] for _ in range(cls._NUM_CATEGORIES)
        ]

    @classmethod
    def _make_segmentation(cls, size):
        return torch.randint(0, cls._NUM_CATEGORIES + 1, size=size, dtype=torch.uint8).numpy()

    @classmethod
725
    def generate(cls, root, *, split):
726
727
728
729
        archive_folder = root / "benchmark_RELEASE"
        dataset_folder = archive_folder / "dataset"
        dataset_folder.mkdir(parents=True, exist_ok=True)

730
731
732
        ids, num_samples_map = cls._make_split_files(
            defaultdict(lambda: dataset_folder, {"train_noval": root}), split=split
        )
733
734
735
736
737
738
739
        sizes = cls._make_anns_folder(dataset_folder, "cls", ids)
        create_image_folder(
            dataset_folder, "img", lambda idx: f"{ids[idx]}.jpg", num_examples=len(ids), size=lambda idx: sizes[idx]
        )

        make_tar(root, "benchmark.tgz", archive_folder, compression="gz")

740
        return num_samples_map[split]
741
742


743
744
@register_mock(configs=combinations_grid(split=("train", "val", "train_noval")))
def sbd(root, config):
745
    return SBDMockData.generate(root, split=config["split"])
746
747


748
749
@register_mock(configs=[dict()])
def semeion(root, config):
750
    num_samples = 3
751
    num_categories = 10
752
753

    images = torch.rand(num_samples, 256)
754
    labels = one_hot(torch.randint(num_categories, size=(num_samples,)), num_classes=num_categories)
755
756
757
758
    with open(root / "semeion.data", "w") as fh:
        for image, one_hot_label in zip(images, labels):
            image_columns = " ".join([f"{pixel.item():.4f}" for pixel in image])
            labels_columns = " ".join([str(label.item()) for label in one_hot_label])
759
            fh.write(f"{image_columns} {labels_columns} \n")
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786

    return num_samples


class VOCMockData:
    _TRAIN_VAL_FILE_NAMES = {
        "2007": "VOCtrainval_06-Nov-2007.tar",
        "2008": "VOCtrainval_14-Jul-2008.tar",
        "2009": "VOCtrainval_11-May-2009.tar",
        "2010": "VOCtrainval_03-May-2010.tar",
        "2011": "VOCtrainval_25-May-2011.tar",
        "2012": "VOCtrainval_11-May-2012.tar",
    }
    _TEST_FILE_NAMES = {
        "2007": "VOCtest_06-Nov-2007.tar",
    }

    @classmethod
    def _make_split_files(cls, root, *, year, trainval):
        split_folder = root / "ImageSets"

        if trainval:
            idcs_map = {
                "train": [0, 1, 2],
                "val": [3, 4],
            }
            idcs_map["trainval"] = [*idcs_map["train"], *idcs_map["val"]]
787
        else:
788
789
790
791
            idcs_map = {
                "test": [5],
            }
        ids_map = {split: [f"{year}_{idx:06d}" for idx in idcs] for split, idcs in idcs_map.items()}
792

793
794
795
796
797
798
        for task_sub_folder in ("Main", "Segmentation"):
            task_folder = split_folder / task_sub_folder
            task_folder.mkdir(parents=True, exist_ok=True)
            for split, ids in ids_map.items():
                with open(task_folder / f"{split}.txt", "w") as fh:
                    fh.writelines(f"{id}\n" for id in ids)
799

800
801
802
803
804
805
806
807
808
809
810
811
812
813
        return sorted(set(itertools.chain(*ids_map.values()))), {split: len(ids) for split, ids in ids_map.items()}

    @classmethod
    def _make_detection_anns_folder(cls, root, name, *, file_name_fn, num_examples):
        folder = root / name
        folder.mkdir(parents=True, exist_ok=True)

        for idx in range(num_examples):
            cls._make_detection_ann_file(folder, file_name_fn(idx))

    @classmethod
    def _make_detection_ann_file(cls, root, name):
        def add_child(parent, name, text=None):
            child = ET.SubElement(parent, name)
814
            child.text = str(text)
815
816
817
818
819
            return child

        def add_name(obj, name="dog"):
            add_child(obj, "name", name)

820
821
822
823
824
        def add_size(obj):
            obj = add_child(obj, "size")
            size = {"width": 0, "height": 0, "depth": 3}
            for name, text in size.items():
                add_child(obj, name, text)
825

826
        def add_bndbox(obj):
827
            obj = add_child(obj, "bndbox")
828
            bndbox = {"xmin": 1, "xmax": 2, "ymin": 3, "ymax": 4}
829
830
831
832
            for name, text in bndbox.items():
                add_child(obj, name, text)

        annotation = ET.Element("annotation")
833
        add_size(annotation)
834
        obj = add_child(annotation, "object")
835
836
        add_name(obj)
        add_bndbox(obj)
837
838
839
840
841
842
843
844

        with open(root / name, "wb") as fh:
            fh.write(ET.tostring(annotation))

    @classmethod
    def generate(cls, root, *, year, trainval):
        archive_folder = root
        if year == "2011":
845
846
847
848
849
            archive_folder = root / "TrainVal"
            data_folder = archive_folder / "VOCdevkit"
        else:
            archive_folder = data_folder = root / "VOCdevkit"
        data_folder = data_folder / f"VOC{year}"
850
851
852
853
854
855
856
857
858
        data_folder.mkdir(parents=True, exist_ok=True)

        ids, num_samples_map = cls._make_split_files(data_folder, year=year, trainval=trainval)
        for make_folder_fn, name, suffix in [
            (create_image_folder, "JPEGImages", ".jpg"),
            (create_image_folder, "SegmentationClass", ".png"),
            (cls._make_detection_anns_folder, "Annotations", ".xml"),
        ]:
            make_folder_fn(data_folder, name, file_name_fn=lambda idx: ids[idx] + suffix, num_examples=len(ids))
859
        make_tar(root, (cls._TRAIN_VAL_FILE_NAMES if trainval else cls._TEST_FILE_NAMES)[year], archive_folder)
860
861
862
863

        return num_samples_map


864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
@register_mock(
    configs=[
        *combinations_grid(
            split=("train", "val", "trainval"),
            year=("2007", "2008", "2009", "2010", "2011", "2012"),
            task=("detection", "segmentation"),
        ),
        *combinations_grid(
            split=("test",),
            year=("2007",),
            task=("detection", "segmentation"),
        ),
    ],
)
def voc(root, config):
    trainval = config["split"] != "test"
    return VOCMockData.generate(root, year=config["year"], trainval=trainval)[config["split"]]
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970


class CelebAMockData:
    @classmethod
    def _make_ann_file(cls, root, name, data, *, field_names=None):
        with open(root / name, "w") as file:
            if field_names:
                file.write(f"{len(data)}\r\n")
                file.write(" ".join(field_names) + "\r\n")
            file.writelines(" ".join(str(item) for item in row) + "\r\n" for row in data)

    _SPLIT_TO_IDX = {
        "train": 0,
        "val": 1,
        "test": 2,
    }

    @classmethod
    def _make_split_file(cls, root):
        num_samples_map = {"train": 4, "val": 3, "test": 2}

        data = [
            (f"{idx:06d}.jpg", cls._SPLIT_TO_IDX[split])
            for split, num_samples in num_samples_map.items()
            for idx in range(num_samples)
        ]
        cls._make_ann_file(root, "list_eval_partition.txt", data)

        image_file_names, _ = zip(*data)
        return image_file_names, num_samples_map

    @classmethod
    def _make_identity_file(cls, root, image_file_names):
        cls._make_ann_file(
            root, "identity_CelebA.txt", [(name, int(make_scalar(low=1, dtype=torch.int))) for name in image_file_names]
        )

    @classmethod
    def _make_attributes_file(cls, root, image_file_names):
        field_names = ("5_o_Clock_Shadow", "Young")
        data = [
            [name, *[" 1" if attr else "-1" for attr in make_tensor((len(field_names),), dtype=torch.bool)]]
            for name in image_file_names
        ]
        cls._make_ann_file(root, "list_attr_celeba.txt", data, field_names=(*field_names, ""))

    @classmethod
    def _make_bounding_boxes_file(cls, root, image_file_names):
        field_names = ("image_id", "x_1", "y_1", "width", "height")
        data = [
            [f"{name}  ", *[f"{coord:3d}" for coord in make_tensor((4,), low=0, dtype=torch.int).tolist()]]
            for name in image_file_names
        ]
        cls._make_ann_file(root, "list_bbox_celeba.txt", data, field_names=field_names)

    @classmethod
    def _make_landmarks_file(cls, root, image_file_names):
        field_names = ("lefteye_x", "lefteye_y", "rightmouth_x", "rightmouth_y")
        data = [
            [
                name,
                *[
                    f"{coord:4d}" if idx else coord
                    for idx, coord in enumerate(make_tensor((len(field_names),), low=0, dtype=torch.int).tolist())
                ],
            ]
            for name in image_file_names
        ]
        cls._make_ann_file(root, "list_landmarks_align_celeba.txt", data, field_names=field_names)

    @classmethod
    def generate(cls, root):
        image_file_names, num_samples_map = cls._make_split_file(root)

        image_files = create_image_folder(
            root, "img_align_celeba", file_name_fn=lambda idx: image_file_names[idx], num_examples=len(image_file_names)
        )
        make_zip(root, image_files[0].parent.with_suffix(".zip").name)

        for make_ann_file_fn in (
            cls._make_identity_file,
            cls._make_attributes_file,
            cls._make_bounding_boxes_file,
            cls._make_landmarks_file,
        ):
            make_ann_file_fn(root, image_file_names)

        return num_samples_map


971
972
973
@register_mock(configs=combinations_grid(split=("train", "val", "test")))
def celeba(root, config):
    return CelebAMockData.generate(root)[config["split"]]
974
975


976
977
978
@register_mock(configs=combinations_grid(split=("train", "val", "test")))
def country211(root, config):
    split_folder = pathlib.Path(root, "country211", "valid" if config["split"] == "val" else config["split"])
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
    split_folder.mkdir(parents=True, exist_ok=True)

    num_examples = {
        "train": 3,
        "val": 4,
        "test": 5,
    }[config["split"]]

    classes = ("AD", "BS", "GR")
    for cls in classes:
        create_image_folder(
            split_folder,
            name=cls,
            file_name_fn=lambda idx: f"{idx}.jpg",
            num_examples=num_examples,
        )
    make_tar(root, f"{split_folder.parent.name}.tgz", split_folder.parent, compression="gz")
    return num_examples * len(classes)


999
1000
@register_mock(configs=combinations_grid(split=("train", "test")))
def food101(root, config):
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
    data_folder = root / "food-101"

    num_images_per_class = 3
    image_folder = data_folder / "images"
    categories = ["apple_pie", "baby_back_ribs", "waffles"]
    image_ids = []
    for category in categories:
        image_files = create_image_folder(
            image_folder,
            category,
            file_name_fn=lambda idx: f"{idx:04d}.jpg",
            num_examples=num_images_per_class,
        )
        image_ids.extend(path.relative_to(path.parents[1]).with_suffix("").as_posix() for path in image_files)

    meta_folder = data_folder / "meta"
    meta_folder.mkdir()

    with open(meta_folder / "classes.txt", "w") as file:
        for category in categories:
            file.write(f"{category}\n")

    splits = ["train", "test"]
    num_samples_map = {}
    for offset, split in enumerate(splits):
        image_ids_in_split = image_ids[offset :: len(splits)]
        num_samples_map[split] = len(image_ids_in_split)
        with open(meta_folder / f"{split}.txt", "w") as file:
            for image_id in image_ids_in_split:
                file.write(f"{image_id}\n")

    make_tar(root, f"{data_folder.name}.tar.gz", compression="gz")

1034
    return num_samples_map[config["split"]]
1035
1036


1037
1038
@register_mock(configs=combinations_grid(split=("train", "val", "test"), fold=(1, 4, 10)))
def dtd(root, config):
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
    data_folder = root / "dtd"

    num_images_per_class = 3
    image_folder = data_folder / "images"
    categories = {"banded", "marbled", "zigzagged"}
    image_ids_per_category = {
        category: [
            str(path.relative_to(path.parents[1]).as_posix())
            for path in create_image_folder(
                image_folder,
                category,
                file_name_fn=lambda idx: f"{category}_{idx:04d}.jpg",
                num_examples=num_images_per_class,
            )
        ]
        for category in categories
    }

    meta_folder = data_folder / "labels"
    meta_folder.mkdir()

    with open(meta_folder / "labels_joint_anno.txt", "w") as file:
        for cls, image_ids in image_ids_per_category.items():
            for image_id in image_ids:
                joint_categories = random.choices(
                    list(categories - {cls}), k=int(torch.randint(len(categories) - 1, ()))
                )
                file.write(" ".join([image_id, *sorted([cls, *joint_categories])]) + "\n")

    image_ids = list(itertools.chain(*image_ids_per_category.values()))
    splits = ("train", "val", "test")
    num_samples_map = {}
    for fold in range(1, 11):
        random.shuffle(image_ids)
        for offset, split in enumerate(splits):
            image_ids_in_config = image_ids[offset :: len(splits)]
            with open(meta_folder / f"{split}{fold}.txt", "w") as file:
                file.write("\n".join(image_ids_in_config) + "\n")

1078
            num_samples_map[(split, fold)] = len(image_ids_in_config)
1079
1080
1081

    make_tar(root, "dtd-r1.0.1.tar.gz", data_folder, compression="gz")

1082
    return num_samples_map[config["split"], config["fold"]]
1083
1084


1085
1086
1087
1088
@register_mock(configs=combinations_grid(split=("train", "test")))
def fer2013(root, config):
    split = config["split"]
    num_samples = 5 if split == "train" else 3
1089

1090
    path = root / f"{split}.csv"
1091
    with open(path, "w", newline="") as file:
1092
        field_names = ["emotion"] if split == "train" else []
1093
1094
1095
1096
1097
1098
1099
1100
1101
        field_names.append("pixels")

        file.write(",".join(field_names) + "\n")

        writer = csv.DictWriter(file, fieldnames=field_names, quotechar='"', quoting=csv.QUOTE_NONNUMERIC)
        for _ in range(num_samples):
            rowdict = {
                "pixels": " ".join([str(int(pixel)) for pixel in torch.randint(256, (48 * 48,), dtype=torch.uint8)])
            }
1102
            if split == "train":
1103
1104
1105
1106
1107
1108
1109
1110
                rowdict["emotion"] = int(torch.randint(7, ()))
            writer.writerow(rowdict)

    make_zip(root, f"{path.name}.zip", path)

    return num_samples


1111
1112
1113
@register_mock(configs=combinations_grid(split=("train", "test")))
def gtsrb(root, config):
    num_examples_per_class = 5 if config["split"] == "train" else 3
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
    classes = ("00000", "00042", "00012")
    num_examples = num_examples_per_class * len(classes)

    csv_columns = ["Filename", "Width", "Height", "Roi.X1", "Roi.Y1", "Roi.X2", "Roi.Y2", "ClassId"]

    def _make_ann_file(path, num_examples, class_idx):
        if class_idx == "random":
            class_idx = torch.randint(1, len(classes) + 1, size=(1,)).item()

        with open(path, "w") as csv_file:
            writer = csv.DictWriter(csv_file, fieldnames=csv_columns, delimiter=";")
            writer.writeheader()
            for image_idx in range(num_examples):
                writer.writerow(
                    {
                        "Filename": f"{image_idx:05d}.ppm",
                        "Width": torch.randint(1, 100, size=()).item(),
                        "Height": torch.randint(1, 100, size=()).item(),
                        "Roi.X1": torch.randint(1, 100, size=()).item(),
                        "Roi.Y1": torch.randint(1, 100, size=()).item(),
                        "Roi.X2": torch.randint(1, 100, size=()).item(),
                        "Roi.Y2": torch.randint(1, 100, size=()).item(),
                        "ClassId": class_idx,
                    }
                )

1140
1141
    archive_folder = root / "GTSRB"

1142
    if config["split"] == "train":
1143
        train_folder = archive_folder / "Training"
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
        train_folder.mkdir(parents=True)

        for class_idx in classes:
            create_image_folder(
                train_folder,
                name=class_idx,
                file_name_fn=lambda image_idx: f"{class_idx}_{image_idx:05d}.ppm",
                num_examples=num_examples_per_class,
            )
            _make_ann_file(
                path=train_folder / class_idx / f"GT-{class_idx}.csv",
                num_examples=num_examples_per_class,
                class_idx=int(class_idx),
            )
1158
        make_zip(root, "GTSRB-Training_fixed.zip", archive_folder)
1159
    else:
1160
        test_folder = archive_folder / "Final_Test"
1161
1162
1163
1164
1165
1166
1167
1168
1169
        test_folder.mkdir(parents=True)

        create_image_folder(
            test_folder,
            name="Images",
            file_name_fn=lambda image_idx: f"{image_idx:05d}.ppm",
            num_examples=num_examples,
        )

1170
        make_zip(root, "GTSRB_Final_Test_Images.zip", archive_folder)
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182

        _make_ann_file(
            path=root / "GT-final_test.csv",
            num_examples=num_examples,
            class_idx="random",
        )

        make_zip(root, "GTSRB_Final_Test_GT.zip", "GT-final_test.csv")

    return num_examples


1183
1184
@register_mock(configs=combinations_grid(split=("train", "val", "test")))
def clevr(root, config):
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
    data_folder = root / "CLEVR_v1.0"

    num_samples_map = {
        "train": 3,
        "val": 2,
        "test": 1,
    }

    images_folder = data_folder / "images"
    image_files = {
        split: create_image_folder(
            images_folder,
            split,
            file_name_fn=lambda idx: f"CLEVR_{split}_{idx:06d}.jpg",
            num_examples=num_samples,
        )
        for split, num_samples in num_samples_map.items()
    }

    scenes_folder = data_folder / "scenes"
    scenes_folder.mkdir()
    for split in ["train", "val"]:
        with open(scenes_folder / f"CLEVR_{split}_scenes.json", "w") as file:
            json.dump(
                {
                    "scenes": [
                        {
                            "image_filename": image_file.name,
                            # We currently only return the number of objects in a scene.
                            # Thus, it is sufficient for now to only mock the number of elements.
                            "objects": [None] * int(torch.randint(1, 5, ())),
                        }
                        for image_file in image_files[split]
                    ]
                },
                file,
            )

1223
    make_zip(root, f"{data_folder.name}.zip", data_folder)
1224

1225
    return num_samples_map[config["split"]]
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282


class OxfordIIITPetMockData:
    @classmethod
    def _meta_to_split_and_classification_ann(cls, meta, idx):
        image_id = "_".join(
            [
                *[(str.title if meta["species"] == "cat" else str.lower)(part) for part in meta["cls"].split()],
                str(idx),
            ]
        )
        class_id = str(meta["label"] + 1)
        species = "1" if meta["species"] == "cat" else "2"
        breed_id = "-1"
        return (image_id, class_id, species, breed_id)

    @classmethod
    def generate(self, root):
        classification_anns_meta = (
            dict(cls="Abyssinian", label=0, species="cat"),
            dict(cls="Keeshond", label=18, species="dog"),
            dict(cls="Yorkshire Terrier", label=36, species="dog"),
        )
        split_and_classification_anns = [
            self._meta_to_split_and_classification_ann(meta, idx)
            for meta, idx in itertools.product(classification_anns_meta, (1, 2, 10))
        ]
        image_ids, *_ = zip(*split_and_classification_anns)

        image_files = create_image_folder(
            root, "images", file_name_fn=lambda idx: f"{image_ids[idx]}.jpg", num_examples=len(image_ids)
        )

        anns_folder = root / "annotations"
        anns_folder.mkdir()
        random.shuffle(split_and_classification_anns)
        splits = ("trainval", "test")
        num_samples_map = {}
        for offset, split in enumerate(splits):
            split_and_classification_anns_in_split = split_and_classification_anns[offset :: len(splits)]
            with open(anns_folder / f"{split}.txt", "w") as file:
                writer = csv.writer(file, delimiter=" ")
                for split_and_classification_ann in split_and_classification_anns_in_split:
                    writer.writerow(split_and_classification_ann)

            num_samples_map[split] = len(split_and_classification_anns_in_split)

        segmentation_files = create_image_folder(
            anns_folder, "trimaps", file_name_fn=lambda idx: f"{image_ids[idx]}.png", num_examples=len(image_ids)
        )

        # The dataset has some rogue files
        for path in image_files[:3]:
            path.with_suffix(".mat").touch()
        for path in segmentation_files:
            path.with_name(f".{path.name}").touch()

1283
1284
        make_tar(root, "images.tar.gz", compression="gz")
        make_tar(root, anns_folder.with_suffix(".tar.gz").name, compression="gz")
1285
1286
1287
1288

        return num_samples_map


1289
1290
1291
@register_mock(name="oxford-iiit-pet", configs=combinations_grid(split=("trainval", "test")))
def oxford_iiit_pet(root, config):
    return OxfordIIITPetMockData.generate(root)[config["split"]]
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371


class _CUB200MockData:
    @classmethod
    def _category_folder(cls, category, idx):
        return f"{idx:03d}.{category}"

    @classmethod
    def _file_stem(cls, category, idx):
        return f"{category}_{idx:04d}"

    @classmethod
    def _make_images(cls, images_folder):
        image_files = []
        for category_idx, category in [
            (1, "Black_footed_Albatross"),
            (100, "Brown_Pelican"),
            (200, "Common_Yellowthroat"),
        ]:
            image_files.extend(
                create_image_folder(
                    images_folder,
                    cls._category_folder(category, category_idx),
                    lambda image_idx: f"{cls._file_stem(category, image_idx)}.jpg",
                    num_examples=5,
                )
            )

        return image_files


class CUB2002011MockData(_CUB200MockData):
    @classmethod
    def _make_archive(cls, root):
        archive_folder = root / "CUB_200_2011"

        images_folder = archive_folder / "images"
        image_files = cls._make_images(images_folder)
        image_ids = list(range(1, len(image_files) + 1))

        with open(archive_folder / "images.txt", "w") as file:
            file.write(
                "\n".join(
                    f"{id} {path.relative_to(images_folder).as_posix()}" for id, path in zip(image_ids, image_files)
                )
            )

        split_ids = torch.randint(2, (len(image_ids),)).tolist()
        counts = Counter(split_ids)
        num_samples_map = {"train": counts[1], "test": counts[0]}
        with open(archive_folder / "train_test_split.txt", "w") as file:
            file.write("\n".join(f"{image_id} {split_id}" for image_id, split_id in zip(image_ids, split_ids)))

        with open(archive_folder / "bounding_boxes.txt", "w") as file:
            file.write(
                "\n".join(
                    " ".join(
                        str(item)
                        for item in [image_id, *make_tensor((4,), dtype=torch.int, low=0).to(torch.float).tolist()]
                    )
                    for image_id in image_ids
                )
            )

        make_tar(root, archive_folder.with_suffix(".tgz").name, compression="gz")

        return image_files, num_samples_map

    @classmethod
    def _make_segmentations(cls, root, image_files):
        segmentations_folder = root / "segmentations"
        for image_file in image_files:
            folder = segmentations_folder.joinpath(image_file.relative_to(image_file.parents[1]))
            folder.mkdir(exist_ok=True, parents=True)
            create_image_file(
                folder,
                image_file.with_suffix(".png").name,
                size=[1, *make_tensor((2,), low=3, dtype=torch.int).tolist()],
            )

1372
        make_tar(root, segmentations_folder.with_suffix(".tgz").name, compression="gz")
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454

    @classmethod
    def generate(cls, root):
        image_files, num_samples_map = cls._make_archive(root)
        cls._make_segmentations(root, image_files)
        return num_samples_map


class CUB2002010MockData(_CUB200MockData):
    @classmethod
    def _make_hidden_rouge_file(cls, *files):
        for file in files:
            (file.parent / f"._{file.name}").touch()

    @classmethod
    def _make_splits(cls, root, image_files):
        split_folder = root / "lists"
        split_folder.mkdir()
        random.shuffle(image_files)
        splits = ("train", "test")
        num_samples_map = {}
        for offset, split in enumerate(splits):
            image_files_in_split = image_files[offset :: len(splits)]

            split_file = split_folder / f"{split}.txt"
            with open(split_file, "w") as file:
                file.write(
                    "\n".join(
                        sorted(
                            str(image_file.relative_to(image_file.parents[1]).as_posix())
                            for image_file in image_files_in_split
                        )
                    )
                )

            cls._make_hidden_rouge_file(split_file)
            num_samples_map[split] = len(image_files_in_split)

        make_tar(root, split_folder.with_suffix(".tgz").name, compression="gz")

        return num_samples_map

    @classmethod
    def _make_anns(cls, root, image_files):
        from scipy.io import savemat

        anns_folder = root / "annotations-mat"
        for image_file in image_files:
            ann_file = anns_folder / image_file.with_suffix(".mat").relative_to(image_file.parents[1])
            ann_file.parent.mkdir(parents=True, exist_ok=True)

            savemat(
                ann_file,
                {
                    "seg": torch.randint(
                        256, make_tensor((2,), low=3, dtype=torch.int).tolist(), dtype=torch.uint8
                    ).numpy(),
                    "bbox": dict(
                        zip(("left", "top", "right", "bottom"), make_tensor((4,), dtype=torch.uint8).tolist())
                    ),
                },
            )

        readme_file = anns_folder / "README.txt"
        readme_file.touch()
        cls._make_hidden_rouge_file(readme_file)

        make_tar(root, "annotations.tgz", anns_folder, compression="gz")

    @classmethod
    def generate(cls, root):
        images_folder = root / "images"
        image_files = cls._make_images(images_folder)
        cls._make_hidden_rouge_file(*image_files)
        make_tar(root, images_folder.with_suffix(".tgz").name, compression="gz")

        num_samples_map = cls._make_splits(root, image_files)
        cls._make_anns(root, image_files)

        return num_samples_map


1455
1456
1457
1458
@register_mock(configs=combinations_grid(split=("train", "test"), year=("2010", "2011")))
def cub200(root, config):
    num_samples_map = (CUB2002011MockData if config["year"] == "2011" else CUB2002010MockData).generate(root)
    return num_samples_map[config["split"]]
1459
1460


1461
1462
@register_mock(configs=[dict()])
def eurosat(root, config):
1463
    data_folder = root / "2750"
1464
1465
1466
    data_folder.mkdir(parents=True)

    num_examples_per_class = 3
1467
1468
    categories = ["AnnualCrop", "Forest"]
    for category in categories:
1469
1470
        create_image_folder(
            root=data_folder,
1471
1472
            name=category,
            file_name_fn=lambda idx: f"{category}_{idx + 1}.jpg",
1473
1474
1475
            num_examples=num_examples_per_class,
        )
    make_zip(root, "EuroSAT.zip", data_folder)
1476
    return len(categories) * num_examples_per_class
1477
1478


1479
1480
@register_mock(configs=combinations_grid(split=("train", "test", "extra")))
def svhn(root, config):
1481
1482
1483
1484
1485
1486
    import scipy.io as sio

    num_samples = {
        "train": 2,
        "test": 3,
        "extra": 4,
1487
    }[config["split"]]
1488
1489

    sio.savemat(
1490
        root / f"{config['split']}_32x32.mat",
1491
1492
1493
1494
1495
1496
        {
            "X": np.random.randint(256, size=(32, 32, 3, num_samples), dtype=np.uint8),
            "y": np.random.randint(10, size=(num_samples,), dtype=np.uint8),
        },
    )
    return num_samples
1497
1498


1499
1500
@register_mock(configs=combinations_grid(split=("train", "val", "test")))
def pcam(root, config):
1501
1502
    import h5py

1503
    num_images = {"train": 2, "test": 3, "val": 4}[config["split"]]
1504

1505
    split = "valid" if config["split"] == "val" else config["split"]
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523

    images_io = io.BytesIO()
    with h5py.File(images_io, "w") as f:
        f["x"] = np.random.randint(0, 256, size=(num_images, 10, 10, 3), dtype=np.uint8)

    targets_io = io.BytesIO()
    with h5py.File(targets_io, "w") as f:
        f["y"] = np.random.randint(0, 2, size=(num_images, 1, 1, 1), dtype=np.uint8)

    # Create .gz compressed files
    images_file = root / f"camelyonpatch_level_2_split_{split}_x.h5.gz"
    targets_file = root / f"camelyonpatch_level_2_split_{split}_y.h5.gz"
    for compressed_file_name, uncompressed_file_io in ((images_file, images_io), (targets_file, targets_io)):
        compressed_data = gzip.compress(uncompressed_file_io.getbuffer())
        with open(compressed_file_name, "wb") as compressed_file:
            compressed_file.write(compressed_data)

    return num_images
1524
1525


1526
1527
@register_mock(name="stanford-cars", configs=combinations_grid(split=("train", "test")))
def stanford_cars(root, config):
1528
1529
1530
    import scipy.io as io
    from numpy.core.records import fromarrays

1531
1532
    split = config["split"]
    num_samples = {"train": 5, "test": 7}[split]
1533
1534
    num_categories = 3

1535
    if split == "train":
1536
        images_folder_name = "cars_train"
1537
1538
        devkit = root / "devkit"
        devkit.mkdir()
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
        annotations_mat_path = devkit / "cars_train_annos.mat"
    else:
        images_folder_name = "cars_test"
        annotations_mat_path = root / "cars_test_annos_withlabels.mat"

    create_image_folder(
        root=root,
        name=images_folder_name,
        file_name_fn=lambda image_index: f"{image_index:5d}.jpg",
        num_examples=num_samples,
    )

1551
    make_tar(root, f"cars_{split}.tgz", images_folder_name)
1552
1553
1554
1555
1556
1557
1558
1559
1560
    bbox = np.random.randint(1, 200, num_samples, dtype=np.uint8)
    classes = np.random.randint(1, num_categories + 1, num_samples, dtype=np.uint8)
    fnames = [f"{i:5d}.jpg" for i in range(num_samples)]
    rec_array = fromarrays(
        [bbox, bbox, bbox, bbox, classes, fnames],
        names=["bbox_x1", "bbox_y1", "bbox_x2", "bbox_y2", "class", "fname"],
    )

    io.savemat(annotations_mat_path, {"annotations": rec_array})
1561
    if split == "train":
1562
1563
1564
        make_tar(root, "car_devkit.tgz", devkit, compression="gz")

    return num_samples
Lezwon Castelino's avatar
Lezwon Castelino committed
1565
1566


1567
1568
1569
@register_mock(configs=combinations_grid(split=("train", "test")))
def usps(root, config):
    num_samples = {"train": 15, "test": 7}[config["split"]]
Lezwon Castelino's avatar
Lezwon Castelino committed
1570

1571
    with bz2.open(root / f"usps{'.t' if not config['split'] == 'train' else ''}.bz2", "wb") as fh:
Lezwon Castelino's avatar
Lezwon Castelino committed
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
        lines = []
        for _ in range(num_samples):
            label = make_tensor(1, low=1, high=11, dtype=torch.int)
            values = make_tensor(256, low=-1, high=1, dtype=torch.float)
            lines.append(
                " ".join([f"{int(label)}", *(f"{idx}:{float(value):.6f}" for idx, value in enumerate(values, 1))])
            )

        fh.write("\n".join(lines).encode())

    return num_samples