From 7cc0f8ef7318f64a7a87c2979898eb8d79e85557 Mon Sep 17 00:00:00 2001 From: liuruilong Date: Sun, 2 Sep 2018 21:04:02 +0800 Subject: [PATCH] run mobilenet+ssd genet --- .../Base.lproj/Main.storyboard | 29 +++++++++---------- .../paddle-mobile-demo/Net/MobileNetSSD.swift | 10 +++++-- .../paddle-mobile-demo/Net/Net.swift | 4 +-- .../paddle-mobile-demo/ViewController.swift | 4 +-- .../paddle-mobile/Common/MetalExtension.swift | 16 +++++----- .../paddle-mobile/Executor.swift | 26 +++++++---------- .../paddle-mobile/paddle-mobile/Loader.swift | 2 +- .../paddle-mobile/Operators/BoxcoderOp.swift | 20 ++++++------- .../paddle-mobile/Operators/ConcatOp.swift | 6 ++-- .../Operators/ConvAddBatchNormReluOp.swift | 2 +- .../Operators/ConvBNReluOp.swift | 2 +- .../paddle-mobile/Operators/ConvOp.swift | 2 +- .../Operators/ConvTransposeOp.swift | 4 +-- .../Operators/DepthwiseConvOp.swift | 2 +- .../Operators/DwConvBNReluOp.swift | 2 +- .../Operators/ElementwiseAddOp.swift | 4 +-- .../paddle-mobile/Operators/FeedOp.swift | 2 +- .../Operators/Kernels/PriorBoxKernel.swift | 8 ++--- .../paddle-mobile/Operators/PreluOp.swift | 4 +-- .../paddle-mobile/Operators/PriorBoxOp.swift | 6 ++-- .../paddle-mobile/Operators/ReshapeOp.swift | 8 ++--- .../paddle-mobile/Operators/SoftmaxOp.swift | 6 ++-- .../paddle-mobile/Operators/TransposeOp.swift | 4 +-- .../paddle-mobile/framework/Texture.swift | 20 +++++++++++-- 24 files changed, 103 insertions(+), 90 deletions(-) diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/Base.lproj/Main.storyboard b/metal/paddle-mobile-demo/paddle-mobile-demo/Base.lproj/Main.storyboard index 38094021c8..a5efadeb97 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/Base.lproj/Main.storyboard +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/Base.lproj/Main.storyboard @@ -19,10 +19,10 @@ - + - - + @@ -203,7 +203,6 @@ - diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/Net/MobileNetSSD.swift b/metal/paddle-mobile-demo/paddle-mobile-demo/Net/MobileNetSSD.swift index e48b5a7906..0be1a9ee15 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/Net/MobileNetSSD.swift +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/Net/MobileNetSSD.swift @@ -33,7 +33,7 @@ class MobileNet_ssd_hand: Net{ return " \(res)" } - func fetchResult(paddleMobileRes: ResultHolder) -> [Float32] { + func fetchResult(paddleMobileRes: ResultHolder) -> [Float32] { guard let interRes = paddleMobileRes.intermediateResults else { fatalError(" need have inter result ") @@ -47,13 +47,17 @@ class MobileNet_ssd_hand: Net{ fatalError() } - var scoreFormatArr: [Float32] = score.metalTexture.realNHWC(dim: (n: score.originDim[0], h: score.originDim[1], w: score.originDim[2], c: score.originDim[3])) + 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 = 200 + nmsCompute.nmsTopK = 400 nmsCompute.keepTopK = 200 nmsCompute.nmsEta = 1.0 nmsCompute.nmsThreshold = 0.45 diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/Net/Net.swift b/metal/paddle-mobile-demo/paddle-mobile-demo/Net/Net.swift index d7738a30a2..c643b4b63d 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/Net/Net.swift +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/Net/Net.swift @@ -37,7 +37,7 @@ protocol Net { var preprocessKernel: CusomKernel { get } func getTexture(image: CGImage, getTexture: @escaping (MTLTexture) -> Void) func resultStr(res: [Float]) -> String - func fetchResult(paddleMobileRes: ResultHolder) -> [Float32] + func fetchResult(paddleMobileRes: ResultHolder) -> [Float32] mutating func load() throws func predict(inTexture: MTLTexture, completion: @escaping ((time:TimeInterval, resultArray: [Float32])) -> Void) throws @@ -82,7 +82,7 @@ extension Net { } } - func fetchResult(paddleMobileRes: ResultHolder) -> [Float32] { + func fetchResult(paddleMobileRes: ResultHolder) -> [Float32] { return paddleMobileRes.resultArr } diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift b/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift index e4697135e9..ecffd6af6d 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift @@ -87,7 +87,7 @@ class ViewController: UIViewController { fatalError() } -// print(result.resultArray) + print(result.resultArray.strideArray()) if i == max - 1 { let time = Date.init().timeIntervalSince(startDate) DispatchQueue.main.async { @@ -109,7 +109,7 @@ class ViewController: UIViewController { threadPickerView.delegate = self threadPickerView.dataSource = self - selectImage = UIImage.init(named: "banana.jpeg") + selectImage = UIImage.init(named: "hand.jpg") selectImageView.image = selectImage net.getTexture(image: selectImage!.cgImage!) {[weak self] (texture) in self?.toPredictTexture = texture diff --git a/metal/paddle-mobile/paddle-mobile/Common/MetalExtension.swift b/metal/paddle-mobile/paddle-mobile/Common/MetalExtension.swift index 95ba4b0c59..afbdccce5d 100644 --- a/metal/paddle-mobile/paddle-mobile/Common/MetalExtension.swift +++ b/metal/paddle-mobile/paddle-mobile/Common/MetalExtension.swift @@ -113,7 +113,7 @@ extension MTLDevice { return tensor } - func tensor2texture

(value: [P], dim: [Int], transpose: [Int] = [0, 1, 2, 3]) -> MTLTexture { + func tensor2texture

(value: [P], dim: [Int], transpose: [Int] = [0, 1, 2, 3], inComputePrecision: ComputePrecision = .Float32) -> MTLTexture { if value.count > 0 { assert(value.count == dim.reduce(1) { $0 * $1 }) } @@ -129,7 +129,13 @@ extension MTLDevice { textureDesc.height = ndim[1] textureDesc.depth = 1 textureDesc.usage = [.shaderRead, .shaderWrite] - textureDesc.pixelFormat = .rgba32Float + + if inComputePrecision == .Float16 { + textureDesc.pixelFormat = .rgba16Float + } else if inComputePrecision == .Float32 { + textureDesc.pixelFormat = .rgba32Float + } + textureDesc.textureType = .type2DArray textureDesc.storageMode = .shared textureDesc.cpuCacheMode = .defaultCache @@ -355,12 +361,7 @@ public extension MTLTexture { // n c h w - dim func toTensor(dim: (n: Int, c: Int, h: Int, w: Int)) -> [Float32] { -// print("origin dim: \(dim)") - print("texture: ") - print(self) var textureArray: [Float32] -// if texturePrecision == .Float16 - if pixelFormat == .rgba32Float { textureArray = floatArray { (i : Float32) -> Float32 in return i @@ -388,7 +389,6 @@ public extension MTLTexture { } } } - print(" tensor count -- \(output.count)") return output } diff --git a/metal/paddle-mobile/paddle-mobile/Executor.swift b/metal/paddle-mobile/paddle-mobile/Executor.swift index 1d7e5a452a..0085cac22f 100644 --- a/metal/paddle-mobile/paddle-mobile/Executor.swift +++ b/metal/paddle-mobile/paddle-mobile/Executor.swift @@ -18,14 +18,14 @@ let testTo = 161 var isTest = false -let computePrecision: ComputePrecision = .Float32 +let computePrecision: ComputePrecision = .Float16 -public class ResultHolder { +public class ResultHolder { public let dim: [Int] - public let resultArr: [P] + public let resultArr: [Float32] public var intermediateResults: [String : [Variant]]? public let elapsedTime: Double - public init(inDim: [Int], inResult: [P], inElapsedTime: Double, inIntermediateResults: [String : [Variant]]? = nil) { + public init(inDim: [Int], inResult: [Float32], inElapsedTime: Double, inIntermediateResults: [String : [Variant]]? = nil) { dim = inDim resultArr = inResult elapsedTime = inElapsedTime @@ -78,7 +78,7 @@ public class Executor { } } - public func predict(input: MTLTexture, dim: [Int], completionHandle: @escaping (ResultHolder

) -> Void, preProcessKernle: CusomKernel? = nil, except: Int = 0) throws { + public func predict(input: MTLTexture, dim: [Int], completionHandle: @escaping (ResultHolder) -> Void, preProcessKernle: CusomKernel? = nil, except: Int = 0) throws { guard let buffer = queue.makeCommandBuffer() else { throw PaddleMobileError.predictError(message: "CommandBuffer is nil") } @@ -114,12 +114,10 @@ public class Executor { buffer.addCompletedHandler { (commandbuffer) in -// let inputArr = resInput.floatArray(res: { (p:P) -> P in -// return p -// }) -// print(inputArr.strideArray()) +// let inputArr = resInput.toTensor(dim: (n: dim[0], c: dim[3], h: dim[1], w: dim[2])) +//// print(inputArr.strideArray()) // -// writeToLibrary(fileName: "banana", array: inputArr) +// writeToLibrary(fileName: "test_image_ssd", array: inputArr) // print("write to library done") // return // print(inputArr) @@ -142,16 +140,14 @@ public class Executor { // return let afterDate = Date.init() - var resultHolder: ResultHolder

+ var resultHolder: ResultHolder if except > 0 { - resultHolder = ResultHolder

.init(inDim: [], inResult: [], inElapsedTime: afterDate.timeIntervalSince(beforeDate), inIntermediateResults: outputTextures) + resultHolder = ResultHolder.init(inDim: [], inResult: [], inElapsedTime: afterDate.timeIntervalSince(beforeDate), inIntermediateResults: outputTextures) } else { let outputVar: Variant = self.program.scope.output()! let output: Texture

= outputVar as! Texture

- resultHolder = ResultHolder

.init(inDim: output.dim.dims, inResult: output.metalTexture.floatArray(res: { (p:P) -> P in - return p - }), inElapsedTime: afterDate.timeIntervalSince(beforeDate)) + resultHolder = ResultHolder.init(inDim: output.dim.dims, inResult: output.toTensor(), inElapsedTime: afterDate.timeIntervalSince(beforeDate)) } completionHandle(resultHolder) diff --git a/metal/paddle-mobile/paddle-mobile/Loader.swift b/metal/paddle-mobile/paddle-mobile/Loader.swift index d9df4cf24c..68ce8c0691 100644 --- a/metal/paddle-mobile/paddle-mobile/Loader.swift +++ b/metal/paddle-mobile/paddle-mobile/Loader.swift @@ -168,7 +168,7 @@ public class Loader { } } else { if varDesc.name == fetchKey { - scope[varDesc.name] = ResultHolder

.init(inDim: [], inResult: [], inElapsedTime: 0.0) + scope[varDesc.name] = ResultHolder.init(inDim: [], inResult: [], inElapsedTime: 0.0) } else if varDesc.name == feedKey { } } diff --git a/metal/paddle-mobile/paddle-mobile/Operators/BoxcoderOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/BoxcoderOp.swift index e36a1cbb4f..d7c1aba249 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/BoxcoderOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/BoxcoderOp.swift @@ -59,28 +59,28 @@ class BoxcoderOp: Operator, BoxcoderParam

func delogOutput() { print(" \(type) output: ") -// let priorBoxOriginDim = para.priorBox.originDim -// let priorBoxArray: [Float32] = para.priorBox.metalTexture.realNHWC(dim: (n: priorBoxOriginDim[0], h: priorBoxOriginDim[1], w: priorBoxOriginDim[2], c: priorBoxOriginDim[3])) +// let priorBoxpadToFourDim = para.priorBox.padToFourDim +// let priorBoxArray: [Float32] = para.priorBox.metalTexture.realNHWC(dim: (n: priorBoxpadToFourDim[0], h: priorBoxpadToFourDim[1], w: priorBoxpadToFourDim[2], c: priorBoxpadToFourDim[3])) // print(" prior box ") // print(priorBoxArray.strideArray()) // -// let priorBoxVarOriginDim = para.priorBoxVar.originDim -// let priorBoxVarArray: [Float32] = para.priorBoxVar.metalTexture.realNHWC(dim: (n: priorBoxVarOriginDim[0], h: priorBoxVarOriginDim[1], w: priorBoxVarOriginDim[2], c: priorBoxVarOriginDim[3])) +// let priorBoxVarpadToFourDim = para.priorBoxVar.padToFourDim +// let priorBoxVarArray: [Float32] = para.priorBoxVar.metalTexture.realNHWC(dim: (n: priorBoxVarpadToFourDim[0], h: priorBoxVarpadToFourDim[1], w: priorBoxVarpadToFourDim[2], c: priorBoxVarpadToFourDim[3])) // print(" prior box var ") // print(priorBoxVarArray.strideArray()) // -// let targetBoxOriginDim = para.targetBox.originDim -// let targetBoxArray: [Float32] = para.targetBox.metalTexture.realNHWC(dim: (n: targetBoxOriginDim[0], h: targetBoxOriginDim[1], w: targetBoxOriginDim[2], c: targetBoxOriginDim[3])) +// let targetBoxpadToFourDim = para.targetBox.padToFourDim +// let targetBoxArray: [Float32] = para.targetBox.metalTexture.realNHWC(dim: (n: targetBoxpadToFourDim[0], h: targetBoxpadToFourDim[1], w: targetBoxpadToFourDim[2], c: targetBoxpadToFourDim[3])) // print(" target box ") // print(targetBoxArray.strideArray()) - let targetBoxOriginDim = para.targetBox.originDim - let targetBoxArray = para.targetBox.metalTexture.realNHWC(dim: (n: targetBoxOriginDim[0], h: targetBoxOriginDim[1], w: targetBoxOriginDim[2], c: targetBoxOriginDim[3])) + let targetBoxpadToFourDim = para.targetBox.padToFourDim + let targetBoxArray = para.targetBox.metalTexture.realNHWC(dim: (n: targetBoxpadToFourDim[0], h: targetBoxpadToFourDim[1], w: targetBoxpadToFourDim[2], c: targetBoxpadToFourDim[3])) print(" target box ") print(targetBoxArray.strideArray()) - let originDim = para.output.originDim - let outputArray: [Float32] = para.output.metalTexture.realNHWC(dim: (n: originDim[0], h: originDim[1], w: originDim[2], c: originDim[3])) + let padToFourDim = para.output.padToFourDim + let outputArray: [Float32] = para.output.metalTexture.realNHWC(dim: (n: padToFourDim[0], h: padToFourDim[1], w: padToFourDim[2], c: padToFourDim[3])) print(" output ") print(outputArray.strideArray()) } diff --git a/metal/paddle-mobile/paddle-mobile/Operators/ConcatOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/ConcatOp.swift index 42e7903937..1abdb66aa7 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/ConcatOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/ConcatOp.swift @@ -65,12 +65,12 @@ class ConcatOp: Operator, ConcatParam

>, Run func delogOutput() { print(" \(type) output: ") - let originDim = para.output.originDim + let padToFourDim = para.output.padToFourDim if para.output.transpose == [0, 1, 2, 3] { - let outputArray: [Float32] = para.output.metalTexture.realNHWC(dim: (n: originDim[0], h: originDim[1], w: originDim[2], c: originDim[3])) + let outputArray: [Float32] = para.output.metalTexture.realNHWC(dim: (n: padToFourDim[0], h: padToFourDim[1], w: padToFourDim[2], c: padToFourDim[3])) print(outputArray.strideArray()) } else if para.output.transpose == [0, 2, 3, 1] { - print(para.output.metalTexture.toTensor(dim: (n: originDim[0], c: originDim[1], h: originDim[2], w: originDim[3])).strideArray()) + print(para.output.metalTexture.toTensor(dim: (n: padToFourDim[0], c: padToFourDim[1], h: padToFourDim[2], w: padToFourDim[3])).strideArray()) } else { fatalError(" not implemet") } diff --git a/metal/paddle-mobile/paddle-mobile/Operators/ConvAddBatchNormReluOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/ConvAddBatchNormReluOp.swift index abdadf70ee..43935b65d1 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/ConvAddBatchNormReluOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/ConvAddBatchNormReluOp.swift @@ -112,7 +112,7 @@ class ConvAddBatchNormReluOp: Operator: Operator, ConvBNReluPa func delogOutput() { print(" \(type) output: ") - print(para.output.metalTexture.toTensor(dim: (n: para.output.originDim[0], c: para.output.originDim[1], h: para.output.originDim[2], w: para.output.originDim[3])).strideArray()) + print(para.output.metalTexture.toTensor(dim: (n: para.output.padToFourDim[0], c: para.output.padToFourDim[1], h: para.output.padToFourDim[2], w: para.output.padToFourDim[3])).strideArray()) } } diff --git a/metal/paddle-mobile/paddle-mobile/Operators/ConvOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/ConvOp.swift index 98f4a07767..e82eb1f475 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/ConvOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/ConvOp.swift @@ -75,7 +75,7 @@ class ConvOp: Operator, ConvParam

>, Runable, func delogOutput() { print("conv output : ") - print(para.output.metalTexture.toTensor(dim: (n: para.output.originDim[0], c: para.output.originDim[1], h: para.output.originDim[2], w: para.output.originDim[3])).strideArray()) + print(para.output.toTensor().strideArray()) // let _: Float16? = para.output.metalTexture.logDesc() } } diff --git a/metal/paddle-mobile/paddle-mobile/Operators/ConvTransposeOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/ConvTransposeOp.swift index e149e0ac15..e1b62c24f4 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/ConvTransposeOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/ConvTransposeOp.swift @@ -45,9 +45,9 @@ class ConvTransposeOp: Operator, ConvTr func delogOutput() { print(" \(type) output: ") - let originDim = para.output.originDim + let padToFourDim = para.output.padToFourDim if para.output.transpose == [0, 1, 2, 3] { - let outputArray: [Float32] = para.output.metalTexture.realNHWC(dim: (n: originDim[0], h: originDim[1], w: originDim[2], c: originDim[3])) + let outputArray: [Float32] = para.output.metalTexture.realNHWC(dim: (n: padToFourDim[0], h: padToFourDim[1], w: padToFourDim[2], c: padToFourDim[3])) print(outputArray.strideArray()) } else if para.output.transpose == [0, 2, 3, 1] { let output = para.output.metalTexture.toTensor(dim: (n: para.output.tensorDim[0], c: para.output.tensorDim[1], h: para.output.tensorDim[2], w: para.output.tensorDim[3])) diff --git a/metal/paddle-mobile/paddle-mobile/Operators/DepthwiseConvOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/DepthwiseConvOp.swift index 639c22ce12..ec76eecf1f 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/DepthwiseConvOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/DepthwiseConvOp.swift @@ -58,6 +58,6 @@ class DepthConvOp: Operator, ConvParam

>, Runa func delogOutput() { print(" \(type) output: ") - print(para.output.metalTexture.toTensor(dim: (n: para.output.originDim[0], c: para.output.originDim[1], h: para.output.originDim[2], w: para.output.originDim[3])).strideArray()) + print(para.output.metalTexture.toTensor(dim: (n: para.output.padToFourDim[0], c: para.output.padToFourDim[1], h: para.output.padToFourDim[2], w: para.output.padToFourDim[3])).strideArray()) } } diff --git a/metal/paddle-mobile/paddle-mobile/Operators/DwConvBNReluOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/DwConvBNReluOp.swift index 16a42d5c7b..8575cfd88c 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/DwConvBNReluOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/DwConvBNReluOp.swift @@ -65,6 +65,6 @@ class DwConvBNReluOp: Operator, ConvBNRelu func delogOutput() { print(" \(type) output: ") - print(para.output.metalTexture.toTensor(dim: (n: para.output.originDim[0], c: para.output.originDim[1], h: para.output.originDim[2], w: para.output.originDim[3])).strideArray()) + print(para.output.metalTexture.toTensor(dim: (n: para.output.padToFourDim[0], c: para.output.padToFourDim[1], h: para.output.padToFourDim[2], w: para.output.padToFourDim[3])).strideArray()) } } diff --git a/metal/paddle-mobile/paddle-mobile/Operators/ElementwiseAddOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/ElementwiseAddOp.swift index 889fef56c5..7fac84812b 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/ElementwiseAddOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/ElementwiseAddOp.swift @@ -74,9 +74,9 @@ class ElementwiseAddOp: Operator, Elem print(para.inputY) - let originDim = para.output.originDim + let padToFourDim = para.output.padToFourDim if para.output.transpose == [0, 1, 2, 3] { - let outputArray: [Float32] = para.output.metalTexture.realNHWC(dim: (n: originDim[0], h: originDim[1], w: originDim[2], c: originDim[3])) + let outputArray: [Float32] = para.output.metalTexture.realNHWC(dim: (n: padToFourDim[0], h: padToFourDim[1], w: padToFourDim[2], c: padToFourDim[3])) print(outputArray.strideArray()) } else if para.output.transpose == [0, 2, 3, 1] { print(para.output.metalTexture.toTensor(dim: (n: para.output.tensorDim[0], c: para.output.tensorDim[1], h: para.output.tensorDim[2], w: para.output.tensorDim[3])).strideArray()) diff --git a/metal/paddle-mobile/paddle-mobile/Operators/FeedOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/FeedOp.swift index 9356058216..382ea58b84 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/FeedOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/FeedOp.swift @@ -61,7 +61,7 @@ class FeedOp: Operator, FeedParam< func delogOutput() { print(" \(type) output: ") - print(para.output.metalTexture.toTensor(dim: (n: para.output.originDim[0], c: para.output.originDim[1], h: para.output.originDim[2], w: para.output.originDim[3])).strideArray()) + print(para.output.metalTexture.toTensor(dim: (n: para.output.padToFourDim[0], c: para.output.padToFourDim[1], h: para.output.padToFourDim[2], w: para.output.padToFourDim[3])).strideArray()) } } diff --git a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/PriorBoxKernel.swift b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/PriorBoxKernel.swift index 07239c3288..426e91cfbe 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/PriorBoxKernel.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/PriorBoxKernel.swift @@ -53,11 +53,11 @@ class PriorBoxKernel: Kernel, Computable{ param.output.dim = Dim.init(inDim: [n, h, w, c]) param.output.transpose = [0, 1, 2, 3] - let imageWidth = Float32(param.inputImage.originDim[3]) - let imageHeight = Float32(param.inputImage.originDim[2]) + let imageWidth = Float32(param.inputImage.padToFourDim[3]) + let imageHeight = Float32(param.inputImage.padToFourDim[2]) - let featureWidth = param.input.originDim[3] - let featureHeight = param.input.originDim[2] + let featureWidth = param.input.padToFourDim[3] + let featureHeight = param.input.padToFourDim[2] if param.stepW == 0 || param.stepH == 0 { param.stepW = Float32(imageWidth) / Float32(featureWidth) diff --git a/metal/paddle-mobile/paddle-mobile/Operators/PreluOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/PreluOp.swift index 10b5816d7b..52cab3425f 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/PreluOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/PreluOp.swift @@ -51,13 +51,13 @@ class PreluOp: Operator, PreluParam

>, Runabl func delogOutput() { print(" \(type) input: ") - print(para.input.metalTexture.toTensor(dim: (n: para.input.originDim[0], c: para.input.originDim[1], h: para.input.originDim[2], w: para.input.originDim[3])).strideArray()) + print(para.input.metalTexture.toTensor(dim: (n: para.input.padToFourDim[0], c: para.input.padToFourDim[1], h: para.input.padToFourDim[2], w: para.input.padToFourDim[3])).strideArray()) print(" \(type) Alpha: ") let _: Float32? = para.alpha.buffer.logDesc(header: " alpha: ", stridable: false) print(" \(type) output: ") - print(para.output.metalTexture.toTensor(dim: (n: para.output.originDim[0], c: para.output.originDim[1], h: para.output.originDim[2], w: para.output.originDim[3])).strideArray()) + print(para.output.metalTexture.toTensor(dim: (n: para.output.padToFourDim[0], c: para.output.padToFourDim[1], h: para.output.padToFourDim[2], w: para.output.padToFourDim[3])).strideArray()) } // print("softmax delog") diff --git a/metal/paddle-mobile/paddle-mobile/Operators/PriorBoxOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/PriorBoxOp.swift index d48fc4cd1c..4a27fba983 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/PriorBoxOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/PriorBoxOp.swift @@ -76,12 +76,12 @@ class PriorBoxOp: Operator, PriorBoxParam

print(outputArray) // output // print(" \(type) output: ") -// let originDim = para.output.originDim +// let padToFourDim = para.output.padToFourDim // if para.output.transpose == [0, 1, 2, 3] { -// let outputArray: [Float32] = para.output.metalTexture.realNHWC(dim: (n: originDim[0], h: originDim[1], w: originDim[2], c: originDim[3]), texturePrecision: computePrecision) +// let outputArray: [Float32] = para.output.metalTexture.realNHWC(dim: (n: padToFourDim[0], h: padToFourDim[1], w: padToFourDim[2], c: padToFourDim[3]), texturePrecision: computePrecision) // print(outputArray.strideArray()) // } else if para.output.transpose == [0, 2, 3, 1] { -// print(para.output.metalTexture.toTensor(dim: (n: originDim[0], c: originDim[1], h: originDim[2], w: originDim[3]), texturePrecision: computePrecision).strideArray()) +// print(para.output.metalTexture.toTensor(dim: (n: padToFourDim[0], c: padToFourDim[1], h: padToFourDim[2], w: padToFourDim[3]), texturePrecision: computePrecision).strideArray()) // } else { // print(" not implement") // } diff --git a/metal/paddle-mobile/paddle-mobile/Operators/ReshapeOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/ReshapeOp.swift index 448896dd33..b37026d6a8 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/ReshapeOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/ReshapeOp.swift @@ -41,8 +41,8 @@ class ReshapeParam: OpParam { for i in 0..: Operator, ReshapeParam

>, print("reshape delog") // let _: P? = para.input.metalTexture.logDesc(header: "reshape input: ", stridable: false) - let originDim = para.output.originDim + let padToFourDim = para.output.padToFourDim - let outputArray: [Float32] = para.output.metalTexture.realNHWC(dim: (n: originDim[0], h: originDim[1], w: originDim[2], c: originDim[3])) + let outputArray: [Float32] = para.output.metalTexture.realNHWC(dim: (n: padToFourDim[0], h: padToFourDim[1], w: padToFourDim[2], c: padToFourDim[3])) print(outputArray.strideArray()) } diff --git a/metal/paddle-mobile/paddle-mobile/Operators/SoftmaxOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/SoftmaxOp.swift index 26a50f1533..66b5c7b314 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/SoftmaxOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/SoftmaxOp.swift @@ -26,7 +26,7 @@ class SoftmaxParam: OpParam { output.dim = input.dim output.tensorDim = input.tensorDim - output.originDim = input.originDim + output.padToFourDim = input.padToFourDim } catch let error { throw error } @@ -55,8 +55,8 @@ class SoftmaxOp: Operator, SoftmaxParam

>, print(para.input) print(para.output) - let originDim = para.output.originDim - let outputArray: [Float32] = para.output.metalTexture.realNHWC(dim: (n: originDim[0], h: originDim[1], w: originDim[2], c: originDim[3])) + let padToFourDim = para.output.padToFourDim + let outputArray: [Float32] = para.output.metalTexture.realNHWC(dim: (n: padToFourDim[0], h: padToFourDim[1], w: padToFourDim[2], c: padToFourDim[3])) print(outputArray.strideArray()) } } diff --git a/metal/paddle-mobile/paddle-mobile/Operators/TransposeOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/TransposeOp.swift index 8281ba5433..0213b52bf1 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/TransposeOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/TransposeOp.swift @@ -48,9 +48,9 @@ class TransposeOp: Operator, TransposeParam func delogOutput() { print(" \(type) output: ") - let originDim = para.output.originDim + let padToFourDim = para.output.padToFourDim if para.output.transpose == [0, 1, 2, 3] { - let outputArray = para.output.metalTexture.realNHWC(dim: (n: originDim[0], h: originDim[1], w: originDim[2], c: originDim[3])) + let outputArray = para.output.metalTexture.realNHWC(dim: (n: padToFourDim[0], h: padToFourDim[1], w: padToFourDim[2], c: padToFourDim[3])) print(outputArray.strideArray()) } else if para.output.transpose == [0, 2, 3, 1] { print(para.output.metalTexture.toTensor(dim: (n: para.output.tensorDim[0], c: para.output.tensorDim[1], h: para.output.tensorDim[2], w: para.output.tensorDim[3])).strideArray()) diff --git a/metal/paddle-mobile/paddle-mobile/framework/Texture.swift b/metal/paddle-mobile/paddle-mobile/framework/Texture.swift index 42a381c50c..d3beb92a5f 100644 --- a/metal/paddle-mobile/paddle-mobile/framework/Texture.swift +++ b/metal/paddle-mobile/paddle-mobile/framework/Texture.swift @@ -41,14 +41,28 @@ extension InputTexture { public class Texture: Tensorial { var dim: Dim public var tensorDim: Dim - public var originDim: Dim + public var padToFourDim: Dim private var textureDesc: MTLTextureDescriptor! public var metalTexture: MTLTexture! var transpose: [Int] = [0, 1, 2, 3] + func toTensor() -> [Float32] { + guard padToFourDim.cout() == 4 else { + fatalError("- not support -") + } + return metalTexture.toTensor(dim: (n: padToFourDim[0], c: padToFourDim[1], h: padToFourDim[2], w: padToFourDim[3])) + } + + func realNHWC() -> [Float32] { + guard padToFourDim.cout() == 4 else { + fatalError(" - not support - ") + } + return metalTexture.realNHWC(dim: (n: padToFourDim[0], h: padToFourDim[1], w: padToFourDim[2], c: padToFourDim[3])) + } + func initTexture(device: MTLDevice, inTranspose: [Int] = [0, 1, 2, 3], computePrecision: ComputePrecision = .Float16) { transpose = inTranspose - let newDim = transpose.map { originDim[$0] } + let newDim = transpose.map { padToFourDim[$0] } let newLayout = transpose.map { layout.layoutWithDim[$0] } @@ -93,7 +107,7 @@ public class Texture: Tensorial { } tensorDim = inDim dim = fourDim - originDim = fourDim + padToFourDim = fourDim layout = DataLayout.init([(.N, fourDim[0]), (.C, fourDim[1]), (.H, fourDim[2]), (.W, fourDim[3])]) } -- GitLab