-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
88 lines (68 loc) · 2.44 KB
/
app.py
File metadata and controls
88 lines (68 loc) · 2.44 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
80
81
82
83
84
85
86
87
88
import random
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten
labels = {
0: "DOG",
1: "CAT"
}
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 / 255.0
X_test = X_test / 255.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)
print(X_train[1,:])
# randomly show an image
idx = random.randint(0, len(X_train))
# plt.imshow(X_train[idx, :])
# plt.show()
# model = Sequential([
# Conv2D(32, (3, 3), activation='relu', input_shape=(100, 100, 3)),
# MaxPooling2D(pool_size=(2, 2)),
# Conv2D(64, (3, 3), activation='relu'),
# MaxPooling2D(pool_size=(2, 2)),
# Flatten(),
# Dense(128, activation='relu'),
# Dense(1, activation='sigmoid')
# ])
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(100, 100, 3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
# model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
# model.add(Dense(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# opt = keras.optimizers.SGD(learning_rate=0.001)
model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
# model.fit(X_train, Y_train, epochs = 10, batch_size = 32)
model.fit(X_train, Y_train, epochs = 5, batch_size = 64)
model.evaluate(X_test, Y_test)
# Making predictions
for i in range(0, 5):
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
# print(y_pred)
if y_pred == False:
pred = 'DOG'
else:
pred = 'CAT'
print("Our model predicts that the image is a " + pred)
print("The actual label is " + labels[int(Y_test[idx2])])
print("\n")