|
| 1 | +import gzip |
| 2 | +import os |
| 3 | +import struct |
| 4 | +import sys |
| 5 | +import urllib.request |
| 6 | +from array import array |
| 7 | +from pathlib import Path |
| 8 | +from typing import TYPE_CHECKING |
| 9 | +from urllib.error import HTTPError |
| 10 | + |
| 11 | +import torch |
| 12 | +from safeds._config import _init_default_device |
| 13 | +from safeds.data.image.containers._single_size_image_list import _SingleSizeImageList |
| 14 | +from safeds.data.labeled.containers import ImageDataset |
| 15 | +from safeds.data.tabular.containers import Column |
| 16 | + |
| 17 | +if TYPE_CHECKING: |
| 18 | + from safeds.data.image.containers import ImageList |
| 19 | + |
| 20 | +_mnist_links: list[str] = ["http://yann.lecun.com/exdb/mnist/", "https://ossci-datasets.s3.amazonaws.com/mnist/"] |
| 21 | +_mnist_files: dict[str, str] = { |
| 22 | + "train-images-idx3": "train-images-idx3-ubyte.gz", |
| 23 | + "train-labels-idx1": "train-labels-idx1-ubyte.gz", |
| 24 | + "test-images-idx3": "t10k-images-idx3-ubyte.gz", |
| 25 | + "test-labels-idx1": "t10k-labels-idx1-ubyte.gz", |
| 26 | +} |
| 27 | +_mnist_labels: dict[int, str] = {0: "0", 1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9"} |
| 28 | +_mnist_folder: str = "mnist" |
| 29 | + |
| 30 | +_fashion_mnist_links: list[str] = ["http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/"] |
| 31 | +_fashion_mnist_files: dict[str, str] = _mnist_files |
| 32 | +_fashion_mnist_labels: dict[int, str] = { |
| 33 | + 0: "T-shirt/top", |
| 34 | + 1: "Trouser", |
| 35 | + 2: "Pullover", |
| 36 | + 3: "Dress", |
| 37 | + 4: "Coat", |
| 38 | + 5: "Sandal", |
| 39 | + 6: "Shirt", |
| 40 | + 7: "Sneaker", |
| 41 | + 8: "Bag", |
| 42 | + 9: "Ankle boot", |
| 43 | +} |
| 44 | +_fashion_mnist_folder: str = "fashion-mnist" |
| 45 | + |
| 46 | +_kuzushiji_mnist_links: list[str] = ["http://codh.rois.ac.jp/kmnist/dataset/kmnist/"] |
| 47 | +_kuzushiji_mnist_files: dict[str, str] = _mnist_files |
| 48 | +_kuzushiji_mnist_labels: dict[int, str] = { |
| 49 | + 0: "\u304a", |
| 50 | + 1: "\u304d", |
| 51 | + 2: "\u3059", |
| 52 | + 3: "\u3064", |
| 53 | + 4: "\u306a", |
| 54 | + 5: "\u306f", |
| 55 | + 6: "\u307e", |
| 56 | + 7: "\u3084", |
| 57 | + 8: "\u308c", |
| 58 | + 9: "\u3092", |
| 59 | +} |
| 60 | +_kuzushiji_mnist_folder: str = "kmnist" |
| 61 | + |
| 62 | + |
| 63 | +def load_mnist(path: str | Path, download: bool = True) -> tuple[ImageDataset[Column], ImageDataset[Column]]: |
| 64 | + """ |
| 65 | + Load the `MNIST <http://yann.lecun.com/exdb/mnist/>`_ datasets. |
| 66 | +
|
| 67 | + Parameters |
| 68 | + ---------- |
| 69 | + path: |
| 70 | + the path were the files are stored or will be downloaded to |
| 71 | + download: |
| 72 | + whether the files should be downloaded to the given path |
| 73 | +
|
| 74 | + Returns |
| 75 | + ------- |
| 76 | + train_dataset, test_dataset: |
| 77 | + The train and test datasets. |
| 78 | +
|
| 79 | + Raises |
| 80 | + ------ |
| 81 | + FileNotFoundError |
| 82 | + if a file of the dataset cannot be found |
| 83 | + """ |
| 84 | + path = Path(path) / _mnist_folder |
| 85 | + path.mkdir(parents=True, exist_ok=True) |
| 86 | + path_files = os.listdir(path) |
| 87 | + missing_files = [] |
| 88 | + for file_path in _mnist_files.values(): |
| 89 | + if file_path not in path_files: |
| 90 | + missing_files.append(file_path) |
| 91 | + if len(missing_files) > 0: |
| 92 | + if download: |
| 93 | + _download_mnist_like( |
| 94 | + path, |
| 95 | + {name: f_path for name, f_path in _mnist_files.items() if f_path in missing_files}, |
| 96 | + _mnist_links, |
| 97 | + ) |
| 98 | + else: |
| 99 | + raise FileNotFoundError(f"Could not find files {[str(path / file) for file in missing_files]}") |
| 100 | + return _load_mnist_like(path, _mnist_files, _mnist_labels) |
| 101 | + |
| 102 | + |
| 103 | +def load_fashion_mnist(path: str | Path, download: bool = True) -> tuple[ImageDataset[Column], ImageDataset[Column]]: |
| 104 | + """ |
| 105 | + Load the `Fashion-MNIST <https://github.com/zalandoresearch/fashion-mnist>`_ datasets. |
| 106 | +
|
| 107 | + Parameters |
| 108 | + ---------- |
| 109 | + path: |
| 110 | + the path were the files are stored or will be downloaded to |
| 111 | + download: |
| 112 | + whether the files should be downloaded to the given path |
| 113 | +
|
| 114 | + Returns |
| 115 | + ------- |
| 116 | + train_dataset, test_dataset: |
| 117 | + The train and test datasets. |
| 118 | +
|
| 119 | + Raises |
| 120 | + ------ |
| 121 | + FileNotFoundError |
| 122 | + if a file of the dataset cannot be found |
| 123 | + """ |
| 124 | + path = Path(path) / _fashion_mnist_folder |
| 125 | + path.mkdir(parents=True, exist_ok=True) |
| 126 | + path_files = os.listdir(path) |
| 127 | + missing_files = [] |
| 128 | + for file_path in _fashion_mnist_files.values(): |
| 129 | + if file_path not in path_files: |
| 130 | + missing_files.append(file_path) |
| 131 | + if len(missing_files) > 0: |
| 132 | + if download: |
| 133 | + _download_mnist_like( |
| 134 | + path, |
| 135 | + {name: f_path for name, f_path in _fashion_mnist_files.items() if f_path in missing_files}, |
| 136 | + _fashion_mnist_links, |
| 137 | + ) |
| 138 | + else: |
| 139 | + raise FileNotFoundError(f"Could not find files {[str(path / file) for file in missing_files]}") |
| 140 | + return _load_mnist_like(path, _fashion_mnist_files, _fashion_mnist_labels) |
| 141 | + |
| 142 | + |
| 143 | +def load_kmnist(path: str | Path, download: bool = True) -> tuple[ImageDataset[Column], ImageDataset[Column]]: |
| 144 | + """ |
| 145 | + Load the `Kuzushiji-MNIST <https://github.com/rois-codh/kmnist>`_ datasets. |
| 146 | +
|
| 147 | + Parameters |
| 148 | + ---------- |
| 149 | + path: |
| 150 | + the path were the files are stored or will be downloaded to |
| 151 | + download: |
| 152 | + whether the files should be downloaded to the given path |
| 153 | +
|
| 154 | + Returns |
| 155 | + ------- |
| 156 | + train_dataset, test_dataset: |
| 157 | + The train and test datasets. |
| 158 | +
|
| 159 | + Raises |
| 160 | + ------ |
| 161 | + FileNotFoundError |
| 162 | + if a file of the dataset cannot be found |
| 163 | + """ |
| 164 | + path = Path(path) / _kuzushiji_mnist_folder |
| 165 | + path.mkdir(parents=True, exist_ok=True) |
| 166 | + path_files = os.listdir(path) |
| 167 | + missing_files = [] |
| 168 | + for file_path in _kuzushiji_mnist_files.values(): |
| 169 | + if file_path not in path_files: |
| 170 | + missing_files.append(file_path) |
| 171 | + if len(missing_files) > 0: |
| 172 | + if download: |
| 173 | + _download_mnist_like( |
| 174 | + path, |
| 175 | + {name: f_path for name, f_path in _kuzushiji_mnist_files.items() if f_path in missing_files}, |
| 176 | + _kuzushiji_mnist_links, |
| 177 | + ) |
| 178 | + else: |
| 179 | + raise FileNotFoundError(f"Could not find files {[str(path / file) for file in missing_files]}") |
| 180 | + return _load_mnist_like(path, _kuzushiji_mnist_files, _kuzushiji_mnist_labels) |
| 181 | + |
| 182 | + |
| 183 | +def _load_mnist_like( |
| 184 | + path: str | Path, |
| 185 | + files: dict[str, str], |
| 186 | + labels: dict[int, str], |
| 187 | +) -> tuple[ImageDataset[Column], ImageDataset[Column]]: |
| 188 | + _init_default_device() |
| 189 | + |
| 190 | + path = Path(path) |
| 191 | + test_labels: Column | None = None |
| 192 | + train_labels: Column | None = None |
| 193 | + test_image_list: ImageList | None = None |
| 194 | + train_image_list: ImageList | None = None |
| 195 | + for file_name, file_path in files.items(): |
| 196 | + if "idx1" in file_name: |
| 197 | + with gzip.open(path / file_path, mode="rb") as label_file: |
| 198 | + magic, size = struct.unpack(">II", label_file.read(8)) |
| 199 | + if magic != 2049: |
| 200 | + raise ValueError(f"Magic number mismatch. Actual {magic} != Expected 2049.") # pragma: no cover |
| 201 | + if "train" in file_name: |
| 202 | + train_labels = Column( |
| 203 | + file_name, |
| 204 | + [labels[label_index] for label_index in array("B", label_file.read())], |
| 205 | + ) |
| 206 | + else: |
| 207 | + test_labels = Column( |
| 208 | + file_name, |
| 209 | + [labels[label_index] for label_index in array("B", label_file.read())], |
| 210 | + ) |
| 211 | + else: |
| 212 | + with gzip.open(path / file_path, mode="rb") as image_file: |
| 213 | + magic, size, rows, cols = struct.unpack(">IIII", image_file.read(16)) |
| 214 | + if magic != 2051: |
| 215 | + raise ValueError(f"Magic number mismatch. Actual {magic} != Expected 2051.") # pragma: no cover |
| 216 | + image_data = array("B", image_file.read()) |
| 217 | + image_tensor = torch.empty(size, 1, rows, cols, dtype=torch.uint8) |
| 218 | + for i in range(size): |
| 219 | + image_tensor[i, 0] = torch.frombuffer( |
| 220 | + image_data[i * rows * cols : (i + 1) * rows * cols], |
| 221 | + dtype=torch.uint8, |
| 222 | + ).reshape(rows, cols) |
| 223 | + image_list = _SingleSizeImageList() |
| 224 | + image_list._tensor = image_tensor |
| 225 | + image_list._tensor_positions_to_indices = list(range(size)) |
| 226 | + image_list._indices_to_tensor_positions = image_list._calc_new_indices_to_tensor_positions() |
| 227 | + if "train" in file_name: |
| 228 | + train_image_list = image_list |
| 229 | + else: |
| 230 | + test_image_list = image_list |
| 231 | + if train_image_list is None or test_image_list is None or train_labels is None or test_labels is None: |
| 232 | + raise ValueError # pragma: no cover |
| 233 | + return ImageDataset[Column](train_image_list, train_labels, 32, shuffle=True), ImageDataset[Column]( |
| 234 | + test_image_list, |
| 235 | + test_labels, |
| 236 | + 32, |
| 237 | + ) |
| 238 | + |
| 239 | + |
| 240 | +def _download_mnist_like(path: str | Path, files: dict[str, str], links: list[str]) -> None: |
| 241 | + path = Path(path) |
| 242 | + for file_name, file_path in files.items(): |
| 243 | + for link in links: |
| 244 | + try: |
| 245 | + print(f"Trying to download file {file_name} via {link + file_path}") # noqa: T201 |
| 246 | + urllib.request.urlretrieve(link + file_path, path / file_path, reporthook=_report_download_progress) |
| 247 | + print() # noqa: T201 |
| 248 | + break |
| 249 | + except HTTPError as e: |
| 250 | + print(f"An error occurred while downloading: {e}") # noqa: T201 # pragma: no cover |
| 251 | + |
| 252 | + |
| 253 | +def _report_download_progress(current_packages: int, package_size: int, file_size: int) -> None: |
| 254 | + percentage = min(((current_packages * package_size) / file_size) * 100, 100) |
| 255 | + sys.stdout.write(f"\rDownloading... {percentage:.1f}%") |
| 256 | + sys.stdout.flush() |
0 commit comments