-
Notifications
You must be signed in to change notification settings - Fork 0
/
train3.py
153 lines (135 loc) · 5.34 KB
/
train3.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""
Process CMU Hand dataset to get cropped hand datasets.
"""
import os
import pickle
import tensorflow as tf
import numpy as np
from simple_model import model
#from iunet import model
#from iunet2 import model
from vgg16 import model
#from vgg19 import model
#from vgg162 import model
#from mobilenetv2 import model
#from mobilenetv2 import model
from matplotlib import pyplot as plt
from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
pickle_in = open("x_train_aug.pickle","rb")
train_images = pickle.load(pickle_in)
pickle_in.close()
pickle_in = open("y_train_aug.pickle","rb")
train_labels = pickle.load(pickle_in)
pickle_in.close()
pickle_in = open("x_test.pickle","rb")
test_images = pickle.load(pickle_in)
pickle_in.close()
pickle_in = open("y_test.pickle","rb")
test_labels = pickle.load(pickle_in)
pickle_in.close()
#train_images = train_images.astype(np.float64)
#test_images = test_images.astype(np.float64)
# plots keypoints on face image
def plot_keypoints(img, points):
# display image
plt.imshow(img, cmap='gray')
#plt.imshow(np.float32(img), cmap='gray')
# plot the keypoints
for i in range(0, 42, 2):
#plt.scatter((points[i] + 0.5)*256, (points[i+1]+0.5)*256, color='red')
plt.scatter(points[i], points[i + 1], color='red')
# cv2.circle(img, (int(points[i]), int(points[i + 1])), 3, (0, 255, 0), thickness=-1) # , lineType=-1)#, shift=0)
plt.show()
id = 150
plot_keypoints(train_images[id], train_labels[id])
# train_images = train_images.reshape(train_images.shape[0], 256, 256, 1)
train_images2 = train_images.reshape(train_images.shape[0], 256, 256, 3)
img_height = train_images2.shape[1]
img_width = train_images2.shape[2]
img_channels = train_images2.shape[3]
input_shape = (img_height, img_width, img_channels)
num_classes = 42
print(input_shape)
def get_model():
#return model(input=input_shape)
return model(input_shape=input_shape, num_classes=num_classes)
#return model(input=input_shape, num_classes=num_classes)
model = get_model()
#optimizer = tf.keras.optimizers.Adam(0.1)
#model.compile(optimizer=optimizer, loss="mean_squared_error", metrics=["accuracy"]) # default lr 0.001,1e-3
model.compile(optimizer="adam", loss="mean_squared_error", metrics=["accuracy"]) # default lr 0.001,1e-3
lr = 1e-3
callbacks = [ModelCheckpoint("test.hdf5", verbose=1, save_best_only=True),
ReduceLROnPlateau(monitor="val_loss", patience=20, factor=0.1, verbose=1, min_lr=1e-6, ),
# sdnt go below min_lr
EarlyStopping(monitor="val_loss", patience=20, verbose=1)]
# history = model.fit(x_train, y_train, batch_size=32, verbose=1, epochs= 500, validation_data=(x_test, y_test), shuffle=False) #callbacks=callbacks)#, class_weight=class_weights )
history = model.fit(train_images, train_labels, batch_size=16, verbose=1, epochs=300, validation_split=0.3,shuffle=False, callbacks=callbacks)
# history = model.fit(x_train, y_train_cat, batch_size=2, verbose=1, epochs= 10, validation_data=(x_test, y_test_cat), shuffle=False)#, class_weight=class_weights )
# shuffle true sshuffles only the training data for every epoch. but may be we need same for checking imporved models.
#model.save("test.hdf5")
# test_images = test_images.reshape(test_images.shape[0], 256, 256, 1)
test_images = test_images.reshape(test_images.shape[0], 256, 256, 3)
_, acc = model.evaluate(test_images, test_labels)
print("Accuracy of test set:", (acc * 100.0), "%")
_, acc = model.evaluate(train_images, train_labels)
print("Accuracy of train set:", (acc * 100.0), "%")
# plot train val acc loss
loss = history.history["loss"]
val_loss = history.history["val_loss"]
epochs = range(1, len(loss) + 1)
plt.plot(epochs, loss, "y", label="loss")
plt.plot(epochs, val_loss, "r", label="val loss")
plt.title("model loss")
plt.xlabel("epoch")
plt.ylabel("loss")
plt.grid()
plt.legend()
plt.savefig("loss1.png")
plt.show()
loss = history.history["loss"]
val_loss = history.history["val_loss"]
epochs = range(1, len(loss) + 1)
# plt.plot(epochs, loss, "y", label="Training loss")
# plt.plot(epochs, val_loss, "r", label="Validation loss")
plt.plot(epochs, loss, color="#1f77b4", label="loss")
plt.plot(epochs, val_loss, color="#ff7f0e", label="val loss")
plt.title("model loss")
plt.xlabel("epoch")
plt.ylabel("loss")
plt.grid()
plt.legend()
plt.savefig("loss.png")
plt.show()
plt.close()
acc = history.history["accuracy"]
val_acc = history.history["val_accuracy"]
# plt.plot(epochs, acc, "y", label="Training Accuracy")
# plt.plot(epochs, val_acc, "r", label="Validation Accuracy")
plt.plot(epochs, acc, color="#1f77b4", label="acc")
plt.plot(epochs, val_acc, color="#ff7f0e", label="val acc")
plt.title("model accuracy")
plt.xlabel("epoch")
plt.ylabel("accuracy")
plt.grid()
plt.legend()
plt.savefig("accuracy.png")
plt.show()
plt.close()
"""
fig = plt.figure(figsize=(15, 15))
# make test images keypoints prediction
points_test = model.predict(test_images)
points_train = model.predict(train_images)
for i in range(16):
ax = fig.add_subplot(4, 4, i + 1, xticks=[], yticks=[])
plot_keypoints(test_images[i], np.squeeze(points_test[i]))
#plot_keypoints(test_images[i], points_test[i])
plt.show()
for i in range(16):
ax = fig.add_subplot(4, 4, i + 1, xticks=[], yticks=[])
plot_keypoints(train_images[i], np.squeeze(points_train[i]))
#plot_keypoints(train_images[i], points_train[i])
plt.show()
"""
a = 1