-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMetrics.py
362 lines (290 loc) · 12.3 KB
/
Metrics.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
362
import cv2
import os
import pandas as pd
import torch
import numpy as np
import matplotlib.pyplot as plt
from skimage.metrics import structural_similarity as compare_ssim
from skimage.metrics import normalized_mutual_information
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
def PSNR(img1, img2):
mse = torch.mean((img1 - img2) ** 2)
if mse == 0:
return 100
max_pixel = 255.0
psnr = 20 * torch.log10(max_pixel / torch.sqrt(mse))
return psnr.item()
def SSIM(img1, img2):
img1 = img1.permute(0, 2, 3, 1).cpu().numpy()
img2 = img2.permute(0, 2, 3, 1).cpu().numpy()
ssim = np.mean([compare_ssim(im1, im2, multichannel=True) for im1, im2 in zip(img1, img2)])
return ssim
# def SSIM(img1, img2):
# img1 = img1.permute(1, 2, 0).cpu().numpy()
# img2 = img2.permute(1, 2, 0).cpu().numpy()
# ssim = np.mean([compare_ssim(im1, im2, multichannel=True) for im1, im2 in zip(img1, img2)])
# return ssim
def FF(img1, img2, fused):
energy1 = torch.sum(img1 ** 2)
energy2 = torch.sum(img2 ** 2)
energyF = torch.sum(fused ** 2)
ff = (energyF - energy1 - energy2) / (2 * torch.sqrt(energy1 * energy2))
return ff.item()
# def NMI(img1, img2):
# img1 = img1.view(-1).cpu().numpy()
# img2 = img2.view(-1).cpu().numpy()
# nmi = normalized_mutual_information(img1, img2)
# return nmi
def NMI(img1, img2):
img1 = img1.reshape(-1).cpu().numpy()
img2 = img2.reshape(-1).cpu().numpy()
nmi = normalized_mutual_information(img1, img2)
return nmi
### Working for images not batches
# def calculate_patch_metrics(image1, image2, patch_size=32):
# # Calculate the number of patches in the images
# num_patches = (image1.shape[0] // patch_size) * (image1.shape[1] // patch_size)
# # print(f"image1 shape, {image1.shape}")
# # Initialize arrays to store the SSIM values for each patch
# ssim_values = np.zeros(num_patches)
# psnr_values = np.zeros(num_patches)
# nmi_values = np.zeros(num_patches)
# # Loop through each patch in the images
# patch_index = 0
# for i in range(0, image1.shape[0], patch_size):
# for j in range(0, image1.shape[1], patch_size):
# # Extract the patch from each image
# patch1 = image1[i:i+patch_size, j:j+patch_size]
# print(f"patch size, {patch1.shape}")
# patch2 = image2[i:i+patch_size, j:j+patch_size]
# # Calculate the SSIM for the patch and store the result
# ssim_values[patch_index] = SSIM(patch1, patch2)
# psnr_values[patch_index] = PSNR(patch1, patch2)
# nmi_values[patch_index] = NMI(patch1, patch2)
# # Increment the patch index
# patch_index += 1
# # Calculate the average SSIM for all patches
# # avg_ssim = np.mean(ssim_values)
# return psnr_values, ssim_values, nmi_values
## Working for batches
def calculate_patch_metrics(image1, image2, patch_size=128):
# Calculate the number of patches in the images
num_patches = (image1.shape[2] // patch_size) * (image1.shape[3] // patch_size)
# Initialize arrays to store the SSIM values for each patch
ssim_values = np.zeros(num_patches)
psnr_values = np.zeros(num_patches)
nmi_values = np.zeros(num_patches)
# Loop through each patch in the images
patch_index = 0
for i in range(0, image1.shape[0], patch_size):
for j in range(0, image1.shape[1], patch_size):
# Extract the patch from each image
patch1 = image1[:, :, i:i+patch_size, j:j+patch_size]
patch2 = image2[:, :, i:i+patch_size, j:j+patch_size]
# Calculate the SSIM for the patch and store the result
ssim_values[patch_index] = SSIM(patch1, patch2)
psnr_values[patch_index] = PSNR(patch1, patch2)
nmi_values[patch_index] = NMI(patch1, patch2)
# Increment the patch index
patch_index += 1
# Calculate the average SSIM for all patches
# avg_ssim = np.mean(ssim_values)
return psnr_values, ssim_values, nmi_values
# def calculate_metrics_with_patch(gen, val_loader, epoch, folder, device):
# psnr_values_vis = []
# ssim_values_vis = []
# ff_values = []
# nmi_values_vis = []
# psnr_values_ir = []
# ssim_values_ir = []
# ff_values = []
# nmi_values_ir = []
# for batch in val_loader:
# img1 = batch['image_vis']
# img2 = batch['image_ir']
# a = batch['target_vis']
# b = batch['target_ir']
# # masked_feat = mask_feat(x, y)
# # mask_x, mask_y = masked_feat[2].to(config.DEVICE), masked_feat[3].to(config.DEVICE)
# img1, img2 = img1.to(device), img2.to(device)
# gen.eval()
# # print(f"image size in main metric fun", {img1.shape})
# with torch.no_grad():
# # Perform image fusion
# y_fake, x_a, y_a = gen(img1, img2)
# # Calculate the metrics for Vis
# psnr_vis, ssim_vis, nmi_vis = calculate_patch_metrics(img1, y_fake, patch_size = 128)
# # print(f"psnr outpot shape from the patch metrics fun, {psnr_vis.shape}")
# # Append the metric values to the lists
# psnr_values_vis.append(psnr_vis)
# ssim_values_vis.append(ssim_vis)
# # ff_values.append(ff)
# nmi_values_vis.append(nmi_vis)
# # Calculate the metrics for ir
# psnr_ir, ssim_ir, nmi_ir = calculate_patch_metrics(img2, y_fake, patch_size = 128)
# # Append the metric values to the lists
# psnr_values_ir.append(psnr_ir)
# ssim_values_ir.append(ssim_ir)
# nmi_values_ir.append(nmi_ir)
# # Create a dictionary to store the metric values
# metrics = {'PSNR_VIS': psnr_values_vis,
# 'SSIM_VIS': ssim_values_vis,
# 'NMI_VIS': nmi_values_vis,
# # 'FF': ff_values,
# 'PSNR_IR' : psnr_values_ir,
# 'SSIM_IR' : ssim_values_ir,
# 'NMI_IR' : nmi_values_ir}
# # Create the folder if it doesn't exist
# if not os.path.exists(folder):
# os.makedirs(folder)
# # Get the number of images and patches
# num_images = len(metrics['PSNR_VIS'])
# num_patches = len(metrics['PSNR_VIS'][0])
# # Create a 3D plot
# fig = plt.figure()
# ax = fig.add_subplot(111, projection='3d')
# # Create arrays to store the X, Y, and Z values
# X = np.arange(num_images)
# Y = np.arange(num_patches)
# X, Y = np.meshgrid(X, Y)
# # Plot each metric as a surface
# for metric, values in metrics.items():
# Z = np.array(values)
# Z = np.reshape(Z, X.shape)
# ax.plot_surface(X, Y, Z, cmap='viridis', label=metric)
# # Set the axis labels and legend
# ax.set_xlabel('Image Number')
# ax.set_ylabel('Patch Number')
# ax.set_zlabel('Metric Value')
# plt.savefig(os.path.join(folder, f'metric_{metric}_{epoch}.png'))
# # ax.legend()
# # # Show the plot
# # plt.show()
# # Get the maximum length of the metric value lists
# # max_len = max(len(x) for x in metrics.values())
# # # Pad the shorter lists with NaN values
# # for key, val in metrics.items():
# # if len(val) < max_len:
# # metrics[key] = val + [float('nan')] * (max_len - len(val))
# # # Create a 3D plot
# # fig = plt.figure()
# # ax = fig.add_subplot(111, projection='3d')
# # # Create arrays to store the X, Y, and Z values
# # num_images = len(metrics['PSNR_VIS'])
# # num_patches = max_len
# # X = np.arange(num_images)
# # Y = np.arange(num_patches)
# # X, Y = np.meshgrid(X, Y)
# # # Plot each metric as a surface
# # for metric, values in metrics.items():
# # Z = np.array(values).reshape(num_images, num_patches)
# # ax.plot_surface(X, Y, Z, cmap='viridis', label=metric)
# # # Set the axis labels and legend
# # ax.set_xlabel('Image Number')
# # ax.set_ylabel('Patch Number')
# # ax.set_zlabel('Metric Value')
# # ax.legend()
# return metrics
def calculate_metrics(gen, val_loader, epoch, folder):
# Initialize the lists to store the metric values
psnr_values_vis = []
ssim_values_vis = []
# ff_values = []
nmi_values_vis = []
psnr_values_ir = []
ssim_values_ir = []
ff_values = []
nmi_values_ir = []
for batch in val_loader:
img1 = batch['image_vis']
img2 = batch['image_ir']
# a = batch['target_vis']
# b = batch['target_ir']
# masked_feat = mask_feat(x, y)
# mask_x, mask_y = masked_feat[2].to(config.DEVICE), masked_feat[3].to(config.DEVICE)
img1, img2 = img1.to(DEVICE), img2.to(DEVICE)
gen.eval()
with torch.no_grad():
# Perform image fusion
y_fake, x_a, y_a, l_a = gen(img1, img2)
# Calculate the metrics for Vis
psnr_vis = PSNR(img1, y_fake)
ssim_vis = SSIM(img1, y_fake)
ff = FF(img1, img2, y_fake)
nmi_vis = NMI(img1, y_fake)
# ff = FF(img1, img2, y_fake)
# Append the metric values to the lists
psnr_values_vis.append(psnr_vis)
ssim_values_vis.append(ssim_vis)
ff_values.append(ff)
nmi_values_vis.append(nmi_vis)
# Calculate the metrics for IR
psnr_ir = PSNR(img2, y_fake)
ssim_ir = SSIM(img2, y_fake)
ff = FF(img1, img2, y_fake)
nmi_ir = NMI(img2, y_fake)
# Append the metric values to the lists
psnr_values_ir.append(psnr_ir)
ssim_values_ir.append(ssim_ir)
nmi_values_ir.append(nmi_ir)
# # Create a dictionary to store the metric values
metrics = {'PSNR_VIS': psnr_values_vis,
'SSIM_VIS': ssim_values_vis,
'NMI_VIS': nmi_values_vis,
'FF': ff_values,
'PSNR_IR' : psnr_values_ir,
'SSIM_IR' : ssim_values_ir,
'NMI_IR' : nmi_values_ir}
# Create a pandas DataFrame from the dictionary
df = pd.DataFrame(metrics)
pd.options.display.float_format = '{:.2f}'.format
# Calculate the statistics
statistics = df.describe().apply(lambda s: s.apply('{0:.2f}'.format))
# statistics = statistics.to_numpy().astype(float)
plt.clf()
# Create the folder if it doesn't exist
if not os.path.exists(folder):
os.makedirs(folder)
# Create a table plot using the pandas built-in function
table = plt.table(cellText=statistics.values,
colLabels=statistics.columns,
rowLabels = statistics.index,
loc='center')
plt.title(f'Stats at epoch {epoch}')
# Set the font size of the table
# table.set_row_colors(['lightgray'] * len(statistics.index))
table.auto_set_font_size(False)
table.set_fontsize(12)
# Hide the axis
plt.axis('off')
# Save the figure as an image
plt.savefig(os.path.join(folder, f'statistics_{epoch}.png'))
plt.clf()
# # Plot the metrics
# fig, ax = plt.subplots()
# for metric_name, metric_values in metrics.items():
# ax.plot(metric_values, label=metric_name)
# ax.set_title('Metrics')
# ax.set_xlabel('Epochs')
# ax.set_ylabel('Metric Value')
# ax.legend(title='Epoch', loc='upper left')
# ax.grid(True)
# plt.savefig(os.path.join(folder, f'metrics_{epoch}.png'))
# # plt.show()
# # plt.clf()
# fig, axs = plt.subplots(2, 4, figsize=(12, 8), sharex = True)
# fig.suptitle('Metrics', fontsize=16)
# axs = axs.ravel() # flatten the axes array for easy indexing
# for i, (metric_name, metric_values) in enumerate(metrics.items()):
# # plot the metric on its own subplot
# axs[i].plot(metric_values)
# axs[i].set_title(metric_name)
# axs[i].set_xlabel('Epochs')
# axs[i].set_ylabel(metric_name)
# # add the epoch number as a legend
# axs[i].legend([f'epoch_{epoch}'], loc='upper left')
# adjust the layout of the subplots and save the figure
# fig.tight_layout()
# plt.savefig(os.path.join(folder, f'metrics.png'))
# Return the DataFrame and statistics
return df, statistics