-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpapervis_eval.py
139 lines (113 loc) · 4.05 KB
/
papervis_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
import os
import cv2
import numpy as np
import pandas as pd
import torch
import torchmetrics
from pathlib import Path
from torch import nn
from tqdm import tqdm
def load_gt(directory):
transform_mask = lambda x: torch.from_numpy(x).long()
video_folders = [f for f in sorted(Path(directory).iterdir()) if f.is_dir() and f.name != 'video_01370']
total_masks = []
for f in tqdm(video_folders):
masks_dir = f.joinpath("mask.npy")
masks = transform_mask(np.load(masks_dir))
masks = masks[-1, :, :]
# print(masks.shape)
total_masks.append(masks)
return torch.stack(total_masks, dim=0)
# def load_pred(directory):
class Metrics:
def __init__(self, logts, pred, true):
self.logits = logits
self.pred = pred
self.true = true
# print('----shape check----')
# print(self.logits.shape, self.pred.shape, self.true.shape)
def calc(self, metric):
if metric == 'iou':
return self.calc_iou()
elif metric == 'ssim':
return self.calc_ssim()
elif metric == 'ce':
return self.calc_ce()
elif metric == 'mse':
return self.calc_mse()
else:
raise NotImplementedError
def calc_iou(self):
jaccard = torchmetrics.JaccardIndex(task="multiclass", num_classes=49)
iou = jaccard(self.pred, self.true).item()
return iou
def calc_ssim(self):
pred, true = self.pred.numpy(), self.true.numpy()
C1 = (0.01 * 255)**2
C2 = (0.03 * 255)**2
img1 = pred.astype(np.float64)
img2 = true.astype(np.float64)
kernel = cv2.getGaussianKernel(11, 1.5)
window = np.outer(kernel, kernel.transpose())
mu1 = cv2.filter2D(img1, -1, window)[5:-5, 5:-5] # valid
mu2 = cv2.filter2D(img2, -1, window)[5:-5, 5:-5]
mu1_sq = mu1**2
mu2_sq = mu2**2
mu1_mu2 = mu1 * mu2
sigma1_sq = cv2.filter2D(img1**2, -1, window)[5:-5, 5:-5] - mu1_sq
sigma2_sq = cv2.filter2D(img2**2, -1, window)[5:-5, 5:-5] - mu2_sq
sigma12 = cv2.filter2D(img1 * img2, -1, window)[5:-5, 5:-5] - mu1_mu2
ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / ((mu1_sq + mu2_sq + C1) *
(sigma1_sq + sigma2_sq + C2))
return ssim_map.mean()
def calc_ce(self):
criterion = nn.CrossEntropyLoss()
ce = criterion(self.logits, self.true)
return ce.item()
def calc_mse(self):
criterion = nn.MSELoss()
mse = criterion(self.pred, self.true.float())
return mse.item()
if __name__ == "__main__":
ground_truth = load_gt('./dataset/val')
# print(grount_truth.shape)
TENSORS = [
'finetune_e50lr3oc_vp_gsta',
'mptrain_e10lr3oc_mp_gsta',
'mptrain_e20lr3cos_mp2_gsta',
'mptrain_e20lr3cos_mp2l_gsta',
]
METRICS = [
'ce',
'mse',
'ssim',
'iou'
]
table_dict = {'name': [t.replace('_', ' ') for t in TENSORS]}
tensor_dir = './papervis/results'
# ious = []
# ssims = []
for t in tqdm(TENSORS):
save_path = os.path.join(tensor_dir, f'{t}.pt')
# print(save_path)
logits = torch.load(save_path).cpu()
_, pred = torch.max(logits, dim=1)
M = Metrics(logits, pred, ground_truth)
for met in METRICS:
res = M.calc(met)
table_dict[met] = table_dict.get(met, []) + [res]
# print('pred', pred.shape)
# ssims.append(calc_ssim(pred, ground_truth))
# ious.append(calc_iou(pred, ground_truth))
# # print('---------------')
# table_dict['ssim'] = ssims
# table_dict['iou'] = ious
print(table_dict)
# Convert dictionary to pandas DataFrame
df = pd.DataFrame(table_dict)
# Transpose the DataFrame
df_transposed = df.transpose()
# Convert transposed DataFrame to LaTeX table
latex_table_transposed = df_transposed.to_latex(header=False)
# Print or save the LaTeX table
print(latex_table_transposed)