Skip to content

load_mnist

Load the MNIST <http://yann.lecun.com/exdb/mnist/>_ 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_mnist(path: str | Path, download: bool = True) -> tuple[ImageDataset[Column], ImageDataset[Column]]:
    """
    Load the `MNIST <http://yann.lecun.com/exdb/mnist/>`_ 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) / _mnist_folder
    path.mkdir(parents=True, exist_ok=True)
    path_files = os.listdir(path)
    missing_files = []
    for file_path in _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 _mnist_files.items() if f_path in missing_files},
                _mnist_links,
            )
        else:
            raise FileNotFoundError(f"Could not find files {[str(path / file) for file in missing_files]}")
    return _load_mnist_like(path, _mnist_files, _mnist_labels)