MNISTs provides an easy way to use MNIST and other MNIST-like datasets (FashionMNIST, KMNIST, EMNIST) in your numpy code.
MNISTs replicates the functionality of torchvision.datasets.mnist
without the need to download dozens of dependencies.
MNISTs has only one dependency - numpy
.
Each dataset stores train/test images as numpy arrays of shape (n_samples, img_height, img_width)
and train/test labels as numpy arrays of shape (n_samples,)
.
MNIST example:
>>> from mnists import MNIST
>>> mnist = MNIST()
>>> type(mnist.train_images())
<class 'numpy.ndarray'>
>>> mnist.train_images().dtype
dtype('uint8')
>>> mnist.train_images().min()
0
>>> mnist.train_images().max()
255
>>> mnist.train_images().shape
(60000, 28, 28)
>>> mnist.train_labels().shape
(60000,)
>>> mnist.test_images().shape
(10000, 28, 28)
>>> mnist.test_labels().shape
(10000,)
>>> mnist.classes[:3]
['0 - zero', '1 - one', '2 - two']
FashionMNIST example:
from mnists import FashionMNIST
import matplotlib.pyplot as plt
fmnist = FashionMNIST()
plt.imshow(fmnist.train_images()[0], cmap='gray')
plt.title(fmnist.classes[fmnist.train_labels()[0]])
plt.axis('off')
plt.show()
EMNIST example
from mnists import EMNIST
import matplotlib.pyplot as plt
emnist = EMNIST()
letters = emnist.Letters()
plt.imshow(
letters.train_images()[:256]
.reshape(16, 16, 28, 28)
.swapaxes(1, 2)
.reshape(16 * 28, -1),
cmap='gray')
plt.axis('off')
plt.show()
Install mnists
from PyPi:
pip install mnists
or from source:
pip install -U git+https://github.com/pczarnik/mnists
The only requirements for MNISTs are numpy>=1.22
and python>=3.9
.
If you want to have progress bars while downloading datasets, install with
pip install mnists[tqdm]
The main inspirations for MNISTs were mnist
and torchvision.datasets.mnist
.