-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathClassificationModel.mm
54 lines (41 loc) · 1.5 KB
/
ClassificationModel.mm
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
#import "ClassificationModel.h"
#import "../../utils/ImageProcessor.h"
#import "../../utils/Numerical.h"
#import "Constants.h"
#import "opencv2/opencv.hpp"
@implementation ClassificationModel
- (cv::Size)getModelImageSize {
NSArray *inputShape = [module getInputShape:0];
NSNumber *widthNumber = inputShape.lastObject;
NSNumber *heightNumber = inputShape[inputShape.count - 2];
int height = [heightNumber intValue];
int width = [widthNumber intValue];
return cv::Size(height, width);
}
- (NSArray *)preprocess:(cv::Mat &)input {
cv::Size modelImageSize = [self getModelImageSize];
cv::resize(input, input, modelImageSize);
NSArray *modelInput = [ImageProcessor matToNSArray:input];
return modelInput;
}
- (NSDictionary *)postprocess:(NSArray *)output {
output = output[0]; // take the first output tensor
std::vector<double> outputVector(output.count);
for (NSUInteger i = 0; i < output.count; ++i) {
outputVector[i] = [output[i] doubleValue];
}
std::vector<double> probabilities = softmax(outputVector);
NSMutableDictionary *result = [NSMutableDictionary dictionary];
for (int i = 0; i < probabilities.size(); ++i) {
NSString *className = @(imagenet1k_v1_labels[i].c_str());
NSNumber *probability = @(probabilities[i]);
result[className] = probability;
}
return result;
}
- (NSDictionary *)runModel:(cv::Mat &)input {
NSArray *modelInput = [self preprocess:input];
NSArray *modelOutput = [self forward:modelInput];
return [self postprocess:modelOutput];
}
@end