-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathevaluate.py
More file actions
274 lines (220 loc) · 7.25 KB
/
Copy pathevaluate.py
File metadata and controls
274 lines (220 loc) · 7.25 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import pandas as pd
from keras.models import Model
from keras.layers import Average
from keras import backend as K
from keras.models import model_from_json
import cv2
import numpy as np
import matplotlib.pyplot as plt
from keras.utils.vis_utils import plot_model
from scipy.signal import find_peaks
def root_mean_squared_error(y_true, y_pred):
return K.sqrt(K.mean(K.square(y_pred - y_true), axis=-1))
def tic():
import time
global startTime_for_tictoc
startTime_for_tictoc = time.time()
def toc():
import time
if 'startTime_for_tictoc' in globals():
toc_ = "Elapsed time is " + str(time.time() - startTime_for_tictoc) + " seconds."
print(toc_)
return toc_
else:
toc_ = "Toc: start time not set"
print(toc_)
return toc_
# PARAMETERS
MODEL_SHAPE_TYPE = 'rect'
DATAPATH = './data/test'
# model_name_details = [
# 'cnn_128_300/simple_rl_and_fix_rmse_diffloss_acc_binary_rect_1',
# 'cnn_128_300/simple_rl_and_fix_rmse_rect_1',
# 'cnn_128_300/rmse_diffloss_rmse_rect_1',
# 'cnn_128_300/simple_rl_and_fix_rmse_diffloss_acc_rect_1',
# 'nn_128_300/rmse_rect_1'
# ]
# model_name_details = [
# 'cnn_128_300/rmse_rect_1',
# 'cnn_128_300/rmse_diffloss_rmse_rect_1',
# 'cnn_128_300/rmse_diffloss_bce_rect_1',
# ]
model_name_details = [
'cnn_128_300/rmse_square_1',
'cnn_128_300/rmse_diffloss_rmse_square_1',
'cnn_128_300/rmse_diffloss_binary_ce_square_1',
]
model_name_details = [
'cnn_128_300/rmse_rect_1',
# 'cnn_rmse_diff_ce_128_300/rmse,diff_ce_rect_1',
# 'cnn_rmse_diff_rmse_128_300/rmse,diff_rmse_rect_1',
]
# label_name = [
# 'RMSE',
# 'RMSE + diff_RMSE',
# 'RMSE + diff_BCE'
# ]
#
# model_name_details = [
# 'cnn_rmse32_128_300/rmse_rect_1',
# 'cnn_rmse_diff_rmse32_128_300/rmse,diff_rmse_rect_1',
# 'cnn_rmse_diff_bce32_128_300/rmse,diff_bce_rect_1',
# ]
model_name_details = [
'cnn_4layer16_dropout0.4noBN_128_300/rmse_rect_1',
'cnn_4layer16_dropout0.4_noBN_128_300/rmse,diff_rmse_rect_1',
'cnn_4layer16_dropout0.4_noBN_128_300/rmse,diff_ce_rect_1',
]
model_name_details = [
'cnn_4l16_d0.4_noBN_128_300/rmse_rect_1',
'cnn_4l16_d0.4_noBN_type1_128_300/rmse_rect_1',
'cnn_4l16_d0.4_noBN_type2_128_300/rmse_rect_1',
]
label_name = [
'all',
'type1',
'type2'
]
colors=[
'green', 'skyblue', 'red'
]
if MODEL_SHAPE_TYPE == 'rect':
img_rows, img_cols, channels = 100, 200, 1
else:
img_rows, img_cols, channels = 200, 200, 1
RESULT_PATH = './result/predict'
model_name = './'
x_test = []
y_test = []
# data_folder = 'binary_test_1001'
# data_id = 63409
# data_folder = 'binary_rl_fix_1014'
# data_id = 756 # 852
# data_folder = 'binary_new_test_501'
# data_id = 131 # 282, 168 132
#
# # #
# #
# # data_folder = 'binary_rl_fix_test_1003'
# # data_id = 557 # 374, 137 557
#
# # data_folder = 'binary_rl_fix_test_1002'
# # data_id = 966
#
# data_folder = 'binary_rl_fix_test_1003'
# data_id = 374
# data_folder = 'binary_rl_limit_test_1001'
# data_id = 9
#
# data_folder = 'binary_rl_test_501'
# data_id = 9
# rank 1
# data_folder = 'binary_new_test_1501'
# data_id = 783 # 259 617, 783
# #
# data_folder = 'binary_rl_fix_test_1005'
# data_id = 205 # 259 614 1339
# 1~ 500
# data_folder = 'binary_new_test_501'
# data_id = 125 # 282, 168 132 293 181 121 178 498
# 501 ~ 2000
data_folder = 'binary_new_test_1501'
data_id = 979 # 259 614 1339 1459 886 927 1481 890 751 785 1208
# 2000 ~ 2999
# data_folder = 'binary_rl_fix_1014'
# data_id = 470 # 724, 793 113
# 3000 ~ 3999
# data_folder = 'binary_rl_fix_1015'
# data_id = 496 # 935,206
# 4000 ~ 4999
data_folder = 'binary_rl_fix_test_1002'
data_id = 425 # 870 959
# 5000 ~ 5999
# data_folder = 'binary_rl_fix_test_1003'
# data_id = 808 # 557 808 178 903
# 6000 ~ 6999
# data_folder = 'binary_rl_fix_test_1004'
# data_id = 146 # 792 146
# 7000 ~ 7999
# data_folder = 'binary_rl_fix_test_1005'
# data_id = 959 # 870 959
# 8000 ~
# data_folder = 'binary_test_1101'
# data_id = 245 # 288, 514, 928, 35, 220 329 930 493 167 245 632 517 985
image = cv2.imread('{}/{}/{}.tiff'.format(DATAPATH, data_folder, data_id), 0)
image = np.array(image, dtype=np.uint8)
image //= 255
if MODEL_SHAPE_TYPE == 'rect':
x_test.append(image)
else:
v_flipped_image = np.flip(image, 0)
square_image = np.vstack([image, v_flipped_image])
x_test.append(square_image)
dataframe = pd.read_csv('{}/{}.csv'.format(DATAPATH, data_folder), delim_whitespace=False, header=None)
dataset = dataframe.values
fileNames = dataset[:, 0]
for idx, val in enumerate(fileNames):
if int(idx+1) == int(data_id):
y_test = dataset[idx, 1:25]
break
x_test = np.array(x_test)
y_test = np.array(y_test)
y_test = np.true_divide(y_test, 2767.1)
if K.image_data_format() == 'channels_first':
x_test = x_test.reshape(x_test.shape[0], channels, img_rows, img_cols)
y_test = y_test.reshape(y_test.shape[0], channels, img_rows, img_cols)
input_shape = (channels, img_rows, img_cols)
else:
x_test = x_test.reshape(1, img_rows, img_cols, channels)
input_shape = (img_rows, img_cols, channels)
result = dict()
result['real'] = x_test
x_axis = range(400, 1600, 50)
fig, ax = plt.subplots(1, 1, figsize=(14, 7))
ax.plot(x_axis, y_test, label='real', color='black')
MODEL_JSON_PATH = ''
MODEL_H5_PATH = ''
myeongjo = 'NanumMyeongjo'
for i, model_name_detail in enumerate(model_name_details):
MODEL_JSON_PATH = 'models_paper/{}/{}.json'.format(model_name, model_name_detail)
MODEL_H5_PATH = 'models_paper/{}/{}.h5'.format(model_name, model_name_detail)
print("Loaded model : {}".format(model_name_detail))
# load json and create model
json_file = open(MODEL_JSON_PATH, 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights(MODEL_H5_PATH)
# evaluate loaded model on test data
loaded_model.compile(loss=root_mean_squared_error, optimizer='adam', metrics=['accuracy'])
tic()
if model_name_detail.startswith('cnn'):
y_predict = loaded_model.predict(x_test)
else:
x_test_nn = x_test.reshape(x_test.shape[0], img_rows * img_cols * channels)
y_predict = loaded_model.predict(x_test_nn)
toc()
ax.plot(x_axis, y_predict[0], label = label_name[i], color = colors[i])
# plot model
# plot_model(loaded_model, to_file='plot_model_{}_{}.png'.format(data_folder, data_id), show_shapes=True, show_layer_names=True)
peaks_positive, _ = find_peaks(y_test, height=0)
peaks_negative, _ = find_peaks(1 - y_test, height=0)
mask = np.zeros_like(y_test, np.bool)
mask[peaks_positive] = 1
mask[peaks_negative] = 1
peak_array = mask * y_test
peak_array[peak_array == 0] = np.nan
plt.plot(x_axis, peak_array, "o", markersize=10)
ax.set_title(r'predict simulation', fontsize = 14, fontname = myeongjo)
ax.set_xlabel('wavelength', fontsize = 14, fontname = myeongjo)
ax.set_ylabel('transmittance', fontsize = 14, fontname = myeongjo)
ax.legend(loc = 'upper left', fontsize = 14)
# ax.legend(loc = 'lower center', fontsize = 14)
ax.grid(True)
# ax.set_ylim(0, 10000)
# ax.set_yticks(np.arange(0, 10000 + 1, 2500))
fig.tight_layout()
fig.set_size_inches(11,8)
plt.savefig('plt_rmse_type1_2_all_{}_{}.png'.format(data_folder, data_id))
plt.show()