-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun_example.py
192 lines (159 loc) · 5.32 KB
/
run_example.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import os.path
import numpy as np
import pyhtm.htm as htm
from pyhtm.nodes.imagesensor import ImageSensor
from pyhtm.utils import extract_patches
from sklearn.neighbors import KNeighborsClassifier
#
# == Configuration ===
#
dataset = {
'name': 'Pictures: subset',
'path': 'pictures-subset',
'image_width': 32,
'image_height': 32,
'image_background': 1, # white
'image_mode': 'bw'
}
train_data = os.path.join('data', dataset['path'], 'train')
test_data = os.path.join('data', dataset['path'], 'test')
show_graphs = True
debug = True
verbose = True
sensor_params = {
'width': dataset['image_width'],
'height': dataset['image_height'],
'background': dataset['image_background'],
'mode': dataset['image_mode']
}
net_params = [
# Level 0
{
'nodeCloning': True,
'size': [8, 8],
# Spatial pooler
'maxCoincidenceCount': 200,
'spatialPoolerAlgorithm': 'gaussian',
'sigma': 1.2,
'maxDistance': 0.01,
# Temporal pooler
'requestedGroupsCount': 30,
'temporalPoolerAlgorithm': 'maxProp',
'transitionMemory': 4,
'symmetrizeTAM': True
},
# Level 1
{
'nodeCloning': True,
'size': [4, 4],
# Spatial pooler
'maxCoincidenceCount': 300,
'spatialPoolerAlgorithm': 'product', # dot
'sigma': 1.2,
'maxDistance': 0.001,
# Temporal pooler
'requestedGroupsCount': 40,
'temporalPoolerAlgorithm': 'maxProp',
'transitionMemory': 8,
}
]
learning_params = [
# Level 0
{'sp':
# Spatial pooler
{
# TODO bounding box problem!
'explorer': ['RandomSweep', {'sweepOffObject':False,
'sweepDirections': 'all'}],
# 'explorer': ['RandomJump', {'jumpOffObject': False}],
'numIterations': 300
},
'tp':
# Temporal pooler
{
'explorer': ['RandomSweep', {'sweepOffObject': False,
'sweepDirections': 'all'}],
# 'explorer': ['ExhaustiveSweep', {'sweepOffObject':False}],
'numIterations': 20000
},
},
# Level 1
{'sp':
{
'explorer': ['RandomSweep', {'sweepOffObject':False,
'sweepDirections': 'all'}],
# 'explorer': ['RandomJump', {'jumpOffObject': False}],
'numIterations': 300
},
'tp':
# Level 1: # Temporal pooler
{
'explorer': ['RandomSweep', {'sweepOffObject':False,
'sweepDirections': 'all'}],
'numIterations': 20000
},
}
]
# assuming square image and level size
patch_size = [sensor_params['width'] / net_params[0]['size'][0]] * 2
# creating sensor
sensor = ImageSensor(width=sensor_params['width'],
height=sensor_params['height'],
background=sensor_params['background'],
mode=sensor_params['mode'])
sensor.loadMultipleImages(train_data)
# create the network
htmNet = htm.HTM(sensor, debug=debug, show_graphs=show_graphs, verbose=verbose)
htmNet.create_network(net_params)
#
# == Learning ==
#
htmNet.learn(learning_params)
#
# == Testing ===
#
print('Testing...')
start_time = time.clock()
# getting training data
sensor.loadMultipleImages(train_data)
sensor.setParameter('explorer', 'Flash')
print(' Num of train images: %d' % sensor.getNumIterations())
train = [] # infered output of HTM for each of the original images
train_set_orig = [] # original training images
train_labels = [] # labels of training original images
for i in range(sensor.getNumIterations()):
data = sensor.compute()
im, cat = data['data'], data['category']
train_set_orig.append(im.reshape(np.prod(im.shape), ))
train_labels.append(cat)
out = htmNet.infer(extract_patches(im, patch_size), merge_output=True)
train.append(np.asarray(out))
# getting testing data
sensor.clearImageList()
sensor.loadMultipleImages(test_data)
sensor.setParameter('explorer', 'Flash')
print(' Num of test images: %d' % sensor.getNumIterations())
test = [] # infered output of HTM for each of the original images
test_set_orig = [] # original testing images
test_labels = [] # labels oftesting original images
for i in range(sensor.getNumIterations()):
data = sensor.compute()
im, cat = data['data'], data['category']
test_set_orig.append(im.reshape(np.prod(im.shape), ))
test_labels.append(cat)
out = htmNet.infer(extract_patches(im, patch_size), merge_output=True)
test.append(np.asarray(out))
# kNN in HTM space
knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(train, train_labels)
test_score = knn.score(test, test_labels)
# kNN in original space
knn_orig = KNeighborsClassifier(n_neighbors=1)
knn_orig.fit(train_set_orig, train_labels)
test_score_orig = knn_orig.score(test_set_orig, test_labels)
print('The test code ran for %.2fm' % ((time.clock() - start_time) / 60.))
print('Testing has completed with classification accuracy of %.4f %%. CA in original space is %.4f %%.'
% (test_score * 100., test_score_orig * 100.))