/
network.py
159 lines (119 loc) · 4.64 KB
/
network.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
154
155
156
157
158
159
import numpy as np
import imageio
import scipy.io as scyio
import keras
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Dropout
from keras.layers import Activation
from keras.layers import Flatten
from keras.layers import Dense
from keras.optimizers import SGD
from keras.layers import BatchNormalization
from src.program import Program
from src.settings import Settings
import keras.models
path_to_dataset = '/home/piki/Desktop/faks/Computer_vision/KinFaceW-I_rezultati/KinFaceW-I' # PODESITI ${PATH} na početku!
meta_names = ['fd_pairs.mat', 'fs_pairs.mat', 'md_pairs.mat', 'ms_pairs.mat']
dirs = ['father-dau/', 'father-son/', 'mother-dau/', 'mother-son/']
coef1 = 0.8 # [0->0.8] train data
coef2 = 0.9 # [0.8 -> 0.9] validation data and [0.9->1] test data
EPOCH_NUM = 10
def create_model():
model = Sequential()
model.add(Convolution2D(16, (5, 5), input_shape=(64, 64, 6)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(64, (5, 5)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(256, (5, 5)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(640))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(2))
model.add(Activation('softmax'))
sgd = SGD(lr=0.0001, momentum=0.9, decay=0.005)
model.compile(optimizer='sgd', loss="categorical_crossentropy", metrics=['accuracy'])
print(model.summary())
return model
#with keras.device('/GPU:0'):
# ----Read metadata from .mat files-------
meta_data = []
for name in meta_names:
mat = scyio.loadmat(path_to_dataset + '/meta_data/' + name)['pairs']
meta_data.append(mat)
# ----Read dataset for network--------
all_images = []
images_path = []
results = []
for category in range(len(meta_data)):
directory = '/images/' + dirs[category]
for elements in meta_data[category]:
parent_image = path_to_dataset + directory + elements[2][0]
child_image = path_to_dataset + directory + elements[3][0]
images_path.append([parent_image, child_image])
parent_image = imageio.imread(parent_image)
child_image = imageio.imread(child_image)
image = np.concatenate((parent_image, child_image), axis=2)
all_images.append(image)
results.append([elements[0][0][0], elements[1][0][0]])
results = np.array(results)
all_images = np.array(all_images)
all_images = all_images.astype('float32')
all_images /= 255
# or this way
# all_images -= np.mean(all_images, axis=0)
# all_images /= np.std(all_images, axis=0)
# -----Shuffle data---------------
state = np.random.get_state()
np.random.shuffle(all_images)
np.random.set_state(state)
np.random.shuffle(results)
np.random.set_state(state)
np.random.shuffle(images_path)
# ----Prepare dataset for network--------
index1 = int(len(all_images) * coef1)
index2 = int(len(all_images) * coef2)
X_train = all_images[0: index1]
X_valid = all_images[index1: index2]
X_test = all_images[index2:]
results = keras.utils.to_categorical([elem[1] for elem in results], 2)
Y_train = results[0: index1]
Y_valid = results[index1: index2]
Y_test = results[index2:]
# -----Train network------
model = create_model()
model.fit(X_train, Y_train, batch_size=64, epochs=EPOCH_NUM, validation_data=(X_valid, Y_valid), shuffle=True)
model.save('modelSaved_eph'+str(EPOCH_NUM)+'.dat')
# -------Load model-------
#model = keras.models.load_model('/home/piki/Desktop/faks/Computer_vision/KinFaceW-I_rezultati/src/modelSaved_eph'+str(EPOCH_NUM)+'.dat')
#model = keras.models.load_model('/home/piki/Desktop/faks/Computer_vision/KinFaceW-I_rezultati/src/modelSaved_eph50.dat')
# -----Test data-----
Settings()
from src.settings import n_test_data
n_test_data=int(n_test_data)
correct_cnt = 0
comp_results = []
new_path = images_path[index2: index2 + n_test_data]
for i in range(n_test_data):
prediction = model.predict(np.array([X_test[i], ]))
print(prediction)
prediction = np.around(prediction)
real_result = Y_test[i]
comp_results.append(prediction[0][1])
print(new_path[i])
if prediction[0][0] == real_result[0] and prediction[0][1] == real_result[1]:
correct_cnt += 1
# -------GUI--------
print('Accuracy: ' + str(correct_cnt/n_test_data))
print()
Program(images_path[index2: index2 + n_test_data], [x[1] for x in Y_test[:n_test_data]], comp_results, correct_cnt/n_test_data)