-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_perceptron_mnist.py
165 lines (130 loc) · 5.14 KB
/
test_perceptron_mnist.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
156
157
158
159
160
161
162
163
164
165
"""
Tests linear and conv2d layers of hxtorch by classifying some
MNIST images. Hagen-mode calibration and MAC operation are used.
"""
from abc import ABCMeta, abstractmethod
import unittest
from pathlib import Path
import torch
import hxtorch
from hxtorch.perceptron.nn import scale_input, scale_weight
hxtorch.logger.reset()
hxtorch.logger.default_config(level=hxtorch.logger.LogLevel.INFO)
hxtorch.logger.set_loglevel(hxtorch.logger.get('grenade'), hxtorch.logger.LogLevel.WARN)
hxtorch.logger.set_loglevel(hxtorch.logger.get('fisch.PlaybackProgramBuilder'), hxtorch.logger.LogLevel.ERROR)
torch.set_num_threads(1)
class HXTorchModel(torch.nn.Module):
"""
Model used to classify MNIST images.
Uses zero padding of 1 to produce images shaped (30, 30).
Uses a convolutional layer with a kernel (10, 10) as first layer.
Uses two dense layers afterwards.
"""
def __init__(self, mock: bool):
super().__init__()
self.conv2d = hxtorch.perceptron.nn.Conv2d(
1, out_channels=20, kernel_size=(10, 10), stride=(5, 5),
bias=False, padding=1,
input_transform=scale_input, weight_transform=scale_weight,
num_sends=3, wait_between_events=2, mock=mock)
self.fc1 = hxtorch.perceptron.nn.Linear(
5 * 5 * 20, 128, bias=False,
input_transform=scale_input, weight_transform=scale_weight,
num_sends=6, wait_between_events=2, mock=mock)
self.fc2 = hxtorch.perceptron.nn.Linear(
128, 10, bias=False,
input_transform=scale_input, weight_transform=scale_weight,
num_sends=6, wait_between_events=2, mock=mock)
def forward(self, *input): # pylint: disable=redefined-builtin
result = torch.nn.functional.relu(self.conv2d(input[0]))
result = result.reshape(-1, 5 * 5 * 20) # flatten
result = torch.nn.functional.relu(self.fc1(result))
result = self.fc2(result)
return result
class PyTorchModel(torch.nn.Module):
"""
The equivalent PyTorch model.
"""
def __init__(self):
super().__init__()
self.conv2d = torch.nn.Conv2d(1, 20, (10, 10), 5, 1, bias=False)
self.fc1 = torch.nn.Linear(5 * 5 * 20, 128, bias=False)
self.fc2 = torch.nn.Linear(128, 10, bias=False)
def forward(self, *input): # pylint: disable=redefined-builtin
result = torch.nn.functional.relu(self.conv2d(input[0]))
result = result.reshape(-1, 5 * 5 * 20) # flatten
result = torch.nn.functional.relu(self.fc1(result))
result = self.fc2(result)
return result
class MNISTTest(unittest.TestCase, metaclass=ABCMeta):
"""
Inference test on the MNIST dataset.
:cvar model_args: Arguments for the model
"""
model_args = {}
@property
@abstractmethod
def model_class(self):
raise NotImplementedError
def test_mnist(self) -> None:
"""
Run MNIST inference.
"""
data_path = Path(__file__).parent.joinpath("test_perceptron_mnist")
model = self.model_class(**self.model_args)
model.load_state_dict(
torch.load(data_path.joinpath("model_state.pkl")))
model.eval()
data = torch.load(data_path.joinpath("test_data.pt")).to(torch.float)
output = model(data)
predicted = output.argmax(dim=1)
target = torch.load(data_path.joinpath("test_labels.pt"))
accuracy = (100. * (predicted == target)).mean().item()
hxtorch.logger.get(f"{self.__class__.__name__}.test_perceptron_mnist").INFO(
f"Classified {len(data)} MNIST images, "
f"accuracy: {accuracy:.1f}% ({self.__class__.__name__[9:]})")
self.assertGreater(
accuracy, 80, "MNIST success is lower than usual.")
class MNISTTestHX(MNISTTest):
"""
Initializes and calibrates the chip.
Uses a pre-trained model to classify some MNIST images.
Asserts the success rate is as expected.
"""
model_args = {"mock": False}
model_class = HXTorchModel
@classmethod
def setUpClass(cls) -> None:
hxtorch.init_hardware(ann=True)
@classmethod
def tearDownClass(cls) -> None:
hxtorch.release_hardware() # also disconnects executor
class MNISTTestMock(MNISTTest):
"""
Tests the MNIST-example with the mock implementations.
"""
model_args = {"mock": True}
model_class = HXTorchModel
@classmethod
def setUpClass(cls) -> None:
hxtorch.perceptron.set_mock_parameter(
hxtorch.perceptron.MockParameter(noise_std=1.6, gain=0.0018))
class MNISTTestMockWithoutNoise(MNISTTest):
"""
Tests the MNIST-example with the mock implementations without added noise.
"""
model_args = {"mock": True}
model_class = HXTorchModel
@classmethod
def setUpClass(cls) -> None:
hxtorch.perceptron.set_mock_parameter(
hxtorch.perceptron.MockParameter(noise_std=0, gain=0.0018))
class MNISTTestPyTorch(MNISTTest):
"""
Tests the MNIST-example with pure pytorch layers.
"""
model_class = PyTorchModel
# keeps testrunners from instantiating the abstract base class
del MNISTTest
if __name__ == "__main__":
unittest.main()