-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheval.py
More file actions
executable file
·151 lines (132 loc) · 5.61 KB
/
eval.py
File metadata and controls
executable file
·151 lines (132 loc) · 5.61 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
'''
This file implements evaluator for each problem.
'''
from abc import ABC, abstractmethod
import torch
import numpy
# from monai.metrics import PSNRMetric, SSIMMetric
from piq import LPIPS, psnr, ssim
class Evaluator(ABC):
def __init__(self, metric_list):
self.metric_list = metric_list
self.metric_state = {key: 0.0 for key in metric_list.keys()}
self.total = 0
@abstractmethod
def __call__(self, pred, target, observation=None):
''''
Args:
- pred (torch.Tensor): (N, C, H, W)
- target (torch.Tensor): (C, H, W) or (N, C, H, W)
- observation (torch.Tensor): (N, *observation.shape) or (*observation.shape)
Returns:
- metric_dict (Dict): a dictionary of metric values
'''
pass
def compute(self):
'''
Returns:
- metric_state (Dict): a dictionary of metric values
'''
metric_state = {key: val / self.total for key, val in self.metric_state.items()}
return metric_state
def relative_l2(pred, target):
''''
Args:
- pred (torch.Tensor): (N, C, H, W)
- target (torch.Tensor): (C, H, W)
Returns:
- rel_l2 (torch.Tensor): (N,), relative L2 error
'''
diff = pred - target
l2_norm = torch.linalg.norm(target.reshape(-1))
rel_l2 = torch.linalg.norm(diff.reshape(diff.shape[0], -1), dim=1) / l2_norm
return rel_l2
class NavierStokes2d(Evaluator):
def __init__(self, ):
metric_list = {'relative l2': relative_l2}
super(NavierStokes2d, self).__init__(metric_list)
def __call__(self, pred, target, observation=None):
'''
Args:
- pred (torch.Tensor): (N, C, H, W)
- target (torch.Tensor): (C, H, W) or (N, C, H, W)
Returns:
- metric_dict (Dict): a dictionary of metric values
'''
self.total += 1
metric_dict = {}
for metric_name, metric_func in self.metric_list.items():
if len(target.shape) == 3:
val = metric_func(pred, target).item()
metric_dict[metric_name] = val
self.metric_state[metric_name] += val
else:
val = metric_func(pred, target).mean().item()
metric_dict[metric_name] = val
self.metric_state[metric_name] += val
return metric_dict
class Image(Evaluator):
def __init__(self, ):
self.eval_batch = 32
metric_list = {'psnr': lambda x, y: psnr(x.clip(0, 1), y.clip(0, 1), data_range=1.0, reduction='none'),
'ssim': lambda x, y: ssim(x.clip(0, 1), y.clip(0, 1), data_range=1.0, reduction='none'),
'lpips': LPIPS(replace_pooling=True, reduction='none')}
super(Image, self).__init__(metric_list)
def __call__(self, pred, target, observation=None):
'''
Args:
- pred (torch.Tensor): (N, C, H, W)
- target (torch.Tensor): (C, H, W) or (N, C, H, W)
Returns:
- metric_dict (Dict): a dictionary of metric values
'''
self.total += 1
metric_dict = {}
for metric_name, metric_func in self.metric_list.items():
metric_dict[metric_name] = 0.0
if pred.shape != target.shape:
num_batches = pred.shape[0] // self.eval_batch
for i in range(num_batches):
pred_batch = pred[i * self.eval_batch: (i + 1) * self.eval_batch]
target_batch = target.repeat(pred_batch.shape[0], 1, 1, 1)
val = metric_func(pred_batch, target_batch).squeeze(-1).sum()
metric_dict[metric_name] += val
metric_dict[metric_name] = metric_dict[metric_name] / pred.shape[0]
self.metric_state[metric_name] += metric_dict[metric_name]
else:
val = metric_func(pred, target).mean().item()
metric_dict[metric_name] = val
self.metric_state[metric_name] += val
return metric_dict
class MRI(Evaluator):
def __init__(self,):
self.eval_batch = 32
metric_list = {'psnr': lambda x, y: psnr(x.clip(0, 1), y.clip(0, 1), data_range=1.0, reduction='none'),
'ssim': lambda x, y: ssim(x.clip(0, 1), y.clip(0, 1), data_range=1.0, reduction='none')}
super(MRI, self).__init__(metric_list)
def __call__(self, pred, target, observation=None):
'''
Args:
- pred (torch.Tensor): (N, C, H, W)
- target (torch.Tensor): (C, H, W) or (N, C, H, W)
Returns:
- metric_dict (Dict): a dictionary of metric values
'''
self.total += 1
metric_dict = {}
for metric_name, metric_func in self.metric_list.items():
metric_dict[metric_name] = 0.0
if pred.shape != target.shape:
num_batches = pred.shape[0] // self.eval_batch
for i in range(num_batches):
pred_batch = pred[i * self.eval_batch: (i + 1) * self.eval_batch]
target_batch = target.repeat(pred_batch.shape[0], 1, 1, 1)
val = metric_func(pred_batch, target_batch).squeeze(-1).sum()
metric_dict[metric_name] += val
metric_dict[metric_name] = metric_dict[metric_name] / pred.shape[0]
self.metric_state[metric_name] += metric_dict[metric_name]
else:
val = metric_func(pred, target).mean().item()
metric_dict[metric_name] = val
self.metric_state[metric_name] += val
return metric_dict