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
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
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/2526/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.

        Parameters:
          name: The name of the dataset, for example `oxford_flowers102`.
          decode_on_demand: if `False` (the default), the images are fully
            decoded when the dataset is loaded; if `True`, the images are
            loaded as byte strings and decoded on demand when accessed.
        """
        path = download_url_to_file(self.URL, f"{name}.tfrecord", f"{name}.tfrecord.LICENSE")
        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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
33
34
35
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
37
38
39
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.

Parameters:

  • name (str) –

    The name of the dataset, for example oxford_flowers102.

  • decode_on_demand (bool, default: False ) –

    if False (the default), the images are fully decoded when the dataset is loaded; if True, the images are loaded as byte strings and decoded on demand when accessed.

Source code in npfl138/datasets/image64_dataset.py
48
49
50
51
52
53
54
55
56
57
58
def __init__(self, name: str, decode_on_demand: bool = False) -> None:
    """Load the given dataset, downloading it if necessary.

    Parameters:
      name: The name of the dataset, for example `oxford_flowers102`.
      decode_on_demand: if `False` (the default), the images are fully
        decoded when the dataset is loaded; if `True`, the images are
        loaded as byte strings and decoded on demand when accessed.
    """
    path = download_url_to_file(self.URL, f"{name}.tfrecord", f"{name}.tfrecord.LICENSE")
    self.train = self.Dataset(path, -1, decode_on_demand)

train instance-attribute

train: Dataset

The training dataset.