This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
/
KNNClassifierRegion.py
1158 lines (962 loc) · 38.2 KB
/
KNNClassifierRegion.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2013-15, Numenta, Inc. Unless you have an agreement
# with Numenta, Inc., for a separate 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 Affero 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 Affero Public License for more details.
#
# You should have received a copy of the GNU Affero Public License
# along with this program. If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------
"""
## @file
This file defines the k Nearest Neighbor classifier region.
"""
import numpy
from nupic.bindings.regions.PyRegion import PyRegion
from nupic.algorithms import KNNClassifier
from nupic.bindings.math import Random
class KNNClassifierRegion(PyRegion):
"""
KNNClassifierRegion implements the k Nearest Neighbor classification algorithm.
By default it will implement vanilla 1-nearest neighbor using the L2 (Euclidean)
distance norm. There are options for using different norms as well as
various ways of sparsifying the input.
Note: categories are ints >= 0.
"""
__VERSION__ = 1
@classmethod
def getSpec(cls):
ns = dict(
description=KNNClassifierRegion.__doc__,
singleNodeOnly=True,
inputs=dict(
categoryIn=dict(
description='Vector of zero or more category indices for this input'
'sample. -1 implies no category.',
dataType='Real32',
count=0,
required=True,
regionLevel=True,
isDefaultInput=False,
requireSplitterMap=False),
bottomUpIn=dict(
description='Belief values over children\'s groups',
dataType='Real32',
count=0,
required=True,
regionLevel=False,
isDefaultInput=True,
requireSplitterMap=False),
partitionIn=dict(
description='Partition ID of the input sample',
dataType='Real32',
count=0,
required=True,
regionLevel=True,
isDefaultInput=False,
requireSplitterMap=False),
auxDataIn=dict(
description='Auxiliary data from the sensor',
dataType='Real32',
count=0,
required=False,
regionLevel=True,
isDefaultInput=False,
requireSplitterMap=False)
),
outputs=dict(
categoriesOut=dict(
description='A vector representing, for each category '
'index, the likelihood that the input to the node belongs '
'to that category based on the number of neighbors of '
'that category that are among the nearest K.',
dataType='Real32',
count=0,
regionLevel=True,
isDefaultOutput=True),
bestPrototypeIndices=dict(
description='A vector that lists, in descending order of '
'the match, the positions of the prototypes '
'that best match the input pattern.',
dataType='Real32',
count=0,
regionLevel=True,
isDefaultOutput=False),
categoryProbabilitiesOut=dict(
description='A vector representing, for each category '
'index, the probability that the input to the node belongs '
'to that category based on the distance to the nearest '
'neighbor of each category.',
dataType='Real32',
count=0,
regionLevel=True,
isDefaultOutput=True),
),
parameters=dict(
learningMode=dict(
description='Boolean (0/1) indicating whether or not a region '
'is in learning mode.',
dataType='UInt32',
count=1,
constraints='bool',
defaultValue=1,
accessMode='ReadWrite'),
inferenceMode=dict(
description='Boolean (0/1) indicating whether or not a region '
'is in inference mode.',
dataType='UInt32',
count=1,
constraints='bool',
defaultValue=0,
accessMode='ReadWrite'),
acceptanceProbability=dict(
description='During learning, inputs are learned with '
'probability equal to this parameter. '
'If set to 1.0, the default, '
'all inputs will be considered '
'(subject to other tests).',
dataType='Real32',
count=1,
constraints='',
defaultValue=1.0,
#accessMode='Create'),
accessMode='ReadWrite'), # and Create too
confusion=dict(
description='Confusion matrix accumulated during inference. '
'Reset with reset(). This is available to Python '
'client code only.',
dataType='Handle',
count=2,
constraints='',
defaultValue=None,
accessMode='Read'),
activeOutputCount=dict(
description='The number of active elements in the '
'"categoriesOut" output.',
dataType='UInt32',
count=1,
constraints='',
defaultValue=0,
accessMode='Read'),
categoryCount=dict(
description='An integer indicating the number of '
'categories that have been learned',
dataType='UInt32',
count=1,
constraints='',
defaultValue=None,
accessMode='Read'),
patternCount=dict(
description='Number of patterns learned by the classifier.',
dataType='UInt32',
count=1,
constraints='',
defaultValue=None,
accessMode='Read'),
patternMatrix=dict(
description='The actual patterns learned by the classifier, '
'returned as a matrix.',
dataType='Handle',
count=1,
constraints='',
defaultValue=None,
accessMode='Read'),
k=dict(
description='The number of nearest neighbors to use '
'during inference.',
dataType='UInt32',
count=1,
constraints='',
defaultValue=1,
accessMode='Create'),
maxCategoryCount=dict(
description='The maximal number of categories the '
'classifier will distinguish between.',
dataType='UInt32',
count=1,
constraints='',
defaultValue=2,
accessMode='Create'),
distanceNorm=dict(
description='The norm to use for a distance metric (i.e., '
'the "p" in Lp-norm)',
dataType='Real32',
count=1,
constraints='',
defaultValue=2.0,
accessMode='ReadWrite'),
#accessMode='Create'),
distanceMethod=dict(
description='Method used to compute distances between inputs and'
'prototypes. Possible options are norm, rawOverlap, '
'pctOverlapOfLarger, and pctOverlapOfProto',
dataType="Byte",
count=0,
constraints='enum: norm, rawOverlap, pctOverlapOfLarger, '
'pctOverlapOfProto, pctOverlapOfInput',
defaultValue='norm',
accessMode='ReadWrite'),
outputProbabilitiesByDist=dict(
description='If True, categoryProbabilitiesOut is the probability of '
'each category based on the distance to the nearest neighbor of '
'each category. If False, categoryProbabilitiesOut is the '
'percentage of neighbors among the top K that are of each category.',
dataType='UInt32',
count=1,
constraints='bool',
defaultValue=0,
accessMode='Create'),
distThreshold=dict(
description='Distance Threshold. If a pattern that '
'is less than distThreshold apart from '
'the input pattern already exists in the '
'KNN memory, then the input pattern is '
'not added to KNN memory.',
dataType='Real32',
count=1,
constraints='',
defaultValue=0.0,
accessMode='ReadWrite'),
inputThresh=dict(
description='Input binarization threshold, used if '
'"doBinarization" is True.',
dataType='Real32',
count=1,
constraints='',
defaultValue=0.5,
accessMode='Create'),
doBinarization=dict(
description='Whether or not to binarize the input vectors.',
dataType='UInt32',
count=1,
constraints='bool',
defaultValue=0,
accessMode='Create'),
useSparseMemory=dict(
description='A boolean flag that determines whether or '
'not the KNNClassifier will use sparse Memory',
dataType='UInt32',
count=1,
constraints='',
defaultValue=1,
accessMode='Create'),
minSparsity=dict(
description="If useSparseMemory is set, only vectors with sparsity"
" >= minSparsity will be stored during learning. A value"
" of 0.0 implies all vectors will be stored. A value of"
" 0.1 implies only vectors with at least 10% sparsity"
" will be stored",
dataType='Real32',
count=1,
constraints='',
defaultValue=0.0,
accessMode='ReadWrite'),
sparseThreshold=dict(
description='If sparse memory is used, input variables '
'whose absolute value is less than this '
'threshold will be stored as zero',
dataType='Real32',
count=1,
constraints='',
defaultValue=0.0,
accessMode='Create'),
relativeThreshold=dict(
description='Whether to multiply sparseThreshold by max value '
' in input',
dataType='UInt32',
count=1,
constraints='bool',
defaultValue=0,
accessMode='Create'),
winnerCount=dict(
description='Only this many elements of the input are '
'stored. All elements are stored if 0.',
dataType='UInt32',
count=1,
constraints='',
defaultValue=0,
accessMode='Create'),
doSphering=dict(
description='A boolean indicating whether or not data should'
'be "sphered" (i.e. each dimension should be normalized such'
'that its mean and variance are zero and one, respectively.) This'
' sphering normalization would be performed after all training '
'samples had been received but before inference was performed. '
'The dimension-specific normalization constants would then '
' be applied to all future incoming vectors prior to performing '
' conventional NN inference.',
dataType='UInt32',
count=1,
constraints='bool',
defaultValue=0,
accessMode='Create'),
SVDSampleCount=dict(
description='If not 0, carries out SVD transformation after '
'that many samples have been seen.',
dataType='UInt32',
count=1,
constraints='',
defaultValue=0,
accessMode='Create'),
SVDDimCount=dict(
description='Number of dimensions to keep after SVD if greater '
'than 0. If set to -1 it is considered unspecified. '
'If set to 0 it is consider "adaptive" and the number '
'is chosen automatically.',
dataType='Int32',
count=1,
constraints='',
defaultValue=-1,
accessMode='Create'),
fractionOfMax=dict(
description='The smallest singular value which is retained '
'as a fraction of the largest singular value. This is '
'used only if SVDDimCount==0 ("adaptive").',
dataType='UInt32',
count=1,
constraints='',
defaultValue=0,
accessMode='Create'),
useAuxiliary=dict(
description='Whether or not the classifier should use auxiliary '
'input data.',
dataType='UInt32',
count=1,
constraints='bool',
defaultValue=0,
accessMode='Create'),
justUseAuxiliary=dict(
description='Whether or not the classifier should ONLUY use the '
'auxiliary input data.',
dataType='UInt32',
count=1,
constraints='bool',
defaultValue=0,
accessMode='Create'),
verbosity=dict(
description='An integer that controls the verbosity level, '
'0 means no verbose output, increasing integers '
'provide more verbosity.',
dataType='UInt32',
count=1,
constraints='',
defaultValue=0 ,
accessMode='ReadWrite'),
keepAllDistances=dict(
description='Whether to store all the protoScores in an array, '
'rather than just the ones for the last inference. '
'When this parameter is changed from True to False, '
'all the scores are discarded except for the most '
'recent one.',
dataType='UInt32',
count=1,
constraints='bool',
defaultValue=None,
accessMode='ReadWrite'),
replaceDuplicates=dict(
description='A boolean flag that determines whether or'
'not the KNNClassifier should replace duplicates'
'during learning. This should be on when online'
'learning.',
dataType='UInt32',
count=1,
constraints='bool',
defaultValue=None,
accessMode='ReadWrite'),
cellsPerCol=dict(
description='If >= 1, we assume the input is organized into columns, '
'in the same manner as the temporal pooler AND '
'whenever we store a new prototype, we only store the '
'start cell (first cell) in any column which is bursting.'
'colum ',
dataType='UInt32',
count=1,
constraints='',
defaultValue=0,
accessMode='Create'),
maxStoredPatterns=dict(
description='Limits the maximum number of the training patterns '
'stored. When KNN learns in a fixed capacity mode, '
'the unused patterns are deleted once the number '
'of stored patterns is greater than maxStoredPatterns'
'columns. [-1 is no limit] ',
dataType='Int32',
count=1,
constraints='',
defaultValue=-1,
accessMode='Create'),
),
commands=dict()
)
return ns
def __init__(self,
maxCategoryCount=0,
bestPrototypeIndexCount=0,
outputProbabilitiesByDist=False,
k=1,
distanceNorm=2.0,
distanceMethod='norm',
distThreshold=0.0,
doBinarization=False,
inputThresh=0.500,
useSparseMemory=True,
sparseThreshold=0.0,
relativeThreshold=False,
winnerCount=0,
acceptanceProbability=1.0,
seed=42,
doSphering=False,
SVDSampleCount=0,
SVDDimCount=0,
fractionOfMax=0,
useAuxiliary=0,
justUseAuxiliary=0,
verbosity=0,
replaceDuplicates=False,
cellsPerCol=0,
maxStoredPatterns=-1,
minSparsity=0.0
):
self.version = KNNClassifierRegion.__VERSION__
# Convert various arguments to match the expectation
# of the KNNClassifier
if SVDSampleCount == 0:
SVDSampleCount = None
if SVDDimCount == -1:
SVDDimCount = None
elif SVDDimCount == 0:
SVDDimCount = 'adaptive'
if fractionOfMax == 0:
fractionOfMax = None
if useAuxiliary == 0:
useAuxiliary = False
if justUseAuxiliary == 0:
justUseAuxiliary = False
# KNN Parameters
self.knnParams = dict(
k=k,
distanceNorm=distanceNorm,
distanceMethod=distanceMethod,
distThreshold=distThreshold,
doBinarization=doBinarization,
binarizationThreshold=inputThresh,
useSparseMemory=useSparseMemory,
sparseThreshold=sparseThreshold,
relativeThreshold=relativeThreshold,
numWinners=winnerCount,
numSVDSamples=SVDSampleCount,
numSVDDims=SVDDimCount,
fractionOfMax=fractionOfMax,
verbosity=verbosity,
replaceDuplicates=replaceDuplicates,
cellsPerCol=cellsPerCol,
maxStoredPatterns=maxStoredPatterns,
minSparsity=minSparsity
)
# Initialize internal structures
self.outputProbabilitiesByDist = outputProbabilitiesByDist
self.learningMode = True
self.inferenceMode = False
self._epoch = 0
self.acceptanceProbability = acceptanceProbability
self._rgen = Random(seed)
self.confusion = numpy.zeros((1, 1))
self.keepAllDistances = False
self._protoScoreCount = 0
self._useAuxiliary = useAuxiliary
self._justUseAuxiliary = justUseAuxiliary
# Sphering normalization
self._doSphering = doSphering
self._normOffset = None
self._normScale = None
self._samples = None
self._labels = None
# Debugging
self.verbosity = verbosity
# Taps
self._tapFileIn = None
self._tapFileOut = None
self._initEphemerals()
self.maxStoredPatterns = maxStoredPatterns
self.maxCategoryCount = maxCategoryCount
self._bestPrototypeIndexCount = bestPrototypeIndexCount
def _getEphemeralAttributes(self):
"""
List of attributes to not save with serialized state.
"""
return ['_firstComputeCall', '_accuracy', '_protoScores',
'_categoryDistances']
def _initEphemerals(self):
"""
Initialize attributes that are not saved with the checkpoint.
"""
self._firstComputeCall = True
self._accuracy = None
self._protoScores = None
self._categoryDistances = None
self._knn = KNNClassifier.KNNClassifier(**self.knnParams)
for x in ('_partitions', '_useAuxiliary', '_doSphering',
'_scanInfo', '_protoScores'):
if not hasattr(self, x):
setattr(self, x, None)
def __setstate__(self, state):
"""Set state from serialized state."""
if 'version' not in state:
self.__dict__.update(state)
elif state['version'] == 1:
# Backward compatibility
if "doSelfValidation" in state:
state.pop("doSelfValidation")
knnState = state['_knn_state']
del state['_knn_state']
self.__dict__.update(state)
self._initEphemerals()
self._knn.__setstate__(knnState)
else:
raise RuntimeError("Invalid KNNClassifierRegion version for __setstate__")
# Set to current version
self.version = KNNClassifierRegion.__VERSION__
def __getstate__(self):
"""Get serializable state."""
state = self.__dict__.copy()
state['_knn_state'] = self._knn.__getstate__()
del state['_knn']
for field in self._getEphemeralAttributes():
del state[field]
return state
def initialize(self, dims, splitterMaps):
assert tuple(dims) == (1,) * len(dims)
def _getActiveOutputCount(self):
if self._knn._categoryList:
return int(max(self._knn._categoryList)+1)
else:
return 0
activeOutputCount = property(fget=_getActiveOutputCount)
def _getSeenCategoryCount(self):
return len(set(self._knn._categoryList))
categoryCount = property(fget=_getSeenCategoryCount)
def _getPatternMatrix(self):
if self._knn._M is not None:
return self._knn._M
else:
return self._knn._Memory
def _getAccuracy(self):
n = self.confusion.shape[0]
assert n == self.confusion.shape[1], "Confusion matrix is non-square."
return self.confusion[range(n), range(n)].sum(), self.confusion.sum()
accuracy = property(fget=_getAccuracy)
def clear(self):
self._knn.clear()
def getAlgorithmInstance(self):
"""Returns instance of the underlying KNNClassifier algorithm object."""
return self._knn
def getParameter(self, name, index=-1):
"""
Get the value of the parameter.
@param name -- the name of the parameter to retrieve, as defined
by the Node Spec.
"""
if name == "patternCount":
return self._knn._numPatterns
elif name == "patternMatrix":
return self._getPatternMatrix()
elif name == "k":
return self._knn.k
elif name == "distanceNorm":
return self._knn.distanceNorm
elif name == "distanceMethod":
return self._knn.distanceMethod
elif name == "distThreshold":
return self._knn.distThreshold
elif name == "inputThresh":
return self._knn.binarizationThreshold
elif name == "doBinarization":
return self._knn.doBinarization
elif name == "useSparseMemory":
return self._knn.useSparseMemory
elif name == "sparseThreshold":
return self._knn.sparseThreshold
elif name == "winnerCount":
return self._knn.numWinners
elif name == "relativeThreshold":
return self._knn.relativeThreshold
elif name == "SVDSampleCount":
v = self._knn.numSVDSamples
return v if v is not None else 0
elif name == "SVDDimCount":
v = self._knn.numSVDDims
return v if v is not None else 0
elif name == "fractionOfMax":
v = self._knn.fractionOfMax
return v if v is not None else 0
elif name == "useAuxiliary":
return self._useAuxiliary
elif name == "justUseAuxiliary":
return self._justUseAuxiliary
elif name == "doSphering":
return self._doSphering
elif name == "cellsPerCol":
return self._knn.cellsPerCol
elif name == "maxStoredPatterns":
return self.maxStoredPatterns
elif name == 'categoryRecencyList':
return self._knn._categoryRecencyList
else:
# If any spec parameter name is the same as an attribute, this call
# will get it automatically, e.g. self.learningMode
return PyRegion.getParameter(self, name, index)
def setParameter(self, name, index, value):
"""
Set the value of the parameter.
@param name -- the name of the parameter to update, as defined
by the Node Spec.
@param value -- the value to which the parameter is to be set.
"""
if name == "learningMode":
self.learningMode = bool(int(value))
self._epoch = 0
elif name == "inferenceMode":
self._epoch = 0
if int(value) and not self.inferenceMode:
self._finishLearning()
self.inferenceMode = bool(int(value))
elif name == "distanceNorm":
self._knn.distanceNorm = value
elif name == "distanceMethod":
self._knn.distanceMethod = value
elif name == "keepAllDistances":
self.keepAllDistances = bool(value)
if not self.keepAllDistances:
# Discard all distances except the latest
if self._protoScores is not None and self._protoScores.shape[0] > 1:
self._protoScores = self._protoScores[-1,:]
if self._protoScores is not None:
self._protoScoreCount = 1
else:
self._protoScoreCount = 0
elif name == "verbosity":
self.verbosity = value
self._knn.verbosity = value
else:
return PyRegion.setParameter(self, name, index, value)
def reset(self):
self.confusion = numpy.zeros((1, 1))
def doInference(self, activeInput):
"""Explicitly run inference on a vector that is passed in and return the
category id. Useful for debugging."""
prediction, inference, allScores = self._knn.infer(activeInput)
return inference
def enableTap(self, tapPath):
"""
Begin writing output tap files.
@param tapPath -- base name of the output tap files to write.
"""
self._tapFileIn = open(tapPath + '.in', 'w')
self._tapFileOut = open(tapPath + '.out', 'w')
def disableTap(self):
"""Disable writing of output tap files. """
if self._tapFileIn is not None:
self._tapFileIn.close()
self._tapFileIn = None
if self._tapFileOut is not None:
self._tapFileOut.close()
self._tapFileOut = None
def handleLogInput(self, inputs):
"""Write inputs to output tap file."""
if self._tapFileIn is not None:
for input in inputs:
for k in range(len(input)):
print >> self._tapFileIn, input[k],
print >> self._tapFileIn
def handleLogOutput(self, output):
"""Write outputs to output tap file."""
#raise Exception('MULTI-LINE DUMMY\nMULTI-LINE DUMMY')
if self._tapFileOut is not None:
for k in range(len(output)):
print >> self._tapFileOut, output[k],
print >> self._tapFileOut
def _storeSample(self, inputVector, trueCatIndex, partition=0):
"""
Store a training sample and associated category label
"""
# If this is the first sample, then allocate a numpy array
# of the appropriate size in which to store all samples.
if self._samples is None:
self._samples = numpy.zeros((0, len(inputVector)), dtype=RealNumpyDType)
assert self._labels is None
self._labels = []
# Add the sample vector and category lable
self._samples = numpy.concatenate((self._samples, numpy.atleast_2d(inputVector)), axis=0)
self._labels += [trueCatIndex]
# Add the partition ID
if self._partitions is None:
self._partitions = []
if partition is None:
partition = 0
self._partitions += [partition]
def compute(self, inputs, outputs):
"""
Process one input sample. This method is called by the runtime engine.
NOTE: the number of input categories may vary, but the array size is fixed
to the max number of categories allowed (by a lower region), so "unused"
indices of the input category array are filled with -1s.
TODO: confusion matrix does not support multi-label classification
"""
#raise Exception('MULTI-LINE DUMMY\nMULTI-LINE DUMMY')
#For backward compatibility
if self._useAuxiliary is None:
self._useAuxiliary = False
# If the first time being called, then print potential warning messsages
if self._firstComputeCall:
self._firstComputeCall = False
if self._useAuxiliary:
#print "\n Auxiliary input stream from Image Sensor enabled."
if self._justUseAuxiliary == True:
print " Warning: You have chosen to ignore the image data and instead just use the auxiliary data stream."
# Format inputs
#childInputs = [x.wvector(0) for x in inputs["bottomUpIn"]]
#inputVector = numpy.concatenate([x.array() for x in childInputs])
inputVector = inputs['bottomUpIn']
# Look for auxiliary input
if self._useAuxiliary==True:
#auxVector = inputs['auxDataIn'][0].wvector(0).array()
auxVector = inputs['auxDataIn']
if auxVector.dtype != numpy.float32:
raise RuntimeError, "KNNClassifierRegion expects numpy.float32 for the auxiliary data vector"
if self._justUseAuxiliary == True:
#inputVector = inputs['auxDataIn'][0].wvector(0).array()
inputVector = inputs['auxDataIn']
else:
#inputVector = numpy.concatenate([inputVector, inputs['auxDataIn'][0].wvector(0).array()])
inputVector = numpy.concatenate([inputVector, inputs['auxDataIn']])
# Logging
#self.handleLogInput(childInputs)
self.handleLogInput([inputVector])
# Read the category.
assert "categoryIn" in inputs, "No linked category input."
categories = inputs['categoryIn']
# Read the partition ID.
if "partitionIn" in inputs:
assert len(inputs["partitionIn"]) == 1, "Must have exactly one link to partition input."
partInput = inputs['partitionIn']
assert len(partInput) == 1, "Partition input element count must be exactly 1."
partition = int(partInput[0])
else:
partition = None
# ---------------------------------------------------------------------
# Inference (can be done simultaneously with learning)
if self.inferenceMode:
categoriesOut = outputs['categoriesOut']
probabilitiesOut = outputs['categoryProbabilitiesOut']
# If we are sphering, then apply normalization
if self._doSphering:
inputVector = (inputVector + self._normOffset) * self._normScale
nPrototypes = 0
if "bestPrototypeIndices" in outputs:
#bestPrototypeIndicesOut = outputs["bestPrototypeIndices"].wvector()
bestPrototypeIndicesOut = outputs["bestPrototypeIndices"]
nPrototypes = len(bestPrototypeIndicesOut)
winner, inference, protoScores, categoryDistances = \
self._knn.infer(inputVector, partitionId=partition)
if not self.keepAllDistances:
self._protoScores = protoScores
else:
# Keep all prototype scores in an array
if self._protoScores is None:
self._protoScores = numpy.zeros((1, protoScores.shape[0]),
protoScores.dtype)
self._protoScores[0,:] = protoScores#.reshape(1, protoScores.shape[0])
self._protoScoreCount = 1
else:
if self._protoScoreCount == self._protoScores.shape[0]:
# Double the size of the array
newProtoScores = numpy.zeros((self._protoScores.shape[0] * 2,
self._protoScores.shape[1]),
self._protoScores.dtype)
newProtoScores[:self._protoScores.shape[0],:] = self._protoScores
self._protoScores = newProtoScores
# Store the new prototype score
self._protoScores[self._protoScoreCount,:] = protoScores
self._protoScoreCount += 1
self._categoryDistances = categoryDistances
# --------------------------------------------------------------------
# Compute the probability of each category
if self.outputProbabilitiesByDist:
scores = 1.0 - self._categoryDistances
else:
scores = inference
# Probability is simply the scores/scores.sum()
total = scores.sum()
if total == 0:
numScores = len(scores)
probabilities = numpy.ones(numScores) / numScores
else:
probabilities = scores / total
# -------------------------------------------------------------------
# Fill the output vectors with our results
nout = min(len(categoriesOut), len(inference))
categoriesOut.fill(0)
categoriesOut[0:nout] = inference[0:nout]
probabilitiesOut.fill(0)
probabilitiesOut[0:nout] = probabilities[0:nout]
if self.verbosity >= 1:
print "KNNRegion: categoriesOut: ", categoriesOut[0:nout]
print "KNNRegion: probabilitiesOut: ", probabilitiesOut[0:nout]
if self._scanInfo is not None:
self._scanResults = [tuple(inference[:nout])]
# Update the stored confusion matrix.
for category in categories:
if category >= 0:
dims = max(category+1, len(inference))
oldDims = len(self.confusion)
if oldDims < dims:
confusion = numpy.zeros((dims, dims))
confusion[0:oldDims, 0:oldDims] = self.confusion
self.confusion = confusion
self.confusion[inference.argmax(), category] += 1
# Calculate the best prototype indices
if nPrototypes > 1:
bestPrototypeIndicesOut.fill(0)
if categoryDistances is not None:
indices = categoryDistances.argsort()
nout = min(len(indices), nPrototypes)
bestPrototypeIndicesOut[0:nout] = indices[0:nout]
elif nPrototypes == 1:
if (categoryDistances is not None) and len(categoryDistances):
bestPrototypeIndicesOut[0] = categoryDistances.argmin()
else:
bestPrototypeIndicesOut[0] = 0
# Logging