forked from scottpurdy/nupic.vision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
86 lines (73 loc) · 3.36 KB
/
demo.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
#! /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 that are
listed in the XML file specified by trainingDataset. The SP is trained
for a maximum number of training cycles given by maxTrainingCycles and then its
classification abilities are tested on the images listed in the XML file
specified by testingDataset.
"""
trainingDataset = "DataSets/OCR/characters/cmr_hex.xml"
maxTrainingCycles = 20
testingDataset = "DataSets/OCR/characters/cmr_hex.xml"
import dataset_readers as data
import image_encoders as encoder
from nupic.research.spatial_pooler import SpatialPooler
from vision_testbench import VisionTestBench
from classifiers import exactMatch
if __name__ == "__main__":
# Get training images and convert them to vectors.
trainingImages, trainingTags = data.getImagesAndTags(trainingDataset)
trainingVectors = encoder.imagesToVectors(trainingImages)
# Instantiate the python spatial pooler
sp = SpatialPooler(
inputDimensions = 32**2, # Size of image patch
columnDimensions = 16, # Number of potential features
potentialRadius = 10000, # Ensures 100% potential pool
potentialPct = 1, # Neurons can connect to 100% of input
globalInhibition = True,
localAreaDensity = -1, # Using numActiveColumnsPerInhArea
#localAreaDensity = 0.02, # one percent of columns active at a time
#numActiveColumnsPerInhArea = -1, # Using percentage instead
numActiveColumnsPerInhArea = 1, # Only one feature active at a time
# All input activity can contribute to feature output
stimulusThreshold = 0,
synPermInactiveDec = 0.3,
synPermActiveInc = 0.3,
synPermConnected = 0.3, # Connected threshold
maxBoost = 2,
seed = 1956, # The seed that Grok uses
spVerbosity = 1)
# Instantiate the spatial pooler test bench.
tb = VisionTestBench(sp)
# Instantiate the classifier
clf = exactMatch()
# Train the spatial pooler on trainingVectors.
numCycles = tb.train(trainingVectors, trainingTags, clf, maxTrainingCycles)
# View the permanences and connections after training.
tb.showPermsAndConns()
#tb.savePermsAndConns('perms_and_conns.jpg')
# Get testing images and convert them to vectors.
testingImages, testingTags = data.getImagesAndTags(testingDataset)
testingVectors = encoder.imagesToVectors(testingImages)
# Test the spatial pooler on testingVectors.
accuracy = tb.test(testingVectors, testingTags, clf, verbose=1)