nerva_torch.shapes_dataset
A synthetic image classification dataset that is generated on the fly.
Each example is a 28x28 grayscale image that contains one of ten shapes: a disk, a ring, a filled square, a square outline, a triangle, a cross, a plus, horizontal bars, vertical bars or an ellipse. The position, size, stroke width, rotation and brightness of the shapes are random, and noise is added to the pixels. The images are flattened to vectors of 784 elements with values in [0, 1], and the targets are class indices in the range 0-9. This is the same format as the flattened MNIST dataset, so models for MNIST can be used without changes.
The dataset needs nothing but numpy, and it is deterministic for a given seed. Generating 60000 images takes a few seconds. The task is easy enough for a small MLP, but not trivial: a linear model reaches about 40% test accuracy, while an MLP with two hidden layers reaches about 85% after 10 epochs, with a clear gap between training and test accuracy.
The dataset can also be saved to a .npz file, for example for the mlp command line tool:
python -m nerva_torch.shapes_dataset –output shapes.npz
Functions
|
Generate a dataset of random shapes, and return a data loader for the training and the test set. |
|
|
|
Generate n images of random shapes. |
|
Generate a training and a test set of random shapes. |
- nerva_torch.shapes_dataset.make_shapes(n: int, size: int = 28, noise: float = 0.25, seed: int | None = None) Tuple[numpy.ndarray, numpy.ndarray][source]
Generate n images of random shapes.
Returns a pair (X, T), with X a float32 array of shape (n, size * size) with values in [0, 1], and T an int64 array of shape (n,) with class indices into SHAPE_CLASSES.
- nerva_torch.shapes_dataset.make_shapes_dataset(num_train: int = 50000, num_test: int = 10000, size: int = 28, noise: float = 0.25, seed: int = 0) Dict[str, numpy.ndarray][source]
Generate a training and a test set of random shapes.
Returns a dictionary with the numpy arrays Xtrain, Ttrain, Xtest and Ttest, in the same format as the .npz files that are used by create_npz_dataloaders.