- MNIST Handwritten Digits: Classifying the famous MNIST dataset, which contains images of handwritten digits (0–9). This is a beginner-friendly example that introduces how to use convolutional neural networks (CNNs) for image classification.
- CIFAR-10 Image Classification: Building a CNN to classify the CIFAR-10 dataset, a set of 60,000 32×32 color images in 10 classes.
pythonCopy codefrom tensorflow.keras import layers, models
from tensorflow.keras.datasets import mnist
# Load MNIST dataset
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
# Normalize pixel values
train_images, test_images = train_images / 255.0, test_images / 255.0
# Build CNN model
model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
# Compile and train the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5, validation_data=(test_images, test_labels))
Leave a Reply