From b9876e17a0e2b00c908c37ec62ad7da1d5935c2f Mon Sep 17 00:00:00 2001 From: liuruilong Date: Sat, 29 Dec 2018 12:31:37 +0800 Subject: [PATCH] fomart files --- .../paddle-mobile-demo/ViewController.swift | 7 +++---- .../paddle-mobile/Common/Extensions.swift | 12 ++++++++++-- .../paddle-mobile/CustomNet/Genet.swift | 2 +- .../paddle-mobile/CustomNet/MobileNet.swift | 4 +--- .../CustomNet/MobileNetCombined.swift | 2 +- .../paddle-mobile/CustomNet/MobilenetSSD_AR.swift | 2 +- .../paddle-mobile/CustomNet/YoloNet.swift | 3 +-- metal/paddle-mobile/paddle-mobile/Net.swift | 14 +++++++++----- .../paddle-mobile/Operators/FetchOp.swift | 2 +- 9 files changed, 28 insertions(+), 20 deletions(-) diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift b/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift index 2a9f93977d..5fcbda1270 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 e51fda8ce9..12bc909be9 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 6babcc744d..06eec498f5 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 db79480008..9f2cebf25c 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 56af7339cb..a36bbab978 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 0cb1cb7755..4dd4f6b249 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 0131ef5981..82aaa26599 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 33bf04244f..b26231c7b5 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()) } } -- GitLab