-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder-decoder_evaluation_extended_data_2pyramids.py
240 lines (185 loc) · 7.36 KB
/
encoder-decoder_evaluation_extended_data_2pyramids.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
"""
Author: Tomasz Hachaj, 2021
Department of Signal Processing and Pattern Recognition
Institute of Computer Science in Pedagogical University of Krakow, Poland
https://sppr.up.krakow.pl/hachaj/
Data source:
https://drive.google.com/file/d/13VIyqFNzQ6zIGmWll9tEHjOdXp5R2GZt/view
https://drive.google.com/file/d/1U8bwYA8PgNuNYQnv5TNtR2az3AleyrEZ/view
https://drive.google.com/file/d/1h5udf2tB64q6-N3lEh0vDhvfIyDOD43N/view
"""
import numpy as np
from keras.applications.vgg16 import VGG16
from keras.applications.nasnet import preprocess_input
import tensorflow as tf
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import Conv2DTranspose
from tensorflow.keras.layers import LeakyReLU
from tensorflow.keras.layers import Activation
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Concatenate
import statistics
import glob
import os
#Run this code 14-teen times changing stimulus_id from 0 to 13
stimulus_id = 13
stimulus = ['ALG_1_v1_Page_1.jpg', 'ALG_1_v2_Page_1.jpg', 'ALG_2_v1_Page_1.jpg', 'ALG_2_v2_Page_1.jpg', 'BIO_Page_1.jpg',
'FIZ_WB1_Page_1.jpg', 'FIZ_WB2.jpg', 'FIZ_WB3_v1_Page_1.jpg', 'FIZ_WB3_v2_Page_1.jpg', 'FIZ_WB4_stereo_Page_1.jpg',
'FIZ_WZORY_Page_1.jpg', 'rz 1_Page_1.jpg', 'rz 2_Page_1.jpg', 'rz 3_Page_1.jpg']
def make_dir_with_check(my_path):
try:
os.mkdir(my_path)
except OSError:
print(my_path + ' exists')
else:
print(my_path + ' created')
make_dir_with_check('data')
make_dir_with_check('res_2pyramids')
make_dir_with_check('checkpoint_2pyramids')
for stim_name in stimulus:
make_dir_with_check('data/' + stim_name)
make_dir_with_check('res_2pyramids/' + stim_name)
make_dir_with_check('checkpoint_2pyramids/' + stim_name)
def enable_tensorflow():
#Enables tensorflow on GPU
physical_devices = tf.config.experimental.list_physical_devices('GPU')
for physical_device in physical_devices:
tf.config.experimental.set_memory_growth(physical_device, True)
return physical_devices
physical_devices = enable_tensorflow()
tf.compat.v1.disable_eager_execution()
my_model = VGG16(weights='imagenet', include_top=False)
for layer in my_model.layers[:]:
layer.trainable = False
X = np.load('data/students.np.npy')
Y = np.load('data/students_map_gray.np.npy')
import csv
stimulus_X = []
with open('data/students_stimulus.txt', newline='') as f:
reader = csv.reader(f)
stimulus_X = list(reader)
string_to_find = stimulus[stimulus_id]
print(my_model.summary())
extractor5 = Model(inputs=my_model.inputs,
outputs=my_model.get_layer("block5_pool").output)
extractor4 = Model(inputs=my_model.inputs,
outputs=my_model.get_layer("block4_pool").output)
extractor3 = Model(inputs=my_model.inputs,
outputs=my_model.get_layer("block3_pool").output)
xx = np.expand_dims(X[0], axis=0)
xx = preprocess_input(xx)
# build the encoder models
encoder5 = Model(extractor5.input, extractor5.output, name="encoder")
encoder4 = Model(extractor4.input, extractor4.output, name="encoder")
encoder3 = Model(extractor3.input, extractor3.output, name="encoder")
def decoder5():
filters = (64, 128, 256, 512, 512)
chanDim = -1
depth_out = 1
dec = encoder5.predict(xx)
print('decoder5')
print(dec.shape)
x = my_model.get_layer("block5_pool").output
for f in filters[::-1]:
# apply a CONV_TRANSPOSE => RELU => BN operation
x = Conv2DTranspose(f, (3, 3), strides=2,
padding="same")(x)
x = LeakyReLU(alpha=0.2)(x)
x = BatchNormalization(axis=chanDim)(x)
x = Conv2DTranspose(depth_out, (3, 3), padding="same", name='decoder51_out')(x)
outputs = Activation("sigmoid", name='decoder5_out')(x)
decoder = Model(my_model.inputs, outputs, name="decoder")
return decoder
def decoder4():
filters = (64, 128, 256, 512)
chanDim = -1
depth_out = 1
dec = encoder4.predict(xx)
print('decoder4')
print(dec.shape)
x = my_model.get_layer("block4_pool").output
for f in filters[::-1]:
# apply a CONV_TRANSPOSE => RELU => BN operation
x = Conv2DTranspose(f, (3, 3), strides=2,
padding="same")(x)
x = LeakyReLU(alpha=0.2)(x)
x = BatchNormalization(axis=chanDim)(x)
x = Conv2DTranspose(depth_out, (3, 3), padding="same", name='decoder41_out')(x)
outputs = Activation("sigmoid", name='decoder4_out')(x)
decoder = Model(my_model.inputs, outputs, name="decoder")
return decoder
def decoder3():
filters = (64, 128, 256)
chanDim = -1
depth_out = 1
dec = encoder3.predict(xx)
print('decoder3')
print(dec.shape)
x = my_model.get_layer("block3_pool").output
for f in filters[::-1]:
# apply a CONV_TRANSPOSE => RELU => BN operation
x = Conv2DTranspose(f, (3, 3), strides=2,
padding="same")(x)
x = LeakyReLU(alpha=0.2)(x)
x = BatchNormalization(axis=chanDim)(x)
x = Conv2DTranspose(depth_out, (3, 3), padding="same", name='decoder31_out')(x)
outputs = Activation("sigmoid", name='decoder3_out')(x)
decoder = Model(my_model.inputs, outputs, name="decoder")
return decoder
def decoder45():
attention = Concatenate()([d4.output, d5.output])
x = Conv2D(1, (3, 3), strides=1, padding="same")(attention)
x = LeakyReLU(alpha=0.2)(x)
x = BatchNormalization(axis=-1)(x)
decoder = Model(my_model.inputs, x, name="decoder")
return decoder
d5 = decoder5()
d4 = decoder4()
d3 = decoder3()
d345 = decoder45()
print(d345.summary())
#exit()
vv = d345.predict(xx)
print(vv.shape)
strings = sum(stimulus_X, [])
substring = stimulus[stimulus_id]
indices_train = [i for i, s in enumerate(strings) if substring not in s]
indices_test = [i for i, s in enumerate(strings) if substring in s]
print(len(indices_train))
print(len(indices_test))
print(len(strings))
X_train = X[indices_train, :, :, :]
X_test = X[indices_test, :, :, :]
Y_train = Y[indices_train, :, :, np.newaxis]
Y_test = Y[indices_test, :, :, np.newaxis]
X_train = X_train.astype("float32") / 255.0
Y_train = Y_train.astype("float32")
X_test = X_test.astype("float32") / 255.0
Y_test = Y_test.astype("float32")
path_to_checkpoints = 'checkpoint_2pyramids/' + stimulus[stimulus_id] + '/'
print(path_to_checkpoints)
list_of_files = glob.glob(path_to_checkpoints + '*.hdf5') # * means all if need specific format then *.csv
latest_file = max(list_of_files, key=os.path.getctime)
d345.load_weights(latest_file)
print("[INFO] making predictions...")
decoded = d345.predict(X_test)
def Average(lst):
return sum(lst) / len(lst)
import cv2
cc_l = []
for i in range(0, Y_test.shape[0]):
# grab the original image and reconstructed image
print(i)
original = (Y_test[i] * 255.0).astype("uint8")
recon = (decoded[i] * 255).astype("uint8")
if np.sum(original) > 0:
cc_l.append(np.corrcoef(original.flatten(), recon.flatten())[0,1])
for i in range(0, Y_test.shape[0]):
original = (Y_test[i] * 255.0).astype("uint8")
recon = (decoded[i] * 255).astype("uint8")
if np.sum(original) > 0:
cv2.imwrite('res_2pyramids/' + stimulus[stimulus_id] + "/original" + str(i) + ".png", original)
cv2.imwrite('res_2pyramids/' + stimulus[stimulus_id] + "/recon" + str(i) + ".png", recon)
print(Average(cc_l))
print(statistics.stdev(cc_l))