Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views4 pages

Convolution Neural Network

The document outlines a machine learning project using TensorFlow to classify images from the CIFAR-10 dataset. It includes data loading, preprocessing, and the implementation of both a simple artificial neural network (ANN) and a convolutional neural network (CNN), with training and evaluation results. The CNN achieved an accuracy of approximately 69.54% on the test set after 10 epochs.

Uploaded by

Sushma SJ
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)
12 views4 pages

Convolution Neural Network

The document outlines a machine learning project using TensorFlow to classify images from the CIFAR-10 dataset. It includes data loading, preprocessing, and the implementation of both a simple artificial neural network (ANN) and a convolutional neural network (CNN), with training and evaluation results. The CNN achieved an accuracy of approximately 69.54% on the test set after 10 epochs.

Uploaded by

Sushma SJ
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

2/11/25, 10:41 AM Untitled14.

ipynb - Colab

import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
import numpy as np

(X_train, y_train), (X_test,y_test) = datasets.cifar10.load_data()


X_train.shape

Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz


170498071/170498071 ━━━━━━━━━━━━━━━━━━━━ 3s 0us/step
(50000, 32, 32, 3)

X_test.shape

(10000, 32, 32, 3)

y_train.shape

(50000, 1)

y_train[:5]

array([[6],
[9],
[9],
[4],
[1]], dtype=uint8)

y_train = y_train.reshape(-1,)
y_train[:5]

array([6, 9, 9, 4, 1], dtype=uint8)

y_test = y_test.reshape(-1,)

classes = ["airplane","automobile","bird","cat","deer","dog","frog","horse","ship","truck"]

def plot_sample(X, y, index):


plt.figure(figsize = (15,2))
plt.imshow(X[index])
plt.xlabel(classes[y[index]])

plot_sample(X_train, y_train, 0)

plot_sample(X_train, y_train, 1)

X_train = X_train / 255.0


X_test = X_test / 255.0

https://colab.research.google.com/drive/1hvjoPged41d5yvB7-rbBeKGd7EZcVgRG#scrollTo=szmFO8UqYD4Q&printMode=true 1/4
2/11/25, 10:41 AM Untitled14.ipynb - Colab
ann = models.Sequential([
layers.Flatten(input_shape=(32,32,3)),
layers.Dense(3000, activation='relu'),
layers.Dense(1000, activation='relu'),
layers.Dense(10, activation='softmax')
])

