From 0b251cc568bfb4280d8c3997700a52e45ecd9a45 Mon Sep 17 00:00:00 2001 From: liuruilong Date: Mon, 7 Jan 2019 13:54:29 +0800 Subject: [PATCH] add metal mobilenet demo --- .gitignore | 1 + .../MobileNetDemo.xcodeproj/project.pbxproj | 498 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + .../xcshareddata/IDEWorkspaceChecks.plist | 8 + .../MobileNetDemo/AppDelegate.swift | 46 ++ .../AppIcon.appiconset/Contents.json | 98 ++++ .../Assets.xcassets/Contents.json | 6 + .../Base.lproj/LaunchScreen.storyboard | 25 + .../MobileNetDemo/Base.lproj/Main.storyboard | 166 ++++++ metal/MobileNetDemo/MobileNetDemo/Info.plist | 47 ++ .../MobileNetDemo/MobileNet.swift | 63 +++ .../MobileNetDemo/MobilenetPreProcess.metal | 38 ++ .../MobileNetDemo/ViewController.swift | 93 ++++ metal/Podfile | 6 + .../paddle-mobile.xcodeproj/project.pbxproj | 14 +- .../Src/{framework => Framework}/Dim.swift | 0 .../{framework => Framework}/Executor.swift | 0 .../Src/{framework => Framework}/Loader.swift | 0 .../Src/{framework => Framework}/Tensor.swift | 0 .../{framework => Framework}/Texture.swift | 6 +- .../paddle-mobile/Src/Operators/FetchOp.swift | 32 -- .../Src/Operators/Kernels/FetchKernel.swift | 61 +++ .../Src/Operators/Kernels/Scale.swift | 20 +- .../Kernels/metal/FetchKernel.inc.metal | 15 + .../Kernels/metal/PoolKernel.inc.metal | 20 +- .../Operators/Kernels/metal/PoolKernel.metal | 8 +- .../python/modeltools/tools/model_combine.py | 6 +- 27 files changed, 1224 insertions(+), 60 deletions(-) create mode 100644 metal/MobileNetDemo/MobileNetDemo.xcodeproj/project.pbxproj create mode 100644 metal/MobileNetDemo/MobileNetDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 metal/MobileNetDemo/MobileNetDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 metal/MobileNetDemo/MobileNetDemo/AppDelegate.swift create mode 100644 metal/MobileNetDemo/MobileNetDemo/Assets.xcassets/AppIcon.appiconset/Contents.json create mode 100644 metal/MobileNetDemo/MobileNetDemo/Assets.xcassets/Contents.json create mode 100644 metal/MobileNetDemo/MobileNetDemo/Base.lproj/LaunchScreen.storyboard create mode 100644 metal/MobileNetDemo/MobileNetDemo/Base.lproj/Main.storyboard create mode 100644 metal/MobileNetDemo/MobileNetDemo/Info.plist create mode 100644 metal/MobileNetDemo/MobileNetDemo/MobileNet.swift create mode 100644 metal/MobileNetDemo/MobileNetDemo/MobilenetPreProcess.metal create mode 100644 metal/MobileNetDemo/MobileNetDemo/ViewController.swift rename metal/paddle-mobile/paddle-mobile/Src/{framework => Framework}/Dim.swift (100%) rename metal/paddle-mobile/paddle-mobile/Src/{framework => Framework}/Executor.swift (100%) rename metal/paddle-mobile/paddle-mobile/Src/{framework => Framework}/Loader.swift (100%) rename metal/paddle-mobile/paddle-mobile/Src/{framework => Framework}/Tensor.swift (100%) rename metal/paddle-mobile/paddle-mobile/Src/{framework => Framework}/Texture.swift (97%) create mode 100644 metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/FetchKernel.swift diff --git a/.gitignore b/.gitignore index 4b3c7a0921..68380e97ab 100644 --- a/.gitignore +++ b/.gitignore @@ -99,3 +99,4 @@ metal/paddle-mobile-demo/paddle-mobile-demo/models metal/paddle-mobile-demo/paddle-mobile-demo/Resources metal/paddle-mobile-demo/paddle-mobile-demo/Resources/images metal/paddle-mobile-demo/paddle-mobile-demo/Resources/models +metal/MobileNetDemo/MobileNetDemo/Resources diff --git a/metal/MobileNetDemo/MobileNetDemo.xcodeproj/project.pbxproj b/metal/MobileNetDemo/MobileNetDemo.xcodeproj/project.pbxproj new file mode 100644 index 0000000000..5596c6f0a2 --- /dev/null +++ b/metal/MobileNetDemo/MobileNetDemo.xcodeproj/project.pbxproj @@ -0,0 +1,498 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 50; + objects = { + +/* Begin PBXBuildFile section */ + FA37E99B9AD29A07FEE8E743 /* Pods_MobileNetDemo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD3A2E3175627EF63DACA36C /* Pods_MobileNetDemo.framework */; }; + FC74BB3621DFAFEC0055232B /* MobileNet.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC74BB3521DFAFEC0055232B /* MobileNet.swift */; }; + FCB40DA221E0B7C60075EC91 /* MobilenetPreProcess.metal in Sources */ = {isa = PBXBuildFile; fileRef = FCB40DA121E0B7C60075EC91 /* MobilenetPreProcess.metal */; }; + FCB40DA421E0B85B0075EC91 /* MetalHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCB40DA321E0B85B0075EC91 /* MetalHelper.swift */; }; + FCB40DE921E0B9410075EC91 /* banana.jpeg in Resources */ = {isa = PBXBuildFile; fileRef = FCB40DD221E0B9410075EC91 /* banana.jpeg */; }; + FCB40E5121E0CEBB0075EC91 /* mobilenet_model in Resources */ = {isa = PBXBuildFile; fileRef = FCB40E4F21E0CEBB0075EC91 /* mobilenet_model */; }; + FCB40E5221E0CEBB0075EC91 /* mobilenet_params in Resources */ = {isa = PBXBuildFile; fileRef = FCB40E5021E0CEBB0075EC91 /* mobilenet_params */; }; + FCB40E5421E0CEF80075EC91 /* synset.txt in Resources */ = {isa = PBXBuildFile; fileRef = FCB40E5321E0CEF80075EC91 /* synset.txt */; }; + FCD3873821E1C31F0052F3D0 /* paddle_mobile.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FCD3873721E1C31F0052F3D0 /* paddle_mobile.framework */; }; + FCD3873921E1C31F0052F3D0 /* paddle_mobile.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = FCD3873721E1C31F0052F3D0 /* paddle_mobile.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; + FCF2870921DFAEC7009A87DA /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCF2870821DFAEC7009A87DA /* AppDelegate.swift */; }; + FCF2870B21DFAEC7009A87DA /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCF2870A21DFAEC7009A87DA /* ViewController.swift */; }; + FCF2870E21DFAEC7009A87DA /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FCF2870C21DFAEC7009A87DA /* Main.storyboard */; }; + FCF2871021DFAEC8009A87DA /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FCF2870F21DFAEC8009A87DA /* Assets.xcassets */; }; + FCF2871321DFAEC8009A87DA /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FCF2871121DFAEC8009A87DA /* LaunchScreen.storyboard */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + FCB40DFC21E0BC360075EC91 /* Embed Frameworks */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = ""; + dstSubfolderSpec = 10; + files = ( + FCD3873921E1C31F0052F3D0 /* paddle_mobile.framework in Embed Frameworks */, + ); + name = "Embed Frameworks"; + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 4FE67FF667A24FCB0134F627 /* Pods-MobileNetDemo.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MobileNetDemo.debug.xcconfig"; path = "../Pods/Target Support Files/Pods-MobileNetDemo/Pods-MobileNetDemo.debug.xcconfig"; sourceTree = ""; }; + DD3A2E3175627EF63DACA36C /* Pods_MobileNetDemo.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_MobileNetDemo.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + E57059FE3629E3A8DE6C7ECF /* Pods-MobileNetDemo.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MobileNetDemo.release.xcconfig"; path = "../Pods/Target Support Files/Pods-MobileNetDemo/Pods-MobileNetDemo.release.xcconfig"; sourceTree = ""; }; + FC74BB3521DFAFEC0055232B /* MobileNet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MobileNet.swift; sourceTree = ""; }; + FCB40DA121E0B7C60075EC91 /* MobilenetPreProcess.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = MobilenetPreProcess.metal; sourceTree = ""; }; + FCB40DA321E0B85B0075EC91 /* MetalHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MetalHelper.swift; path = "../../paddle-mobile-demo/paddle-mobile-demo/MetalHelper.swift"; sourceTree = ""; }; + FCB40DD221E0B9410075EC91 /* banana.jpeg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = banana.jpeg; sourceTree = ""; }; + FCB40E4F21E0CEBB0075EC91 /* mobilenet_model */ = {isa = PBXFileReference; lastKnownFileType = file; path = mobilenet_model; sourceTree = ""; }; + FCB40E5021E0CEBB0075EC91 /* mobilenet_params */ = {isa = PBXFileReference; lastKnownFileType = file; path = mobilenet_params; sourceTree = ""; }; + FCB40E5321E0CEF80075EC91 /* synset.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = synset.txt; sourceTree = ""; }; + FCD3873721E1C31F0052F3D0 /* paddle_mobile.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = paddle_mobile.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + FCF2870521DFAEC7009A87DA /* MobileNetDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MobileNetDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; + FCF2870821DFAEC7009A87DA /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + FCF2870A21DFAEC7009A87DA /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; + FCF2870D21DFAEC7009A87DA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; + FCF2870F21DFAEC8009A87DA /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + FCF2871221DFAEC8009A87DA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; + FCF2871421DFAEC8009A87DA /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + FCF2870221DFAEC7009A87DA /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + FCD3873821E1C31F0052F3D0 /* paddle_mobile.framework in Frameworks */, + FA37E99B9AD29A07FEE8E743 /* Pods_MobileNetDemo.framework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 0DDBA47E92A64BC7B0385B0F /* Frameworks */ = { + isa = PBXGroup; + children = ( + DD3A2E3175627EF63DACA36C /* Pods_MobileNetDemo.framework */, + ); + name = Frameworks; + sourceTree = ""; + }; + 1EACBAAF38D9EDE0AC2B3F90 /* Pods */ = { + isa = PBXGroup; + children = ( + 4FE67FF667A24FCB0134F627 /* Pods-MobileNetDemo.debug.xcconfig */, + E57059FE3629E3A8DE6C7ECF /* Pods-MobileNetDemo.release.xcconfig */, + ); + name = Pods; + sourceTree = ""; + }; + FCB40DCF21E0B9410075EC91 /* Resources */ = { + isa = PBXGroup; + children = ( + FCB40DD021E0B9410075EC91 /* images */, + FCB40DD921E0B9410075EC91 /* models */, + ); + path = Resources; + sourceTree = ""; + }; + FCB40DD021E0B9410075EC91 /* images */ = { + isa = PBXGroup; + children = ( + FCB40DD221E0B9410075EC91 /* banana.jpeg */, + ); + path = images; + sourceTree = ""; + }; + FCB40DD921E0B9410075EC91 /* models */ = { + isa = PBXGroup; + children = ( + FCB40E4E21E0CEBB0075EC91 /* mobilenet_combine */, + ); + path = models; + sourceTree = ""; + }; + FCB40E4E21E0CEBB0075EC91 /* mobilenet_combine */ = { + isa = PBXGroup; + children = ( + FCB40E5321E0CEF80075EC91 /* synset.txt */, + FCB40E4F21E0CEBB0075EC91 /* mobilenet_model */, + FCB40E5021E0CEBB0075EC91 /* mobilenet_params */, + ); + path = mobilenet_combine; + sourceTree = ""; + }; + FCF286FC21DFAEC7009A87DA = { + isa = PBXGroup; + children = ( + FCD3873721E1C31F0052F3D0 /* paddle_mobile.framework */, + FCF2870721DFAEC7009A87DA /* MobileNetDemo */, + FCF2870621DFAEC7009A87DA /* Products */, + 1EACBAAF38D9EDE0AC2B3F90 /* Pods */, + 0DDBA47E92A64BC7B0385B0F /* Frameworks */, + ); + sourceTree = ""; + }; + FCF2870621DFAEC7009A87DA /* Products */ = { + isa = PBXGroup; + children = ( + FCF2870521DFAEC7009A87DA /* MobileNetDemo.app */, + ); + name = Products; + sourceTree = ""; + }; + FCF2870721DFAEC7009A87DA /* MobileNetDemo */ = { + isa = PBXGroup; + children = ( + FCB40DCF21E0B9410075EC91 /* Resources */, + FCB40DA321E0B85B0075EC91 /* MetalHelper.swift */, + FC74BB3521DFAFEC0055232B /* MobileNet.swift */, + FCF2870821DFAEC7009A87DA /* AppDelegate.swift */, + FCF2870A21DFAEC7009A87DA /* ViewController.swift */, + FCF2870C21DFAEC7009A87DA /* Main.storyboard */, + FCF2870F21DFAEC8009A87DA /* Assets.xcassets */, + FCF2871121DFAEC8009A87DA /* LaunchScreen.storyboard */, + FCF2871421DFAEC8009A87DA /* Info.plist */, + FCB40DA121E0B7C60075EC91 /* MobilenetPreProcess.metal */, + ); + path = MobileNetDemo; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + FCF2870421DFAEC7009A87DA /* MobileNetDemo */ = { + isa = PBXNativeTarget; + buildConfigurationList = FCF2871721DFAEC8009A87DA /* Build configuration list for PBXNativeTarget "MobileNetDemo" */; + buildPhases = ( + B4EB56AEEFF6F3965DA3D2DA /* [CP] Check Pods Manifest.lock */, + FCF2870121DFAEC7009A87DA /* Sources */, + FCF2870221DFAEC7009A87DA /* Frameworks */, + FCF2870321DFAEC7009A87DA /* Resources */, + 1D801B9681ACFCA70D444D2C /* [CP] Embed Pods Frameworks */, + FCB40DFC21E0BC360075EC91 /* Embed Frameworks */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = MobileNetDemo; + productName = MobileNetDemo; + productReference = FCF2870521DFAEC7009A87DA /* MobileNetDemo.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + FCF286FD21DFAEC7009A87DA /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 1010; + LastUpgradeCheck = 1010; + ORGANIZATIONNAME = Ray; + TargetAttributes = { + FCF2870421DFAEC7009A87DA = { + CreatedOnToolsVersion = 10.1; + }; + }; + }; + buildConfigurationList = FCF2870021DFAEC7009A87DA /* Build configuration list for PBXProject "MobileNetDemo" */; + compatibilityVersion = "Xcode 9.3"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = FCF286FC21DFAEC7009A87DA; + productRefGroup = FCF2870621DFAEC7009A87DA /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + FCF2870421DFAEC7009A87DA /* MobileNetDemo */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + FCF2870321DFAEC7009A87DA /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + FCF2871321DFAEC8009A87DA /* LaunchScreen.storyboard in Resources */, + FCB40E5121E0CEBB0075EC91 /* mobilenet_model in Resources */, + FCB40DE921E0B9410075EC91 /* banana.jpeg in Resources */, + FCF2871021DFAEC8009A87DA /* Assets.xcassets in Resources */, + FCB40E5421E0CEF80075EC91 /* synset.txt in Resources */, + FCB40E5221E0CEBB0075EC91 /* mobilenet_params in Resources */, + FCF2870E21DFAEC7009A87DA /* Main.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXShellScriptBuildPhase section */ + 1D801B9681ACFCA70D444D2C /* [CP] Embed Pods Frameworks */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + ); + inputPaths = ( + "${SRCROOT}/../Pods/Target Support Files/Pods-MobileNetDemo/Pods-MobileNetDemo-frameworks.sh", + "${BUILT_PRODUCTS_DIR}/SwiftProtobuf/SwiftProtobuf.framework", + ); + name = "[CP] Embed Pods Frameworks"; + outputFileListPaths = ( + ); + outputPaths = ( + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SwiftProtobuf.framework", + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "\"${SRCROOT}/../Pods/Target Support Files/Pods-MobileNetDemo/Pods-MobileNetDemo-frameworks.sh\"\n"; + showEnvVarsInLog = 0; + }; + B4EB56AEEFF6F3965DA3D2DA /* [CP] Check Pods Manifest.lock */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + ); + inputPaths = ( + "${PODS_PODFILE_DIR_PATH}/Podfile.lock", + "${PODS_ROOT}/Manifest.lock", + ); + name = "[CP] Check Pods Manifest.lock"; + outputFileListPaths = ( + ); + outputPaths = ( + "$(DERIVED_FILE_DIR)/Pods-MobileNetDemo-checkManifestLockResult.txt", + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n"; + showEnvVarsInLog = 0; + }; +/* End PBXShellScriptBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + FCF2870121DFAEC7009A87DA /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + FC74BB3621DFAFEC0055232B /* MobileNet.swift in Sources */, + FCB40DA421E0B85B0075EC91 /* MetalHelper.swift in Sources */, + FCB40DA221E0B7C60075EC91 /* MobilenetPreProcess.metal in Sources */, + FCF2870B21DFAEC7009A87DA /* ViewController.swift in Sources */, + FCF2870921DFAEC7009A87DA /* AppDelegate.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + FCF2870C21DFAEC7009A87DA /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + FCF2870D21DFAEC7009A87DA /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; + FCF2871121DFAEC8009A87DA /* LaunchScreen.storyboard */ = { + isa = PBXVariantGroup; + children = ( + FCF2871221DFAEC8009A87DA /* Base */, + ); + name = LaunchScreen.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + FCF2871521DFAEC8009A87DA /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 12.1; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + FCF2871621DFAEC8009A87DA /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 12.1; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + FCF2871821DFAEC8009A87DA /* Debug */ = { + isa = XCBuildConfiguration; + baseConfigurationReference = 4FE67FF667A24FCB0134F627 /* Pods-MobileNetDemo.debug.xcconfig */; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = A798K58VVL; + INFOPLIST_FILE = MobileNetDemo/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = Ray.MobileNetDemo; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + FCF2871921DFAEC8009A87DA /* Release */ = { + isa = XCBuildConfiguration; + baseConfigurationReference = E57059FE3629E3A8DE6C7ECF /* Pods-MobileNetDemo.release.xcconfig */; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = A798K58VVL; + INFOPLIST_FILE = MobileNetDemo/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = Ray.MobileNetDemo; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + FCF2870021DFAEC7009A87DA /* Build configuration list for PBXProject "MobileNetDemo" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + FCF2871521DFAEC8009A87DA /* Debug */, + FCF2871621DFAEC8009A87DA /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + FCF2871721DFAEC8009A87DA /* Build configuration list for PBXNativeTarget "MobileNetDemo" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + FCF2871821DFAEC8009A87DA /* Debug */, + FCF2871921DFAEC8009A87DA /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = FCF286FD21DFAEC7009A87DA /* Project object */; +} diff --git a/metal/MobileNetDemo/MobileNetDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/metal/MobileNetDemo/MobileNetDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000000..73ab39f972 --- /dev/null +++ b/metal/MobileNetDemo/MobileNetDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/metal/MobileNetDemo/MobileNetDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/metal/MobileNetDemo/MobileNetDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000000..18d981003d --- /dev/null +++ b/metal/MobileNetDemo/MobileNetDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/metal/MobileNetDemo/MobileNetDemo/AppDelegate.swift b/metal/MobileNetDemo/MobileNetDemo/AppDelegate.swift new file mode 100644 index 0000000000..4152b9be89 --- /dev/null +++ b/metal/MobileNetDemo/MobileNetDemo/AppDelegate.swift @@ -0,0 +1,46 @@ +// +// AppDelegate.swift +// MobileNetDemo +// +// Created by liuRuiLong on 2019/1/4. +// Copyright © 2019 Ray. All rights reserved. +// + +import UIKit + +@UIApplicationMain +class AppDelegate: UIResponder, UIApplicationDelegate { + + var window: UIWindow? + + + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { + // Override point for customization after application launch. + return true + } + + func applicationWillResignActive(_ application: UIApplication) { + // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. + // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. + } + + func applicationDidEnterBackground(_ application: UIApplication) { + // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. + // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. + } + + func applicationWillEnterForeground(_ application: UIApplication) { + // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. + } + + func applicationDidBecomeActive(_ application: UIApplication) { + // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. + } + + func applicationWillTerminate(_ application: UIApplication) { + // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. + } + + +} + diff --git a/metal/MobileNetDemo/MobileNetDemo/Assets.xcassets/AppIcon.appiconset/Contents.json b/metal/MobileNetDemo/MobileNetDemo/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000000..d8db8d65fd --- /dev/null +++ b/metal/MobileNetDemo/MobileNetDemo/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,98 @@ +{ + "images" : [ + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "3x" + }, + { + "idiom" : "ipad", + "size" : "20x20", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "29x29", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "40x40", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "76x76", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "76x76", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "83.5x83.5", + "scale" : "2x" + }, + { + "idiom" : "ios-marketing", + "size" : "1024x1024", + "scale" : "1x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/metal/MobileNetDemo/MobileNetDemo/Assets.xcassets/Contents.json b/metal/MobileNetDemo/MobileNetDemo/Assets.xcassets/Contents.json new file mode 100644 index 0000000000..da4a164c91 --- /dev/null +++ b/metal/MobileNetDemo/MobileNetDemo/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/metal/MobileNetDemo/MobileNetDemo/Base.lproj/LaunchScreen.storyboard b/metal/MobileNetDemo/MobileNetDemo/Base.lproj/LaunchScreen.storyboard new file mode 100644 index 0000000000..bfa3612941 --- /dev/null +++ b/metal/MobileNetDemo/MobileNetDemo/Base.lproj/LaunchScreen.storyboard @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/metal/MobileNetDemo/MobileNetDemo/Base.lproj/Main.storyboard b/metal/MobileNetDemo/MobileNetDemo/Base.lproj/Main.storyboard new file mode 100644 index 0000000000..067a594bf3 --- /dev/null +++ b/metal/MobileNetDemo/MobileNetDemo/Base.lproj/Main.storyboard @@ -0,0 +1,166 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/metal/MobileNetDemo/MobileNetDemo/Info.plist b/metal/MobileNetDemo/MobileNetDemo/Info.plist new file mode 100644 index 0000000000..fbe86e68a6 --- /dev/null +++ b/metal/MobileNetDemo/MobileNetDemo/Info.plist @@ -0,0 +1,47 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + LSRequiresIPhoneOS + + NSCameraUsageDescription + use camera + UILaunchStoryboardName + LaunchScreen + UIMainStoryboardFile + Main + UIRequiredDeviceCapabilities + + armv7 + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + UISupportedInterfaceOrientations~ipad + + UIInterfaceOrientationPortrait + UIInterfaceOrientationPortraitUpsideDown + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + + diff --git a/metal/MobileNetDemo/MobileNetDemo/MobileNet.swift b/metal/MobileNetDemo/MobileNetDemo/MobileNet.swift new file mode 100644 index 0000000000..9a50f3db8d --- /dev/null +++ b/metal/MobileNetDemo/MobileNetDemo/MobileNet.swift @@ -0,0 +1,63 @@ +/* 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 +import paddle_mobile + +public class MobileNet: Net{ + class MobilenetPreProccess: CusomKernel { + init(device: MTLDevice) { + let s = Shape.init(inWidth: 224, inHeight: 224, inChannel: 3) + super.init(device: device, inFunctionName: "mobilenet_preprocess", outputDim: s, metalLoadModel: .LoadMetalInDefaultLib, metalLibPath: nil) + } + } + + class PreWords { + var contents: [String] = [] + init(fileName: String, type: String = "txt", inBundle: Bundle = Bundle.main) { + if let filePath = inBundle.path(forResource: fileName, ofType: type) { + let string = try! String.init(contentsOfFile: filePath) + contents = string.components(separatedBy: CharacterSet.newlines).filter{$0.count > 10}.map{ + String($0[$0.index($0.startIndex, offsetBy: 10)...]) + } + }else{ + fatalError("no file call \(fileName)") + } + } + subscript(index: Int) -> String { + return contents[index] + } + } + + let labels = PreWords.init(fileName: "synset") + + override public func resultStr(res: ResultHolder) -> String { + let resPointer = res.result + var s: [String] = [] + (0.. +using namespace metal; + +kernel void mobilenet_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(123.68f, 116.78f, 103.94f, 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 mobilenet_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(123.68f, 116.78f, 103.94f, 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); +} diff --git a/metal/MobileNetDemo/MobileNetDemo/ViewController.swift b/metal/MobileNetDemo/MobileNetDemo/ViewController.swift new file mode 100644 index 0000000000..4e31282f03 --- /dev/null +++ b/metal/MobileNetDemo/MobileNetDemo/ViewController.swift @@ -0,0 +1,93 @@ +// +// ViewController.swift +// MobileNetDemo +// +// Created by liuRuiLong on 2019/1/4. +// Copyright © 2019 Ray. All rights reserved. +// + +import UIKit +import paddle_mobile + +class ViewController: UIViewController { + @IBOutlet weak var resultTextView: UITextView! + @IBOutlet weak var selectImageView: UIImageView! + @IBOutlet weak var elapsedTimeLabel: UILabel! + var net: MobileNet! + var runner: Runner! + var toPredictTexture: MTLTexture? + + override func viewDidLoad() { + super.viewDidLoad() + GlobalConfig.shared.computePrecision = .Float16 + net = MobileNet.init(device: MetalHelper.shared.device) + runner = Runner.init(inNet: net, commandQueue: MetalHelper.shared.queue) + + if let selectImage = UIImage.init(named: "banana.jpeg") { + selectImageView.image = selectImage + runner.getTexture(image: selectImage.cgImage!) {[weak self] (texture) in + self?.toPredictTexture = texture + } + } + + } + + @IBAction func loadAct(_ sender: Any) { + if runner.load() { + let resutText = " load success ! " + print(resutText) + self.resultTextView.text = resutText + } else { + fatalError(" load error ") + } + } + + @IBAction func selectImageAct(_ sender: Any) { + let imagePicker = UIImagePickerController() + imagePicker.sourceType = .camera + imagePicker.delegate = self + self.present(imagePicker, animated: true, completion: nil) + } + + @IBAction func clearAct(_ sender: Any) { + runner.clear() + } + + @IBAction func predictAct(_ sender: Any) { + + if let texture = toPredictTexture { + let beginDate = Date.init() + runner.predict(texture: texture) { [weak self] (success, resultHolder) in + if success, let inResultHolder = resultHolder { + let timeUse = Date.init().timeIntervalSince(beginDate) + DispatchQueue.main.async { + self?.elapsedTimeLabel.text = "\(timeUse * 1000)ms" + self?.resultTextView.text = self?.net.resultStr(res: inResultHolder) + } + + } else { + print(" predict fail ") + } + } + } else { + print(" toPredictTexture is nil ") + } + + } + +} + +extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate { + func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { + picker.dismiss(animated: true){[weak self] in + guard let sSelf = self, let image = info["UIImagePickerControllerOriginalImage"] as? UIImage else { + fatalError("no image") + } + sSelf.selectImageView.image = image + sSelf.runner.getTexture(image: image.cgImage!, getTexture: { (texture) in + sSelf.toPredictTexture = texture + }) + } + } +} + diff --git a/metal/Podfile b/metal/Podfile index 6e9a6c6e37..0262c9beaf 100644 --- a/metal/Podfile +++ b/metal/Podfile @@ -17,3 +17,9 @@ target 'paddle-mobile-unit-test' do project 'paddle-mobile-unit-test/paddle-mobile-unit-test.xcodeproj' pod 'SwiftProtobuf', '~> 1.0' end + +target 'MobileNetDemo' do + project 'MobileNetDemo/MobileNetDemo.xcodeproj' + pod 'SwiftProtobuf', '~> 1.0' +end + diff --git a/metal/paddle-mobile/paddle-mobile.xcodeproj/project.pbxproj b/metal/paddle-mobile/paddle-mobile.xcodeproj/project.pbxproj index e532ae900f..4cc97d0e9e 100644 --- a/metal/paddle-mobile/paddle-mobile.xcodeproj/project.pbxproj +++ b/metal/paddle-mobile/paddle-mobile.xcodeproj/project.pbxproj @@ -92,6 +92,7 @@ FCA67CD52138272900BD58AA /* ConvAddMetal.metal in Sources */ = {isa = PBXBuildFile; fileRef = FCA67CD42138272900BD58AA /* ConvAddMetal.metal */; }; FCA67CD7213827AC00BD58AA /* ConvAddBNReluKernel.metal in Sources */ = {isa = PBXBuildFile; fileRef = FCA67CD6213827AC00BD58AA /* ConvAddBNReluKernel.metal */; }; FCA67CD92138287B00BD58AA /* ConvBNReluKernel.metal in Sources */ = {isa = PBXBuildFile; fileRef = FCA67CD82138287B00BD58AA /* ConvBNReluKernel.metal */; }; + FCB40E5921E0DCAB0075EC91 /* FetchKernel.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCB40E5821E0DCAB0075EC91 /* FetchKernel.swift */; }; FCBCCC572122F41300D94F7E /* DwConvBNReluOp.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCBCCC562122F41300D94F7E /* DwConvBNReluOp.swift */; }; FCBCCC592122F42700D94F7E /* ConvBNReluOp.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCBCCC582122F42700D94F7E /* ConvBNReluOp.swift */; }; FCBCCC5B2122F66F00D94F7E /* ConvBNReluKernel.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCBCCC5A2122F66F00D94F7E /* ConvBNReluKernel.swift */; }; @@ -227,6 +228,7 @@ FCA67CD42138272900BD58AA /* ConvAddMetal.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = ConvAddMetal.metal; sourceTree = ""; }; FCA67CD6213827AC00BD58AA /* ConvAddBNReluKernel.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = ConvAddBNReluKernel.metal; sourceTree = ""; }; FCA67CD82138287B00BD58AA /* ConvBNReluKernel.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = ConvBNReluKernel.metal; sourceTree = ""; }; + FCB40E5821E0DCAB0075EC91 /* FetchKernel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FetchKernel.swift; sourceTree = ""; }; FCBCCC562122F41300D94F7E /* DwConvBNReluOp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DwConvBNReluOp.swift; sourceTree = ""; }; FCBCCC582122F42700D94F7E /* ConvBNReluOp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConvBNReluOp.swift; sourceTree = ""; }; FCBCCC5A2122F66F00D94F7E /* ConvBNReluKernel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConvBNReluKernel.swift; sourceTree = ""; }; @@ -348,7 +350,7 @@ path = Common; sourceTree = ""; }; - FC039B9C20E11CB20081E9F8 /* framework */ = { + FC039B9C20E11CB20081E9F8 /* Framework */ = { isa = PBXGroup; children = ( FC039BA120E11CB70081E9F8 /* Loader.swift */, @@ -357,7 +359,7 @@ FC039B9E20E11CB20081E9F8 /* Dim.swift */, FC9D038320E23B01000F735A /* Texture.swift */, ); - path = framework; + path = Framework; sourceTree = ""; }; FC039BA320E11CBC0081E9F8 /* Operators */ = { @@ -447,6 +449,7 @@ FCE3A1AA2153DE8C00C37CDE /* ConvAddAddPreluKernel.swift */, FCE3A1AE2153E8EE00C37CDE /* ElementwiseAddPreluKernel.swift */, FC2BFD4521DF685F00C262B2 /* Scale.swift */, + FCB40E5821E0DCAB0075EC91 /* FetchKernel.swift */, ); path = Kernels; sourceTree = ""; @@ -466,7 +469,7 @@ children = ( FC039BAE20E11CC20081E9F8 /* Program */, FC039BA320E11CBC0081E9F8 /* Operators */, - FC039B9C20E11CB20081E9F8 /* framework */, + FC039B9C20E11CB20081E9F8 /* Framework */, FC039B9320E11C9A0081E9F8 /* Common */, ); path = Src; @@ -520,16 +523,16 @@ FCA67CD82138287B00BD58AA /* ConvBNReluKernel.metal */, FC0226552138F33800F395E2 /* TransposeKernel.metal */, 4AA1EAAD214F5FD900D0F791 /* TransposeKernel.inc.metal */, - FCCED5E021D71FC000BE8D5F /* PoolKernel.inc.metal */, FC0226572138F38D00F395E2 /* PoolKernel.metal */, + FCCED5E021D71FC000BE8D5F /* PoolKernel.inc.metal */, FC803BC2214CB79C0094B8E5 /* ConvAddPreluKernel.metal */, FC803BC4214CB8F00094B8E5 /* ConvAddPrelu.inc.metal */, FC803BC6214CBA820094B8E5 /* Macro.metal */, FC803BC8214CFC8D0094B8E5 /* FetchKernel.metal */, + FC9C2A0C21D3D185005856C6 /* FetchKernel.inc.metal */, FCE9D7B8214FAA4800B520C3 /* NMSFetchResultKernel.metal */, FCE3A1B02153E90F00C37CDE /* ElementwiseAddPreluKernel.inc.metal */, FCE3A1B22153E91900C37CDE /* ElementwiseAddPreluKernel.metal */, - FC9C2A0C21D3D185005856C6 /* FetchKernel.inc.metal */, FC2BFD5021DF8E0400C262B2 /* Scale.metal */, ); path = metal; @@ -717,6 +720,7 @@ FC0E2DBA20EE3B8D009C1FAC /* ReluKernel.swift in Sources */, 4AA1EA862146625E00D0F791 /* BilinearInterpOp.swift in Sources */, FCBCCC6D2123073A00D94F7E /* BoxcoderKernel.swift in Sources */, + FCB40E5921E0DCAB0075EC91 /* FetchKernel.swift in Sources */, FCBCCC69212306D300D94F7E /* ConcatKernel.swift in Sources */, FCDDC6C8212FA3CA00E5EF74 /* ConvTransposeKernel.swift in Sources */, FC82735920E3C04200BE430A /* OpCreator.swift in Sources */, diff --git a/metal/paddle-mobile/paddle-mobile/Src/framework/Dim.swift b/metal/paddle-mobile/paddle-mobile/Src/Framework/Dim.swift similarity index 100% rename from metal/paddle-mobile/paddle-mobile/Src/framework/Dim.swift rename to metal/paddle-mobile/paddle-mobile/Src/Framework/Dim.swift diff --git a/metal/paddle-mobile/paddle-mobile/Src/framework/Executor.swift b/metal/paddle-mobile/paddle-mobile/Src/Framework/Executor.swift similarity index 100% rename from metal/paddle-mobile/paddle-mobile/Src/framework/Executor.swift rename to metal/paddle-mobile/paddle-mobile/Src/Framework/Executor.swift diff --git a/metal/paddle-mobile/paddle-mobile/Src/framework/Loader.swift b/metal/paddle-mobile/paddle-mobile/Src/Framework/Loader.swift similarity index 100% rename from metal/paddle-mobile/paddle-mobile/Src/framework/Loader.swift rename to metal/paddle-mobile/paddle-mobile/Src/Framework/Loader.swift diff --git a/metal/paddle-mobile/paddle-mobile/Src/framework/Tensor.swift b/metal/paddle-mobile/paddle-mobile/Src/Framework/Tensor.swift similarity index 100% rename from metal/paddle-mobile/paddle-mobile/Src/framework/Tensor.swift rename to metal/paddle-mobile/paddle-mobile/Src/Framework/Tensor.swift diff --git a/metal/paddle-mobile/paddle-mobile/Src/framework/Texture.swift b/metal/paddle-mobile/paddle-mobile/Src/Framework/Texture.swift similarity index 97% rename from metal/paddle-mobile/paddle-mobile/Src/framework/Texture.swift rename to metal/paddle-mobile/paddle-mobile/Src/Framework/Texture.swift index bc15b59f62..14631464d8 100644 --- a/metal/paddle-mobile/paddle-mobile/Src/framework/Texture.swift +++ b/metal/paddle-mobile/paddle-mobile/Src/Framework/Texture.swift @@ -68,11 +68,11 @@ extension InputTexture { .height = 1 .len = 1 */ - - public class Texture: Tensorial { public var dim: Dim public var tensorDim: Dim + + /// tensor dim pad to four public var padToFourDim: Dim private var textureDesc: MTLTextureDescriptor! public var metalTexture: MTLTexture! @@ -163,7 +163,9 @@ public class Texture: Tensorial { padToFourDim = fourDim } + // 初始化时 dim padToFourDim 模型中的维度(一般来说 nchw),前面补全0 init(device: MTLDevice, inDim: Dim) { + print(" in dim > \(inDim)") var fourDim: Dim if inDim.cout() == 4 { fourDim = inDim diff --git a/metal/paddle-mobile/paddle-mobile/Src/Operators/FetchOp.swift b/metal/paddle-mobile/paddle-mobile/Src/Operators/FetchOp.swift index 1e31442b07..477384cdac 100644 --- a/metal/paddle-mobile/paddle-mobile/Src/Operators/FetchOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Src/Operators/FetchOp.swift @@ -33,38 +33,6 @@ class FetchParam: OpParam{ //typealias ParamPrecisionType = P } -class FetchKernel: Kernel, Computable { - - func compute(commandBuffer: MTLCommandBuffer, param: FetchParam

) throws { - guard let encoder = commandBuffer.makeComputeCommandEncoder() else { - throw PaddleMobileError.predictError(message: " encode is nil") - } - encoder.setTexture(param.input.metalTexture, index: 0) - encoder.setBuffer(param.output.resultBuffer!, offset: 0, index: 0) - encoder.dispatch(computePipline: pipline, outTexture: param.input.metalTexture) - encoder.endEncoding() - } - - required init(device: MTLDevice, param: FetchParam

, initContext: InitContext) { - param.output.initBuffer(device: device) - if GlobalConfig.shared.computePrecision == .Float16 { - if param.input.transpose == [0, 2, 3, 1] { - super.init(device: device, inFunctionName: "fetch_half", initContext: initContext) - } else { - fatalError(" not support ") - } - } else if GlobalConfig.shared.computePrecision == .Float32 { - if param.input.transpose == [0, 2, 3, 1] { - super.init(device: device, inFunctionName: "fetch_float", initContext: initContext) - } else { - fatalError(" not support ") - } - } else { - fatalError(" not support ") - } - } -} - class FetchOp: Operator< FetchKernel

