-
Notifications
You must be signed in to change notification settings - Fork 5
/
MPSupervisedGestureRecognizer.m
206 lines (155 loc) · 8.07 KB
/
MPSupervisedGestureRecognizer.m
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
//
// MPSupervisedGestureRecognizer.m
// MPGestures
//
// Created by Matias Piipari on 12/01/2014.
// Copyright (c) 2014 de.ur. All rights reserved.
//
#import "MPSupervisedGestureRecognizer.h"
#import "MPStrokeSequenceDatabase.h"
#import <MPRandomForest/MPALGLIBDecisionForestClassifier.h>
#import <MPRandomForest/MPDataTable.h>
#import <MPRandomForest/MPIdentityTransformer.h>
#import "MPStrokeSequenceDimensionMapper.h"
#import "MPPointCloud.h"
#import "MPStrokeSequenceRecognition.h"
#import "MPStrokeSequence.h"
#import <MPRandomForest/NSArray+MaxValue.h>
#import <OpenCV2/objdetect/objdetect.hpp>
@interface MPSupervisedGestureRecognizer ()
@property (readonly) MPStrokeSequenceDatabase *trainingDatabase;
@property (readonly) MPStrokeSequenceDatabase *referenceDatabase;
@end
@interface MPRandomForestGestureRecognizer ()
@property (readonly) MPALGLIBDecisionForestClassifier *classifier;
@property (readonly) MPStrokeSequenceDimensionMapper *dimensionMapper;
@end
@implementation MPSupervisedGestureRecognizer
- (id)init
{
@throw [NSException exceptionWithName:@"MPAbstractClassException" reason:nil userInfo:nil];
return nil;
}
- (instancetype)initWithTrainingDatabase:(MPStrokeSequenceDatabase *)trainingDatabase
referenceDatabase:(MPStrokeSequenceDatabase *)referenceDatabase
{
self = [super init];
if (self) {
_trainingDatabase = trainingDatabase;
_referenceDatabase = referenceDatabase;
}
return self;
}
- (NSArray *)labelValues {
return [[[_trainingDatabase strokeSequenceNameSet] allObjects] sortedArrayUsingSelector:@selector(compare:)];
}
- (MPStrokeSequenceRecognition *)recognizeStrokeSequence:(MPStrokeSequence *)sequence {
@throw [NSException exceptionWithName:@"MPAbstractMethodException" reason:nil userInfo:nil];
}
// FIXME: move evaluation code to the classifier.
- (NSArray *)evaluateRecognizerWithStrokeSequences:(NSArray *)strokeSequences
confusionMatrix:(id<MPDataSet> *)confusionMatrix
precision:(float *)precision {
NSMutableArray *recognitions = [NSMutableArray arrayWithCapacity:strokeSequences.count];
NSMutableArray *colTypes = [NSMutableArray arrayWithCapacity:self.labelValues.count];
NSMutableArray *confusions = [NSMutableArray arrayWithCapacity:self.labelValues.count];
NSUInteger correct = 0;
NSUInteger incorrect = 0;
for (NSUInteger i = 0, cnt = self.labelValues.count; i < cnt; i++) {
[colTypes addObject:@(MPColumnTypeIntegral)];
[confusions addObject:@(0)];
}
id<MPDataSet> confMatrix = [[MPDataTable alloc] initWithColumnTypes:[colTypes copy]
columnNames:self.labelValues
labelColumnIndex:NSNotFound
datumCapacity:strokeSequences.count];
for (NSUInteger i = 0, cnt = self.labelValues.count; i < cnt; i++) {
id<MPDatum> datum = [[MPDataTableRow alloc] initWithValues:confusions.copy columnTypes:confMatrix.columnTypes];
[confMatrix appendDatum:datum];
}
for (MPStrokeSequence *seq in strokeSequences) {
MPStrokeSequenceRecognition *recognition = [self recognizeStrokeSequence:seq];
recognition.correctName = seq.name;
[recognitions addObject:recognition];
// update confusion matrix for misclassifications.
if (![recognition.name isEqualToString:seq.name]) {
incorrect++;
} else {
correct++;
}
NSUInteger datumIndex = [self.labelValues indexOfObject:seq.name];
NSUInteger columnIndex = [self.labelValues indexOfObject:recognition.name];
id<MPDatum> datum = [confMatrix datumAtIndex:datumIndex];
NSUInteger val = [[datum valueForColumn:columnIndex] unsignedIntegerValue];
[datum setValue:@(val + 1) forColumn:columnIndex];
}
if (confusionMatrix)
*confusionMatrix = confMatrix;
if (precision)
*precision = (float)correct / (float)(correct + incorrect);
return recognitions;
}
@end
#pragma mark - Random forest gesture recognizer
@interface MPRandomForestGestureRecognizer ()
@property (readonly) id<MPTrainableDataSet> trainingDataSet;
@property (readonly) NSArray *referenceStrokeSequences;
@property (readwrite, getter = isTrained) BOOL trained;
@property (readwrite) NSUInteger strokeResampleRate;
@property (readwrite) NSUInteger maxStrokeSequencesPerName;
@end
@implementation MPRandomForestGestureRecognizer
- (instancetype)initWithTrainingDatabase:(MPStrokeSequenceDatabase *)trainingDatabase
referenceDatabase:(MPStrokeSequenceDatabase *)referenceDatabase
{
return [self initWithTrainingDatabase:trainingDatabase
referenceSequenceDatabase:referenceDatabase
maxReferenceSequencesPerLabel:0];
}
- (instancetype)initWithTrainingDatabase:(MPStrokeSequenceDatabase *)trainingDatabase
referenceSequenceDatabase:(MPStrokeSequenceDatabase *)referenceDatabase
maxReferenceSequencesPerLabel:(NSUInteger)maxSeqPerLabel
{
self = [super initWithTrainingDatabase:trainingDatabase referenceDatabase:referenceDatabase];
if (self) {
_strokeResampleRate = MPPointCloudDefaultResampleRate;
id<MPStrokeSequenceDataSet> trainingSequenceDataset = [trainingDatabase dataSetRepresentation];
// if there's a limit to stroke sequences per label, the reference database is swapped here to a database with this limit enforced.
if (maxSeqPerLabel > 0)
referenceDatabase = [[MPStrokeSequenceDatabase alloc] initWithStrokeSequenceDatabase:referenceDatabase
maxStrokeSequencesWithMatchingName:maxSeqPerLabel];
_referenceStrokeSequences = [referenceDatabase.strokeSequenceSet.allObjects sortedArrayUsingSelector:@selector(compare:)];
_dimensionMapper
= [[MPStrokeSequenceDimensionMapper alloc] initWithDataSet:trainingSequenceDataset
referenceStrokeSequences:_referenceStrokeSequences
resampleRate:_strokeResampleRate];
_trainingDataSet = [_dimensionMapper mappedDataSet];
#ifdef DEBUG
NSError *err = nil;
[[_trainingDataSet CSVRepresentationWithDelimiter:@"," quoteStrings:NO includeHeader:YES] writeToURL:[NSURL fileURLWithPath:@"/Users/mz2/Desktop/train.csv"] atomically:YES encoding:NSUTF8StringEncoding error:&err];
#endif
_classifier = [[MPALGLIBDecisionForestClassifier alloc]
initWithTransformer:[[MPIdentityTransformer alloc] initWithDataSet:_trainingDataSet]
trainingData:_trainingDataSet];
}
return self;
}
- (MPStrokeSequenceRecognition *)recognizeStrokeSequence:(MPStrokeSequence *)seq {
// FIXME: don't require creating a database just for the sake of creating a dataset representation?
MPStrokeSequenceDatabase *seqDB = [[MPStrokeSequenceDatabase alloc] initWithIdentifier:seq.name strokeSequence:seq];
MPStrokeSequenceDimensionMapper *mapper
= [[MPStrokeSequenceDimensionMapper alloc] initWithDataSet:[seqDB dataSetRepresentation]
referenceStrokeSequences:_referenceStrokeSequences
resampleRate:_strokeResampleRate];
id <MPTrainableDataSet> seqData = [mapper mappedDataSet];
assert(seqData.datumCount == 1);
NSArray *probs
= [_classifier posteriorProbabilitiesForClassifyingDatum:[seqData datumAtIndex:0]];
float maxValue = INFINITY;
NSUInteger index = [probs indexOfMaxFloatValue:&maxValue];
NSString *label = [_trainingDataSet labelValues][index];
MPStrokeSequenceRecognition *recognition = [[MPStrokeSequenceRecognition alloc] initWithName:label score:maxValue];
recognition.scores = probs;
return recognition;
}
@end