Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keras includes ready-to-load MNIST, Fashion-MNIST, CIFAR-10, and CIFAR-100 datasets. Each loader returns training and test arrays, but image shape, color format, and label shape vary. The examples below show how to load them, inspect their contents, display correctly labeled image grids, and prepare normalized copies for a model.

Compare the built-in Keras vision datasets

These small, pre-vectorized NumPy datasets are useful for debugging and straightforward examples. Their image modality and label format affect how you display and preprocess them.

Dataset Training / test images Image shape and format Labels
MNIST 60,000 / 10,000 28 × 28 grayscale 10 digit classes; y has shape (n,)
Fashion-MNIST 60,000 / 10,000 28 × 28 grayscale 10 fashion categories; y has shape (n,)
CIFAR-10 50,000 / 10,000 32 × 32 RGB 10 classes; y has shape (n, 1)
CIFAR-100 50,000 / 10,000 32 × 32 RGB 100 fine or 20 coarse classes; y has shape (n, 1)

Counts, formats, and label shapes are those stated in the current Keras dataset documentation for MNIST, Fashion-MNIST, CIFAR-10, and CIFAR-100.

Load a dataset and check its array shapes

Each loader returns ((x_train, y_train), (x_test, y_test)). Replace mnist with fashion_mnist, cifar10, or cifar100 to select another dataset.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import keras

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
print(x_train.shape, y_train.shape, x_test.shape, y_test.shape)

For MNIST, the image arrays have a height and width dimension but no explicit channel dimension. CIFAR arrays include a final RGB channel dimension. CIFAR label arrays retain a one-element second dimension, unlike MNIST and Fashion-MNIST labels.

Select CIFAR-100 label granularity

CIFAR-100 supports either 100 fine classes or 20 coarse classes. Choose the mode when loading the dataset:

Rank #2
Sale
(x_train, y_train), (x_test, y_test) = keras.datasets.cifar100.load_data(label_mode="fine")
# Or: label_mode="coarse"

Use class names that match the chosen mode when adding labels to a plot; an integer label is not itself a readable category name.

Display a grid with the correct labels

The following example displays the first ten training images in two rows. Set class_names to the names corresponding to the selected dataset and, for CIFAR-100, its fine or coarse mode.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Computer Vision
  • Used Book in Good Condition
import matplotlib.pyplot as plt

class_names = [str(i) for i in range(10)]  # Replace with matching dataset class names
fig, axes = plt.subplots(2, 5, figsize=(10, 4))

for i, ax in enumerate(axes.flat):
    image = x_train[i]
    label = int(y_train[i]) if getattr(y_train[i], "shape", ()) == () else int(y_train[i][0])

    if image.ndim == 2:
        ax.imshow(image, cmap="gray", vmin=0, vmax=255)
    else:
        ax.imshow(image)

    ax.set_title(class_names[label])
    ax.axis("off")

plt.tight_layout()
plt.show()

Grayscale rendering suits MNIST and Fashion-MNIST; CIFAR images are shown as RGB. The label conversion handles both one-dimensional labels and the one-element arrays returned for CIFAR. For a meaningful plot title, replace the example numeric names with the dataset’s actual class names.

Keep CIFAR-10 label noise in mind

Keras’s CIFAR-10 documentation warns that a small percentage of samples are mislabeled. If an image appears inconsistent with its displayed label, that may be label noise rather than a plotting error. Visual inspection and evaluation should account for that possibility.

Prepare image arrays for modeling without losing the originals

For the MNIST example, Keras converts image values to floating point, scales them by 255, and adds a final channel dimension. Keep the original arrays if you want to inspect or display their original pixel values; make separate arrays for model input.

import numpy as np

x_train_model = x_train.astype("float32") / 255
x_test_model = x_test.astype("float32") / 255
x_train_model = np.expand_dims(x_train_model, -1)
x_test_model = np.expand_dims(x_test_model, -1)

This channel-expansion step is appropriate for the two-dimensional MNIST image arrays. CIFAR images already have an RGB channel dimension, so do not add another one. The MNIST preprocessing pattern is shown in the official Keras MNIST example.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Load your own class-folder images

For images outside the four built-in datasets, Keras provides keras.utils.image_dataset_from_directory. It infers labels from subdirectories and returns a tf.data.Dataset. The Keras image-loading guide also documents load_img, img_to_array, save_img, and array_to_img for working with individual images.

See the Keras image-loading utilities documentation for the directory loader and image conversion functions.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.