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 4e9fcf8e743a4bb209d8c719d997d1d9abe40139..78370395195c57e3fcab85a355eaafd6cc6d9021 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo.xcodeproj/project.pbxproj +++ b/metal/paddle-mobile-demo/paddle-mobile-demo.xcodeproj/project.pbxproj @@ -20,6 +20,8 @@ FC8CFEF9213551D10094D569 /* model in Resources */ = {isa = PBXBuildFile; fileRef = FC8CFEF7213551D00094D569 /* model */; }; FC918191211DBC3500B6F354 /* paddle-mobile.png in Resources */ = {isa = PBXBuildFile; fileRef = FC918190211DBC3500B6F354 /* paddle-mobile.png */; }; FC918193211DC70500B6F354 /* iphone.JPG in Resources */ = {isa = PBXBuildFile; fileRef = FC918192211DC70500B6F354 /* iphone.JPG */; }; + FC9A19E72148C38400CD9CBF /* ar_model in Resources */ = {isa = PBXBuildFile; fileRef = FC9A19E52148C38400CD9CBF /* ar_model */; }; + FC9A19E82148C38400CD9CBF /* ar_params in Resources */ = {isa = PBXBuildFile; fileRef = FC9A19E62148C38400CD9CBF /* ar_params */; }; FCA3A16121313E1F00084FE5 /* hand.jpg in Resources */ = {isa = PBXBuildFile; fileRef = FCA3A16021313E1F00084FE5 /* hand.jpg */; }; FCBCCC522122EEDC00D94F7E /* ssd_hand_params in Resources */ = {isa = PBXBuildFile; fileRef = FCBCCC502122EEDC00D94F7E /* ssd_hand_params */; }; FCBCCC532122EEDC00D94F7E /* ssd_hand_model in Resources */ = {isa = PBXBuildFile; fileRef = FCBCCC512122EEDC00D94F7E /* ssd_hand_model */; }; @@ -64,6 +66,8 @@ FC8CFEF7213551D00094D569 /* model */ = {isa = PBXFileReference; lastKnownFileType = file; path = model; sourceTree = ""; }; FC918190211DBC3500B6F354 /* paddle-mobile.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "paddle-mobile.png"; sourceTree = ""; }; FC918192211DC70500B6F354 /* iphone.JPG */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = iphone.JPG; sourceTree = ""; }; + FC9A19E52148C38400CD9CBF /* ar_model */ = {isa = PBXFileReference; lastKnownFileType = file; path = ar_model; sourceTree = ""; }; + FC9A19E62148C38400CD9CBF /* ar_params */ = {isa = PBXFileReference; lastKnownFileType = file; path = ar_params; sourceTree = ""; }; FCA3A16021313E1F00084FE5 /* hand.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = hand.jpg; sourceTree = ""; }; FCBCCC502122EEDC00D94F7E /* ssd_hand_params */ = {isa = PBXFileReference; lastKnownFileType = file; path = ssd_hand_params; sourceTree = ""; }; FCBCCC512122EEDC00D94F7E /* ssd_hand_model */ = {isa = PBXFileReference; lastKnownFileType = file; path = ssd_hand_model; sourceTree = ""; }; @@ -156,6 +160,7 @@ FC0E2C2020EDC03B009C1FAC /* models */ = { isa = PBXGroup; children = ( + FC9A19E42148C38400CD9CBF /* fluid_fssd_new_ar */, FC8CFEF5213551D00094D569 /* mobilenet */, FC8CFEE32135452B0094D569 /* genet */, FCBCCC4F2122EEDC00D94F7E /* mobilenet_ssd_hand */, @@ -191,6 +196,15 @@ path = mobilenet; sourceTree = ""; }; + FC9A19E42148C38400CD9CBF /* fluid_fssd_new_ar */ = { + isa = PBXGroup; + children = ( + FC9A19E52148C38400CD9CBF /* ar_model */, + FC9A19E62148C38400CD9CBF /* ar_params */, + ); + path = fluid_fssd_new_ar; + sourceTree = ""; + }; FCBCCC4F2122EEDC00D94F7E /* mobilenet_ssd_hand */ = { isa = PBXGroup; children = ( @@ -276,6 +290,8 @@ FCA3A16121313E1F00084FE5 /* hand.jpg in Resources */, FC8CFEE62135452C0094D569 /* genet_params in Resources */, FCBCCC532122EEDC00D94F7E /* ssd_hand_model in Resources */, + FC9A19E72148C38400CD9CBF /* ar_model in Resources */, + FC9A19E82148C38400CD9CBF /* ar_params in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/Net/PreProcessKernel.metal b/metal/paddle-mobile-demo/paddle-mobile-demo/Net/PreProcessKernel.metal index edbb19c980102796a504252bf35ebc9a6d0513fb..ac07e449bc5919a37a57143aa6881f79507a45b4 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/Net/PreProcessKernel.metal +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/Net/PreProcessKernel.metal @@ -44,7 +44,6 @@ kernel void mobilenet_preprocess_half( outTexture.write(half4(inColor.z, inColor.y, inColor.x, 0.0f), gid); } - kernel void mobilenet_ssd_preprocess( texture2d inTexture [[texture(0)]], texture2d outTexture [[texture(1)]], @@ -73,7 +72,6 @@ kernel void mobilenet_ssd_preprocess_half( outTexture.write(half4(inColor.z, inColor.y, inColor.x, 0.0f), gid); } - kernel void genet_preprocess(texture2d inTexture [[texture(0)]], texture2d outTexture [[texture(1)]], uint2 gid [[thread_position_in_grid]]) { if (gid.x >= outTexture.get_width() || @@ -96,6 +94,28 @@ kernel void genet_preprocess_half(texture2d inTexture [[text outTexture.write(half4(inColor.z, inColor.y, inColor.x, 0.0f), gid); } +kernel void mobilent_ar_preprocess(texture2d inTexture [[texture(0)]], texture2d outTexture [[texture(1)]], uint2 gid [[thread_position_in_grid]]) +{ + if (gid.x >= outTexture.get_width() || + gid.y >= outTexture.get_height()) { + return; + } + const auto means = float4(128.0f, 128.0f, 128.0f, 0.0f); + const float4 inColor = (inTexture.read(gid) * 255.0 - means) * 0.017; + outTexture.write(float4(inColor.z, inColor.y, inColor.x, 0.0f), gid); +} + +kernel void mobilent_ar_preprocess_half(texture2d inTexture [[texture(0)]], texture2d outTexture [[texture(1)]], uint2 gid [[thread_position_in_grid]]) +{ + if (gid.x >= outTexture.get_width() || + gid.y >= outTexture.get_height()) { + return; + } + const auto means = half4(128.0f, 128.0f, 128.0f, 0.0f); + const half4 inColor = (inTexture.read(gid) * 255.0 - means) * 0.017; + outTexture.write(half4(inColor.z, inColor.y, inColor.x, 0.0f), gid); +} + kernel void scale(texture2d inTexture [[texture(0)]], texture2d outTexture [[texture(1)]], uint2 gid [[thread_position_in_grid]]) { if (gid.x >= outTexture.get_width() || gid.y >= outTexture.get_height()) return; @@ -115,4 +135,3 @@ kernel void scale_half(texture2d inTexture [[texture(0)]] float4 input = inTexture.sample(sample, float2(gid.x * w_stride, gid.y * h_stride), 0); outTexture.write(half4(input), gid); } - diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift b/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift index e249c3aea909869aec73f5ac8fa3d8ca63382c0f..3bc9e1668750af53d53bb14fd05c363c96e924c0 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift @@ -21,17 +21,20 @@ let platform: Platform = .GPU let threadSupport = [1] let modelHelperMap: [SupportModel : Runner] = [.mobilenet_ssd : Runner.init(inNet: MobileNet_ssd_hand.init(device: MetalHelper.shared.device), commandQueue: MetalHelper.shared.queue, inPlatform: platform), - .genet : Runner.init(inNet: Genet.init(device: MetalHelper.shared.device), commandQueue: MetalHelper.shared.queue, inPlatform: platform)] + .genet : Runner.init(inNet: Genet.init(device: MetalHelper.shared.device), commandQueue: MetalHelper.shared.queue, inPlatform: platform), + .mobilenet_ssd_ar : Runner.init(inNet: MobileNet_ssd_AR.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 SupportModel: String{ // case mobilenet = "mobilenet" - case mobilenet_ssd = "mobilenetssd" - case genet = "genet" + case mobilenet_ssd = "mobilenetssd" + case genet = "genet" + case mobilenet_ssd_ar = "mobilenetssd_ar" + static func supportedModels() -> [SupportModel] { //.mobilenet, - return [.mobilenet_ssd, .genet] + return [.mobilenet_ssd, .genet, .mobilenet_ssd_ar] } } @@ -78,7 +81,7 @@ class ViewController: UIViewController { } @IBAction func predictAct(_ sender: Any) { - let max = 50 + let max = 1 switch platform { case .GPU: guard let inTexture = toPredictTexture else { @@ -102,9 +105,10 @@ class ViewController: UIViewController { } } } +// print("sleep before ") +// usleep(33000) +// print("sleep after ") } - - case .CPU: guard let inInputPointer = inputPointer else { fatalError( " need input pointer " ) diff --git a/metal/paddle-mobile/paddle-mobile.xcodeproj/project.pbxproj b/metal/paddle-mobile/paddle-mobile.xcodeproj/project.pbxproj index 42e4984dd98fc5556d9869c5e759aa68c2a7932a..56b8e363e990bdecae5f859ca91a7c8400d77f0b 100644 --- a/metal/paddle-mobile/paddle-mobile.xcodeproj/project.pbxproj +++ b/metal/paddle-mobile/paddle-mobile.xcodeproj/project.pbxproj @@ -15,7 +15,6 @@ 4AA1EA90214664CD00D0F791 /* Split.metal in Sources */ = {isa = PBXBuildFile; fileRef = 4AA1EA8F214664CD00D0F791 /* Split.metal */; }; 4AA1EA92214665D700D0F791 /* ShapeOp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AA1EA91214665D700D0F791 /* ShapeOp.swift */; }; 4AA1EA942146661500D0F791 /* ShapeKernel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AA1EA932146661500D0F791 /* ShapeKernel.swift */; }; - 4AA1EA962146665A00D0F791 /* FlattenKernel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AA1EA952146665A00D0F791 /* FlattenKernel.swift */; }; 4AA1EA982146666500D0F791 /* FlattenOp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AA1EA972146666500D0F791 /* FlattenOp.swift */; }; 4AA1EA9E2148D6F900D0F791 /* ConcatKernel.metal.inc in Headers */ = {isa = PBXBuildFile; fileRef = 4AA1EA9D2148D6F900D0F791 /* ConcatKernel.metal.inc */; }; 4AA1EAA02148DEEE00D0F791 /* ReshapeKernel.metal.inc in Sources */ = {isa = PBXBuildFile; fileRef = 4AA1EA9F2148DEEE00D0F791 /* ReshapeKernel.metal.inc */; }; @@ -59,6 +58,7 @@ FC292C82214255BD00CF622F /* MobileNetSSD.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC292C7E214255BC00CF622F /* MobileNetSSD.swift */; }; FC292C85214257CB00CF622F /* CPUCompute.h in Headers */ = {isa = PBXBuildFile; fileRef = FC292C7D214255BC00CF622F /* CPUCompute.h */; settings = {ATTRIBUTES = (Public, ); }; }; FC292C872142624800CF622F /* Genet.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC292C862142624800CF622F /* Genet.swift */; }; + FC33B0F02147659000714A93 /* MobileNet.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC33B0EF2147659000714A93 /* MobileNet.swift */; }; FC3602CC2108819F00FACB58 /* PaddleMobileUnitTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC3602CB2108819F00FACB58 /* PaddleMobileUnitTest.swift */; }; FC4CB74920F0B954007C0C6D /* ConvKernel.metal in Sources */ = {isa = PBXBuildFile; fileRef = FC4CB74820F0B954007C0C6D /* ConvKernel.metal */; }; FC4CB74B20F12C30007C0C6D /* ProgramOptimize.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC4CB74A20F12C30007C0C6D /* ProgramOptimize.swift */; }; @@ -69,6 +69,7 @@ FC5163F620EF556E00636C28 /* Texture2DTo2DArrayKernel.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC5163F520EF556E00636C28 /* Texture2DTo2DArrayKernel.swift */; }; FC60DB8920E9AAA500FF203F /* MetalExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC60DB8820E9AAA500FF203F /* MetalExtension.swift */; }; FC82735920E3C04200BE430A /* OpCreator.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC82735820E3C04200BE430A /* OpCreator.swift */; }; + FC9A19E32148C31300CD9CBF /* MobilenetSSD_AR.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC9A19E22148C31300CD9CBF /* MobilenetSSD_AR.swift */; }; FC9D037920E229E4000F735A /* OpParam.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC9D037820E229E4000F735A /* OpParam.swift */; }; FC9D038020E22FBB000F735A /* FeedOp.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC9D037F20E22FBB000F735A /* FeedOp.swift */; }; FC9D038220E2312E000F735A /* FetchOp.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC9D038120E2312E000F735A /* FetchOp.swift */; }; @@ -124,7 +125,6 @@ 4AA1EA8F214664CD00D0F791 /* Split.metal */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.metal; path = Split.metal; sourceTree = ""; }; 4AA1EA91214665D700D0F791 /* ShapeOp.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShapeOp.swift; sourceTree = ""; }; 4AA1EA932146661500D0F791 /* ShapeKernel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShapeKernel.swift; sourceTree = ""; }; - 4AA1EA952146665A00D0F791 /* FlattenKernel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FlattenKernel.swift; sourceTree = ""; }; 4AA1EA972146666500D0F791 /* FlattenOp.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FlattenOp.swift; sourceTree = ""; }; 4AA1EA9D2148D6F900D0F791 /* ConcatKernel.metal.inc */ = {isa = PBXFileReference; explicitFileType = sourcecode.metal; fileEncoding = 4; path = ConcatKernel.metal.inc; sourceTree = ""; }; 4AA1EA9F2148DEEE00D0F791 /* ReshapeKernel.metal.inc */ = {isa = PBXFileReference; explicitFileType = sourcecode.metal; fileEncoding = 4; path = ReshapeKernel.metal.inc; sourceTree = ""; }; @@ -173,6 +173,7 @@ FC292C7D214255BC00CF622F /* CPUCompute.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CPUCompute.h; sourceTree = ""; }; FC292C7E214255BC00CF622F /* MobileNetSSD.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MobileNetSSD.swift; sourceTree = ""; }; FC292C862142624800CF622F /* Genet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Genet.swift; sourceTree = ""; }; + FC33B0EF2147659000714A93 /* MobileNet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MobileNet.swift; sourceTree = ""; }; FC3602CB2108819F00FACB58 /* PaddleMobileUnitTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaddleMobileUnitTest.swift; sourceTree = ""; }; FC4CB74820F0B954007C0C6D /* ConvKernel.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = ConvKernel.metal; sourceTree = ""; }; FC4CB74A20F12C30007C0C6D /* ProgramOptimize.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProgramOptimize.swift; sourceTree = ""; }; @@ -183,6 +184,7 @@ FC5163F520EF556E00636C28 /* Texture2DTo2DArrayKernel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Texture2DTo2DArrayKernel.swift; sourceTree = ""; }; FC60DB8820E9AAA500FF203F /* MetalExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetalExtension.swift; sourceTree = ""; }; FC82735820E3C04200BE430A /* OpCreator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpCreator.swift; sourceTree = ""; }; + FC9A19E22148C31300CD9CBF /* MobilenetSSD_AR.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MobilenetSSD_AR.swift; sourceTree = ""; }; FC9D037820E229E4000F735A /* OpParam.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpParam.swift; sourceTree = ""; }; FC9D037F20E22FBB000F735A /* FeedOp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedOp.swift; sourceTree = ""; }; FC9D038120E2312E000F735A /* FetchOp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FetchOp.swift; sourceTree = ""; }; @@ -282,6 +284,8 @@ FC039B6C20E11C3C0081E9F8 /* paddle-mobile */ = { isa = PBXGroup; children = ( + FC9A19E22148C31300CD9CBF /* MobilenetSSD_AR.swift */, + FC33B0EF2147659000714A93 /* MobileNet.swift */, FC292C862142624800CF622F /* Genet.swift */, FC292C7E214255BC00CF622F /* MobileNetSSD.swift */, FC292C7C214255BC00CF622F /* CPUCompute.mm */, @@ -399,7 +403,6 @@ FCBCCC6C2123073A00D94F7E /* BoxcoderKernel.swift */, 4AA1EA8D2146647F00D0F791 /* SplitKernel.swift */, 4AA1EA932146661500D0F791 /* ShapeKernel.swift */, - 4AA1EA952146665A00D0F791 /* FlattenKernel.swift */, 4AA1EA87214662BD00D0F791 /* BilinearInterpKernel.swift */, FCBCCC70212309A700D94F7E /* MulticlassNMSKernel.swift */, FCDDC6C5212F9FB800E5EF74 /* PreluKernel.swift */, @@ -596,6 +599,7 @@ FCEBC0F420F1FDD90099DBAF /* ConvAddBatchNormReluOp.swift in Sources */, FC0E2DC020EE461F009C1FAC /* ElementwiseAddKernel.swift in Sources */, 4AF928772133F1DB005B6C3A /* BoxCoder.metal in Sources */, + FC33B0F02147659000714A93 /* MobileNet.swift in Sources */, FCEB684C212F093800D2448E /* PreluOp.swift in Sources */, FCA67CD92138287B00BD58AA /* ConvBNReluKernel.metal in Sources */, FC60DB8920E9AAA500FF203F /* MetalExtension.swift in Sources */, @@ -622,7 +626,6 @@ FCD04E7420F3437E0007374F /* ConvAddKernel.swift in Sources */, FC039BB920E11CC20081E9F8 /* Scope.swift in Sources */, FC292C5621421B4600CF622F /* PaddleMobileGPU.m in Sources */, - 4AA1EA962146665A00D0F791 /* FlattenKernel.swift in Sources */, FCD04E6620F314C50007374F /* PoolOp.swift in Sources */, FC039BAC20E11CBC0081E9F8 /* BatchNormOp.swift in Sources */, FCBCCC6F2123097100D94F7E /* MulticlassNMSOp.swift in Sources */, @@ -655,6 +658,7 @@ FCD04E6C20F31A280007374F /* SoftmaxKernel.swift in Sources */, FCEB684A212F00DB00D2448E /* PreluKernel.metal in Sources */, 4AA1EAA02148DEEE00D0F791 /* ReshapeKernel.metal.inc in Sources */, + FC9A19E32148C31300CD9CBF /* MobilenetSSD_AR.swift in Sources */, FCDDC6CF212FE14700E5EF74 /* PriorBoxKernel.metal in Sources */, FC4CB74B20F12C30007C0C6D /* ProgramOptimize.swift in Sources */, FC5163F620EF556E00636C28 /* Texture2DTo2DArrayKernel.swift in Sources */, diff --git a/metal/paddle-mobile/paddle-mobile/MobileNet.swift b/metal/paddle-mobile/paddle-mobile/MobileNet.swift index 6c2487b0b17f68b7b60bf13b9e9f7d446ea14c43..a383a75d7216b3c574d3bd881d3b63774b9e36b8 100644 --- a/metal/paddle-mobile/paddle-mobile/MobileNet.swift +++ b/metal/paddle-mobile/paddle-mobile/MobileNet.swift @@ -13,16 +13,9 @@ limitations under the License. */ import Foundation -import paddle_mobile class MobileNet: Net{ - var means: [Float] = [123.68, 116.78, 103.94] - - var scale: Float = 0.017 - - let except: Int = 0 - class MobilenetPreProccess: CusomKernel { init(device: MTLDevice) { let s = CusomKernel.Shape.init(inWidth: 224, inHeight: 224, inChannel: 3) @@ -49,7 +42,7 @@ class MobileNet: Net{ let labels = PreWords.init(fileName: "synset") - func resultStr(res: [Float]) -> String { + override public func resultStr(res: [Float]) -> String { var s: [String] = [] res.top(r: 5).enumerated().forEach{ s.append(String(format: "%d: %@ (%3.2f%%)", $0 + 1, labels[$1.0], $1.1 * 100)) @@ -57,17 +50,18 @@ class MobileNet: Net{ return s.joined(separator: "\n") } - var preprocessKernel: CusomKernel - let dim = (n: 1, h: 224, w: 224, c: 3) - let modelPath: String - let paramPath: String - let modelDir: String + - init(device: MTLDevice) { + override 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" modelDir = "" preprocessKernel = MobilenetPreProccess.init(device: device) + dim = (n: 1, h: 224, w: 224, c: 3) } } diff --git a/metal/paddle-mobile/paddle-mobile/MobilenetSSD_AR.swift b/metal/paddle-mobile/paddle-mobile/MobilenetSSD_AR.swift new file mode 100644 index 0000000000000000000000000000000000000000..106a1882ff9dd6edeff107f013c282346fd83977 --- /dev/null +++ b/metal/paddle-mobile/paddle-mobile/MobilenetSSD_AR.swift @@ -0,0 +1,79 @@ +/* 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. */ + +import Foundation + +public class MobileNet_ssd_AR: Net{ + @objc public override init(device: MTLDevice) { + super.init(device: device) + means = [103.94, 116.78, 123.68] + scale = 1 + except = 2 + modelPath = Bundle.main.path(forResource: "ar_model", ofType: nil) ?! "model null" + paramPath = Bundle.main.path(forResource: "ar_params", ofType: nil) ?! "para null" + modelDir = "" + preprocessKernel = MobilenetssdPreProccess.init(device: device) + dim = (n: 1, h: 160, w: 160, c: 3) + } + + class MobilenetssdPreProccess: CusomKernel { + init(device: MTLDevice) { + let s = CusomKernel.Shape.init(inWidth: 160, inHeight: 160, inChannel: 3) + super.init(device: device, inFunctionName: "mobilent_ar_preprocess_half", outputDim: s, usePaddleMobileLib: false) + } + } + + override public func resultStr(res: [Float]) -> String { + return " \(res)" + } + + override func fetchResult(paddleMobileRes: ResultHolder) -> [Float32] { + + guard let interRes = paddleMobileRes.intermediateResults else { + fatalError(" need have inter result ") + } + + guard let scores = interRes["Scores"], scores.count > 0, let score = scores[0] as? Texture else { + fatalError(" need score ") + } + + guard let bboxs = interRes["BBoxes"], bboxs.count > 0, let bbox = bboxs[0] as? Texture else { + fatalError() + } + + var scoreFormatArr: [Float32] = score.metalTexture.realNHWC(dim: (n: score.padToFourDim[0], h: score.padToFourDim[1], w: score.padToFourDim[2], c: score.padToFourDim[3])) + // print("score: ") + // print(scoreFormatArr.strideArray()) + // + var bboxArr = bbox.metalTexture.float32Array() + // print("bbox: ") + // print(bboxArr.strideArray()) + + let nmsCompute = NMSCompute.init() + nmsCompute.scoreThredshold = 0.01 + nmsCompute.nmsTopK = 400 + nmsCompute.keepTopK = 200 + nmsCompute.nmsEta = 1.0 + nmsCompute.nmsThreshold = 0.45 + nmsCompute.background_label = 0; + nmsCompute.scoreDim = [NSNumber.init(value: score.tensorDim[0]), NSNumber.init(value: score.tensorDim[1]), NSNumber.init(value: score.tensorDim[2])] + nmsCompute.bboxDim = [NSNumber.init(value: bbox.tensorDim[0]), NSNumber.init(value: bbox.tensorDim[1]), NSNumber.init(value: bbox.tensorDim[2])] + guard let result = nmsCompute.compute(withScore: &scoreFormatArr, andBBoxs: &bboxArr) else { + fatalError( " result error " ) + } + + let output: [Float32] = result.map { $0.floatValue } + return output + } +} diff --git a/metal/paddle-mobile/paddle-mobile/Operators/Base/Operator.swift b/metal/paddle-mobile/paddle-mobile/Operators/Base/Operator.swift index 0bd6b3692d0f983a8b154bec9727468942ef7a51..dded09dae7985829c062eac67f9df47cbcbd6084 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/Base/Operator.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/Base/Operator.swift @@ -38,7 +38,8 @@ extension Runable where Self: OperatorProtocol{ } func inputVariant() -> [String : [Variant]] { - fatalError(" op \(type) need implement inputVariant") + return [:] +// fatalError(" op \(type) need implement inputVariant") } func delogOutput() { @@ -166,7 +167,7 @@ let opInfos = [gConvType : (inputs: ["Input"], outputs: ["Out gPreluType : (inputs: ["X"], outputs: ["Out"]), gConv2dTransposeType : (inputs: ["Input"], outputs: ["Output"]), gBilinearInterpType : (inputs: ["X"], outputs: ["Out"]), - gSplit : (inputs: ["Input"], outputs: ["Out"]), + gSplit : (inputs: ["X"], outputs: ["Out"]), gShape : (inputs: ["Input"], outputs: ["Out"]), - gFlatten : (inputs: ["Input"], outputs: ["Out"]) + gFlatten : (inputs: ["X"], outputs: ["Out"]) ] diff --git a/metal/paddle-mobile/paddle-mobile/Operators/FlattenOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/FlattenOp.swift index a7e92bdff7161a41a8c278176e710474898814ef..70dd1c0fc8d47e336bf0d4d2695caa0fc7846ca9 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/FlattenOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/FlattenOp.swift @@ -14,21 +14,9 @@ import Foundation -class FlattenParam: OpParam { - typealias ParamPrecisionType = P - required init(opDesc: OpDesc, inScope: Scope) throws { - do { - output = try FlattenParam.output(outputs: opDesc.outputs, from: inScope) - } catch let error { - throw error - } - } - var output: Texture

-} - -class FlattenOp: Operator, FlattenParam

>, Runable, Creator, InferShaperable{ +class FlattenOp: Operator, ReshapeParam

>, Runable, Creator, InferShaperable{ - typealias OpType = SplitOp

+ typealias OpType = FlattenOp

func inferShape() { // para.output.dim = para.input.dim diff --git a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/FlattenKernel.swift b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/FlattenKernel.swift deleted file mode 100644 index 87c317f68ad22995e7981537ef7be29fb9a19cc5..0000000000000000000000000000000000000000 --- a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/FlattenKernel.swift +++ /dev/null @@ -1,40 +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. */ - -import Foundation - -struct FlattenMetalParam { -} - -class FlattenKernel: Kernel, Computable{ - func compute(commandBuffer: MTLCommandBuffer, param: FlattenParam

) throws { - guard let encoder = commandBuffer.makeComputeCommandEncoder() else { - throw PaddleMobileError.predictError(message: " encode is nil") - } - encoder.setTexture(param.output.metalTexture, index: 0) - encoder.endEncoding() - } - - required init(device: MTLDevice, param: FlattenParam

) { - param.output.initTexture(device: device, computePrecision: computePrecision) - if computePrecision == .Float32 { - super.init(device: device, inFunctionName: "split") - } else if computePrecision == .Float16 { - super.init(device: device, inFunctionName: "split_half") - } else { - fatalError() - } - } - -} diff --git a/metal/paddle-mobile/paddle-mobile/Operators/SplitOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/SplitOp.swift index 6ea783b55a206454e304ad0e117237b05a634c4d..5adc47c663e5abd639f2cc2f4c95ae4c565e897a 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/SplitOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/SplitOp.swift @@ -18,7 +18,8 @@ class SplitParam: OpParam { typealias ParamPrecisionType = P required init(opDesc: OpDesc, inScope: Scope) throws { do { - output = try SplitParam.output(outputs: opDesc.outputs, from: inScope) +// output = try SplitParam.output(outputs: opDesc.outputs, from: inScope) + output = try SplitParam.outputOut(outputs: opDesc.outputs, from: inScope) } catch let error { throw error } diff --git a/metal/paddle-mobile/paddle-mobile/PaddleMobile.swift b/metal/paddle-mobile/paddle-mobile/PaddleMobile.swift index be768f665f1d3ea315cb129e75ffae48038b3f93..cd329394c6cce744d6e3c4de61ed3df6aa00b070 100644 --- a/metal/paddle-mobile/paddle-mobile/PaddleMobile.swift +++ b/metal/paddle-mobile/paddle-mobile/PaddleMobile.swift @@ -16,7 +16,7 @@ import Foundation class ScaleKernel: CusomKernel { init(device: MTLDevice, shape: Shape) { - super.init(device: device, inFunctionName: "scale", outputDim: shape, usePaddleMobileLib: false) + super.init(device: device, inFunctionName: "scale_half", outputDim: shape, usePaddleMobileLib: false) } } @@ -37,7 +37,7 @@ public class Net: NSObject { fatalError() } func fetchResult(paddleMobileRes: ResultHolder) -> [Float32] { - fatalError() + return paddleMobileRes.resultArr } @objc public init(device: MTLDevice) { super.init() diff --git a/metal/paddle-mobile/paddle-mobile/framework/Executor.swift b/metal/paddle-mobile/paddle-mobile/framework/Executor.swift index 48f31341a0730f23eaa99b26997e64514e597de7..595bf2e5b541e6465df4342cd2ba2f5362c9b086 100644 --- a/metal/paddle-mobile/paddle-mobile/framework/Executor.swift +++ b/metal/paddle-mobile/paddle-mobile/framework/Executor.swift @@ -14,10 +14,9 @@ import Foundation -let testTo = 161 +let testTo = 3 var isTest = false - let computePrecision: ComputePrecision = .Float16 public class ResultHolder { @@ -59,8 +58,10 @@ public class Executor { var ops: [Runable & InferShaperable] = [] let program: Program let device: MTLDevice + let inflightSemaphore: DispatchSemaphore let queue: MTLCommandQueue public init(inDevice:MTLDevice, inQueue: MTLCommandQueue, inProgram: Program) throws { + self.inflightSemaphore = DispatchSemaphore(value: 3) program = inProgram device = inDevice queue = inQueue @@ -82,6 +83,8 @@ public class Executor { guard let buffer = queue.makeCommandBuffer() else { throw PaddleMobileError.predictError(message: "CommandBuffer is nil") } + inflightSemaphore.wait() + let resInput: MTLTexture if let inPre = preProcessKernle { do { @@ -112,12 +115,14 @@ public class Executor { outputTextures = ops[ops.count - except].inputVariant() } - buffer.addCompletedHandler { (commandbuffer) in + buffer.addCompletedHandler { [weak self] (commandbuffer) in // let inputArr = resInput.toTensor(dim: (n: dim[0], c: dim[3], h: dim[1], w: dim[2])) //// print(inputArr.strideArray()) -// -// writeToLibrary(fileName: "test_image_ssd", array: inputArr) + +// print(dim) +// writeToLibrary(fileName: "test_image_ssd_ar", array: inputArr) + // print("write to library done") // return // print(inputArr) @@ -139,18 +144,23 @@ public class Executor { // return + guard let SSelf = self else { + fatalError() + } + let afterDate = Date.init() var resultHolder: ResultHolder if except > 0 { resultHolder = ResultHolder.init(inDim: [], inResult: [], inElapsedTime: afterDate.timeIntervalSince(beforeDate), inIntermediateResults: outputTextures) } else { - let outputVar: Variant = self.program.scope.output()! + let outputVar: Variant = SSelf.program.scope.output()! let output: Texture

= outputVar as! Texture

resultHolder = ResultHolder.init(inDim: output.dim.dims, inResult: output.toTensor(), inElapsedTime: afterDate.timeIntervalSince(beforeDate)) } completionHandle(resultHolder) + SSelf.inflightSemaphore.signal() } buffer.commit() }