-
Notifications
You must be signed in to change notification settings - Fork 1
/
objcWrapper.mm
41 lines (33 loc) · 1.46 KB
/
objcWrapper.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
#import <CoreML/CoreML.h>
#import <Accelerate/Accelerate.h>
#import "objcWrapper.h"
#import "coreml/CoremlEncoder.h"
#include <stdlib.h>
#if __cplusplus
extern "C" {
#endif
const void* loadModel(const char* modelPath) {
NSString* modelPathStr = [[NSString alloc] initWithUTF8String:modelPath];
NSURL* modelURL = [NSURL fileURLWithPath: modelPathStr];
const void* model = CFBridgingRetain([[CoremlEncoder alloc] initWithContentsOfURL:modelURL error:nil]);
return model;
}
void predictWith(const void* model, float* melSegment, float* encoderOutput) {
MLMultiArray *inMultiArray = [[MLMultiArray alloc] initWithDataPointer: melSegment
shape: @[@1, @80, @3000]
dataType: MLMultiArrayDataTypeFloat32
strides: @[@(240000), @(3000), @1]
deallocator: nil
error: nil];
CoremlEncoderOutput *modelOutput = [(__bridge id)model predictionFromMelSegment:inMultiArray error:nil];
MLMultiArray *outMA = modelOutput.output;
cblas_scopy((int)outMA.count,
(float*)outMA.dataPointer, 1,
encoderOutput, 1);
}
void closeModel(const void* model) {
CFRelease(model);
}
#if __cplusplus
} //Extern C
#endif