0% found this document useful (0 votes)
4 views2 pages

Ex 3

The document outlines a basic image classification project using Python and TensorFlow. It details the steps to load the CIFAR-10 dataset, normalize the data, build and compile an artificial neural network, train the model, evaluate its performance, and visualize predictions. The provided code successfully implements these steps and achieves the classification task.

Uploaded by

amirthamm2083
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)
4 views2 pages

Ex 3

The document outlines a basic image classification project using Python and TensorFlow. It details the steps to load the CIFAR-10 dataset, normalize the data, build and compile an artificial neural network, train the model, evaluate its performance, and visualize predictions. The provided code successfully implements these steps and achieves the classification task.

Uploaded by

amirthamm2083
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/ 2

EX.

NO: BASIC IMAGE CLASSIFICATION


DATE:

AIM:

To write the python code for Basic Image Classification.

ALGORITHM:

STEP 1: Import the necessary libraries.

STEP 2: Load the CIFAR-10 dataset using tf.keras.datasets.cifar10.load_data().

STEP 3: Normalize the image data by scaling pixel values to the range [0, 1].

STEP 4: Flatten the 32x32x3 images into 1D vectors of size 3072.

STEP 5: Build an Artificial Neural Network (ANN) using Sequential() with Dense layers.

STEP 6: Compile the model using the Adam optimizer, sparse categorical crossentropy loss, and
accuracy metric.

STEP 7: Train the model using the training data for 10 epochs with a batch size of 90 and 20%
validation split.

STEP 8: Evaluate the model on the test data using model.evaluate().

STEP 9: Predict the labels for test images using model.predict().

STEP 10: Visualize the first 5 test images with their predicted labels using Matplotlib.

PROGRAM:

import tensorflow as t

from tensorflow.keras import layers, models

import matplotlib.pyplot as plt

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()

x_train, x_test = x_train / 255.0, x_test / 255.0 # Normalize to [0, 1]

x_train = x_train.reshape(-1, 32 * 32 * 3)

CSE/NM/NN&DL / 513322104003/DHIVYA G
x_test = x_test.reshape(-1, 32 * 32 * 3)

model = models.Sequential([

layers.Dense(512, activation='relu', input_shape=(32 * 32 * 3,)),

layers.Dense(256, activation='relu'),

layers.Dense(10, activation='softmax') # 10 classes for CIFAR-10

])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model.fit(x_train, y_train, epochs=10, batch_size=90, validation_split=0.2)

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

print(f'Test accuracy: {test_acc:.4f}')

predictions = model.predict(x_test)

plt.figure(figsize=(10, 5))

for i in range(5):

plt.subplot(1, 5, i + 1)

plt.imshow(x_test[i].reshape(32, 32, 3)) # Reshape back to 32x32x3 for display

plt.title(f'Predicted: {predictions[i].argmax()}')

plt.axis('off')

plt.show()

OUTPUT:

RESULT:

Thus, the python code for Basic Image Classification is executed successfully.

CSE/NM/NN&DL / 513322104003/DHIVYA G

You might also like