forked from scottpurdy/nupic.vision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_and_test.py
111 lines (90 loc) · 4.14 KB
/
train_and_test.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
#!/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 the spatial pooler (SP) on a set of images 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. Then its image
recognition abilities are tested on another set of images.
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 nupic.research.spatial_pooler import SpatialPooler as SpatialPooler
#from nupic.bindings.algorithms import SpatialPooler as SpatialPooler
from vision_testbench import VisionTestBench
from classifiers import KNNClassifier
from nupic.support.unittesthelpers.algorithm_test_helpers import convertSP
import numpy # just for debugging
trainingDataset = 'DataSets/OCR/characters/cmr_all.xml'
testingDataset = 'DataSets/OCR/characters/cmr_all.xml'
minAccuracy = 100.0 # force max training cycles
maxTrainingCycles = 5
if __name__ == "__main__":
# 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
#localAreaDensity = 0.02, # one percent of columns active at a time
#numActiveColumnsPerInhArea = -1, # Using percentage instead
numActiveColumnsPerInhArea = 64,
# All input activity can contribute to feature output
stimulusThreshold = 0,
synPermInactiveDec = 0.001,
synPermActiveInc = 0.001,
synPermConnected = 0.3,
minPctOverlapDutyCycle=0.001,
minPctActiveDutyCycle=0.001,
dutyCyclePeriod=1000,
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()
# Get training images and convert them to vectors.
trainingImages, trainingTags = data.getImagesAndTags(trainingDataset)
trainingVectors = encoder.imagesToVectors(trainingImages)
# 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, learn=True)
print "Number of training cycles:", numCycles