-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
110 lines (79 loc) · 2.75 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
# SPDX-License-Identifier: EUPL-1.2
# Unmodified code written by Adam Bielski is licensed under the BSD-3-Clause license
# All further additions and modifications: Copyright (c) 2020, Martynas Janonis
# Licensed under the EUPL-1.2-or-later
from torch.nn.modules import distance
class Metric:
def __init__(self):
pass
def __call__(self, outputs, target, loss):
raise NotImplementedError
def reset(self):
raise NotImplementedError
def value(self):
raise NotImplementedError
def name(self):
raise NotImplementedError
class AccumulatedAccuracyMetric(Metric):
"""
Works with classification model
"""
def __init__(self):
self.correct = 0
self.total = 0
def __call__(self, outputs, target, loss):
pred = outputs[0].data.max(1, keepdim=True)[1]
self.correct += pred.eq(target[0].data.view_as(pred)).cpu().sum()
self.total += target[0].size(0)
return self.value()
def reset(self):
self.correct = 0
self.total = 0
def value(self):
return 100 * float(self.correct) / self.total
def name(self):
return "Accuracy"
class AccumulatedDistanceAccuracyMetric(Metric):
"""
If the distance between the two outputs is less than the margin,
classify as positive; else negative
"""
def __init__(self, margin):
self.correct = 0
self.total = 0
self.margin = margin
def __call__(self, outputs, target, loss):
pred = distance.PairwiseDistance().forward(outputs[0], outputs[1])
pred = pred.flatten() < self.margin
self.correct += sum(pred == target[0])
self.total += target[0].size(0)
return self.value()
def reset(self):
self.correct = 0
self.total = 0
def value(self):
return 100 * float(self.correct) / self.total
def name(self):
return "Accuracy"
class TripletAccumulatedDistanceAccuracyMetric(Metric):
"""
If the distance between the anchor and the positive is less than the distance between the anchor and the negative,
classify as positive; else negative
"""
def __init__(self):
self.correct = 0
self.total = 0
def __call__(self, outputs, target, loss):
dist_pos = distance.PairwiseDistance().forward(outputs[0], outputs[1])
dist_neg = distance.PairwiseDistance().forward(outputs[0], outputs[2])
pred = dist_pos.flatten() < dist_neg.flatten()
self.correct += sum(pred == True)
self.total += pred.size(0)
return self.value()
def reset(self):
self.correct = 0
self.total = 0
def value(self):
return 100 * float(self.correct) / self.total
def name(self):
return "Accuracy"