ann.compile(optimizer='SGD',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

ann.fit(X_train, y_train, epochs=5)

/usr/local/lib/python3.11/dist-packages/keras/src/layers/reshaping/flatten.py:37: UserWarning: Do not pass an `input_shape`/`input_d


super().__init__(**kwargs)
Epoch 1/5
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 136s 87ms/step - accuracy: 0.3064 - loss: 1.9256
Epoch 2/5
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 129s 83ms/step - accuracy: 0.4190 - loss: 1.6453
Epoch 3/5
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 141s 82ms/step - accuracy: 0.4519 - loss: 1.5599
Epoch 4/5
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 129s 83ms/step - accuracy: 0.4774 - loss: 1.4891
Epoch 5/5
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 132s 85ms/step - accuracy: 0.4941 - loss: 1.4336
<keras.src.callbacks.history.History at 0x793778105590>

from sklearn.metrics import confusion_matrix , classification_report


import numpy as np
y_pred = ann.predict(X_test)
y_pred_classes = [np.argmax(element) for element in y_pred]

print("Classification Report: \n", classification_report(y_test, y_pred_classes))

313/313 ━━━━━━━━━━━━━━━━━━━━ 8s 24ms/step


Classification Report:
precision recall f1-score support

0 0.65 0.47 0.55 1000


1 0.59 0.61 0.60 1000
2 0.32 0.45 0.38 1000
3 0.34 0.40 0.37 1000
4 0.58 0.17 0.26 1000
5 0.53 0.26 0.35 1000
6 0.39 0.75 0.52 1000
7 0.71 0.40 0.52 1000
8 0.65 0.62 0.63 1000
9 0.47 0.67 0.55 1000

accuracy 0.48 10000


macro avg 0.52 0.48 0.47 10000
weighted avg 0.52 0.48 0.47 10000

cnn = models.Sequential([
layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=(32, 32, 3)),
layers.MaxPooling2D((2, 2)),

layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),


layers.MaxPooling2D((2, 2)),

layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])

/usr/local/lib/python3.11/dist-packages/keras/src/layers/convolutional/base_conv.py:107: UserWarning: Do not pass an `input_shape`/`


super().__init__(activity_regularizer=activity_regularizer, **kwargs)

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

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

cnn.fit(X_train, y_train, epochs=10)

https://colab.research.google.com/drive/1hvjoPged41d5yvB7-rbBeKGd7EZcVgRG#scrollTo=szmFO8UqYD4Q&printMode=true 2/4
2/11/25, 10:41 AM Untitled14.ipynb - Colab

Epoch 1/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 63s 39ms/step - accuracy: 0.3697 - loss: 1.7332
Epoch 2/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 83s 40ms/step - accuracy: 0.5904 - loss: 1.1666
Epoch 3/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 79s 38ms/step - accuracy: 0.6446 - loss: 1.0153
Epoch 4/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 83s 38ms/step - accuracy: 0.6863 - loss: 0.9019
Epoch 5/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 82s 38ms/step - accuracy: 0.7145 - loss: 0.8222
Epoch 6/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 81s 38ms/step - accuracy: 0.7303 - loss: 0.7792
Epoch 7/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 82s 38ms/step - accuracy: 0.7519 - loss: 0.7229
Epoch 8/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 63s 40ms/step - accuracy: 0.7652 - loss: 0.6781
Epoch 9/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 78s 37ms/step - accuracy: 0.7825 - loss: 0.6378
Epoch 10/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 61s 39ms/step - accuracy: 0.7925 - loss: 0.5942
<keras.src.callbacks.history.History at 0x7937739427d0>

cnn.evaluate(X_test,y_test)

313/313 ━━━━━━━━━━━━━━━━━━━━ 5s 15ms/step - accuracy: 0.6954 - loss: 0.9308


[0.9453693628311157, 0.6901000142097473]

y_pred = cnn.predict(X_test)
y_pred[:5]

313/313 ━━━━━━━━━━━━━━━━━━━━ 4s 12ms/step


array([[7.4865692e-03, 2.7796368e-03, 7.1732290e-03, 7.4293673e-01,
3.7643305e-04, 1.2739043e-02, 5.2001318e-03, 1.6462379e-04,
2.1846449e-01, 2.6790022e-03],
[4.6811588e-04, 2.5241630e-04, 2.0797746e-07, 2.7866752e-08,
2.6515330e-08, 2.4286340e-09, 3.9974406e-09, 4.7505940e-12,
9.9926269e-01, 1.6571721e-05],
[4.2976011e-02, 1.5265481e-01, 1.8517359e-03, 4.9414434e-03,
3.6864341e-04, 9.3378703e-04, 3.7320360e-04, 9.3939138e-04,
7.7043647e-01, 2.4524510e-02],
[9.6218473e-01, 1.1622400e-02, 3.6273047e-03, 2.3498379e-03,
3.5243129e-04, 2.4741837e-05, 1.5613366e-05, 2.5058744e-04,
1.9445583e-02, 1.2685513e-04],
[5.3737513e-06, 1.4722309e-04, 2.5779877e-03, 1.3266653e-02,
4.8716691e-01, 5.5117994e-03, 4.9108568e-01, 1.7947119e-06,
2.3589112e-04, 5.6283750e-07]], dtype=float32)

y_classes = [np.argmax(element) for element in y_pred]


y_classes[:5]

[3, 8, 8, 0, 6]

y_test[:5]

array([3, 8, 8, 0, 6], dtype=uint8)

plot_sample(X_test, y_test,3)

classes[y_classes[3]]

'airplane'

plot_sample(X_test, y_test,5)

https://colab.research.google.com/drive/1hvjoPged41d5yvB7-rbBeKGd7EZcVgRG#scrollTo=szmFO8UqYD4Q&printMode=true 3/4
2/11/25, 10:41 AM Untitled14.ipynb - Colab

classes[y_classes[5]]

'frog'

Start coding or generate with AI.

https://colab.research.google.com/drive/1hvjoPged41d5yvB7-rbBeKGd7EZcVgRG#scrollTo=szmFO8UqYD4Q&printMode=true 4/4

You might also like