forked from bfortuner/pytorch-kaggle-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.py
146 lines (104 loc) · 3.85 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
from __future__ import absolute_import
from __future__ import print_function
import torch as th
from .utils import th_matrixcorr
from .callbacks import Callback
class MetricContainer(object):
def __init__(self, metrics, prefix=''):
self.metrics = metrics
self.helper = None
self.prefix = prefix
def set_helper(self, helper):
self.helper = helper
def reset(self):
for metric in self.metrics:
metric.reset()
def __call__(self, output_batch, target_batch):
logs = {}
for metric in self.metrics:
logs[self.prefix+metric._name] = self.helper.calculate_loss(output_batch,
target_batch,
metric)
return logs
class Metric(object):
def __call__(self, y_pred, y_true):
raise NotImplementedError('Custom Metrics must implement this function')
def reset(self):
raise NotImplementedError('Custom Metrics must implement this function')
class MetricCallback(Callback):
def __init__(self, container):
self.container = container
def on_epoch_begin(self, epoch_idx, logs):
self.container.reset()
class CategoricalAccuracy(Metric):
def __init__(self, top_k=1):
self.top_k = top_k
self.correct_count = 0
self.total_count = 0
self.accuracy = 0
self._name = 'acc_metric'
def reset(self):
self.correct_count = 0
self.total_count = 0
self.accuracy = 0
def __call__(self, y_pred, y_true):
top_k = y_pred.topk(self.top_k,1)[1]
true_k = y_true.view(len(y_true),1).expand_as(top_k)
self.correct_count += top_k.eq(true_k).float().sum().data[0]
self.total_count += len(y_pred)
accuracy = 100. * float(self.correct_count) / float(self.total_count)
return accuracy
class BinaryAccuracy(Metric):
def __init__(self):
self.correct_count = 0
self.total_count = 0
self.accuracy = 0
self._name = 'acc_metric'
def reset(self):
self.correct_count = 0
self.total_count = 0
self.accuracy = 0
def __call__(self, y_pred, y_true):
y_pred_round = y_pred.round().long()
self.correct_count += y_pred_round.eq(y_true).float().sum().data[0]
self.total_count += len(y_pred)
accuracy = 100. * float(self.correct_count) / float(self.total_count)
return accuracy
class ProjectionCorrelation(Metric):
def __init__(self):
self.corr_sum = 0.
self.total_count = 0.
self._name = 'corr_metric'
def reset(self):
self.corr_sum = 0.
self.total_count = 0.
self.average = 0.
def __call__(self, y_pred, y_true=None):
"""
y_pred should be two projections
"""
covar_mat = th.abs(th_matrixcorr(y_pred[0].data, y_pred[1].data))
self.corr_sum += th.trace(covar_mat)
self.total_count += covar_mat.size(0)
return self.corr_sum / self.total_count
class ProjectionAntiCorrelation(Metric):
def __init__(self):
self.anticorr_sum = 0.
self.total_count = 0.
self.average = 0.
self._name = 'anticorr_metric'
def reset(self):
self.anticorr_sum = 0.
self.total_count = 0.
self.average = 0.
def __call__(self, y_pred, y_true=None):
"""
y_pred should be two projections
"""
covar_mat = th.abs(th_matrixcorr(y_pred[0].data, y_pred[1].data))
upper_sum = th.sum(th.triu(covar_mat,1))
lower_sum = th.sum(th.tril(covar_mat,-1))
self.anticorr_sum += upper_sum
self.anticorr_sum += lower_sum
self.total_count += covar_mat.size(0)*(covar_mat.size(1) - 1)
return self.anticorr_sum / self.total_count