From d91c89dd9a8635cbf335ad1eba19871a353b535c Mon Sep 17 00:00:00 2001 From: liuruilong Date: Fri, 28 Dec 2018 16:38:02 +0800 Subject: [PATCH] remove cpu in gpu project --- .../project.pbxproj | 24 ++++++ .../MultiPredictViewController.swift | 4 +- .../paddle-mobile-demo/ViewController.swift | 82 ++++-------------- .../paddle-mobile.xcodeproj/project.pbxproj | 24 ++---- .../xcschemes/paddle-mobile.xcscheme | 80 +++++++++++++++++ .../paddle-mobile/CPU/PaddleMobileCPU.h | 85 ------------------- .../paddle-mobile/paddle-mobile/CPUCompute.h | 1 - .../paddle-mobile/paddle-mobile/CPUCompute.mm | 4 - .../paddle-mobile/MobileNet.swift | 8 +- metal/paddle-mobile/paddle-mobile/Net.swift | 17 ++-- .../Operators/ResizeBilinearOp.swift | 4 +- .../paddle-mobile/PaddleMobileGPU.m | 7 +- .../paddle-mobile/paddle-mobile/Runner.swift | 76 ++--------------- .../paddle-mobile/SuperResolutionNet.swift | 3 +- .../paddle-mobile/framework/Executor.swift | 10 +-- .../paddle-mobile/paddle_mobile.h | 1 - 16 files changed, 166 insertions(+), 264 deletions(-) create mode 100644 metal/paddle-mobile/paddle-mobile.xcodeproj/xcshareddata/xcschemes/paddle-mobile.xcscheme delete mode 100644 metal/paddle-mobile/paddle-mobile/CPU/PaddleMobileCPU.h diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo.xcodeproj/project.pbxproj b/metal/paddle-mobile-demo/paddle-mobile-demo.xcodeproj/project.pbxproj index 9add81211c..4de49759d0 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo.xcodeproj/project.pbxproj +++ b/metal/paddle-mobile-demo/paddle-mobile-demo.xcodeproj/project.pbxproj @@ -25,6 +25,10 @@ FC704C2521D237FC00F98BAB /* yolo_model in Resources */ = {isa = PBXBuildFile; fileRef = FC704C2121D237FC00F98BAB /* yolo_model */; }; FC803BCD214D27930094B8E5 /* FPSCounter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC803BCB214D27920094B8E5 /* FPSCounter.swift */; }; FC803BCE214D27930094B8E5 /* VideoCapture.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC803BCC214D27920094B8E5 /* VideoCapture.swift */; }; + FC9797BE21D6045B00F2FD90 /* banana.jpeg in Resources */ = {isa = PBXBuildFile; fileRef = FC9797BD21D6045B00F2FD90 /* banana.jpeg */; }; + FC9797C221D608E000F2FD90 /* mobilenet_model in Resources */ = {isa = PBXBuildFile; fileRef = FC9797C021D608DF00F2FD90 /* mobilenet_model */; }; + FC9797C321D608E000F2FD90 /* mobilenet_params in Resources */ = {isa = PBXBuildFile; fileRef = FC9797C121D608DF00F2FD90 /* mobilenet_params */; }; + FC9797C721D609FB00F2FD90 /* synset.txt in Resources */ = {isa = PBXBuildFile; fileRef = FC9797C621D609FB00F2FD90 /* synset.txt */; }; FCBCCC552122EF5500D94F7E /* MetalHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCBCCC542122EF5400D94F7E /* MetalHelper.swift */; }; FCEBEC2C20E1391F00C0B14D /* paddle_mobile.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FCEBEC2B20E1391F00C0B14D /* paddle_mobile.framework */; }; FCEBEC2D20E1391F00C0B14D /* paddle_mobile.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = FCEBEC2B20E1391F00C0B14D /* paddle_mobile.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; @@ -70,6 +74,10 @@ FC704C2121D237FC00F98BAB /* yolo_model */ = {isa = PBXFileReference; lastKnownFileType = file; path = yolo_model; sourceTree = ""; }; FC803BCB214D27920094B8E5 /* FPSCounter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FPSCounter.swift; sourceTree = ""; }; FC803BCC214D27920094B8E5 /* VideoCapture.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VideoCapture.swift; sourceTree = ""; }; + FC9797BD21D6045B00F2FD90 /* banana.jpeg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = banana.jpeg; sourceTree = ""; }; + FC9797C021D608DF00F2FD90 /* mobilenet_model */ = {isa = PBXFileReference; lastKnownFileType = file; path = mobilenet_model; sourceTree = ""; }; + FC9797C121D608DF00F2FD90 /* mobilenet_params */ = {isa = PBXFileReference; lastKnownFileType = file; path = mobilenet_params; sourceTree = ""; }; + FC9797C621D609FB00F2FD90 /* synset.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = synset.txt; sourceTree = ""; }; FCBCCC542122EF5400D94F7E /* MetalHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MetalHelper.swift; sourceTree = ""; }; FCEBEC2B20E1391F00C0B14D /* paddle_mobile.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = paddle_mobile.framework; sourceTree = BUILT_PRODUCTS_DIR; }; FCF437E7214B6DDB00943429 /* MultiPredictViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MultiPredictViewController.swift; sourceTree = ""; }; @@ -150,6 +158,7 @@ FC203FA821CBFDBA00B37166 /* images */ = { isa = PBXGroup; children = ( + FC9797BD21D6045B00F2FD90 /* banana.jpeg */, FC203FA921CBFDBA00B37166 /* test.jpg */, ); name = images; @@ -159,6 +168,7 @@ FC203FAA21CBFDBA00B37166 /* models */ = { isa = PBXGroup; children = ( + FC9797BF21D608DF00F2FD90 /* mobilenet */, FC704C1B21D237FC00F98BAB /* vision_model */, FC704C1621D2375300F98BAB /* superresoltion */, ); @@ -220,6 +230,16 @@ path = Net; sourceTree = ""; }; + FC9797BF21D608DF00F2FD90 /* mobilenet */ = { + isa = PBXGroup; + children = ( + FC9797C621D609FB00F2FD90 /* synset.txt */, + FC9797C021D608DF00F2FD90 /* mobilenet_model */, + FC9797C121D608DF00F2FD90 /* mobilenet_params */, + ); + path = mobilenet; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -286,11 +306,15 @@ FC704C2221D237FC00F98BAB /* combined_mobilenet_params in Resources */, FC704C1921D2375300F98BAB /* super_params in Resources */, FC039B8920E11C560081E9F8 /* Assets.xcassets in Resources */, + FC9797C721D609FB00F2FD90 /* synset.txt in Resources */, FC704C1A21D2375300F98BAB /* super_model in Resources */, FC039B8720E11C550081E9F8 /* Main.storyboard in Resources */, + FC9797C221D608E000F2FD90 /* mobilenet_model in Resources */, FC203FB221CBFDBA00B37166 /* test.jpg in Resources */, FC704C2321D237FC00F98BAB /* combined_mobilenet_model in Resources */, + FC9797C321D608E000F2FD90 /* mobilenet_params in Resources */, FC704C2421D237FC00F98BAB /* yolo_params in Resources */, + FC9797BE21D6045B00F2FD90 /* banana.jpeg in Resources */, FC704C2521D237FC00F98BAB /* yolo_model in Resources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/MultiPredictViewController.swift b/metal/paddle-mobile-demo/paddle-mobile-demo/MultiPredictViewController.swift index bd07da61d0..22fb5723ac 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/MultiPredictViewController.swift +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/MultiPredictViewController.swift @@ -22,10 +22,10 @@ class MultiPredictViewController: UIViewController { super.viewDidLoad() let mobileNet = MobileNet_ssd_hand.init(device: MetalHelper.shared.device) let genet = Genet.init(device: MetalHelper.shared.device) - runner1 = Runner.init(inNet: mobileNet, commandQueue: MetalHelper.shared.queue, inPlatform: .GPU) + runner1 = Runner.init(inNet: mobileNet, commandQueue: MetalHelper.shared.queue) let queue2 = MetalHelper.shared.device.makeCommandQueue() - runner2 = Runner.init(inNet: genet, commandQueue: MetalHelper.shared.queue, inPlatform: .GPU) + runner2 = Runner.init(inNet: genet, commandQueue: MetalHelper.shared.queue) } @IBAction func predictAct(_ sender: Any) { diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift b/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift index bf59e3c358..2a9f93977d 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift @@ -18,23 +18,11 @@ import CoreMedia import paddle_mobile import MetalPerformanceShaders -var platform: Platform = .GPU -let threadSupport: [(Platform, String)] = [(.GPU, "GPU"), (.CPU, "CPU")] - -let netSupport: [SupportModel : Net] = [ - .super_resolution : SuperResolutionNet.init(device: MetalHelper.shared.device), - .yolo : YoloNet.init(device: MetalHelper.shared.device), - .mobilenet_combined : MobileNetCombined.init(device: MetalHelper.shared.device)] - -//.mobilenet_ssd : Runner.init(inNet: MobileNet_ssd_hand.init(device: MetalHelper.shared.device), commandQueue: MetalHelper.shared.queue, inPlatform: platform), -let modelHelperMap: [SupportModel : Runner] = [ - .super_resolution : Runner.init(inNet: netSupport[.super_resolution]!, commandQueue: MetalHelper.shared.queue, inPlatform: platform), - .yolo : Runner.init(inNet: YoloNet.init(device: MetalHelper.shared.device), commandQueue: MetalHelper.shared.queue, inPlatform: platform), - .mobilenet_combined : Runner.init(inNet: MobileNetCombined.init(device: MetalHelper.shared.device), commandQueue: MetalHelper.shared.queue, inPlatform: platform)] -//, .genet : Genet.init() -//let modelHelperMap: [SupportModel : Net] = [.mobilenet : MobileNet.init(), .mobilenet_ssd : MobileNet_ssd_hand.init()] - +enum Platform { + case GPU +} +let platformSupport: [(Platform, String)] = [(.GPU, "GPU")] enum SupportModel: String{ // case mobilenet = "mobilenet" @@ -42,14 +30,21 @@ enum SupportModel: String{ case yolo = "yolo" case mobilenet_combined = "mobilenet_combined" case super_resolution = "superresoltion" + case mobilenet = "mobilenet" static func supportedModels() -> [SupportModel] { // .mobilenet, // .mobilenet_ssd, - return [.super_resolution, .yolo, .mobilenet_combined] + return [.super_resolution, .yolo, .mobilenet_combined, .mobilenet] } } +let netSupport: [SupportModel : Net] = [ + .super_resolution : SuperResolutionNet.init(device: MetalHelper.shared.device), + .yolo : YoloNet.init(device: MetalHelper.shared.device), + .mobilenet_combined : MobileNetCombined.init(device: MetalHelper.shared.device), + .mobilenet : MobileNet.init(device: MetalHelper.shared.device)] + class ViewController: UIViewController { @IBOutlet weak var resultTextView: UITextView! @IBOutlet weak var selectImageView: UIImageView! @@ -65,23 +60,16 @@ class ViewController: UIViewController { var toPredictTexture: MTLTexture? var runner: Runner! - + var platform: Platform = .GPU var threadNum = 1 @IBAction func loadAct(_ sender: Any) { - runner = Runner.init(inNet: netSupport[modelType]!, commandQueue: MetalHelper.shared.queue, inPlatform: platform) - - if platform == .CPU { - if inputPointer == nil { - inputPointer = runner.preproccess(image: selectImage!.cgImage!) - } - } else if platform == .GPU { + runner = Runner.init(inNet: netSupport[modelType]!, commandQueue: MetalHelper.shared.queue) + if platform == .GPU { if self.toPredictTexture == nil { - runner.getTexture(image: selectImage!.cgImage!) { [weak self] (texture) in self?.toPredictTexture = texture } - } } else { fatalError( " unsupport " ) @@ -138,39 +126,6 @@ class ViewController: UIViewController { } } } - - case .CPU: - guard let inInputPointer = inputPointer else { - fatalError( " need input pointer " ) - } - - for _ in 0..<10 { - runner.predict(inputPointer: inInputPointer) { (success, res) in - res?.releaseOutput() - } - } - - let startDate = Date.init() - for i in 0.. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/metal/paddle-mobile/paddle-mobile/CPU/PaddleMobileCPU.h b/metal/paddle-mobile/paddle-mobile/CPU/PaddleMobileCPU.h deleted file mode 100644 index c68d81f328..0000000000 --- a/metal/paddle-mobile/paddle-mobile/CPU/PaddleMobileCPU.h +++ /dev/null @@ -1,85 +0,0 @@ -/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. */ - -#pragma once - -#import -#import - -@interface PaddleMobileCPUResult: NSObject - -@property (assign, nonatomic, readonly) float *output; - -@property (assign, nonatomic, readonly) int outputSize; - --(void)releaseOutput; - -@end - -@interface PaddleMobileCPU : NSObject - -/* - 创建对象 -*/ -- (instancetype)init; - -/* - load 模型, 开辟内存 -*/ -- (BOOL)load:(NSString *)modelPath andWeightsPath:(NSString *)weighsPath; - -/* - 加载散开形式的模型, 需传入模型的目录 -*/ -- (BOOL)load:(NSString *)modelAndWeightPath; - -/* - * 从内存中加载模型 - * */ -- (BOOL)LoadCombinedMemory:(size_t)modelLen - andModelBuf:(const uint8_t *)modelBuf - andModelParamsLen:(size_t)combinedParamsLen - andCombinedParamsBuf:(const uint8_t *)combinedParamsBuf; - -/* - * 对图像进行预处理, 需要外部开辟 output 内存, 外部释放 output 内存 - * */ --(void)preprocess:(CGImageRef)image - output:(float *)output - means:(NSArray *)means - scale:(float)scale - dim:(NSArray *)dim; - -/* - * 预测预处理后的数据, 返回结果使用结束需要调用其 realseOutput 函数进行释放 - * */ -- (PaddleMobileCPUResult *)predictInput:(float *)input - dim:(NSArray *)dim; - -/* - 进行预测, means 和 scale 为训练模型时的预处理参数, 如训练时没有做这些预处理则直接使用 predict -*/ -- (NSArray *)predict:(CGImageRef)image dim:(NSArray *)dim means:(NSArray *)means scale:(float)scale; - -/* - 进行预测, 默认 means 为 0, scale 为 1.0 -*/ -- (NSArray *)predict:(CGImageRef)image dim:(NSArray *)dim; - -/* - 清理内存 -*/ -- (void)clear; - -@end diff --git a/metal/paddle-mobile/paddle-mobile/CPUCompute.h b/metal/paddle-mobile/paddle-mobile/CPUCompute.h index ed12dd60df..752a67bfa1 100644 --- a/metal/paddle-mobile/paddle-mobile/CPUCompute.h +++ b/metal/paddle-mobile/paddle-mobile/CPUCompute.h @@ -16,7 +16,6 @@ #import - @interface CPUResult: NSObject @property (assign, nonatomic) float *output; @property (assign, nonatomic) int outputSize; diff --git a/metal/paddle-mobile/paddle-mobile/CPUCompute.mm b/metal/paddle-mobile/paddle-mobile/CPUCompute.mm index b97153765b..fac8af2527 100644 --- a/metal/paddle-mobile/paddle-mobile/CPUCompute.mm +++ b/metal/paddle-mobile/paddle-mobile/CPUCompute.mm @@ -12,7 +12,6 @@ See the License for the specific language governing permissions and limitations under the License. */ - #import "CPUCompute.h" #import @@ -20,9 +19,6 @@ #import #import - - - struct NMSParam { float *score_data; diff --git a/metal/paddle-mobile/paddle-mobile/MobileNet.swift b/metal/paddle-mobile/paddle-mobile/MobileNet.swift index 447997f7d1..9acc8f602c 100644 --- a/metal/paddle-mobile/paddle-mobile/MobileNet.swift +++ b/metal/paddle-mobile/paddle-mobile/MobileNet.swift @@ -14,7 +14,7 @@ import Foundation -class MobileNet: Net{ +public class MobileNet: Net{ class MobilenetPreProccess: CusomKernel { init(device: MTLDevice) { @@ -53,13 +53,13 @@ class MobileNet: Net{ return s.joined(separator: "\n") } - override init(device: MTLDevice) { + override public init(device: MTLDevice) { super.init(device: device) means = [123.68, 116.78, 103.94] scale = 0.017 except = 0 - modelPath = Bundle.main.path(forResource: "model", ofType: nil) ?! "model null" - paramPath = Bundle.main.path(forResource: "params", ofType: nil) ?! "para null" + modelPath = Bundle.main.path(forResource: "mobilenet_model", ofType: nil) ?! "model null" + paramPath = Bundle.main.path(forResource: "mobilenet_params", ofType: nil) ?! "para null" modelDir = "" preprocessKernel = MobilenetPreProccess.init(device: device) inputDim_ = Dim.init(inDim: [1, 224, 224, 3]) diff --git a/metal/paddle-mobile/paddle-mobile/Net.swift b/metal/paddle-mobile/paddle-mobile/Net.swift index 2c302a7c9a..0131ef5981 100644 --- a/metal/paddle-mobile/paddle-mobile/Net.swift +++ b/metal/paddle-mobile/paddle-mobile/Net.swift @@ -33,6 +33,8 @@ public class ResultHolder: NSObject { public class Net: NSObject { var except: Int = 0 + + // for CPU var means: [Float] = [] var scale: Float = 0.0 @@ -69,7 +71,16 @@ public class Net: NSObject { self.device = device super.init() } + + @objc public init(device: MTLDevice) { + self.device = device + super.init() + } + @objc public func updateInputDim(inDim: [Int]) { + inputDim = Dim.init(inDim: inDim) + } + public func resultStr(res: ResultHolder) -> String { fatalError() } @@ -78,12 +89,6 @@ public class Net: NSObject { return ResultHolder.init(inResult: paddleMobileRes.resultPointer, inCapacity: paddleMobileRes.capacity) } - @objc public init(device: MTLDevice) { - self.device = device - super.init() - } - func updateProgram(program: Program) { - } } diff --git a/metal/paddle-mobile/paddle-mobile/Operators/ResizeBilinearOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/ResizeBilinearOp.swift index e0e699cdb8..e915f561c8 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/ResizeBilinearOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/ResizeBilinearOp.swift @@ -29,8 +29,8 @@ class ResizeBilinearParam: OpParam { throw error } } - let input: Texture

- var output: Texture

+ let input: Texture + var output: Texture let out_h: Int32 let out_w: Int32 } diff --git a/metal/paddle-mobile/paddle-mobile/PaddleMobileGPU.m b/metal/paddle-mobile/paddle-mobile/PaddleMobileGPU.m index 79e643613f..97b938ff40 100644 --- a/metal/paddle-mobile/paddle-mobile/PaddleMobileGPU.m +++ b/metal/paddle-mobile/paddle-mobile/PaddleMobileGPU.m @@ -12,9 +12,10 @@ See the License for the specific language governing permissions and limitations under the License. */ -#import -#import "PaddleMobileGPU.h" #import "paddle_mobile.h" +#import "PaddleMobileGPU.h" + +#import #import @implementation ModelConfig @@ -59,7 +60,7 @@ } else if (netType == MobileNetType) { } - runner = [[Runner alloc] initInNet:net commandQueue:queue inPlatform:PlatformGPU]; + runner = [[Runner alloc] initInNet:net commandQueue:queue]; } return self; } diff --git a/metal/paddle-mobile/paddle-mobile/Runner.swift b/metal/paddle-mobile/paddle-mobile/Runner.swift index 4b1798da87..766e6ec9f6 100644 --- a/metal/paddle-mobile/paddle-mobile/Runner.swift +++ b/metal/paddle-mobile/paddle-mobile/Runner.swift @@ -9,10 +9,6 @@ import MetalKit import Foundation -@objc public enum Platform: Int{ - case CPU, GPU -} - class ScaleKernel: CusomKernel { init(device: MTLDevice, shape: Shape) { if computePrecision == .Float32 { @@ -32,8 +28,6 @@ class ScaleKernel: CusomKernel { var textureLoader: MTKTextureLoader? public let net: Net let device: MTLDevice? - let platform: Platform - var cpuPaddleMobile: PaddleMobileCPU? let numel: Int let meansNumber: [NSNumber] @@ -44,7 +38,7 @@ class ScaleKernel: CusomKernel { * commandQueue: GPU 是需要传入 * inPlatform: 需要使用的平台, GPU or CPU */ - @objc public init(inNet: Net, commandQueue: MTLCommandQueue?, inPlatform: Platform) { + @objc public init(inNet: Net, commandQueue: MTLCommandQueue?) { guard inNet.inputDim.cout() == 4 else { fatalError(" input dim count must 4 ") } @@ -52,14 +46,10 @@ class ScaleKernel: CusomKernel { net = inNet queue = commandQueue device = queue?.device - platform = inPlatform if let inDevice = device { textureLoader = MTKTextureLoader.init(device: inDevice) } - if platform == .CPU { - cpuPaddleMobile = PaddleMobileCPU.init() - } - + numel = net.inputDim.numel() meansNumber = net.means.map { NSNumber.init(value: $0) } dimsNum = [NSNumber.init(value: net.inputDim[0]), @@ -72,7 +62,6 @@ class ScaleKernel: CusomKernel { * load 模型, 返回 true 可进行预测 */ @objc public func load() -> Bool { - if platform == .GPU { guard let inDevice = device, let inQueue = queue else { print(" paddle mobile gpu load error, need MTLCommandQueue") return false @@ -88,20 +77,9 @@ class ScaleKernel: CusomKernel { print(error) return false } - } else { - return cpuPaddleMobile?.load(net.modelPath, andWeightsPath: net.paramPath) ?? false - } return true } - @objc public func predict(inputPointer: UnsafeMutablePointer, completion: @escaping ( _ success: Bool, _ result: PaddleMobileCPUResult?) -> Void) { - guard let res = cpuPaddleMobile?.predictInput(inputPointer, dim: dimsNum) else { - completion(false, nil) - return - } - completion(true, res) - } - /** * GPU 版本 predict * texture: 需要预测的 texture 需要做过预处理 @@ -124,57 +102,15 @@ class ScaleKernel: CusomKernel { } } - /** - * CPU GPU 通用版本 predict - * cgImage: 需要预测的图片 - * ( _ success: Bool, _ time:TimeInterval, _ resultArray: [Float32]) -> Void : 回调闭包, 三个参数分别为: 是否成功, 预测耗时, 结果数组 - */ - // @objc public func predict(cgImage: CGImage, completion: @escaping ( _ success: Bool, _ resultArray: [Float32]) -> Void) { - // if platform == .GPU { - // getTexture(image: cgImage) { [weak self] (texture) in - // guard let SSelf = self else { - // fatalError( "" ) - // } - // SSelf.predict(texture: texture, completion: completion) - // } - // } else if platform == .CPU { - // let input = preproccess(image: cgImage) - // predict(inputPointer: input, completion: completion) - // input.deinitialize(count: numel) - // input.deallocate() - // } - // } - /* * 清理内存, 调用此函数后, 不能再使用, 需重新 load */ @objc public func clear() { - if platform == .GPU { - executor?.clear() - executor = nil - program = nil - } else if platform == .CPU { - cpuPaddleMobile?.clear() - } - } - - @objc public func preproccess(image: CGImage) -> UnsafeMutablePointer { - let output = UnsafeMutablePointer.allocate(capacity: numel) - let means = net.means.map { NSNumber.init(value: $0) } - - if net.inputDim.cout() == 4 { - let dims = [NSNumber.init(value: net.inputDim[0]), - NSNumber.init(value: net.inputDim[3]), - NSNumber.init(value: net.inputDim[1]), - NSNumber.init(value: net.inputDim[2])] - cpuPaddleMobile?.preprocess(image, output: output, means: means, scale: net.scale, dim: dims) - return output - } - - fatalError() - + executor?.clear() + executor = nil + program = nil } - + /* * 获取 texture, 对 texture 进行预处理, GPU 预测时使用 */ diff --git a/metal/paddle-mobile/paddle-mobile/SuperResolutionNet.swift b/metal/paddle-mobile/paddle-mobile/SuperResolutionNet.swift index f463a242ad..134aa3b9d8 100644 --- a/metal/paddle-mobile/paddle-mobile/SuperResolutionNet.swift +++ b/metal/paddle-mobile/paddle-mobile/SuperResolutionNet.swift @@ -28,7 +28,8 @@ public class SuperResolutionNet: Net{ paramPath = Bundle.main.path(forResource: "super_params", ofType: nil) ?! "para null" modelDir = "" preprocessKernel = nil - inputDim_ = Dim.init(inDim: [1, Int(552 * 1.414), Int(310 * 1.414), 1]) +// inputDim_ = Dim.init(inDim: [1, Int(552 * 1.414), Int(310 * 1.414), 1]) + inputDim_ = Dim.init(inDim: [1, 224, 224, 1]) } override func updateProgram(program: Program) { diff --git a/metal/paddle-mobile/paddle-mobile/framework/Executor.swift b/metal/paddle-mobile/paddle-mobile/framework/Executor.swift index a7e77cc344..5c33fb9ea7 100644 --- a/metal/paddle-mobile/paddle-mobile/framework/Executor.swift +++ b/metal/paddle-mobile/paddle-mobile/framework/Executor.swift @@ -19,8 +19,7 @@ let testTo = 22 var isTest = false -let computePrecision: ComputePrecision = .Float16 - +let computePrecision: ComputePrecision = .Float32 public class GPUResultHolder { public let dim: [Int] public let capacity: Int @@ -153,17 +152,18 @@ public class Executor { /* let inputArr = resInput.toTensor(dim: (n: dim[0], c: dim[3], h: dim[1], w: dim[2])) print(dim) - writeToLibrary(fileName: "test_image_ssd_ar", array: inputArr) + writeToLibrary(fileName: "test_image_yolo", array: inputArr) print(" write done ") return */ + /* 输出 op 计算结果 for op in SSelf.ops { - op.delogOutput() + op.delogOutput() } */ - + let afterDate = Date.init() var resultHolder: GPUResultHolder if except > 0 { diff --git a/metal/paddle-mobile/paddle-mobile/paddle_mobile.h b/metal/paddle-mobile/paddle-mobile/paddle_mobile.h index 50b60e9fe6..c9621d8d00 100644 --- a/metal/paddle-mobile/paddle-mobile/paddle_mobile.h +++ b/metal/paddle-mobile/paddle-mobile/paddle_mobile.h @@ -14,7 +14,6 @@ #pragma once -#import "PaddleMobileCPU.h" #import "CPUCompute.h" #import "PaddleMobileGPU.h" #import -- GitLab