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

DL EXP2.ipynb - Colaboratory

xyz

Uploaded by

Nivetha Nivetha
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 views6 pages

DL EXP2.ipynb - Colaboratory

xyz

Uploaded by

Nivetha Nivetha
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/ 6

9/4/23, 10:36 PM DL EXP2.

ipynb - Colaboratory

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation, Flatten, Conv2D, MaxPooling2D
from sklearn.metrics import accuracy_score, precision_score,mean_squared_error
mnist=tf.keras.datasets.mnist
(x_train,y_train),(x_test,y_test)=mnist.load_data()
x_train.shape

(60000, 28, 28)

import matplotlib.pyplot as plt


plt.imshow(x_train[0])
plt.show()
plt.imshow(x_train[0], cmap = plt.cm.binary)

<matplotlib.image.AxesImage at 0x7e78294d4b80>

print(x_train[0])

[[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 3 18 18 18 126 136
175 26 166 255 247 127 0 0 0 0]
[ 0 0 0 0 0 0 0 0 30 36 94 154 170 253 253 253 253 253
225 172 253 242 195 64 0 0 0 0]
[ 0 0 0 0 0 0 0 49 238 253 253 253 253 253 253 253 253 251
93 82 82 56 39 0 0 0 0 0]
[ 0 0 0 0 0 0 0 18 219 253 253 253 253 253 198 182 247 241

https://colab.research.google.com/drive/1owijo9G1_4Ea2ZaS6Fd8EBkYCm3e89vO#scrollTo=F6uZUHsB8pCJ 1/6
9/4/23, 10:36 PM DL EXP2.ipynb - Colaboratory
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 80 156 107 253 253 205 11 0 43 154
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 14 1 154 253 90 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 139 253 190 2 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 11 190 253 70 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 35 241 225 160 108 1
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 81 240 253 253 119
25 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 45 186 253 253
150 27 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 93 252
253 187 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 249
253 249 64 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 46 130 183 253
253 207 2 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 39 148 229 253 253 253
250 182 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 24 114 221 253 253 253 253 201
78 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 23 66 213 253 253 253 253 198 81 2
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 18 171 219 253 253 253 253 195 80 9 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 55 172 226 253 253 253 253 244 133 11 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 136 253 253 253 212 135 132 16 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]]

x_train=tf.keras.utils.normalize(x_train, axis=1)
x_test=tf.keras.utils.normalize(x_test, axis=1)
plt.imshow(x_train[0],cmap = plt.cm.binary)

<matplotlib.image.AxesImage at 0x7e7827355d20>

print(x_train[0])

https://colab.research.google.com/drive/1owijo9G1_4Ea2ZaS6Fd8EBkYCm3e89vO#scrollTo=F6uZUHsB8pCJ 2/6
9/4/23, 10:36 PM DL EXP2.ipynb - Colaboratory
0.40899334 0.39653769 0. 0. 0. 0.
0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0.04117838 0.16813739
0.28960162 0.32790981 0.36833534 0.3689874 0.34978968 0.25961929
0.12760592 0. 0. 0. 0. 0.
0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0.
0. 0. 0.04431706 0.11961607 0.36545809 0.37314701
0.33153488 0.32790981 0.36833534 0.28877275 0.111988 0.00258328
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0.
0.05298497 0.42752138 0.4219755 0.45852825 0.43408872 0.37314701
0.33153488 0.25273681 0.11646967 0.01312603 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. ]
[0. 0. 0. 0. 0.37491383 0.56222061
0.66525569 0.63253163 0.48748768 0.45852825 0.43408872 0.359873
0.17428513 0.01425695 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. ]
[0. 0. 0. 0. 0.92705966 0.82698729
0.74473314 0.63253163 0.4084877 0.24466922 0.22648107 0.02359823
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. ]]

print(y_train[0])

img_size=28
x_trainr=np.array(x_train).reshape(-1,img_size,img_size,1)
x_testr=np.array(x_test).reshape(-1,img_size,img_size,1)
print("training sample dimentions",x_trainr.shape)
print("testing sample dimentions",x_testr.shape)

training sample dimentions (60000, 28, 28, 1)


testing sample dimentions (10000, 28, 28, 1)

model= Sequential()

#first convolution layer 0 1 2 3 (60000,28,28,1) 28-3+1=26*26


model.add(Conv2D(64,(3,3), input_shape = x_trainr.shape[1:]))
model.add(Activation("relu")) #actvation function make it non-linear
model.add(MaxPooling2D(pool_size=(2,2)))

#2nd convolution layer


