提交 02a5b655 编写于 作者: R Ray Liu 提交者: GitHub

Merge pull request #1440 from codeWorm2015/develop

ios metal gpu add multi-output api
......@@ -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..<res.capacity).map { resPointer[$0] }.top(r: 5).enumerated().forEach{
s.append(String(format: "%d: %@ (%3.2f%%)", $0 + 1, labels[$1.0], $1.1 * 100))
......
......@@ -41,8 +41,8 @@ public class Genet: Net {
}
}
override public func resultStr(res: ResultHolder) -> String {
return " \(res.result[0]) ... "
override public func resultStr(res: [ResultHolder]) -> String {
return " \(res[0].result[0]) ... "
}
}
......@@ -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..<res.capacity).map { resPointer[$0] }.top(r: 5).enumerated().forEach{
s.append(String(format: "%d: %@ (%3.2f%%)", $0 + 1, labels[$1.0], $1.1 * 100))
......
......@@ -26,8 +26,8 @@ public class MobileNetCombined: 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]) ... "
}
}
......@@ -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 ")
......
......@@ -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 ")
......
......@@ -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]) ... "
}
}
......@@ -26,6 +26,8 @@ typedef enum : NSUInteger {
@property (assign, nonatomic) int outputSize;
@property (strong, nonatomic) NSArray <NSNumber *>*dim;
-(void)releaseOutput;
@end
......@@ -88,13 +90,13 @@ typedef enum : NSUInteger {
* texture: 需要进行预测的图像转换的 texture
* completion: 预测完成回调
*/
-(void)predict:(id<MTLTexture>)texture withCompletion:(void (^)(BOOL, NSArray<NSNumber *> *))completion;
-(void)predict:(id<MTLTexture>)texture withCompletion:(void (^)(BOOL, NSArray<NSArray <NSNumber *>*> *))completion;
/*
* texture: 需要进行预测的图像转换的 texture
* completion: 预测完成回调
*/
-(void)predict:(id<MTLTexture>)texture withResultCompletion:(void (^)(BOOL, PaddleMobileGPUResult *))completion;
-(void)predict:(id<MTLTexture>)texture withResultCompletion:(void (^)(BOOL, NSArray <PaddleMobileGPUResult *> *))completion;
/*
* 清理内存
......
......@@ -66,24 +66,34 @@
return [runner load];
}
-(void)predict:(id<MTLTexture>)texture withCompletion:(void (^)(BOOL, NSArray<NSNumber *> *))completion {
-(void)predict:(id<MTLTexture>)texture withCompletion:(void (^)(BOOL, NSArray<NSArray <NSNumber *>*> *))completion {
[runner predictWithTexture:texture completion:^(BOOL success, ResultHolder * _Nullable result) {
NSMutableArray<NSNumber *> *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<ResultHolder *> * _Nullable resultArr) {
NSMutableArray<NSMutableArray <NSNumber *>*> *ocResultArray = [NSMutableArray arrayWithCapacity:resultArr.count];
for (int i = 0; i < resultArr.count; ++i) {
ResultHolder *resultHolder = resultArr[i];
NSMutableArray <NSNumber *>*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<MTLTexture>)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<MTLTexture>)texture withResultCompletion:(void (^)(BOOL, NSArray <PaddleMobileGPUResult *> *))completion {
[runner predictWithTexture:texture completion:^(BOOL success, NSArray<ResultHolder *> * _Nullable resultArr) {
NSMutableArray <PaddleMobileGPUResult *> *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);
}];
}
......
......@@ -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 "未实现"
}
......
......@@ -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()
})
}
}
......
......@@ -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) {
}
}
......@@ -18,10 +18,12 @@ import Foundation
@objc public class ResultHolder: NSObject {
@objc public let result: UnsafeMutablePointer<Float32>
@objc public let capacity: Int
@objc public let dim: [Int]
init(inResult: UnsafeMutablePointer<Float32>, inCapacity: Int) {
init(inResult: UnsafeMutablePointer<Float32>, 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 {
......
......@@ -69,7 +69,7 @@ public class Executor<P: PrecisionType> {
}
}
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<P: PrecisionType> {
resultHolder = GPUResultHolder.init(inDim: output.dim.dims, inPointer: output.result, inCapacity: output.capacity)
}
completionHandle(resultHolder)
completionHandle([resultHolder])
SSelf.inflightSemaphore.signal()
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册