Skip to content

Image64Dataset

Image64Dataset is a dataset of unlabeled color 64x64 images.

npfl138.datasets.image64_dataset.Image64Dataset

Source code in npfl138/datasets/image64_dataset.py
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
class Image64Dataset:
    C: int = 3
    """The number of channels of the images."""
    H: int = 64
    """The height of the images."""
    W: int = 64
    """The width of the images."""

    Element = TypedDict("Element", {"image": torch.Tensor})
    """The type of a single dataset element."""

    URL: str = "https://ufal.mff.cuni.cz/~straka/courses/npfl138/2425/datasets/"

    class Dataset(TFRecordDataset):
        def __init__(self, path: str, size: int, decode_on_demand: bool) -> None:
            super().__init__(path, size, decode_on_demand)

        def __len__(self) -> int:
            """Return the number of elements in the dataset."""
            return super().__len__()

        def __getitem__(self, index: int) -> "Image64Dataset.Element":
            """Return the `index`-th element of the dataset."""
            return super().__getitem__(index)

        def _tfrecord_decode(self, data: dict, indices: dict, index: int) -> "Image64Dataset.Element":
            return {
                "image": torchvision.io.decode_image(
                    data["image"][indices["image"][index]:indices["image"][index + 1]],
                    torchvision.io.ImageReadMode.RGB),
            }

    def __init__(self, name: str, decode_on_demand: bool = False) -> None:
        """Load the given dataset, downloading it if necessary."""
        path = "{}.tfrecord".format(name)
        if not os.path.exists(path):
            print("Downloading file {}...".format(path), file=sys.stderr)
            urllib.request.urlretrieve("{}/{}.LICENSE".format(self.URL, path), filename="{}.LICENSE".format(path))
            urllib.request.urlretrieve("{}/{}".format(self.URL, path), filename="{}.tmp".format(path))
            os.rename("{}.tmp".format(path), path)

        self.train = self.Dataset(path, -1, decode_on_demand)

    train: Dataset
    """The training dataset."""

C class-attribute instance-attribute

C: int = 3

The number of channels of the images.

H class-attribute instance-attribute

H: int = 64

The height of the images.

W class-attribute instance-attribute

W: int = 64

The width of the images.

Element class-attribute instance-attribute

Element = TypedDict('Element', {'image': Tensor})

The type of a single dataset element.

Dataset

Bases: TFRecordDataset

Source code in npfl138/datasets/image64_dataset.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Dataset(TFRecordDataset):
    def __init__(self, path: str, size: int, decode_on_demand: bool) -> None:
        super().__init__(path, size, decode_on_demand)

    def __len__(self) -> int:
        """Return the number of elements in the dataset."""
        return super().__len__()

    def __getitem__(self, index: int) -> "Image64Dataset.Element":
        """Return the `index`-th element of the dataset."""
        return super().__getitem__(index)

    def _tfrecord_decode(self, data: dict, indices: dict, index: int) -> "Image64Dataset.Element":
        return {
            "image": torchvision.io.decode_image(
                data["image"][indices["image"][index]:indices["image"][index + 1]],
                torchvision.io.ImageReadMode.RGB),
        }

__len__

__len__() -> int

Return the number of elements in the dataset.

Source code in npfl138/datasets/image64_dataset.py
35
36
37
def __len__(self) -> int:
    """Return the number of elements in the dataset."""
    return super().__len__()

__getitem__

__getitem__(index: int) -> Element

Return the index-th element of the dataset.

Source code in npfl138/datasets/image64_dataset.py
39
40
41
def __getitem__(self, index: int) -> "Image64Dataset.Element":
    """Return the `index`-th element of the dataset."""
    return super().__getitem__(index)

__init__

__init__(name: str, decode_on_demand: bool = False) -> None

Load the given dataset, downloading it if necessary.

Source code in npfl138/datasets/image64_dataset.py
50
51
52
53
54
55
56
57
58
59
def __init__(self, name: str, decode_on_demand: bool = False) -> None:
    """Load the given dataset, downloading it if necessary."""
    path = "{}.tfrecord".format(name)
    if not os.path.exists(path):
        print("Downloading file {}...".format(path), file=sys.stderr)
        urllib.request.urlretrieve("{}/{}.LICENSE".format(self.URL, path), filename="{}.LICENSE".format(path))
        urllib.request.urlretrieve("{}/{}".format(self.URL, path), filename="{}.tmp".format(path))
        os.rename("{}.tmp".format(path), path)

    self.train = self.Dataset(path, -1, decode_on_demand)

train instance-attribute

train: Dataset

The training dataset.