model.add(Conv2D(64,(3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))

#3rd convolution layer


model.add(Conv2D(64,(3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))

#fully connected layer #1


model.add(Flatten())#before using fully connected layer we need to flatten so that it converts from 2D to 1D
model.add(Dense(64))
model.add(Activation("relu"))

#fully connected layer #2


model.add(Dense(32))
model.add(Activation("relu"))

#fully connected layer , output layer must be equal to the number of classes, 10(0-9)
model.add(Dense(10))

https://colab.research.google.com/drive/1owijo9G1_4Ea2ZaS6Fd8EBkYCm3e89vO#scrollTo=F6uZUHsB8pCJ 3/6
9/4/23, 10:36 PM DL EXP2.ipynb - Colaboratory
model.add(Activation("softmax"))
model.summary()

Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 26, 26, 64) 640

activation (Activation) (None, 26, 26, 64) 0

max_pooling2d (MaxPooling2D (None, 13, 13, 64) 0


)

conv2d_1 (Conv2D) (None, 11, 11, 64) 36928

activation_1 (Activation) (None, 11, 11, 64) 0

max_pooling2d_1 (MaxPooling (None, 5, 5, 64) 0


2D)

conv2d_2 (Conv2D) (None, 3, 3, 64) 36928

activation_2 (Activation) (None, 3, 3, 64) 0

max_pooling2d_2 (MaxPooling (None, 1, 1, 64) 0


2D)

flatten (Flatten) (None, 64) 0

dense (Dense) (None, 64) 4160

activation_3 (Activation) (None, 64) 0

dense_1 (Dense) (None, 32) 2080

activation_4 (Activation) (None, 32) 0

dense_2 (Dense) (None, 10) 330

activation_5 (Activation) (None, 10) 0

=================================================================
Total params: 81,066
Trainable params: 81,066
Non-trainable params: 0
_________________________________________________________________

model.compile(loss="sparse_categorical_crossentropy",optimizer="adam",metrics=['accuracy'])
model.fit(x_trainr,y_train,epochs=5, validation_split=0.3)

Epoch 1/5
1313/1313 [==============================] - 85s 64ms/step - loss: 0.3523 - accuracy: 0.8896 - val_loss: 0.1329 - val_accuracy: 0.9
Epoch 2/5
1313/1313 [==============================] - 81s 62ms/step - loss: 0.1113 - accuracy: 0.9662 - val_loss: 0.0941 - val_accuracy: 0.9
Epoch 3/5
1313/1313 [==============================] - 83s 63ms/step - loss: 0.0804 - accuracy: 0.9755 - val_loss: 0.0811 - val_accuracy: 0.9
Epoch 4/5
1313/1313 [==============================] - 84s 64ms/step - loss: 0.0644 - accuracy: 0.9807 - val_loss: 0.0805 - val_accuracy: 0.9
Epoch 5/5
1313/1313 [==============================] - 82s 62ms/step - loss: 0.0517 - accuracy: 0.9836 - val_loss: 0.0741 - val_accuracy: 0.9
<keras.callbacks.History at 0x7e7822d57b50>

#evaluating the model


test_loss,test_acc= model.evaluate(x_testr,y_test)
print("test loss on 10000 test samples: ",test_loss)
print("validation accuracy on 10000 test samples: ",test_acc)

313/313 [==============================] - 6s 19ms/step - loss: 0.0717 - accuracy: 0.9802


test loss on 10000 test samples: 0.07174694538116455
validation accuracy on 10000 test samples: 0.9801999926567078

prediction= model.predict([x_testr])
print(prediction)

313/313 [==============================] - 5s 16ms/step


[[1.1922811e-09 2.2768957e-06 1.4116866e-07 ... 9.9998862e-01
3.4180331e-07 3.1316872e-06]
[2.3966408e-03 7.1716684e-05 9.9639350e-01 ... 6.4024134e-05
3.5425834e-04 5.1603700e-05]
[7.1960667e-06 9.9991721e-01 2.6356180e-05 ... 2.0419495e-05
6.7836127e-06 3.8043618e-06]
...
[1.0620044e-08 7.3428179e-09 9.4947401e-09 ... 1.3691077e-07
7.0470219e-07 2.1433100e-06]

https://colab.research.google.com/drive/1owijo9G1_4Ea2ZaS6Fd8EBkYCm3e89vO#scrollTo=F6uZUHsB8pCJ 4/6
9/4/23, 10:36 PM DL EXP2.ipynb - Colaboratory
[8.6073496e-06 1.5391150e-06 1.4301283e-08 ... 1.9632823e-06
4.9263407e-03 2.1478088e-06]
[2.2130098e-05 1.8055583e-05 1.2126981e-04 ... 1.2822547e-07
3.4455697e-03 1.0398350e-06]]

print(np.argmax(prediction[0]))

#check the answer


plt.imshow(x_test[0])

<matplotlib.image.AxesImage at 0x7e77e0a6bb20>

import cv2
img = cv2.imread('/content/8.png')
plt.imshow(img)

<matplotlib.image.AxesImage at 0x7e77e2ffbbb0>

img.shape

(256, 197, 3)

gray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img.shape

(256, 197, 3)

resized=cv2.resize(gray,(28,28), interpolation=cv2.INTER_AREA)
resized.shape

(28, 28)

https://colab.research.google.com/drive/1owijo9G1_4Ea2ZaS6Fd8EBkYCm3e89vO#scrollTo=F6uZUHsB8pCJ 5/6
9/4/23, 10:36 PM DL EXP2.ipynb - Colaboratory
newing = tf.keras.utils.normalize(resized, axis=1)
newing=np.array(newing).reshape(-1,img_size,img_size,1)
newing.shape

(1, 28, 28, 1)

prediction=model.predict(newing)
print(np.argmax(prediction))

1/1 [==============================] - 0s 29ms/step


6

check 0s completed at 10:36 PM

https://colab.research.google.com/drive/1owijo9G1_4Ea2ZaS6Fd8EBkYCm3e89vO#scrollTo=F6uZUHsB8pCJ 6/6

You might also like