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

Image Classifications

This document loads and preprocesses the CIFAR-10 image dataset. It then defines and trains two neural network models - a simple ANN and a CNN - to classify the images. The ANN achieves 49.9% accuracy over 5 epochs, while the CNN achieves 87.58% accuracy. Classification reports for both models on the test set are also presented.

Uploaded by

Anvesh Silagana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Image Classifications

This document loads and preprocesses the CIFAR-10 image dataset. It then defines and trains two neural network models - a simple ANN and a CNN - to classify the images. The ANN achieves 49.9% accuracy over 5 epochs, while the CNN achieves 87.58% accuracy. Classification reports for both models on the test set are also presented.

Uploaded by

Anvesh Silagana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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()

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


170498071/170498071 [==============================] - 4s 0us/step

X_train.shape

(50000, 32, 32, 3)

X_test.shape

(10000, 32, 32, 3)

X_test
[[ 12, 25, 6],
[ 20, 37, 7],
[ 24, 36, 15],
...,
[115, 134, 138],
[149, 168, 177],
[104, 117, 131]],

[[ 12, 25, 11],


[ 15, 29, 6],
[ 34, 40, 24],
...,
[154, 172, 182],
[157, 175, 192],
[116, 129, 151]],

...,

[[100, 129, 81],


[103, 132, 84],
[104, 134, 86],
...,
[ 97, 128, 84],
[ 98, 126, 84],
[ 91, 121, 79]],

[[103, 132, 83],


[104, 131, 83],
[107, 135, 87],
...,
[101, 132, 87],
[ 99, 127, 84],
[ 92, 121, 79]],

[[ 95, 126, 78],


[ 95, 123, 76],
[101, 128, 81],
...,
[ 93, 124, 80],
[ 95, 123, 81],
[ 92, 120, 80]]],

[[[ 73, 78, 75],


[ 98, 103, 113],
[ 99, 106, 114],
...,
[135, 150, 152],
[135, 149, 154],
[203, 215, 223]],

[[ 69, 73, 70],


[ 84, 89, 97],
[ 68, 75, 81],
...,
[ 85, 95, 89],
[ 71, 82, 80],
[120, 133, 135]],

plt.imshow(X_train[1])
<matplotlib.image.AxesImage at 0x7fb5ea7f0c10>

print(y_train[:5])

[[6]
[9]
[9]
[4]
[1]]

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

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

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

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

plot_samples(X_train,y_train,0)

X_train = X_train/255
X_test = X_test/255

ann_model = models.Sequential([
    layers.Flatten(input_shape=(32,32,3)),
    layers.Dense(3000,activation='relu'),
    layers.Dense(1000,activation='relu'),
    layers.Dense(10,activation='sigmoid')
])

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

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

Epoch 1/5
1563/1563 [==============================] - 13s 5ms/step - loss: 1.8142 - accuracy: 0.3542
Epoch 2/5
1563/1563 [==============================] - 7s 5ms/step - loss: 1.6250 - accuracy: 0.4262
Epoch 3/5
1563/1563 [==============================] - 7s 4ms/step - loss: 1.5436 - accuracy: 0.4563
Epoch 4/5
1563/1563 [==============================] - 7s 5ms/step - loss: 1.4811 - accuracy: 0.4796
Epoch 5/5
1563/1563 [==============================] - 7s 4ms/step - loss: 1.4345 - accuracy: 0.4949
<keras.callbacks.History at 0x7fb56c106580>

from sklearn.metrics import confusion_matrix, classification_report
import numpy as np
y_pred = ann_model.predict(X_test)
y_pred_classes = [np.argmax(element) for element in y_pred]
print("Classification Report:", classification_report(y_test,y_pred_classes))

313/313 [==============================] - 1s 2ms/step


Classification Report: precision recall f1-score support

0 0.66 0.41 0.51 1000


1 0.62 0.61 0.61 1000
2 0.35 0.41 0.38 1000
3 0.36 0.29 0.32 1000
4 0.57 0.24 0.34 1000
5 0.41 0.40 0.41 1000
6 0.45 0.68 0.54 1000
7 0.66 0.44 0.53 1000
8 0.48 0.76 0.59 1000
9 0.50 0.61 0.55 1000

accuracy 0.49 10000


macro avg 0.51 0.49 0.48 10000
weighted avg 0.51 0.49 0.48 10000

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

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

cnn_model.compile(optimizer='Adam',
            loss='sparse_categorical_crossentropy',
            metrics=['accuracy'])

cnn_model.fit(X_train,y_train,epochs=5,batch_size=32)

Epoch 1/5
1563/1563 [==============================] - 18s 8ms/step - loss: 1.3959 - accuracy: 0.5071
Epoch 2/5
1563/1563 [==============================] - 12s 7ms/step - loss: 1.0267 - accuracy: 0.6390
Epoch 3/5
1563/1563 [==============================] - 12s 7ms/step - loss: 0.7931 - accuracy: 0.7206
Epoch 4/5
1563/1563 [==============================] - 12s 7ms/step - loss: 0.5650 - accuracy: 0.8042
Epoch 5/5
1563/1563 [==============================] - 12s 8ms/step - loss: 0.3639 - accuracy: 0.8758
<keras.callbacks.History at 0x7fb4eec543a0>

np.argmax(y_pred[0])

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

Classification Report:
precision recall f1-score support

0 0.66 0.41 0.51 1000


1 0.62 0.61 0.61 1000
2 0.35 0.41 0.38 1000
3 0.36 0.29 0.32 1000
4 0.57 0.24 0.34 1000
5 0.41 0.40 0.41 1000
6 0.45 0.68 0.54 1000
7 0.66 0.44 0.53 1000
8 0.48 0.76 0.59 1000
9 0.50 0.61 0.55 1000

accuracy 0.49 10000


macro avg 0.51 0.49 0.48 10000
weighted avg 0.51 0.49 0.48 10000

lt i h (X t i [9])
plt.imshow(X_train[9])

<matplotlib.image.AxesImage at 0x7fb4c82d1340>

Colab paid products - Cancel contracts here

You might also like