From cc23582c981233351c6d245e8f7e1ffeb7f1a199 Mon Sep 17 00:00:00 2001 From: liuruilong Date: Wed, 2 Jan 2019 21:44:50 +0800 Subject: [PATCH] commit --- .../project.pbxproj | 8 + .../paddle-mobile-demo/ViewController.swift | 47 +++++- .../CustomNet/SuperResolutionNet.swift | 7 - .../paddle-mobile/framework/Executor.swift | 14 +- test/net/test_super.cpp | 154 +++++++++--------- test/test_helper.h | 2 +- 6 files changed, 138 insertions(+), 94 deletions(-) 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 bee3fa3c18..20353bf9d5 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo.xcodeproj/project.pbxproj +++ b/metal/paddle-mobile-demo/paddle-mobile-demo.xcodeproj/project.pbxproj @@ -17,6 +17,7 @@ FC039B8920E11C560081E9F8 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FC039B8820E11C560081E9F8 /* Assets.xcassets */; }; FC039B8C20E11C560081E9F8 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FC039B8A20E11C560081E9F8 /* LaunchScreen.storyboard */; }; FC203FB221CBFDBA00B37166 /* test.jpg in Resources */ = {isa = PBXBuildFile; fileRef = FC203FA921CBFDBA00B37166 /* test.jpg */; }; + FC5E03B221DCE8D90016C137 /* mingren_input_data in Resources */ = {isa = PBXBuildFile; fileRef = FC5E03B121DCE8D90016C137 /* mingren_input_data */; }; FC704C1921D2375300F98BAB /* super_params in Resources */ = {isa = PBXBuildFile; fileRef = FC704C1721D2375300F98BAB /* super_params */; }; FC704C1A21D2375300F98BAB /* super_model in Resources */ = {isa = PBXBuildFile; fileRef = FC704C1821D2375300F98BAB /* super_model */; }; FC704C2221D237FC00F98BAB /* combined_mobilenet_params in Resources */ = {isa = PBXBuildFile; fileRef = FC704C1D21D237FC00F98BAB /* combined_mobilenet_params */; }; @@ -32,6 +33,7 @@ FC9797CF21D6506F00F2FD90 /* mingren.jpg in Resources */ = {isa = PBXBuildFile; fileRef = FC9797CE21D6506F00F2FD90 /* mingren.jpg */; }; FC9797D121D6616600F2FD90 /* BufferToTexture.metal in Sources */ = {isa = PBXBuildFile; fileRef = FC9797D021D6616600F2FD90 /* BufferToTexture.metal */; }; FCBCCC552122EF5500D94F7E /* MetalHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCBCCC542122EF5400D94F7E /* MetalHelper.swift */; }; + FCCED60521D7646E00BE8D5F /* test_image_super in Resources */ = {isa = PBXBuildFile; fileRef = FCCED60421D7646E00BE8D5F /* test_image_super */; }; 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, ); }; }; FCF437E8214B6DDB00943429 /* MultiPredictViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCF437E7214B6DDB00943429 /* MultiPredictViewController.swift */; }; @@ -68,6 +70,7 @@ FC203FA921CBFDBA00B37166 /* test.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = test.jpg; sourceTree = ""; }; FC27991121343A39000B6BAD /* paddle-mobile-demo-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "paddle-mobile-demo-Bridging-Header.h"; sourceTree = ""; }; FC4FD97B2140EE250073E130 /* libc++.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = "libc++.tbd"; path = "usr/lib/libc++.tbd"; sourceTree = SDKROOT; }; + FC5E03B121DCE8D90016C137 /* mingren_input_data */ = {isa = PBXFileReference; lastKnownFileType = file; path = mingren_input_data; sourceTree = ""; }; FC704C1721D2375300F98BAB /* super_params */ = {isa = PBXFileReference; lastKnownFileType = file; path = super_params; sourceTree = ""; }; FC704C1821D2375300F98BAB /* super_model */ = {isa = PBXFileReference; lastKnownFileType = file; path = super_model; sourceTree = ""; }; FC704C1D21D237FC00F98BAB /* combined_mobilenet_params */ = {isa = PBXFileReference; lastKnownFileType = file; path = combined_mobilenet_params; sourceTree = ""; }; @@ -83,6 +86,7 @@ FC9797CE21D6506F00F2FD90 /* mingren.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = mingren.jpg; sourceTree = ""; }; FC9797D021D6616600F2FD90 /* BufferToTexture.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = BufferToTexture.metal; sourceTree = ""; }; FCBCCC542122EF5400D94F7E /* MetalHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MetalHelper.swift; sourceTree = ""; }; + FCCED60421D7646E00BE8D5F /* test_image_super */ = {isa = PBXFileReference; lastKnownFileType = file; path = test_image_super; 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 = ""; }; /* End PBXFileReference section */ @@ -162,6 +166,8 @@ FC203FA821CBFDBA00B37166 /* images */ = { isa = PBXGroup; children = ( + FC5E03B121DCE8D90016C137 /* mingren_input_data */, + FCCED60421D7646E00BE8D5F /* test_image_super */, FC9797CE21D6506F00F2FD90 /* mingren.jpg */, FC9797BD21D6045B00F2FD90 /* banana.jpeg */, FC203FA921CBFDBA00B37166 /* test.jpg */, @@ -308,12 +314,14 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + FCCED60521D7646E00BE8D5F /* test_image_super in Resources */, FC039B8C20E11C560081E9F8 /* LaunchScreen.storyboard in Resources */, FC9797CF21D6506F00F2FD90 /* mingren.jpg in Resources */, FC704C2221D237FC00F98BAB /* combined_mobilenet_params in Resources */, FC704C1921D2375300F98BAB /* super_params in Resources */, FC039B8920E11C560081E9F8 /* Assets.xcassets in Resources */, FC9797C721D609FB00F2FD90 /* synset.txt in Resources */, + FC5E03B221DCE8D90016C137 /* mingren_input_data in Resources */, FC704C1A21D2375300F98BAB /* super_model in Resources */, FC039B8720E11C550081E9F8 /* Main.storyboard in Resources */, FC9797C221D608E000F2FD90 /* mobilenet_model in Resources */, diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift b/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift index 5fcbda1270..62f22dd6bd 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift @@ -18,6 +18,34 @@ import CoreMedia import paddle_mobile import MetalPerformanceShaders + +class FileReader { + let file: UnsafeMutablePointer + let fileSize: Int + init(paramPath: String) throws { + guard let tmpFile = fopen(paramPath, "rb") else { + throw PaddleMobileError.loaderError(message: "open param file error" + paramPath) + } + file = tmpFile + fseek(file, 0, SEEK_END) + fileSize = ftell(file) + guard fileSize > 0 else { + throw PaddleMobileError.loaderError(message: "param file size is too small") + } + rewind(file) + } + + func read() -> UnsafeMutablePointer { + let ptr = UnsafeMutablePointer.allocate(capacity: MemoryLayout.size * fileSize) + fread(ptr, fileSize, 1, file) + return ptr + } + + deinit { + fclose(file) + } +} + enum Platform { case GPU } @@ -66,10 +94,24 @@ class ViewController: UIViewController { @IBAction func loadAct(_ sender: Any) { runner = Runner.init(inNet: netSupport[modelType]!, commandQueue: MetalHelper.shared.queue) if platform == .GPU { + let filePath = Bundle.main.path(forResource: "mingren_input_data", ofType: nil) + let fileReader = try! FileReader.init(paramPath: filePath!) + let pointer: UnsafeMutablePointer = fileReader.read() + + + let buffer = MetalHelper.shared.device.makeBuffer(length: fileReader.fileSize, options: .storageModeShared) + + buffer?.contents().copyMemory(from: pointer, byteCount: fileReader.fileSize) + + if self.toPredictTexture == nil { - runner.getTexture(image: selectImage!.cgImage!) { [weak self] (texture) in + + runner.getTexture(inBuffer: buffer!) { [weak self] (texture) in self?.toPredictTexture = texture } + +// runner.getTexture(image: selectImage!.cgImage!) { [weak self] (texture) in +// } } } else { fatalError( " unsupport " ) @@ -108,7 +150,8 @@ class ViewController: UIViewController { guard let sSelf = self else { fatalError() } - if let inResultHolder = resultHolder, success { + + if success, let inResultHolder = resultHolder { if i == max - 1 { let time = Date.init().timeIntervalSince(startDate) diff --git a/metal/paddle-mobile/paddle-mobile/CustomNet/SuperResolutionNet.swift b/metal/paddle-mobile/paddle-mobile/CustomNet/SuperResolutionNet.swift index 9df8afb977..18e07d0dca 100644 --- a/metal/paddle-mobile/paddle-mobile/CustomNet/SuperResolutionNet.swift +++ b/metal/paddle-mobile/paddle-mobile/CustomNet/SuperResolutionNet.swift @@ -15,13 +15,6 @@ import Foundation -class SuperResolutionPreProccess: CusomKernel { - init(device: MTLDevice) { - let s = Shape.init(inWidth: 224, inHeight: 224, inChannel: 3) - super.init(device: device, inFunctionName: "super_resolution_preprocess", outputDim: s, usePaddleMobileLib: false) - } -} - public class SuperResolutionNet: Net{ override public func resultStr(res: ResultHolder) -> String { return "未实现" diff --git a/metal/paddle-mobile/paddle-mobile/framework/Executor.swift b/metal/paddle-mobile/paddle-mobile/framework/Executor.swift index 53c2690c5e..db8a3da966 100644 --- a/metal/paddle-mobile/paddle-mobile/framework/Executor.swift +++ b/metal/paddle-mobile/paddle-mobile/framework/Executor.swift @@ -144,23 +144,23 @@ public class Executor { guard let SSelf = self else { fatalError() } - + //将输入写进文件 /* - let inputArr = resInput.toTensor(dim: (n: dim[0], c: dim[3], h: dim[1], w: dim[2])) print(dim) - writeToLibrary(fileName: "test_image_mingren", array: inputArr) + writeToLibrary(fileName: "test_image_super", array: inputArr) print(" write done ") - return */ /* 输出 op 计算结果 - for op in SSelf.ops { - op.delogOutput() - } + */ + + for op in SSelf.ops { + op.delogOutput() + } var resultHolder: GPUResultHolder if except > 0 { diff --git a/test/net/test_super.cpp b/test/net/test_super.cpp index 7a10f826c4..28d9bfe170 100644 --- a/test/net/test_super.cpp +++ b/test/net/test_super.cpp @@ -21,7 +21,7 @@ int main() { paddle_mobile::PaddleMobileConfigInternal config; config.load_when_predict = true; - paddle_mobile::PaddleMobile paddle_mobile(config); + paddle_mobile::PaddleMobile paddle_mobile(config); // paddle_mobile.SetThreadNum(4); auto time1 = paddle_mobile::time(); #ifdef PADDLE_MOBILE_CL @@ -38,84 +38,84 @@ int main() { std::cout << "load cost :" << paddle_mobile::time_diff(time1, time2) << "ms" << std::endl; // 300*300 - std::vector input; - std::vector dims{1, 1, 300, 300}; - GetInput(g_test_image_1x3x224x224, &input, dims); - - std::vector vec_result; +// std::vector input; +// std::vector dims{1, 1, 300, 300}; +// GetInput(g_test_image_1x3x224x224, &input, dims); +// +// std::vector vec_result; auto time3 = paddle_mobile::time(); - int max = 10; - - for (int i = 0; i < max; ++i) { - auto time5 = paddle_mobile::time(); - vec_result = paddle_mobile.Predict(input, dims); - auto time6 = paddle_mobile::time(); - std::cout << "300 predict cost :第" << i << ": " - << paddle_mobile::time_diff(time5, time6) << "ms" << std::endl; - } - auto time4 = paddle_mobile::time(); - - std::cout << "300 predict cost :" - << paddle_mobile::time_diff(time3, time4) / max << "ms" - << std::endl; - auto biggest = - std::max_element(std::begin(vec_result), std::end(vec_result)); - std::cout << "300 Max element is " << *biggest << " at position " - << std::distance(std::begin(vec_result), biggest) << std::endl; - - // 500*500 - std::vector vec_result2; - - std::vector input2; - std::vector dims2{1, 1, 500, 500}; - GetInput(g_test_image_1x3x224x224, &input2, dims2); - - time3 = paddle_mobile::time(); - for (int i = 0; i < max; ++i) { - auto time5 = paddle_mobile::time(); - vec_result2 = paddle_mobile.Predict(input2, dims2); - auto time6 = paddle_mobile::time(); - std::cout << "500 predict cost :第" << i << ": " - << paddle_mobile::time_diff(time5, time6) << "ms" << std::endl; - } - - time4 = paddle_mobile::time(); - std::cout << "500 predict cost :" - << paddle_mobile::time_diff(time3, time4) / max << "ms" - << std::endl; - biggest = std::max_element(std::begin(vec_result2), std::end(vec_result2)); - std::cout << "500 Max element is " << *biggest << " at position " - << std::distance(std::begin(vec_result2), biggest) << std::endl; - - // 1000*1000 - - std::vector vec_result3; - std::vector input3; - std::vector dims3{1, 1, 1000, 1000}; - GetInput(g_test_image_1x3x224x224, &input3, dims3); - - time3 = paddle_mobile::time(); - - for (int i = 0; i < max; ++i) { - auto time5 = paddle_mobile::time(); - vec_result3 = paddle_mobile.Predict(input3, dims3); - auto time6 = paddle_mobile::time(); - std::cout << "1000*1000 predict cost :第" << i << ": " - << paddle_mobile::time_diff(time5, time6) << "ms" << std::endl; - } - time4 = paddle_mobile::time(); - std::cout << "1000*1000 predict cost :" - << paddle_mobile::time_diff(time3, time4) / max << "ms" - << std::endl; - biggest = std::max_element(std::begin(vec_result3), std::end(vec_result3)); - std::cout << "1000*1000 Max element is " << *biggest << " at position " - << std::distance(std::begin(vec_result3), biggest) << std::endl; + int max = 1; + +// for (int i = 0; i < max; ++i) { +// auto time5 = paddle_mobile::time(); +// vec_result = paddle_mobile.Predict(input, dims); +// auto time6 = paddle_mobile::time(); +// std::cout << "300 predict cost :第" << i << ": " +// << paddle_mobile::time_diff(time5, time6) << "ms" << std::endl; +// } +// auto time4 = paddle_mobile::time(); +// +// std::cout << "300 predict cost :" +// << paddle_mobile::time_diff(time3, time4) / max << "ms" +// << std::endl; +// auto biggest = +// std::max_element(std::begin(vec_result), std::end(vec_result)); +// std::cout << "300 Max element is " << *biggest << " at position " +// << std::distance(std::begin(vec_result), biggest) << std::endl; +// +// // 500*500 +// std::vector vec_result2; +// +// std::vector input2; +// std::vector dims2{1, 1, 500, 500}; +// GetInput(g_test_image_1x3x224x224, &input2, dims2); +// +// time3 = paddle_mobile::time(); +// for (int i = 0; i < max; ++i) { +// auto time5 = paddle_mobile::time(); +// vec_result2 = paddle_mobile.Predict(input2, dims2); +// auto time6 = paddle_mobile::time(); +// std::cout << "500 predict cost :第" << i << ": " +// << paddle_mobile::time_diff(time5, time6) << "ms" << std::endl; +// } +// +// time4 = paddle_mobile::time(); +// std::cout << "500 predict cost :" +// << paddle_mobile::time_diff(time3, time4) / max << "ms" +// << std::endl; +// biggest = std::max_element(std::begin(vec_result2), std::end(vec_result2)); +// std::cout << "500 Max element is " << *biggest << " at position " +// << std::distance(std::begin(vec_result2), biggest) << std::endl; +// +// // 1000*1000 +// +// std::vector vec_result3; +// std::vector input3; +// std::vector dims3{1, 1, 1000, 1000}; +// GetInput(g_test_image_1x3x224x224, &input3, dims3); +// +// time3 = paddle_mobile::time(); +// +// for (int i = 0; i < max; ++i) { +// auto time5 = paddle_mobile::time(); +// vec_result3 = paddle_mobile.Predict(input3, dims3); +// auto time6 = paddle_mobile::time(); +// std::cout << "1000*1000 predict cost :第" << i << ": " +// << paddle_mobile::time_diff(time5, time6) << "ms" << std::endl; +// } +// time4 = paddle_mobile::time(); +// std::cout << "1000*1000 predict cost :" +// << paddle_mobile::time_diff(time3, time4) / max << "ms" +// << std::endl; +// biggest = std::max_element(std::begin(vec_result3), std::end(vec_result3)); +// std::cout << "1000*1000 Max element is " << *biggest << " at position " +// << std::distance(std::begin(vec_result3), biggest) << std::endl; // 224*224 std::vector vec_result4; std::vector input4; - std::vector dims4{1, 1, 224, 224}; + std::vector dims4{1, 1, 300, 300}; GetInput(g_test_image_1x3x224x224, &input4, dims4); time3 = paddle_mobile::time(); @@ -127,13 +127,13 @@ int main() { << paddle_mobile::time_diff(time5, time6) << "ms" << std::endl; } - time4 = paddle_mobile::time(); + auto time4 = paddle_mobile::time(); std::cout << "224*224 predict cost :" << paddle_mobile::time_diff(time3, time4) / max << "ms" << std::endl; - biggest = std::max_element(std::begin(vec_result4), std::end(vec_result4)); - std::cout << "224*224 Max element is " << *biggest << " at position " - << std::distance(std::begin(vec_result4), biggest) << std::endl; +// biggest = std::max_element(std::begin(vec_result4), std::end(vec_result4)); +// std::cout << "224*224 Max element is " << *biggest << " at position " +// << std::distance(std::begin(vec_result4), biggest) << std::endl; } return 0; diff --git a/test/test_helper.h b/test/test_helper.h index 9b06796429..b96cd34a5b 100644 --- a/test/test_helper.h +++ b/test/test_helper.h @@ -62,7 +62,7 @@ static const char *g_imgfssd_ar = "../images/test_image_ssd_ar"; static const char *g_imgfssd_ar1 = "../images/003_0001.txt"; static const char *g_img = "../images/img.bin"; static const char *g_yolo_img = "../images/in_put_1_3_416_416_2"; -static const char *g_super_img = "../images/test_image_super"; +static const char *g_super_img = "../images/mingren_input_data"; static const char *g_mobilenet_img = "../images/image"; using paddle_mobile::framework::DDim; -- GitLab