diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift b/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift index 2a9f93977dd3adf4262390ad0b27c45bfc3e1c08..5fcbda127004d6941ad573f9a0f6509665da8bc9 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift @@ -108,15 +108,14 @@ class ViewController: UIViewController { guard let sSelf = self else { fatalError() } - if success { + if let inResultHolder = resultHolder, success { if i == max - 1 { let time = Date.init().timeIntervalSince(startDate) - print(Array.floatArrWithBuffer(floatArrBuffer: resultHolder!.result!, count: resultHolder!.capacity).strideArray()) + + print(inResultHolder.result.floatArr(count: inResultHolder.capacity).strideArray()) DispatchQueue.main.async { - // print(resultHolder!.result![0]) sSelf.resultTextView.text = sSelf.runner.net.resultStr(res: resultHolder!) sSelf.elapsedTimeLabel.text = "平均耗时: \(time/Double(max) * 1000.0) ms" - } } } diff --git a/metal/paddle-mobile/paddle-mobile/Common/Extensions.swift b/metal/paddle-mobile/paddle-mobile/Common/Extensions.swift index e51fda8ce966f7c3ffde68779d944286f62d8e53..12bc909be97b372ce9f82daf035dced0b969cdc7 100644 --- a/metal/paddle-mobile/paddle-mobile/Common/Extensions.swift +++ b/metal/paddle-mobile/paddle-mobile/Common/Extensions.swift @@ -120,9 +120,17 @@ extension Array { } } +extension UnsafeMutablePointer { + public func floatArr(count: Int) -> [Pointee]{ + var arr: [Pointee] = [] + for i in 0.. UnsafePointer? { return (self as NSString).utf8String } diff --git a/metal/paddle-mobile/paddle-mobile/CustomNet/Genet.swift b/metal/paddle-mobile/paddle-mobile/CustomNet/Genet.swift index 6babcc744d71aab8c722d1cf9d5ec332e138c0c2..06eec498f558633fbc19e64b3b12dd139ace518d 100644 --- a/metal/paddle-mobile/paddle-mobile/CustomNet/Genet.swift +++ b/metal/paddle-mobile/paddle-mobile/CustomNet/Genet.swift @@ -48,7 +48,7 @@ public class Genet: Net { override public func resultStr(res: ResultHolder) -> String { // fatalError() - return " \(res.result![0]) ... " + return " \(res.result[0]) ... " } } diff --git a/metal/paddle-mobile/paddle-mobile/CustomNet/MobileNet.swift b/metal/paddle-mobile/paddle-mobile/CustomNet/MobileNet.swift index db7948000866c14f443bf7611de5f4a13bb49bf7..9f2cebf25c33d13dc2d50feabaf21572c51bbbd7 100644 --- a/metal/paddle-mobile/paddle-mobile/CustomNet/MobileNet.swift +++ b/metal/paddle-mobile/paddle-mobile/CustomNet/MobileNet.swift @@ -43,9 +43,7 @@ public class MobileNet: Net{ let labels = PreWords.init(fileName: "synset") override public func resultStr(res: ResultHolder) -> String { - guard let resPointer = res.result else { - fatalError() - } + let resPointer = res.result var s: [String] = [] (0.. String { // fatalError() - return " \(res.result![0]) ... " + return " \(res.result[0]) ... " } } diff --git a/metal/paddle-mobile/paddle-mobile/CustomNet/MobilenetSSD_AR.swift b/metal/paddle-mobile/paddle-mobile/CustomNet/MobilenetSSD_AR.swift index 56af7339cb0a91a0b208b8f2c327829567448091..a36bbab9782e0429bc8cf6cc36bdcd7bc9d79910 100644 --- a/metal/paddle-mobile/paddle-mobile/CustomNet/MobilenetSSD_AR.swift +++ b/metal/paddle-mobile/paddle-mobile/CustomNet/MobilenetSSD_AR.swift @@ -47,7 +47,7 @@ public class MobileNet_ssd_AR: Net{ } override public func resultStr(res: ResultHolder) -> String { - return " \(res.result![0])" + return " \(res.result[0])" } override func fetchResult(paddleMobileRes: GPUResultHolder) -> ResultHolder { diff --git a/metal/paddle-mobile/paddle-mobile/CustomNet/YoloNet.swift b/metal/paddle-mobile/paddle-mobile/CustomNet/YoloNet.swift index 0cb1cb7755cde70d25b71a7078b451fb01b92fd9..4dd4f6b249a8eb15de4ff5c9ed7291e214b4c559 100644 --- a/metal/paddle-mobile/paddle-mobile/CustomNet/YoloNet.swift +++ b/metal/paddle-mobile/paddle-mobile/CustomNet/YoloNet.swift @@ -43,8 +43,7 @@ public class YoloNet: Net { // } override public func resultStr(res: ResultHolder) -> String { - // fatalError() - return " \(res.result![0]) ... " + return " \(res.result[0]) ... " } } diff --git a/metal/paddle-mobile/paddle-mobile/Net.swift b/metal/paddle-mobile/paddle-mobile/Net.swift index 0131ef5981e38506cb77af36887b0f4e9ef1386d..82aaa265997d9bb5d1017e51f5cc5769a56395b8 100644 --- a/metal/paddle-mobile/paddle-mobile/Net.swift +++ b/metal/paddle-mobile/paddle-mobile/Net.swift @@ -17,17 +17,17 @@ import Foundation import Metal public class ResultHolder: NSObject { - @objc public let result: UnsafeMutablePointer? + @objc public let result: UnsafeMutablePointer @objc public let capacity: Int - init(inResult: UnsafeMutablePointer?, inCapacity: Int) { + init(inResult: UnsafeMutablePointer, inCapacity: Int) { result = inResult capacity = inCapacity } @objc public func releasePointer() { - result?.deinitialize(count: capacity) - result?.deallocate() + result.deinitialize(count: capacity) + result.deallocate() } } @@ -86,7 +86,11 @@ public class Net: NSObject { } func fetchResult(paddleMobileRes: GPUResultHolder) -> ResultHolder { - return ResultHolder.init(inResult: paddleMobileRes.resultPointer, inCapacity: paddleMobileRes.capacity) + guard let inResPointer = paddleMobileRes.resultPointer else { + fatalError() + } + + return ResultHolder.init(inResult: inResPointer, inCapacity: paddleMobileRes.capacity) } func updateProgram(program: Program) { diff --git a/metal/paddle-mobile/paddle-mobile/Operators/FetchOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/FetchOp.swift index 33bf04244f1d5eb5e3ae3b059f6b3922bd866726..b26231c7b5e4e45b268dc5b59eb7374d3622d7f5 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/FetchOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/FetchOp.swift @@ -83,7 +83,7 @@ class FetchOp: Operator< FetchKernel

, FetchParam

>, Runab func delogOutput() { print("fetch output: ") - let resArr = Array.floatArrWithBuffer(floatArrBuffer: self.para.output.result, count: self.para.output.capacity) + let resArr = self.para.output.result.floatArr(count: self.para.output.capacity) print(resArr.strideArray()) } }