diff --git a/metal/MobileNetDemo/MobileNetDemo/MobileNet.swift b/metal/MobileNetDemo/MobileNetDemo/MobileNet.swift index 9a50f3db8d7d1b57853d5ba6893e4af14879173c..5904af7881a7f3ffffc06956ef4ade9893420cd9 100644 --- a/metal/MobileNetDemo/MobileNetDemo/MobileNet.swift +++ b/metal/MobileNetDemo/MobileNetDemo/MobileNet.swift @@ -42,8 +42,9 @@ public class MobileNet: Net{ let labels = PreWords.init(fileName: "synset") - override public func resultStr(res: ResultHolder) -> String { - let resPointer = res.result + override public func resultStr(res: [ResultHolder]) -> String { + let firstRes = res[0] + let resPointer = firstRes.result var s: [String] = [] (0.. String { - return " \(res.result[0]) ... " + override public func resultStr(res: [ResultHolder]) -> String { + return " \(res[0].result[0]) ... " } } diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/Net/MobileNet.swift b/metal/paddle-mobile-demo/paddle-mobile-demo/Net/MobileNet.swift index 02fcbefe0b265a3f5898bb23956014183c95fc5e..e15befc6fb1ef7260c242e0d3fef48e0fa7b8844 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/Net/MobileNet.swift +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/Net/MobileNet.swift @@ -43,8 +43,8 @@ public class MobileNet: Net{ let labels = PreWords.init(fileName: "synset") - override public func resultStr(res: ResultHolder) -> String { - let resPointer = res.result + override public func resultStr(res: [ResultHolder]) -> String { + let resPointer = res[0].result var s: [String] = [] (0.. String { - return " \(res.result[0]) ... " + override public func resultStr(res: [ResultHolder]) -> String { + return " \(res[0].result[0]) ... " } } diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/Net/MobileNetSSD.swift b/metal/paddle-mobile-demo/paddle-mobile-demo/Net/MobileNetSSD.swift index 280a0507810a27c3d5b220789b74ef7358a5d215..41d2c82644b4b190dbf6f1e7c14029b8cad10897 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/Net/MobileNetSSD.swift +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/Net/MobileNetSSD.swift @@ -45,11 +45,11 @@ public class MobileNet_ssd_hand: Net { } } - override public func resultStr(res: ResultHolder) -> String { - return " \(res)" + override public func resultStr(res: [ResultHolder]) -> String { + return " \(res[0])" } - override public func fetchResult(paddleMobileRes: GPUResultHolder) -> ResultHolder { + override public func fetchResult(paddleMobileRes: [GPUResultHolder]) -> [ResultHolder] { // guard let interRes = paddleMobileRes.intermediateResults else { // fatalError(" need have inter result ") diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/Net/MobilenetSSD_AR.swift b/metal/paddle-mobile-demo/paddle-mobile-demo/Net/MobilenetSSD_AR.swift index 3080dfb469774aa2aa9bd0ed58b9c5d03c52ca5f..60aa3adf0706f8bd2f7f958834fbdd64698ad319 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/Net/MobilenetSSD_AR.swift +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/Net/MobilenetSSD_AR.swift @@ -39,11 +39,11 @@ public class MobileNet_ssd_AR: Net { } } - override public func resultStr(res: ResultHolder) -> String { - return " \(res.result[0])" + override public func resultStr(res: [ResultHolder]) -> String { + return " \(res[0].result[0])" } - override public func fetchResult(paddleMobileRes: GPUResultHolder) -> ResultHolder { + override public func fetchResult(paddleMobileRes: [GPUResultHolder]) -> [ResultHolder] { fatalError() // guard let interRes = paddleMobileRes.intermediateResults else { // fatalError(" need have inter result ") diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/Net/YoloNet.swift b/metal/paddle-mobile-demo/paddle-mobile-demo/Net/YoloNet.swift index 5017d3274da34459dfd26060e3c3598b76ba8d31..7aeabb0f730caf21292077158d5af37a5abdf6a8 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/Net/YoloNet.swift +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/Net/YoloNet.swift @@ -27,8 +27,8 @@ public class YoloNet: Net { // metalLibPath = Bundle.main.path(forResource: "PaddleMobileMetal", ofType: "metallib") ?! " can't be nil " } - override public func resultStr(res: ResultHolder) -> String { - return " \(res.result[0]) ... " + override public func resultStr(res: [ResultHolder]) -> String { + return " \(res[0].result[0]) ... " } } diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/OCInterface/PaddleMobileGPU.h b/metal/paddle-mobile-demo/paddle-mobile-demo/OCInterface/PaddleMobileGPU.h index 683ef3272dddf6f65a52730493bf7a3d12b77c4d..cd99ddad430902d1403c0b2e1752c15fa4f21722 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/OCInterface/PaddleMobileGPU.h +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/OCInterface/PaddleMobileGPU.h @@ -26,6 +26,8 @@ typedef enum : NSUInteger { @property (assign, nonatomic) int outputSize; +@property (strong, nonatomic) NSArray *dim; + -(void)releaseOutput; @end @@ -88,13 +90,13 @@ typedef enum : NSUInteger { * texture: 需要进行预测的图像转换的 texture * completion: 预测完成回调 */ --(void)predict:(id)texture withCompletion:(void (^)(BOOL, NSArray *))completion; +-(void)predict:(id)texture withCompletion:(void (^)(BOOL, NSArray*> *))completion; /* * texture: 需要进行预测的图像转换的 texture * completion: 预测完成回调 */ --(void)predict:(id)texture withResultCompletion:(void (^)(BOOL, PaddleMobileGPUResult *))completion; +-(void)predict:(id)texture withResultCompletion:(void (^)(BOOL, NSArray *))completion; /* * 清理内存 diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/OCInterface/PaddleMobileGPU.m b/metal/paddle-mobile-demo/paddle-mobile-demo/OCInterface/PaddleMobileGPU.m index 7625788b534a0c4f4dedb4664efe8b47f6bb1eb3..3a58db2988f8ba70a02d96f4e7e92b49d61305bd 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/OCInterface/PaddleMobileGPU.m +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/OCInterface/PaddleMobileGPU.m @@ -66,24 +66,34 @@ return [runner load]; } --(void)predict:(id)texture withCompletion:(void (^)(BOOL, NSArray *))completion { +-(void)predict:(id)texture withCompletion:(void (^)(BOOL, NSArray*> *))completion { - [runner predictWithTexture:texture completion:^(BOOL success, ResultHolder * _Nullable result) { - NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:result.capacity]; - for (int i = 0; i < result.capacity; ++i) { - [resultArray addObject:[NSNumber numberWithFloat:result.result[i]]]; + [runner predictWithTexture:texture completion:^(BOOL success, NSArray * _Nullable resultArr) { + NSMutableArray*> *ocResultArray = [NSMutableArray arrayWithCapacity:resultArr.count]; + for (int i = 0; i < resultArr.count; ++i) { + ResultHolder *resultHolder = resultArr[i]; + NSMutableArray *res = [NSMutableArray arrayWithCapacity:resultHolder.capacity]; + for (int j = 0; j < resultHolder.capacity; ++j) { + [res addObject:[NSNumber numberWithFloat:resultHolder.result[i]]]; + } + [ocResultArray addObject:res]; + [resultHolder releasePointer]; } - completion(success, resultArray); - [result releasePointer]; - + completion(success, ocResultArray); }]; } --(void)predict:(id)texture withResultCompletion:(void (^)(BOOL, PaddleMobileGPUResult *))completion { - [runner predictWithTexture:texture completion:^(BOOL success, ResultHolder * _Nullable result) { - PaddleMobileGPUResult *gpuResult = [[PaddleMobileGPUResult alloc] init]; - [gpuResult setOutputResult:result]; - completion(success, gpuResult); +-(void)predict:(id)texture withResultCompletion:(void (^)(BOOL, NSArray *))completion { + [runner predictWithTexture:texture completion:^(BOOL success, NSArray * _Nullable resultArr) { + NSMutableArray *ocResultArr = [NSMutableArray arrayWithCapacity:resultArr.count]; + for (int i = 0; i < resultArr.count; ++i) { + ResultHolder *result = resultArr[i]; + PaddleMobileGPUResult *gpuResult = [[PaddleMobileGPUResult alloc] init]; + gpuResult.dim = result.dim; + [gpuResult setOutputResult:result]; + [ocResultArr addObject:gpuResult]; + } + completion(success, ocResultArr); }]; } diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/OCInterface/SuperResolutionNet.swift b/metal/paddle-mobile-demo/paddle-mobile-demo/OCInterface/SuperResolutionNet.swift index 284612a04df57ffae78d59827547d8d26826b019..8745efaed04a09f2d92bc2c1bcbc639ec261297d 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/OCInterface/SuperResolutionNet.swift +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/OCInterface/SuperResolutionNet.swift @@ -16,7 +16,7 @@ import Foundation import paddle_mobile @objc public class SuperResolutionNet: Net{ - override public func resultStr(res: ResultHolder) -> String { + override public func resultStr(res: [ResultHolder]) -> String { return "未实现" } diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift b/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift index 5d40da577121ffb7c6481831efe7b11e36696d81..612a986d85994a03f45b3f6641e1851784d35787 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift @@ -147,7 +147,8 @@ class ViewController: UIViewController { fatalError() } - if success, let inResultHolder = resultHolder { + if success, let inResultHolderArr = resultHolder { + let inResultHolder = inResultHolderArr[0] if i == max - 1 { let time = Date.init().timeIntervalSince(startDate) @@ -160,7 +161,7 @@ class ViewController: UIViewController { } DispatchQueue.main.async { - resultHolder?.releasePointer() + resultHolder?.first?.releasePointer() } } } @@ -272,7 +273,7 @@ extension ViewController: VideoCaptureDelegate{ runner.scaleTexture(input: texture) { (scaledTexture) in self.runner.predict(texture: scaledTexture, completion: { (success, resultHolder) in // print(resultHolder!.result![0]) - resultHolder?.releasePointer() + resultHolder?.first?.releasePointer() }) } } diff --git a/metal/paddle-mobile/paddle-mobile/API/Net.swift b/metal/paddle-mobile/paddle-mobile/API/Net.swift index 6eb7732410a54829c2633bc6cb3449ef25b5ec9d..cc8a4c6ca2c132de3a96555f9c360c932fa12142 100644 --- a/metal/paddle-mobile/paddle-mobile/API/Net.swift +++ b/metal/paddle-mobile/paddle-mobile/API/Net.swift @@ -71,17 +71,20 @@ import Foundation super.init() } - @objc open func resultStr(res: ResultHolder) -> String { + @objc open func resultStr(res: [ResultHolder]) -> String { fatalError() } - @objc open func fetchResult(paddleMobileRes: GPUResultHolder) -> ResultHolder { - guard let inResPointer = paddleMobileRes.resultPointer else { - fatalError() + @objc open func fetchResult(paddleMobileRes: [GPUResultHolder]) -> [ResultHolder] { + return paddleMobileRes.map { (gpuRes) -> ResultHolder in + guard let inResPointer = gpuRes.resultPointer else { + fatalError() + } + return ResultHolder.init(inResult: inResPointer, inCapacity: paddleMobileRes.capacity, inDim: gpuRes.dim) } - return ResultHolder.init(inResult: inResPointer, inCapacity: paddleMobileRes.capacity) } open func updateProgram(program: Program) { } + } diff --git a/metal/paddle-mobile/paddle-mobile/API/Runner.swift b/metal/paddle-mobile/paddle-mobile/API/Runner.swift index b4b7403ec8d0f6086eca373ffca34cee33545c94..2d7bf9d190fcc99dd3932915d6a523098b94b3f7 100644 --- a/metal/paddle-mobile/paddle-mobile/API/Runner.swift +++ b/metal/paddle-mobile/paddle-mobile/API/Runner.swift @@ -18,10 +18,12 @@ import Foundation @objc public class ResultHolder: NSObject { @objc public let result: UnsafeMutablePointer @objc public let capacity: Int + @objc public let dim: [Int] - init(inResult: UnsafeMutablePointer, inCapacity: Int) { + init(inResult: UnsafeMutablePointer, inCapacity: Int, inDim: [Int]) { result = inResult capacity = inCapacity + dim = inDim } @objc public func releasePointer() { @@ -99,7 +101,7 @@ import Foundation /// - Parameters: /// - texture: 输入 texture 需要使用 getTexture 获得 /// - completion: 结果回调, 当 success 为 true 时 result 不为 nil - @objc public func predict(texture: MTLTexture, completion: @escaping ( _ success: Bool, _ result: ResultHolder?) -> Void) { + @objc public func predict(texture: MTLTexture, completion: @escaping ( _ success: Bool, _ result: [ResultHolder]?) -> Void) { do { try self.executor?.predict(input: texture, dim: self.net.inputDim, completionHandle: { [weak self] (res) in guard let SSelf = self else { diff --git a/metal/paddle-mobile/paddle-mobile/Src/Framework/Executor.swift b/metal/paddle-mobile/paddle-mobile/Src/Framework/Executor.swift index ec29df04e72794709da1cce18f7f8e13da6b2af8..8f02bf17bcf0eed9e6b7a7bcb524cf6d18aa49fa 100644 --- a/metal/paddle-mobile/paddle-mobile/Src/Framework/Executor.swift +++ b/metal/paddle-mobile/paddle-mobile/Src/Framework/Executor.swift @@ -69,7 +69,7 @@ public class Executor { } } - public func predict(input: MTLTexture, dim: Dim, completionHandle: @escaping (GPUResultHolder) -> Void, preProcessKernle: CusomKernel? = nil, except: Int = 0) throws { + public func predict(input: MTLTexture, dim: Dim, completionHandle: @escaping ([GPUResultHolder]) -> Void, preProcessKernle: CusomKernel? = nil, except: Int = 0) throws { inflightSemaphore.wait() guard let buffer = queue.makeCommandBuffer() else { @@ -135,7 +135,7 @@ public class Executor { resultHolder = GPUResultHolder.init(inDim: output.dim.dims, inPointer: output.result, inCapacity: output.capacity) } - completionHandle(resultHolder) + completionHandle([resultHolder]) SSelf.inflightSemaphore.signal() }