, FetchParam

>, Runable, Creator, InferShaperable { typealias OpType = FetchOp

diff --git a/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/FetchKernel.swift b/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/FetchKernel.swift new file mode 100644 index 0000000000..7d6e68e699 --- /dev/null +++ b/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/FetchKernel.swift @@ -0,0 +1,61 @@ +/* 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 + +class FetchKernel: Kernel, Computable { + + required init(device: MTLDevice, param: FetchParam

, initContext: InitContext) { + param.output.initBuffer(device: device) + if GlobalConfig.shared.computePrecision == .Float16 { + if param.input.transpose == [0, 2, 3, 1] { + super.init(device: device, inFunctionName: "fetch_half", initContext: initContext) + } else if param.input.transpose == [0, 1, 2, 3] { + switch param.input.tensorDim.cout() { + case 1, 2: + super.init(device: device, inFunctionName: "fetch_1or2_half", initContext: initContext) + default: + fatalError(" not support ") + } + } else { + fatalError(" not support ") + } + } else if GlobalConfig.shared.computePrecision == .Float32 { + if param.input.transpose == [0, 2, 3, 1] { + super.init(device: device, inFunctionName: "fetch_float", initContext: initContext) + } else if param.input.transpose == [0, 1, 2, 3] { + switch param.input.tensorDim.cout() { + case 1, 2: + super.init(device: device, inFunctionName: "fetch_1or2_float", initContext: initContext) + default: + fatalError(" not support ") + } + } else { + fatalError(" not support ") + } + } else { + fatalError(" not support ") + } + } + + func compute(commandBuffer: MTLCommandBuffer, param: FetchParam

) throws { + guard let encoder = commandBuffer.makeComputeCommandEncoder() else { + throw PaddleMobileError.predictError(message: " encode is nil") + } + encoder.setTexture(param.input.metalTexture, index: 0) + encoder.setBuffer(param.output.resultBuffer!, offset: 0, index: 0) + encoder.dispatch(computePipline: pipline, outTexture: param.input.metalTexture) + encoder.endEncoding() + } +} 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 56c3a969b6..2afee5607d 100644 --- a/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/Scale.swift +++ b/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/Scale.swift @@ -1,10 +1,16 @@ -// -// Scale.swift -// paddle-mobile -// -// Created by liuRuiLong on 2019/1/4. -// Copyright © 2019 orange. All rights reserved. -// +/* 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 diff --git a/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/metal/FetchKernel.inc.metal b/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/metal/FetchKernel.inc.metal index 5a883d8ce3..9655b0fc1a 100644 --- a/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/metal/FetchKernel.inc.metal +++ b/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/metal/FetchKernel.inc.metal @@ -43,4 +43,19 @@ kernel void FUNC_T(fetch, P)(texture2d_array inTexture [[textur output[gid.z * output_to + 3 * input_width * input_height + gid.y * input_width + gid.x] = input.w; } +kernel void FUNC(fetch, 1or2, P)(texture2d_array inTexture [[texture(0)]], + device float4 *output [[buffer(0)]], + uint3 gid [[thread_position_in_grid]]) { + if (gid.x >= inTexture.get_width() || + gid.y >= inTexture.get_height() || + gid.z >= inTexture.get_array_size()) { + return; + } + + int input_width = inTexture.get_width(); + const VECTOR(P, 4) input = inTexture.read(gid.xy, gid.z); + output[gid.y * input_width + gid.x] = float4(input); +} + + #endif diff --git a/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/metal/PoolKernel.inc.metal b/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/metal/PoolKernel.inc.metal index 5b0cae3e3a..3c36ba06f5 100644 --- a/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/metal/PoolKernel.inc.metal +++ b/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/metal/PoolKernel.inc.metal @@ -1,10 +1,16 @@ -// -// PoolKernel.inc.metal -// paddle-mobile -// -// Created by liuRuiLong on 2018/12/29. -// Copyright © 2018 orange. All rights reserved. -// +/* 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. */ #ifdef P diff --git a/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/metal/PoolKernel.metal b/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/metal/PoolKernel.metal index d17536eab6..e76b4ac742 100644 --- a/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/metal/PoolKernel.metal +++ b/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/metal/PoolKernel.metal @@ -27,10 +27,10 @@ struct PoolParam { int poolType; }; -#define P float -#import "PoolKernel.inc.metal" +#define P half +#include "PoolKernel.inc.metal" #undef P -#define P half -#import "PoolKernel.inc.metal" +#define P float +#include "PoolKernel.inc.metal" #undef P diff --git a/tools/python/modeltools/tools/model_combine.py b/tools/python/modeltools/tools/model_combine.py index ae3ca8a786..1fe8e6a9cd 100644 --- a/tools/python/modeltools/tools/model_combine.py +++ b/tools/python/modeltools/tools/model_combine.py @@ -1,14 +1,14 @@ # coding=utf-8 import os -path = "yolo_v2_tofile_source/" # 文件夹目录 -to_file_path = "yolo_v2_tofile_combined/params" +path = "mobilenet/" # 文件夹目录 +to_file_path = "mobilenet_combine/params" files = os.listdir(path) # 得到文件夹下的所有文件名称 files.sort(cmp=None, key=str.lower) to_file = open(to_file_path, "wb") for file in files: # 遍历文件夹 - if not os.path.isdir(file): # 判断是否是文件夹,不是文件夹才打开 + if not os.path.isdir(file) and file != ".DS_Store": # 判断是否是文件夹,不是文件夹才打开 f = open(path + "/" + file) # 打开文件 name = f.name print 'name: ' + name -- GitLab