0% found this document useful (0 votes)
2 views

Ex_6

The document outlines the implementation of a Convolutional Neural Network (CNN) using TensorFlow and Keras to classify images from the CIFAR-10 dataset. It details the procedure for importing libraries, preprocessing data, building and training the model, and evaluating its accuracy. The CNN achieved a test accuracy of approximately 68% in classifying the images.

Uploaded by

Narenkumar. N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Ex_6

The document outlines the implementation of a Convolutional Neural Network (CNN) using TensorFlow and Keras to classify images from the CIFAR-10 dataset. It details the procedure for importing libraries, preprocessing data, building and training the model, and evaluating its accuracy. The CNN achieved a test accuracy of approximately 68% in classifying the images.

Uploaded by

Narenkumar. N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EXP NO 6 Image Classification using CNN on

DATE CIFAR-10 Dataset

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)

fig, axes = plt.subplots(1, 5, figsize=(10, 2))


for i, ax in enumerate(axes):
ax.imshow(x_train[i])
ax.axis('off')
plt.show()

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)

test_loss, test_acc = model.evaluate(x_test, y_test)


print(f"Test Accuracy: {test_acc:.2f}")
sample_idx = np.random.choice(len(x_test), 5, replace=False)
x_sample, y_sample = x_test[sample_idx], y_test[sample_idx]
predictions = model.predict(x_sample)

fig, axes = plt.subplots(1, 5, figsize=(10, 2))


for i, ax in enumerate(axes):
ax.imshow(x_sample[i])
ax.axis('off')
ax.set_title(f"P: {np.argmax(predictions[i])}", color='green')
plt.show()

OUTPUT :

313/313 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step -


accuracy: 0.6779 - loss: 1.0766
Test Accuracy: 0.68
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 127ms/step
RESULT :
The CNN model successfully classified images from the CIFAR-10
dataset with good accuracy.

You might also like