-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtest_metrics.py
155 lines (114 loc) · 4.95 KB
/
test_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
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2020 Apple Inc. All Rights Reserved.
#
"""Test metrics."""
import pytest
import torch
import torch.nn as nn
import torch.nn.functional as F
from quant.common.metrics import LossMetric, Top1Accuracy, TopKAccuracy
def test_loss_metric_no_accumulate():
"""Test loss metric returns correct value with no accumulate."""
criterion = F.nll_loss
metric = LossMetric(criterion, accumulate=False)
model = nn.LogSoftmax(dim=1)
X = torch.randn(3, 5)
output = model(X)
target = torch.tensor([1, 0, 4])
metric.update(output, target)
assert F.nll_loss(output, target).item() == metric.compute()
# Check this is true after re-computation
assert F.nll_loss(output, target).item() == metric.compute()
# Check update
Y = torch.randn(3, 5)
output2 = model(Y)
metric.update(output2, target)
assert F.nll_loss(output2, target).item() == metric.compute()
# Check this is true after reset & re-computation
metric.reset()
metric.update(output, target)
assert F.nll_loss(output, target).item() == metric.compute()
def test_loss_metric_accumulate():
"""Test loss metric returns correct value with accumulate."""
criterion = F.nll_loss
metric = LossMetric(criterion, accumulate=True)
model = nn.LogSoftmax(dim=1)
X = torch.randn(3, 5)
output = model(X)
target = torch.tensor([1, 0, 4])
metric.update(output, target)
assert F.nll_loss(output, target).item() == pytest.approx(metric.compute())
# Check this is true after re-computation
assert F.nll_loss(output, target).item() == pytest.approx(metric.compute())
# Check update
Y = torch.randn(3, 5)
output2 = model(Y)
metric.update(output2, target)
assert F.nll_loss(torch.cat([output, output2]), torch.cat([target, target])).item() \
== pytest.approx(metric.compute())
# Check this is true after reset & re-computation
metric.reset()
metric.update(output, target)
assert F.nll_loss(output, target).item() == pytest.approx(metric.compute())
def test_top_1_accuracy_metric_no_accumulate():
"""Test top-1 accuracy metric returns correct value with no accumulate."""
metric = Top1Accuracy(accumulate=False)
metric.update(torch.tensor([[0.1, 0.2, 0.3]]), torch.tensor([2]))
assert metric.compute() == 1.0
# Check this is true after re-computation
assert metric.compute() == 1.0
metric.update(torch.tensor([[0.1, 0.2, 0.3]]), torch.tensor([1]))
assert metric.compute() == 0
# Check after reset & re-computation
metric.reset()
metric.update(torch.tensor([[0.1, 0.2, 0.3]]), torch.tensor([2]))
assert metric.compute() == 1.0
def test_top_1_accuracy_metric_accumulate():
"""Test top-1 accuracy metric returns correct value with accumulate."""
metric = Top1Accuracy(accumulate=True)
metric.update(torch.tensor([[0.1, 0.2, 0.3]]), torch.tensor([2]))
assert metric.compute() == 1.0
metric.update(torch.tensor([[0.1, 0.2, 0.3]]), torch.tensor([1]))
assert metric.compute() == 0.5
metric.update(torch.tensor([[0.1, 0.2, 0.3]]), torch.tensor([0]))
assert metric.compute() == 1 / 3
# Check this is true after re-computation
assert metric.compute() == 1 / 3
# Check this is true after reset & re-computation
metric.reset()
metric.update(torch.tensor([[0.1, 0.2, 0.3]]), torch.tensor([2]))
assert metric.compute() == 1.0
def test_top_k_accuracy_metric_no_accumulate():
"""Test top-k accuracy metric returns correct value with no accumulate."""
output = torch.tensor([[0.1, 0.2, 0.3, 0, 0.5],
[0.2, 0.3, 0.4, 0.1, 0]])
metric_k2 = TopKAccuracy(2, accumulate=False)
metric_k2.update(output, torch.tensor([4, 0]))
assert metric_k2.compute() == 0.5
metric_k2.update(torch.tensor([[0.1, 0.5, 0.3, 0.2, 0.4]]), torch.tensor([1]))
assert metric_k2.compute() == 1.0
# Check re-computation does not change value
assert metric_k2.compute() == 1.0
# Check reset works
metric_k2.reset()
metric_k2.update(output, torch.tensor([4, 0]))
assert metric_k2.compute() == 0.5
def test_top_k_accuracy_metric_accumulate():
"""Test top-k accuracy metric returns correct value with accumulate."""
output = torch.tensor([[0.1, 0.2, 0.3, 0, 0.5],
[0.2, 0.3, 0.4, 0.1, 0]])
metric_k2 = TopKAccuracy(2, accumulate=True)
metric_k2.update(output, torch.tensor([4, 0]))
assert metric_k2.compute() == 0.5
metric_k3 = TopKAccuracy(3, accumulate=True)
metric_k3.update(output, torch.tensor([4, 0]))
assert metric_k3.compute() == 1.0
metric_k2.update(torch.tensor([[0.1, 0.5, 0.3, 0.2, 0.4]]), torch.tensor([1]))
assert metric_k2.compute() == 2 / 3
# Check re-computation does not change value
assert metric_k2.compute() == 2 / 3
# Check reset works
metric_k2.reset()
metric_k2.update(output, torch.tensor([4, 0]))
assert metric_k2.compute() == 0.5