-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimg_classif
More file actions
79 lines (60 loc) · 2.24 KB
/
img_classif
File metadata and controls
79 lines (60 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#This program is for binary image classification, here two pet animals are used for
#the classification, dogs and cats. This is an example
#of how CNN can be used for this kind of problem.
import numpy as np
import matplotlib.pyplot as plt
import random
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten
# the data set can be found here- https://drive.google.com/drive/u/0/folders/1dZvL1gi5QLwOGrfdn9XEsi4EnXx535bD
X_train = np.loadtxt('input.csv', delimiter = ",")
Y_train = np.loadtxt('labels.csv', delimiter = ",")
X_test = np.loadtxt('input_test.csv', delimiter = ",")
Y_test = np.loadtxt('labels_test.csv', delimiter = ",")
X_train = X_train.reshape(len(X_train),100,100,3)
Y_train = Y_train.reshape(len(Y_train),1)
X_test = X_test.reshape(len(X_test),100,100,3)
Y_test = Y_test.reshape(len(Y_test),1)
X_train = X_train/225.0
X_test = X_test/225.0
print("Shape of X_train : ", X_train.shape)
print("Shape of Y_train : ", Y_train.shape)
print("Shape of X_test : ", X_test.shape)
print("Shape of Y_test : ", Y_test.shape)
X_train[1,:]
idx=random.randint(0,len(X_train))
plt.imshow(X_train[idx,:])
plt.show()
#model
model= Sequential([
Conv2D(32,(3,3), activation = 'relu', input_shape = (100, 100, 3)),
MaxPooling2D((2,2)),
Conv2D(32, (3,3), activation = 'relu'),
MaxPooling2D((2,2)),
Flatten(),
Dense(64, activation = 'relu'),
Dense (1, activation = 'sigmoid')
])
'''model =Sequential()
model.add(Conv2D(32,(3,3), activation = 'relu', input_shape = (100, 100, 3)))
model.add(MaxPooling2D((2,2)))
model.add(Conv2D(32, (3,3), activation = 'relu'))
model.add(MaxPooling2D((2,2)))
model.add(Flatten())
model.add(Dense(64, activation = 'relu'))
model.add(Dense (1, activation = 'sigmoid'))'''
model.compile(loss='binary_crossentropy', optimizer = 'adam', metrics= ['accuracy'])
model.fit(X_train, Y_train, epochs = 5, batch_size =64)
model.evaluate(X_test, Y_test)
#making the predictions
idx2 = random.randint(0,len(Y_test))
plt.imshow(X_test[idx2,:])
plt.show()
y_pred=model.predict(X_test[idx2, :].reshape(1,100,100,3))
print(y_pred)
y_pred = y_pred > 0.5
if(y_pred ==0):
pred = 'dog'
else:
pred = "cat"
print("The model says that it is :", pred)