-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy patheval.py
361 lines (311 loc) · 13.2 KB
/
eval.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# -*- coding: utf-8 -*-
"""
# @file name : eval.py
# @author : chenzhanpeng https://github.com/chenzpstar
# @date : 2023-09-10
# @brief : 图像评估
# @reference : https://blog.csdn.net/fovever_/article/details/129332278
"""
import os
import sys
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(BASE_DIR, '..'))
import cv2
import numpy as np
import torch
from natsort import natsorted
from openpyxl import Workbook, load_workbook
from openpyxl.utils import get_column_letter
from common import get_test_args
from core.metric import *
def eval_metrics(img1, img2, imgf):
sd = calc_std(imgf)
ag = calc_ag(imgf)
sf = calc_sf(imgf)
mse = (calc_mse(img1, imgf) + calc_mse(img2, imgf)) * 0.5
psnr = calc_psnr(mse)
cc = (calc_cc(img1, imgf) + calc_cc(img2, imgf)) * 0.5
scd = calc_scd(img1, img2, imgf)
en = calc_entropy(imgf)
ce = calc_cross_ent(img1, imgf) + calc_cross_ent(img2, imgf)
mi = calc_mul_info(img1, imgf, normalized=True) + calc_mul_info(
img2, imgf, normalized=True)
qabf, nabf, labf = calc_Qabf(img1, img2, imgf, L=1.5, full=True)
ssim = (calc_ssim(img1, imgf) + calc_ssim(img2, imgf)) * 0.5
msssim = (calc_msssim(img1, imgf) + calc_msssim(img2, imgf)) * 0.5
viff = calc_viff(img1, img2, imgf, simple=False)
return {
'sd': sd.item(),
'ag': ag.item(),
'sf': sf.item(),
'mse': mse.item(),
'psnr': psnr.item(),
'cc': cc.item(),
'scd': scd.item(),
'en': en.item(),
'ce': ce.item(),
'mi': mi.item(),
'qabf': qabf.item(),
'nabf': nabf.item(),
'labf': labf.item(),
'ssim': ssim.item(),
'msssim': msssim.item(),
'viff': viff.item(),
# 'qabf': 1,
# 'nabf': 1,
# 'labf': 1,
# 'ssim': 1,
# 'msssim': 1,
# 'viff': 1,
}
def write_excel(file_name, sheet_name='test', column=0, data=None):
try:
workbook = load_workbook(file_name)
except FileNotFoundError:
workbook = Workbook() # 若文件不存在,则创建新文件
# 获取或创建指定工作表
if sheet_name in workbook.sheetnames:
worksheet = workbook[sheet_name]
else:
worksheet = workbook.create_sheet(title=sheet_name)
# 在指定列中插入数据
column = get_column_letter(column + 1)
for i, value in enumerate(data):
cell = worksheet[column + str(i + 1)]
cell.value = value
# 保存文件
workbook.save(file_name)
if __name__ == '__main__':
import time
torch.cuda.empty_cache()
args = get_test_args()
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# device = torch.device('cuda:0' if args.gpu else 'cpu')
# device = torch.device('cpu')
sheet_name = 'method'
# sheet_name = 'metric'
method_list = [
'DeepFuse', 'DenseFuse', 'VIFNet', 'DBNet', 'SEDRFuse', 'NestFuse',
'RFNNest', 'UNFusion', 'Res2Fusion', 'MAFusion', 'IFCNN', 'DIFNet',
'PMGI', 'PFNetv1', 'PFNetv2', 'MyFusion'
]
method_names = [method_list[0]]
exp_name = None
# exp_name = 'exp1'
# data_dir = os.path.join(BASE_DIR, 'data', 'samples', args.data)
data_dir = os.path.join(BASE_DIR, '..', 'datasets', args.data)
# save_path = os.path.join(BASE_DIR, '..', f'metrics_{args.data}.xlsx')
if args.data in ['tno']:
img1_dir = os.path.join(data_dir, 'vis')
img2_dir = os.path.join(data_dir, 'ir')
elif args.data in ['roadscene', 'msrs']:
img1_dir = os.path.join(data_dir, 'test', 'vis')
img2_dir = os.path.join(data_dir, 'test', 'ir')
elif args.data in ['polar']:
img1_dir = os.path.join(data_dir, 'test', 'vis')
img2_dir = os.path.join(data_dir, 'test', 'po')
if exp_name is None:
ckpt_dir = os.path.join(BASE_DIR, '..', 'checkpoints', args.ckpt)
else:
ckpt_dir = os.path.join(BASE_DIR, '..', 'checkpoints', exp_name,
args.ckpt)
# save_path = os.path.join(ckpt_dir, f'metrics_{args.data}.xlsx')
save_path = os.path.join(ckpt_dir,
f'metrics_{args.data}_{method_names[0]}.xlsx')
imgf_dir = os.path.join(ckpt_dir, args.data)
# results_dir = os.path.join(BASE_DIR, '..', 'results', args.data)
# save_path = os.path.join(results_dir, f'metrics_{args.data}.xlsx')
for i, method_name in enumerate(method_names):
sd_list = []
ag_list = []
sf_list = []
mse_list = []
psnr_list = []
cc_list = []
scd_list = []
en_list = []
ce_list = []
mi_list = []
qabf_list = []
nabf_list = []
labf_list = []
ssim_list = []
msssim_list = []
viff_list = []
name_list = []
# 评估方法
# imgf_dir = os.path.join(results_dir, method_name)
print(f'evaluating {method_name} ...')
start = time.time()
for i, img in enumerate(natsorted(os.listdir(img1_dir))):
# 读取数据
img1_path = os.path.join(img1_dir, img)
img2_path = os.path.join(img2_dir, img)
imgf_path = os.path.join(imgf_dir, f'{i + 1:0>2}.bmp')
img1 = cv2.imread(img1_path,
cv2.IMREAD_GRAYSCALE).astype(np.float32)
img2 = cv2.imread(img2_path,
cv2.IMREAD_GRAYSCALE).astype(np.float32)
imgf = cv2.imread(imgf_path,
cv2.IMREAD_GRAYSCALE).astype(np.float32)
img1 = torch.from_numpy(
img1.copy()).float().unsqueeze(0).unsqueeze(0)
img2 = torch.from_numpy(
img2.copy()).float().unsqueeze(0).unsqueeze(0)
imgf = torch.from_numpy(
imgf.copy()).float().unsqueeze(0).unsqueeze(0)
# imgf = torch.rand_like(img1)
img1.to(device, non_blocking=True)
img2.to(device, non_blocking=True)
imgf.to(device, non_blocking=True)
# 评估图像
print(f'evaluating {img} ...')
with torch.no_grad():
results = eval_metrics(img1, img2, imgf)
# 记录结果
sd_list.append(results['sd'])
ag_list.append(results['ag'])
sf_list.append(results['sf'])
mse_list.append(results['mse'])
psnr_list.append(results['psnr'])
cc_list.append(results['cc'])
scd_list.append(results['scd'])
en_list.append(results['en'])
ce_list.append(results['ce'])
mi_list.append(results['mi'])
qabf_list.append(results['qabf'])
nabf_list.append(results['nabf'])
labf_list.append(results['labf'])
ssim_list.append(results['ssim'])
msssim_list.append(results['msssim'])
viff_list.append(results['viff'])
name_list.append(img)
end = time.time()
print(f'evaluating {method_name} done, cost {end - start:.3f}s')
# 计算均值
sd_list.insert(0, np.mean(sd_list))
ag_list.insert(0, np.mean(ag_list))
sf_list.insert(0, np.mean(sf_list))
mse_list.insert(0, np.mean(mse_list))
psnr_list.insert(0, np.mean(psnr_list))
cc_list.insert(0, np.mean(cc_list))
scd_list.insert(0, np.mean(scd_list))
en_list.insert(0, np.mean(en_list))
ce_list.insert(0, np.mean(ce_list))
mi_list.insert(0, np.mean(mi_list))
qabf_list.insert(0, np.mean(qabf_list))
nabf_list.insert(0, np.mean(nabf_list))
labf_list.insert(0, np.mean(labf_list))
ssim_list.insert(0, np.mean(ssim_list))
msssim_list.insert(0, np.mean(msssim_list))
viff_list.insert(0, np.mean(viff_list))
name_list.insert(0, 'mean')
# 计算标准差
sd_list.insert(1, np.std(sd_list))
ag_list.insert(1, np.std(ag_list))
sf_list.insert(1, np.std(sf_list))
mse_list.insert(1, np.std(mse_list))
psnr_list.insert(1, np.std(psnr_list))
cc_list.insert(1, np.std(cc_list))
scd_list.insert(1, np.std(scd_list))
en_list.insert(1, np.std(en_list))
ce_list.insert(1, np.std(ce_list))
mi_list.insert(1, np.std(mi_list))
qabf_list.insert(1, np.std(qabf_list))
nabf_list.insert(1, np.std(nabf_list))
labf_list.insert(1, np.std(labf_list))
ssim_list.insert(1, np.std(ssim_list))
msssim_list.insert(1, np.std(msssim_list))
viff_list.insert(1, np.std(viff_list))
name_list.insert(1, 'std')
if sheet_name == 'method':
# 插入名字
sd_list.insert(0, f'{"SD"}')
ag_list.insert(0, f'{"AG"}')
sf_list.insert(0, f'{"SF"}')
mse_list.insert(0, f'{"MSE"}')
psnr_list.insert(0, f'{"PSNR"}')
cc_list.insert(0, f'{"CC"}')
scd_list.insert(0, f'{"SCD"}')
en_list.insert(0, f'{"EN"}')
ce_list.insert(0, f'{"CE"}')
mi_list.insert(0, f'{"MI"}')
qabf_list.insert(0, f'{"Qabf"}')
nabf_list.insert(0, f'{"Nabf"}')
labf_list.insert(0, f'{"Labf"}')
ssim_list.insert(0, f'{"SSIM"}')
msssim_list.insert(0, f'{"MSSSIM"}')
viff_list.insert(0, f'{"VIFF"}')
name_list.insert(0, '')
# 写入文件
write_excel(save_path, method_name, 0, name_list)
write_excel(save_path, method_name, 1, sd_list)
write_excel(save_path, method_name, 2, ag_list)
write_excel(save_path, method_name, 3, sf_list)
write_excel(save_path, method_name, 4, mse_list)
write_excel(save_path, method_name, 5, psnr_list)
write_excel(save_path, method_name, 6, cc_list)
write_excel(save_path, method_name, 7, scd_list)
write_excel(save_path, method_name, 8, en_list)
write_excel(save_path, method_name, 9, ce_list)
write_excel(save_path, method_name, 10, mi_list)
write_excel(save_path, method_name, 11, qabf_list)
write_excel(save_path, method_name, 12, nabf_list)
write_excel(save_path, method_name, 13, labf_list)
write_excel(save_path, method_name, 14, ssim_list)
write_excel(save_path, method_name, 15, msssim_list)
write_excel(save_path, method_name, 16, viff_list)
elif sheet_name == 'metric':
# 插入名字
sd_list.insert(0, f'{method_name}')
ag_list.insert(0, f'{method_name}')
sf_list.insert(0, f'{method_name}')
mse_list.insert(0, f'{method_name}')
psnr_list.insert(0, f'{method_name}')
cc_list.insert(0, f'{method_name}')
scd_list.insert(0, f'{method_name}')
en_list.insert(0, f'{method_name}')
ce_list.insert(0, f'{method_name}')
mi_list.insert(0, f'{method_name}')
qabf_list.insert(0, f'{method_name}')
nabf_list.insert(0, f'{method_name}')
labf_list.insert(0, f'{method_name}')
ssim_list.insert(0, f'{method_name}')
msssim_list.insert(0, f'{method_name}')
viff_list.insert(0, f'{method_name}')
name_list.insert(0, '')
# 写入文件
if i == 0:
write_excel(save_path, 'SD', 0, name_list)
write_excel(save_path, 'AG', 0, name_list)
write_excel(save_path, 'SF', 0, name_list)
write_excel(save_path, 'MSE', 0, name_list)
write_excel(save_path, 'PSNR', 0, name_list)
write_excel(save_path, 'CC', 0, name_list)
write_excel(save_path, 'SCD', 0, name_list)
write_excel(save_path, 'EN', 0, name_list)
write_excel(save_path, 'CE', 0, name_list)
write_excel(save_path, 'MI', 0, name_list)
write_excel(save_path, 'Qabf', 0, name_list)
write_excel(save_path, 'Nabf', 0, name_list)
write_excel(save_path, 'Labf', 0, name_list)
write_excel(save_path, 'SSIM', 0, name_list)
write_excel(save_path, 'MSSSIM', 0, name_list)
write_excel(save_path, 'VIFF', 0, name_list)
write_excel(save_path, 'SD', i + 1, sd_list)
write_excel(save_path, 'AG', i + 1, ag_list)
write_excel(save_path, 'SF', i + 1, sf_list)
write_excel(save_path, 'MSE', i + 1, mse_list)
write_excel(save_path, 'PSNR', i + 1, psnr_list)
write_excel(save_path, 'CC', i + 1, cc_list)
write_excel(save_path, 'SCD', i + 1, scd_list)
write_excel(save_path, 'EN', i + 1, en_list)
write_excel(save_path, 'CE', i + 1, ce_list)
write_excel(save_path, 'MI', i + 1, mi_list)
write_excel(save_path, 'Qabf', i + 1, qabf_list)
write_excel(save_path, 'Nabf', i + 1, nabf_list)
write_excel(save_path, 'Labf', i + 1, labf_list)
write_excel(save_path, 'SSIM', i + 1, ssim_list)
write_excel(save_path, 'MSSSIM', i + 1, msssim_list)
write_excel(save_path, 'VIFF', i + 1, viff_list)