From 94624b1d5b8848449d364688b01d31b1b4ebfd3d Mon Sep 17 00:00:00 2001 From: liuruilong Date: Mon, 11 Mar 2019 22:11:19 +0800 Subject: [PATCH] add preprocess Kernel --- .../Net/MobileNetCombined.swift | 5 ++++- .../paddle-mobile-demo/Net/YoloNet.swift | 2 ++ .../paddle-mobile-demo/ViewController.swift | 17 ++++++++--------- .../Src/Operators/Kernels/Scale.swift | 4 ++-- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/Net/MobileNetCombined.swift b/metal/paddle-mobile-demo/paddle-mobile-demo/Net/MobileNetCombined.swift index 1f5468d797..3afe84fde9 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/Net/MobileNetCombined.swift +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/Net/MobileNetCombined.swift @@ -23,8 +23,11 @@ public class MobileNetCombined: Net { paramPath = Bundle.main.path(forResource: "combined_mobilenet_params", ofType: nil) ?! "para null" inputDim = Dim.init(inDim: [1, 224, 224, 3]) metalLoadMode = .LoadMetalInCustomMetalLib - metalLibPath = Bundle.main.path(forResource: "paddle-mobile-metallib", ofType: "metallib") + let paddleMobileMetallib = Bundle.main.path(forResource: "paddle-mobile-metallib", ofType: "metallib") + metalLibPath = paddleMobileMetallib useMPS = true + preprocessKernel = ScaleKernel.init(device: device, shape: Shape.init(inWidth: 224, inHeight: 224, inChannel: 3), metalLoadMode: .LoadMetalInCustomMetalLib, metalLibPath: paddleMobileMetallib) + } let labels = PreWords.init(fileName: "vision_synset") 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 3fad441a2e..a13b6303ac 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/Net/YoloNet.swift +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/Net/YoloNet.swift @@ -27,6 +27,8 @@ public class YoloNet: Net { metalLibPath = Bundle.main.path(forResource: "paddle-mobile-metallib", ofType: "metallib") useMPS = true paramPrecision = .Float16 + preprocessKernel = ScaleKernel.init(device: device, shape: Shape.init(inWidth: 416, inHeight: 416, inChannel: 3), metalLoadMode: .LoadMetalInCustomMetalLib, metalLibPath: Bundle.main.path(forResource: "paddle-mobile-metallib", ofType: "metallib")) + } override public func resultStr(res: [ResultHolder]) -> String { diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift b/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift index 1aa9d82654..53c417e88d 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift @@ -100,16 +100,15 @@ class ViewController: UIViewController { if self.toPredictTexture == nil { - - // runner.getTexture(inBuffer: buffer!) { [weak self] (texture) in - // self?.toPredictTexture = texture - // } - let beforeDate = Date.init() - runner.getTexture(image: selectImage!.cgImage!) { [weak self] (texture) in - let timeUse = Date.init().timeIntervalSince(beforeDate) - print("get texture time use: \(timeUse)") - self?.toPredictTexture = texture + if modelType == .mobilenet_combined || modelType == .yolo { + self.toPredictTexture = try! MetalHelper.shared.textureLoader.newTexture(cgImage: selectImage!.cgImage!, options: nil) + } else { + runner.getTexture(image: selectImage!.cgImage!) { [weak self] (texture) in + let timeUse = Date.init().timeIntervalSince(beforeDate) + print("get texture time use: \(timeUse)") + self?.toPredictTexture = texture + } } } } else { diff --git a/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/Scale.swift b/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/Scale.swift index 4a6a9a3ee4..cab3f3b0db 100644 --- a/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/Scale.swift +++ b/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/Scale.swift @@ -14,8 +14,8 @@ import Foundation -class ScaleKernel: CusomKernel { - init(device: MTLDevice, shape: Shape, metalLoadMode: MetalLoadMode, metalLibPath: String?) { +public class ScaleKernel: CusomKernel { + public init(device: MTLDevice, shape: Shape, metalLoadMode: MetalLoadMode, metalLibPath: String?) { if GlobalConfig.shared.computePrecision == .Float32 { super.init(device: device, inFunctionName: "scale", outputDim: shape, metalLoadModel: metalLoadMode, metalLibPath: metalLibPath) } else if GlobalConfig.shared.computePrecision == .Float16 { -- GitLab