Ex_6
Ex_6
AIM :
To implement a Convolutional Neural Network (CNN) using TensorFlow
and Keras for classifying images from the CIFAR-10 dataset.
PROCEDURE :
1.Import necessary libraries (tensorflow, numpy, matplotlib).
2.Load and preprocess the CIFAR-10 dataset (normalize pixel values,
apply one-hot encoding).
3.Display sample images from the training set.
4.Build a CNN model with two convolutional layers, max pooling, and
fully connected layers.
5.Compile and train the model using Adam optimizer and categorical
cross-entropy loss.
6.Evaluate the model on test data and display accuracy.
7.Predict and visualize results for five random test images.
PROGRAM:
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten,
Dense
from tensorflow.keras.utils import to_categorical
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
y_train, y_test = to_categorical(y_train, 10), to_categorical(y_test, 10)
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Flatten(),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=15, validation_split=0.2, verbose=0)
OUTPUT :