From ad4730f940f71ff127a3cb58b95967d7687c9bbf Mon Sep 17 00:00:00 2001 From: xiaohaichun Date: Fri, 21 Sep 2018 18:10:47 +0800 Subject: [PATCH] update ssdar --- .../LoadPointerViewController.m | 20 ++++++++---- .../paddle-mobile/MobilenetSSD_AR.swift | 32 +++++++++++++------ .../paddle-mobile/PaddleMobileGPU.m | 2 +- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/LoadPointerViewController.m b/metal/paddle-mobile-demo/paddle-mobile-demo/LoadPointerViewController.m index 8837a7c3de..857745686f 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/LoadPointerViewController.m +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/LoadPointerViewController.m @@ -31,8 +31,11 @@ self.queue = [self.device newCommandQueue]; // Do any additional setup after loading the view. - NSString *modelPath = [[NSBundle mainBundle] URLForResource:@"genet_model" withExtension:nil].path; - NSString *paramPath = [[NSBundle mainBundle] URLForResource:@"genet_params" withExtension:nil].path; +// NSString *modelPath = [[NSBundle mainBundle] URLForResource:@"genet_model" withExtension:nil].path; +// NSString *paramPath = [[NSBundle mainBundle] URLForResource:@"genet_params" withExtension:nil].path; + + NSString *modelPath = [[NSBundle mainBundle] URLForResource:@"ar_model" withExtension:nil].path; + NSString *paramPath = [[NSBundle mainBundle] URLForResource:@"ar_params" withExtension:nil].path; long fileSize; FILE *fp; @@ -55,16 +58,21 @@ fclose(parmaFilePointer); _modelConfig = [[ModelConfig alloc] init]; - _modelConfig.means = @[[NSNumber numberWithFloat:128.0], [NSNumber numberWithFloat:128.0], [NSNumber numberWithFloat:128.0]]; - _modelConfig.scale = 0.017; - _modelConfig.dims = @[[NSNumber numberWithFloat:1], [NSNumber numberWithFloat:128.], [NSNumber numberWithFloat:128.0],[NSNumber numberWithFloat:3.0]]; +// _modelConfig.means = @[[NSNumber numberWithFloat:128.0], [NSNumber numberWithFloat:128.0], [NSNumber numberWithFloat:128.0]]; +// _modelConfig.scale = 0.017; +// _modelConfig.dims = @[[NSNumber numberWithFloat:1], [NSNumber numberWithFloat:128.], [NSNumber numberWithFloat:128.0],[NSNumber numberWithFloat:3.0]]; + _modelConfig.means = @[[NSNumber numberWithFloat:103.94], [NSNumber numberWithFloat:116.78], [NSNumber numberWithFloat:123.68]]; + _modelConfig.scale = 1; + _modelConfig.dims = @[[NSNumber numberWithFloat:1], [NSNumber numberWithFloat:160.], [NSNumber numberWithFloat:160.0],[NSNumber numberWithFloat:3.0]]; _modelConfig.modelPointer = buffer; _modelConfig.modelSize = (int)fileSize; _modelConfig.paramPointer = parmaBuffer; _modelConfig.paramSize = (int)paramfileSize; } - (IBAction)loaderButtonPressed:(id)sender { - _runner = [[PaddleMobileGPU alloc] initWithCommandQueue:self.queue net:GenetType modelConfig:_modelConfig]; +// _runner = [[PaddleMobileGPU alloc] initWithCommandQueue:self.queue net:GenetType modelConfig:_modelConfig]; + _runner = [[PaddleMobileGPU alloc] initWithCommandQueue:self.queue net:MobileNetSSDType modelConfig:_modelConfig]; + [_runner load]; } - (IBAction)predictButtonPressed:(id)sender { diff --git a/metal/paddle-mobile/paddle-mobile/MobilenetSSD_AR.swift b/metal/paddle-mobile/paddle-mobile/MobilenetSSD_AR.swift index aeb657403c..6c7bd9b9c6 100644 --- a/metal/paddle-mobile/paddle-mobile/MobilenetSSD_AR.swift +++ b/metal/paddle-mobile/paddle-mobile/MobilenetSSD_AR.swift @@ -27,6 +27,18 @@ public class MobileNet_ssd_AR: Net{ dim = (n: 1, h: 160, w: 160, c: 3) } + @objc override public init(device: MTLDevice,paramPointer: UnsafeMutableRawPointer, paramSize:Int, modePointer: UnsafeMutableRawPointer, modelSize: Int) { + super.init(device:device,paramPointer:paramPointer,paramSize:paramSize,modePointer:modePointer,modelSize:modelSize) + means = [103.94, 116.78, 123.68] + scale = 1 + except = 2 + modelPath = "" + paramPath = "" + 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) @@ -90,32 +102,32 @@ public class MobileNet_ssd_AR: Net{ let v = program.scope[output]! let originTexture = v as! Texture originTexture.tensorDim = Dim.init(inDim: [originTexture.tensorDim[1] / 7, originTexture.tensorDim[0] * 7]) - + originTexture.dim = Dim.init(inDim: [1, 1, originTexture.dim[3] / 7, originTexture.dim[2] * 7]) - + originTexture.padToFourDim = Dim.init(inDim: [1, 1, originTexture.padToFourDim[3] / 7, originTexture.padToFourDim[2] * 7]) - + program.scope[output] = originTexture - + if i == 99 { opDesc.attrs["axis"] = 0 } else { opDesc.attrs["shape"] = originTexture.tensorDim.dims.map { Int32($0) } } } - + for i in [58, 59, 88, 89, 95, 96, 68, 69, 78, 79] { let opDesc = program.programDesc.blocks[0].ops[i] let output = opDesc.outputs["Out"]!.first! let v = program.scope[output]! - - - + + + let originTexture = v as! Texture originTexture.tensorDim = Dim.init(inDim: [originTexture.tensorDim[1], originTexture.tensorDim[2]]) opDesc.attrs["shape"] = originTexture.tensorDim.dims.map { Int32($0) } } - + for i in [60, 101, 90, 97, 70, 80] { let opDesc = program.programDesc.blocks[0].ops[i] let output = opDesc.outputs["Out"]!.first! @@ -124,7 +136,7 @@ public class MobileNet_ssd_AR: Net{ originTexture.tensorDim = Dim.init(inDim: [originTexture.tensorDim[1], originTexture.tensorDim[2]]) opDesc.attrs["axis"] = (opDesc.attrs["axis"]! as! Int) - 1 } - + for i in [102] { let opDesc = program.programDesc.blocks[0].ops[i] for output in opDesc.outputs["Out"]! { diff --git a/metal/paddle-mobile/paddle-mobile/PaddleMobileGPU.m b/metal/paddle-mobile/paddle-mobile/PaddleMobileGPU.m index 4d572df9ff..8f2e8f9ce6 100644 --- a/metal/paddle-mobile/paddle-mobile/PaddleMobileGPU.m +++ b/metal/paddle-mobile/paddle-mobile/PaddleMobileGPU.m @@ -49,7 +49,7 @@ if (netType == GenetType) { net = [[Genet alloc] initWithDevice:queue.device paramPointer:config.paramPointer paramSize:config.paramSize modePointer:config.modelPointer modelSize:config.modelSize]; } else if (netType == MobileNetSSDType) { - net = [[MobileNet_ssd_hand alloc] initWithDevice:queue.device paramPointer:config.paramPointer paramSize:config.paramSize modePointer:config.modelPointer modelSize:config.modelSize]; + net = [[MobileNet_ssd_AR alloc] initWithDevice:queue.device paramPointer:config.paramPointer paramSize:config.paramSize modePointer:config.modelPointer modelSize:config.modelSize]; } else if (netType == MobileNetType) { } -- GitLab