9/11/23, 2:56 AM Image Classification
IMPORTS
In [81]:
import numpy as np
import random
import [Link] as plt
from [Link] import Sequential
from [Link] import Conv2D, MaxPooling2D, Dense, Flatten
Loading the DataSet
In [82]:
xtrain = [Link]('[Link]', delimiter = ',')
ytrain = [Link]('[Link]', delimiter = ',')
xtest = [Link]('input_test.csv', delimiter = ',')
ytest = [Link]('labels_test.csv', delimiter = ',')
In [83]:
xtrain = [Link](len(xtrain), 100, 100, 3)#reshaping to standard form
ytrain = [Link](len(ytrain), 1)
xtest = [Link](len(xtest), 100, 100, 3)
ytest = [Link](len(ytest), 1)
xtrain = xtrain/255.0 #rescalling to train the model properly
xtest = xtest/255.0
In [84]:
print("Shape of xtrain: ", [Link])
print("Shape of ytrain: ", [Link])
print("Shape of xtest: ", [Link])
print("Shape of ytest: ", [Link])
Shape of xtrain: (2000, 100, 100, 3)
Shape of ytrain: (2000, 1)
Shape of xtest: (400, 100, 100, 3)
Shape of ytest: (400, 1)
In [85]:
idx = [Link](0, len(xtrain))
[Link](xtrain[idx, :])
[Link]()
localhost:8888/nbconvert/html/Image [Link]?download=false 1/3
9/11/23, 2:56 AM Image Classification
Model
In [86]:
model = Sequential([
Conv2D(32, (3,3), activation = 'relu', input_shape = (100, 100, 3)),
MaxPooling2D((2,2)), #1st Layer of CNN
Conv2D(32, (3,3), activation = 'relu'),
MaxPooling2D((2,2)), #2nd Layer of CNN
Flatten(),
Dense(64, activation = 'relu'), #FCL
Dense(1, activation = 'sigmoid') #Final layer,& func in Binary<sigmoid>
])
In [87]:
#Another way of creating Sequential Model
#model = Sequential()
#[Link](Conv2D(32, (3,3), activation = 'relu', input_shape = (100, 100, 3)))
#[Link](MaxPooling2D((2,2)))
#[Link](Conv2D(32, (3,3), activation = 'relu'))
#[Link](MaxPooling2D((2,2)))
#[Link](Flatten())
#[Link](Dense(64, activation = 'relu'))
#[Link](Dense(1, activation = 'sigmoid'))
Training The Model
In [19]:
[Link](loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy
In [43]:
[Link](xtrain, ytrain, epochs = 5, batch_size = 64) #traing func
Epoch 1/5
32/32 [==============================] - 13s 399ms/step - loss: 0.1610 - accuracy:
0.9460
Epoch 2/5
32/32 [==============================] - 12s 380ms/step - loss: 0.1257 - accuracy:
0.9670
Epoch 3/5
localhost:8888/nbconvert/html/Image [Link]?download=false 2/3
9/11/23, 2:56 AM Image Classification
32/32 [==============================] - 14s 422ms/step - loss: 0.0813 - accuracy:
0.9810
Epoch 4/5
32/32 [==============================] - 14s 425ms/step - loss: 0.0555 - accuracy:
0.9890
Epoch 5/5
32/32 [==============================] - 14s 435ms/step - loss: 0.0405 - accuracy:
0.9960
Out[43]: <[Link] at 0x29e81faaf40>
Evaluation
In [45]:
[Link](xtest, ytest)
13/13 [==============================] - 0s 34ms/step - loss: 1.1696 - accuracy: 0.6
575
Out[45]: [1.1695667505264282, 0.6575000286102295]
Marking the Prediction Values
In [78]:
idx2 = [Link](0, len(ytest))
[Link](xtest[idx2, :])
[Link]()
y_pred = [Link](xtest[idx2, :].reshape(1, 100, 100, 3))
print(y_pred) #probablistic value
[[0.9953381]]
Making Prediction using the values
In [89]:
pred = y_pred > 0.5 #Creating the class of animals
if(pred == 0):
pred = 'dog'
else:
pred = 'cat'
print("Our model says it is a :", pred)
Our model says it is a : cat
localhost:8888/nbconvert/html/Image [Link]?download=false 3/3