Skip to content

load_kmnist

Load the Kuzushiji-MNIST <https://github.com/rois-codh/kmnist>_ datasets.

Parameters:

Name Type Description Default
path str | Path

the path were the files are stored or will be downloaded to

required
download bool

whether the files should be downloaded to the given path

True

Returns:

Type Description
train_dataset, test_dataset:

The train and test datasets.

Raises:

Type Description
FileNotFoundError

if a file of the dataset cannot be found

Source code in src/safeds_datasets/image/_mnist/_mnist.py
def load_kmnist(path: str | Path, download: bool = True) -> tuple[ImageDataset[Column], ImageDataset[Column]]:
    """
    Load the `Kuzushiji-MNIST <https://github.com/rois-codh/kmnist>`_ datasets.

    Parameters
    ----------
    path:
        the path were the files are stored or will be downloaded to
    download:
        whether the files should be downloaded to the given path

    Returns
    -------
    train_dataset, test_dataset:
        The train and test datasets.

    Raises
    ------
    FileNotFoundError
        if a file of the dataset cannot be found
    """
    path = Path(path) / _kuzushiji_mnist_folder
    path.mkdir(parents=True, exist_ok=True)
    path_files = os.listdir(path)
    missing_files = []
    for file_path in _kuzushiji_mnist_files.values():
        if file_path not in path_files:
            missing_files.append(file_path)
    if len(missing_files) > 0:
        if download:
            _download_mnist_like(
                path,
                {name: f_path for name, f_path in _kuzushiji_mnist_files.items() if f_path in missing_files},
                _kuzushiji_mnist_links,
            )
        else:
            raise FileNotFoundError(f"Could not find files {[str(path / file) for file in missing_files]}")
    return _load_mnist_like(path, _kuzushiji_mnist_files, _kuzushiji_mnist_labels)