forked from scottpurdy/nupic.vision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameter_search.py
132 lines (104 loc) · 4.9 KB
/
parameter_search.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
#!/usr/bin/env python
# ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2015, Numenta, Inc. Unless you have purchased from
# Numenta, Inc. a separate commercial license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------
"""
This script trains and tests the spatial pooler (SP) over and over while varying
some SP parameters. For each set of SP parameter values it trains the spatial
pooler (SP) on a set of training images and tests its image recognition
abilities on a set of test images.
During training the SP is trained until it achieves either a minimum specified
image recognition accuracy on the training data set or until a maximum number of
training cycles is reached.
After training and testing is completed for all combinations of the parameter
values, a summary of the results is displayed.
trainingDataset - name of XML file that lists the training images
testingDataset - name of XML file that lists the testing images
minAccuracy - minimum accuracy requred to stop training before
maxTrainingCycles is reached
maxTrainingCycles - maximum number of training cycles to perform
"""
import dataset_readers as data
import image_encoders as encoder
from parameters import Parameters
from nupic.research.spatial_pooler import SpatialPooler
from vision_testbench import VisionTestBench
from classifiers import KNNClassifier
trainingDataset = 'DataSets/OCR/characters/cmr_hex.xml'
minAccuracy = 100.0
maxTrainingCycles = 5
testingDataset = 'DataSets/OCR/characters/cmr_hex.xml'
if __name__ == "__main__":
# Get training images and convert them to vectors.
trainingImages, trainingTags = data.getImagesAndTags(trainingDataset)
trainingVectors = encoder.imagesToVectors(trainingImages)
# Specify parameter values to search
parameters = Parameters()
parameters.define("synPermConn", [0.5])
parameters.define("synPermDecFrac", [1.0, 0.5, 0.1])
parameters.define("synPermIncFrac", [1.0, 0.5, 0.1])
# Run the model until all combinations have been tried
while parameters.getNumResults() < parameters.numCombinations:
# Pick a combination of parameter values
parameters.nextCombination()
#parameters.nextRandomCombination()
synPermConn = parameters.getValue("synPermConn")
synPermDec = synPermConn*parameters.getValue("synPermDecFrac")
synPermInc = synPermConn*parameters.getValue("synPermIncFrac")
# Instantiate our spatial pooler
sp = SpatialPooler(
inputDimensions= (32, 32), # Size of image patch
columnDimensions = (32, 32),
potentialRadius = 10000, # Ensures 100% potential pool
potentialPct = 0.8,
globalInhibition = True,
localAreaDensity = -1, # Using numActiveColumnsPerInhArea
numActiveColumnsPerInhArea = 64,
# All input activity can contribute to feature output
stimulusThreshold = 0,
synPermInactiveDec = synPermDec,
synPermActiveInc = synPermInc,
synPermConnected = synPermConn,
maxBoost = 1.0,
seed = 1956, # The seed that Grok uses
spVerbosity = 1)
# Instantiate the spatial pooler test bench.
tb = VisionTestBench(sp)
# Instantiate the classifier
clf = KNNClassifier()
# Train the spatial pooler on trainingVectors.
numCycles = tb.train(trainingVectors, trainingTags, clf, maxTrainingCycles,
minAccuracy)
# Save the permanences and connections after training.
#tb.savePermanences('perms.jpg')
#tb.showPermanences()
#tb.showConnections()
# Get testing images and convert them to vectors.
testingImages, testingTags = data.getImagesAndTags(testingDataset)
testingVectors = encoder.imagesToVectors(testingImages)
# Reverse the order of the vectors and tags for testing
testingTags = [testingTag for testingTag in reversed(testingTags)]
testingVectors = [testingVector for testingVector in reversed(testingVectors)]
# Test the spatial pooler on testingVectors.
accuracy = tb.test(testingVectors, testingTags, clf)
# Add results to the list
parameters.appendResults([accuracy, numCycles])
parameters.printResults(["Percent Accuracy", "Training Cycles"], [", %.2f", ", %d"])
print "The maximum number of training cycles is set to:", maxTrainingCycles