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

CIFAR-10 CNN Model Training Guide

The document outlines a TensorFlow script for training a Convolutional Neural Network (CNN) on the CIFAR-10 dataset, achieving a test accuracy of 70.61%. It includes steps for data loading, model definition, compilation, training, evaluation, and making predictions on new images. The model predicts the class of a new image, returning 'deer' as the predicted class index 4.

Uploaded by

ramsai
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)
11 views2 pages

CIFAR-10 CNN Model Training Guide

The document outlines a TensorFlow script for training a Convolutional Neural Network (CNN) on the CIFAR-10 dataset, achieving a test accuracy of 70.61%. It includes steps for data loading, model definition, compilation, training, evaluation, and making predictions on new images. The model predicts the class of a new image, returning 'deer' as the predicted class index 4.

Uploaded by

ramsai
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

In [1]:

import tensorflow as tf
from [Link] import datasets, layers, models
import [Link] as plt
import numpy as np
from PIL import Image

# Step 1: Load and preprocess the CIFAR-10 dataset


(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()

# Normalize pixel values to be between 0 and 1


train_images, test_images = train_images / 255.0, test_images / 255.0

# Step 2: Define the CNN model


model = [Link]([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
[Link](),
[Link](64, activation='relu'),
[Link](10) # Output layer with 10 classes
])

# Step 3: Compile the model


[Link](optimizer='adam',
loss=[Link](from_logits=True),
metrics=['accuracy'])

# Step 4: Train the model


history = [Link](train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))

# Step 5: Evaluate the model


test_loss, test_acc = [Link](test_images, test_labels, verbose=2)
print(f"Test accuracy: {test_acc*100:.2f}%")

# Step 6: Make predictions on a new image


def predict_new_image(image_path, model):
img = [Link](image_path)
img = [Link]((32, 32))
img_array = [Link](img) / 255.0
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
predictions = [Link](img_array)
class_index = [Link](predictions[0])
return class_index

new_image_path = '[Link]'
class_index= predict_new_image(new_image_path, model)
l=["airplane","automobile","bird","cat","deer","dog","frog","horse","ship","truck"]
print(l[class_index])
print(f"Predicted class index: {class_index}")

Epoch 1/10
1563/1563 [==============================] - 48s 30ms/step - loss: 1.5246 - accuracy: 0.4450 - val_loss: 1.3093 - val_accuracy: 0.5379
Epoch 2/10
1563/1563 [==============================] - 47s 30ms/step - loss: 1.1922 - accuracy: 0.5789 - val_loss: 1.0812 - val_accuracy: 0.6116
Epoch 3/10
1563/1563 [==============================] - 45s 29ms/step - loss: 1.0344 - accuracy: 0.6366 - val_loss: 1.0275 - val_accuracy: 0.6396
Epoch 4/10
1563/1563 [==============================] - 47s 30ms/step - loss: 0.9392 - accuracy: 0.6707 - val_loss: 0.9740 - val_accuracy: 0.6582
Epoch 5/10
1563/1563 [==============================] - 48s 30ms/step - loss: 0.8719 - accuracy: 0.6952 - val_loss: 0.9012 - val_accuracy: 0.6892
Epoch 6/10
1563/1563 [==============================] - 51s 32ms/step - loss: 0.8102 - accuracy: 0.7138 - val_loss: 0.8784 - val_accuracy: 0.7001
Epoch 7/10
1563/1563 [==============================] - 48s 31ms/step - loss: 0.7664 - accuracy: 0.7299 - val_loss: 0.8704 - val_accuracy: 0.7029
Epoch 8/10
1563/1563 [==============================] - 45s 29ms/step - loss: 0.7188 - accuracy: 0.7483 - val_loss: 0.8548 - val_accuracy: 0.7056
Epoch 9/10
1563/1563 [==============================] - 46s 29ms/step - loss: 0.6822 - accuracy: 0.7596 - val_loss: 0.8431 - val_accuracy: 0.7141
Epoch 10/10
1563/1563 [==============================] - 46s 29ms/step - loss: 0.6434 - accuracy: 0.7740 - val_loss: 0.8731 - val_accuracy: 0.7061
313/313 - 3s - loss: 0.8731 - accuracy: 0.7061 - 3s/epoch - 9ms/step
Test accuracy: 70.61%
1/1 [==============================] - 0s 203ms/step
deer
Predicted class index: 4

In [ ]:

You might also like