-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmozafari_mnist_original.py
196 lines (156 loc) · 7.61 KB
/
mozafari_mnist_original.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import os, spyker, torch, numpy
from sklearn.svm import LinearSVC
from torch.utils.data import TensorDataset, DataLoader
from spyker import DoGFilter as F
def dataset(root, device, batch):
device = torch.device(device.kind)
trainx, trainy, testx, testy = spyker.read_mnist(
root+'/train-images-idx3-ubyte', root+'/train-labels-idx1-ubyte',
root+ '/t10k-images-idx3-ubyte', root+ '/t10k-labels-idx1-ubyte')
trainx, trainy, testx, testy = spyker.to_torch(trainx, trainy, testx, testy)
trainx = trainx.div(255).to(torch.float32).to(device)
testx = testx.div(255).to(torch.float32).to(device)
train = DataLoader(TensorDataset(trainx, trainy), batch_size=batch)
test = DataLoader(TensorDataset(testx, testy), batch_size=batch)
return train, test
def update(config, mult, limit):
ratio = config.negative / config.positive
config.positive = min(config.positive * mult, limit)
config.negative = config.positive * ratio
def total(network, transform, dataset):
total_data, total_target = [], []
for data, target in dataset:
data = network(transform(data))
total_data.append(data.cpu())
total_target.append(target)
return torch.cat(total_data), torch.cat(total_target)
def perform(labels, target):
perf = numpy.zeros(4)
silent = (labels == -1).sum().item()
perf[0] = (labels == target).sum().item()
perf[1] = (labels != target).sum().item() - silent
perf[2] = silent
perf[3] = len(target)
return perf
def test(network, transform, dataset):
perf = numpy.zeros(4)
for data, target in dataset:
perf += perform(network(transform(data)), target)
return perf
class Transform:
def __init__(self, device):
filters = [F(3/9, 6/9), F(6/9, 3/9), F(7/9, 14/9),
F(14/9, 7/9), F(13/9, 26/9), F(26/9, 13/9)]
self.filters = spyker.DoG(3, filters, pad=3, device=device)
def __call__(self, array):
return spyker.code(spyker.threshold(self.filters(array), .02), 15)
class Network:
def __init__(self, device):
self.count1, self.count2, self.thresh1, self.thresh2 = 0, 0, 15, 5
self.conv1 = spyker.Conv(6, 30, 5, pad=2, mean=.8, std=.05, device=device)
self.conv2 = spyker.Conv(30, 250, 3, pad=1, mean=.8, std=.05, device=device)
self.conv3 = spyker.Conv(250, 200, 5, pad=2, mean=.8, std=.05, device=device)
self.conv1.stdpconfig = [spyker.STDPConfig(.004, -.003)]
self.conv2.stdpconfig = [spyker.STDPConfig(.004, -.003)]
reward = spyker.STDPConfig(.004, -.003, False, .2, .8)
punish = spyker.STDPConfig(-.004, .0005, False, .2, .8)
self.conv3.stdpconfig = [reward, punish]
self.wta1 = lambda x: spyker.convwta(x, 3, 5)
self.wta2 = lambda x: spyker.convwta(x, 1, 8)
self.wta3 = lambda x: spyker.convwta(x, 0, 1)
def train_layer1(self, array):
output = spyker.inhibit(spyker.threshold(self.conv1(array), self.thresh1))
self.conv1.stdp(array, self.wta1(output), spyker.fire(output))
self.count1 += array.size(0)
if self.count1 > 500:
self.count1 -= 500
update(self.conv1.stdpconfig[0], 2, .15)
def train_layer2(self, array):
array = spyker.pool(spyker.fire(self.conv1(array), self.thresh1), 2)
output = spyker.inhibit(spyker.threshold(self.conv2(array), self.thresh2))
self.conv2.stdp(array, self.wta2(output), spyker.fire(output))
self.count2 += array.size(0)
if self.count2 > 500:
self.count2 -= 500
update(self.conv2.stdpconfig[0], 2, .15)
def train_layer3(self, array, target):
array = spyker.pool(spyker.fire(self.conv1(array), self.thresh1), 2)
array = spyker.pool(spyker.fire(self.conv2(array), self.thresh2), 3)
output = spyker.infinite(self.conv3(array))
winners = self.wta3(output)
labels = torch.zeros(len(winners), dtype=torch.long).fill_(-1)
for i in range(len(winners)):
if len(winners[i]) == 1:
labels[i] = winners[i][0].z // (output.size(2) // 10)
winners[i][0].c = labels[i] != target[i]
output = spyker.fire(output)
self.conv3.stdp(array, winners, output)
return labels
def __call__(self, array):
array = spyker.pool(spyker.fire(self.conv1(array), self.thresh1), 2)
array = spyker.pool(spyker.fire(self.conv2(array), self.thresh2), 3)
array = spyker.infinite(self.conv3(array))
winners = self.wta3(array)
labels = torch.zeros(len(winners), dtype=torch.long).fill_(-1)
for i in range(len(winners)):
if len(winners[i]) == 1:
labels[i] = winners[i][0].z // (array.size(2) // 10)
return labels
def save(self, path):
kernel1 = spyker.to_numpy(self.conv1.kernel)
kernel2 = spyker.to_numpy(self.conv2.kernel)
kernel3 = spyker.to_numpy(self.conv3.kernel)
numpy.savez(path, conv1_kernel=kernel1, conv2_kernel=kernel2, conv3_kernel=kernel3)
def load(self, path):
data = numpy.load(path)
spyker.to_tensor(data['conv1_kernel']).to(self.conv1.kernel)
spyker.to_tensor(data['conv2_kernel']).to(self.conv2.kernel)
spyker.to_tensor(data['conv3_kernel']).to(self.conv3.kernel)
if __name__ == "__main__":
batch_size = 64
data_root = './MNIST/'
model_path = 'mozafari_mnist_original.npz'
device = spyker.device('cuda' if spyker.cuda_available() else 'cpu')
network = Network(device)
transform = Transform(device)
trainset, testset = dataset(data_root, device, batch_size)
if not os.path.isfile(model_path):
for i in range(2):
print(f'Training first layer iteration: {i+1}')
for data, target in trainset:
network.train_layer1(transform(data))
for i in range(4):
print(f'Training second layer iteration: {i+1}')
for data, target in trainset:
network.train_layer2(transform(data))
train_max, test_max = .0, .0
rpos = network.conv3.stdpconfig[0].positive
rneg = network.conv3.stdpconfig[0].negative
ppos = network.conv3.stdpconfig[1].positive
pneg = network.conv3.stdpconfig[1].negative
for i in range(680):
print(f'Training third layer iteration: {i+1}')
perf = numpy.zeros(4)
perf_batch = numpy.zeros(4)
for data, target in trainset:
labels = network.train_layer3(transform(data), target)
temp = perform(labels, target)
perf_batch += temp
perf += temp
if perf_batch[3] % 1024 == 0: # Note: BATCH must be in the form 2^N
network.conv3.stdpconfig[0].positive = rpos * (perf_batch[1] / perf_batch[3])
network.conv3.stdpconfig[0].negative = rneg * (perf_batch[1] / perf_batch[3])
network.conv3.stdpconfig[1].positive = ppos * (perf_batch[0] / perf_batch[3])
network.conv3.stdpconfig[1].negative = pneg * (perf_batch[0] / perf_batch[3])
perf_batch = numpy.zeros(4)
train_max = max(train_max, perf[0] / perf[3] * 100)
perf = test(network, transform, testset)
test_now = perf[0] / perf[3] * 100
if (test_now > test_max):
print(f'Saving model with accuracy {test_now} to: {model_path}')
network.save(model_path)
test_max = test_now
print(f'Loading model from: {model_path}')
network.load(model_path)
perf = test(network, transform, testset)
print(f'Accuracy: {perf[0]/perf[3]*100}')