├── .gitignore ├── Convert ├── license.txt └── mnist.py ├── LICENSE ├── MNISTPrediction.xcodeproj ├── project.pbxproj └── project.xcworkspace │ └── contents.xcworkspacedata ├── MNISTPrediction ├── AppDelegate.swift ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ ├── Contents.json │ ├── eight.imageset │ │ ├── 14978199835997.png │ │ └── Contents.json │ ├── four.imageset │ │ ├── 14978202165336.png │ │ └── Contents.json │ ├── one.imageset │ │ ├── 14978178986780.png │ │ └── Contents.json │ ├── three.imageset │ │ ├── 1497820050162.png │ │ └── Contents.json │ └── two.imageset │ │ ├── 14978214768760.png │ │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── Info.plist ├── MNIST.mlmodel ├── UIImage+PixelBuffer.swift └── ViewController.swift ├── README.md └── model.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | .DS_Store 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | ## Playgrounds 32 | timeline.xctimeline 33 | playground.xcworkspace 34 | 35 | # Swift Package Manager 36 | # 37 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 38 | # Packages/ 39 | # Package.pins 40 | .build/ 41 | 42 | # CocoaPods 43 | # 44 | # We recommend against adding the Pods directory to your .gitignore. However 45 | # you should judge for yourself, the pros and cons are mentioned at: 46 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 47 | # 48 | # Pods/ 49 | 50 | # Carthage 51 | # 52 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 53 | # Carthage/Checkouts 54 | 55 | Carthage/Build 56 | 57 | # fastlane 58 | # 59 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 60 | # screenshots whenever they are needed. 61 | # For more information about the recommended setup visit: 62 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 63 | 64 | fastlane/report.xml 65 | fastlane/Preview.html 66 | fastlane/screenshots 67 | fastlane/test_output 68 | -------------------------------------------------------------------------------- /Convert/license.txt: -------------------------------------------------------------------------------- 1 | COPYRIGHT 2 | 3 | All contributions by François Chollet: 4 | Copyright (c) 2015, François Chollet. 5 | All rights reserved. 6 | 7 | All contributions by Google: 8 | Copyright (c) 2015, Google, Inc. 9 | All rights reserved. 10 | 11 | All contributions by Microsoft: 12 | Copyright (c) 2017, Microsoft, Inc. 13 | All rights reserved. 14 | 15 | All other contributions: 16 | Copyright (c) 2015 - 2017, the respective contributors. 17 | All rights reserved. 18 | 19 | Each contributor holds copyright over their respective contributions. 20 | The project versioning (Git) records all such contribution source information. 21 | 22 | LICENSE 23 | 24 | The MIT License (MIT) 25 | 26 | Permission is hereby granted, free of charge, to any person obtaining a copy 27 | of this software and associated documentation files (the "Software"), to deal 28 | in the Software without restriction, including without limitation the rights 29 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 30 | copies of the Software, and to permit persons to whom the Software is 31 | furnished to do so, subject to the following conditions: 32 | 33 | The above copyright notice and this permission notice shall be included in all 34 | copies or substantial portions of the Software. 35 | 36 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 37 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 38 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 39 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 40 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 41 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 42 | SOFTWARE. 43 | -------------------------------------------------------------------------------- /Convert/mnist.py: -------------------------------------------------------------------------------- 1 | '''Trains a simple convnet on the MNIST dataset. 2 | Gets to 99.25% test accuracy after 12 epochs 3 | (there is still a lot of margin for parameter tuning). 4 | 16 seconds per epoch on a GRID K520 GPU. 5 | ''' 6 | 7 | from __future__ import print_function 8 | import numpy as np 9 | np.random.seed(1337) # for reproducibility 10 | 11 | from keras.datasets import mnist 12 | from keras.models import Sequential 13 | from keras.layers import Dense, Dropout, Activation, Flatten 14 | from keras.layers import Convolution2D, MaxPooling2D 15 | from keras.utils import np_utils 16 | from keras import backend as K 17 | 18 | batch_size = 128 19 | nb_classes = 10 20 | nb_epoch = 12 21 | 22 | # input image dimensions 23 | img_rows, img_cols = 28, 28 24 | # number of convolutional filters to use 25 | nb_filters = 32 26 | # size of pooling area for max pooling 27 | pool_size = (2, 2) 28 | # convolution kernel size 29 | kernel_size = (3, 3) 30 | 31 | # the data, shuffled and split between train and test sets 32 | (X_train, y_train), (X_test, y_test) = mnist.load_data() 33 | 34 | if K.image_dim_ordering() == 'th': 35 | X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols) 36 | X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols) 37 | input_shape = (1, img_rows, img_cols) 38 | else: 39 | X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1) 40 | X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1) 41 | input_shape = (img_rows, img_cols, 1) 42 | 43 | X_train = X_train.astype('float32') 44 | X_test = X_test.astype('float32') 45 | X_train /= 255 46 | X_test /= 255 47 | print('X_train shape:', X_train.shape) 48 | print(X_train.shape[0], 'train samples') 49 | print(X_test.shape[0], 'test samples') 50 | 51 | # convert class vectors to binary class matrices 52 | Y_train = np_utils.to_categorical(y_train, nb_classes) 53 | Y_test = np_utils.to_categorical(y_test, nb_classes) 54 | 55 | model = Sequential() 56 | 57 | model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1], 58 | border_mode='valid', 59 | input_shape=input_shape)) 60 | model.add(Activation('relu')) 61 | model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1])) 62 | model.add(Activation('relu')) 63 | model.add(MaxPooling2D(pool_size=pool_size)) 64 | model.add(Dropout(0.25)) 65 | 66 | model.add(Flatten()) 67 | model.add(Dense(128)) 68 | model.add(Activation('relu')) 69 | model.add(Dropout(0.5)) 70 | model.add(Dense(nb_classes)) 71 | model.add(Activation('softmax')) 72 | 73 | model.compile(loss='categorical_crossentropy', 74 | optimizer='adadelta', 75 | metrics=['accuracy']) 76 | 77 | model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, 78 | verbose=1, validation_data=(X_test, Y_test)) 79 | score = model.evaluate(X_test, Y_test, verbose=0) 80 | print('Test score:', score[0]) 81 | print('Test accuracy:', score[1]) 82 | 83 | import coremltools 84 | coreml_model = coremltools.converters.keras.convert(model, input_names='image', image_input_names='image', output_names='prediction', class_labels=[0,1,2,3,4,5,6,7,8,9]) 85 | coreml_model.author = 'Philipp Gabriel' 86 | coreml_model.license = 'MIT' 87 | coreml_model.short_description = 'Predicts a handwritten digit.' 88 | coreml_model.input_description['image'] = 'Image to analyze' 89 | coreml_model.output_description['prediction'] = 'Array of predictions mapped to their indices' 90 | coreml_model.save('MNIST.mlmodel') -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Philipp Gabriel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MNISTPrediction.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 2523166F1EF71675008E3527 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2523166E1EF71675008E3527 /* AppDelegate.swift */; }; 11 | 252316711EF71675008E3527 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 252316701EF71675008E3527 /* ViewController.swift */; }; 12 | 252316741EF71675008E3527 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 252316721EF71675008E3527 /* Main.storyboard */; }; 13 | 252316761EF71675008E3527 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 252316751EF71675008E3527 /* Assets.xcassets */; }; 14 | 252316791EF71675008E3527 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 252316771EF71675008E3527 /* LaunchScreen.storyboard */; }; 15 | 253087DA2035F8D1008F89B4 /* UIImage+PixelBuffer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 253087D92035F8D1008F89B4 /* UIImage+PixelBuffer.swift */; }; 16 | 256933B51EF81FC2003997BB /* MNIST.mlmodel in Sources */ = {isa = PBXBuildFile; fileRef = 256933B41EF81FB8003997BB /* MNIST.mlmodel */; }; 17 | /* End PBXBuildFile section */ 18 | 19 | /* Begin PBXFileReference section */ 20 | 2523166B1EF71675008E3527 /* MNISTPrediction.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MNISTPrediction.app; sourceTree = BUILT_PRODUCTS_DIR; }; 21 | 2523166E1EF71675008E3527 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 22 | 252316701EF71675008E3527 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 23 | 252316731EF71675008E3527 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 24 | 252316751EF71675008E3527 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 25 | 252316781EF71675008E3527 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 26 | 2523167A1EF71675008E3527 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 27 | 253087D92035F8D1008F89B4 /* UIImage+PixelBuffer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+PixelBuffer.swift"; sourceTree = ""; }; 28 | 256933B41EF81FB8003997BB /* MNIST.mlmodel */ = {isa = PBXFileReference; lastKnownFileType = file.mlmodel; path = MNIST.mlmodel; sourceTree = ""; }; 29 | /* End PBXFileReference section */ 30 | 31 | /* Begin PBXFrameworksBuildPhase section */ 32 | 252316681EF71675008E3527 /* Frameworks */ = { 33 | isa = PBXFrameworksBuildPhase; 34 | buildActionMask = 2147483647; 35 | files = ( 36 | ); 37 | runOnlyForDeploymentPostprocessing = 0; 38 | }; 39 | /* End PBXFrameworksBuildPhase section */ 40 | 41 | /* Begin PBXGroup section */ 42 | 252316621EF71675008E3527 = { 43 | isa = PBXGroup; 44 | children = ( 45 | 2523166D1EF71675008E3527 /* MNISTPrediction */, 46 | 2523166C1EF71675008E3527 /* Products */, 47 | ); 48 | sourceTree = ""; 49 | }; 50 | 2523166C1EF71675008E3527 /* Products */ = { 51 | isa = PBXGroup; 52 | children = ( 53 | 2523166B1EF71675008E3527 /* MNISTPrediction.app */, 54 | ); 55 | name = Products; 56 | sourceTree = ""; 57 | }; 58 | 2523166D1EF71675008E3527 /* MNISTPrediction */ = { 59 | isa = PBXGroup; 60 | children = ( 61 | 256933B41EF81FB8003997BB /* MNIST.mlmodel */, 62 | 253087D92035F8D1008F89B4 /* UIImage+PixelBuffer.swift */, 63 | 2523166E1EF71675008E3527 /* AppDelegate.swift */, 64 | 252316701EF71675008E3527 /* ViewController.swift */, 65 | 252316721EF71675008E3527 /* Main.storyboard */, 66 | 252316751EF71675008E3527 /* Assets.xcassets */, 67 | 252316771EF71675008E3527 /* LaunchScreen.storyboard */, 68 | 2523167A1EF71675008E3527 /* Info.plist */, 69 | ); 70 | path = MNISTPrediction; 71 | sourceTree = ""; 72 | }; 73 | /* End PBXGroup section */ 74 | 75 | /* Begin PBXNativeTarget section */ 76 | 2523166A1EF71675008E3527 /* MNISTPrediction */ = { 77 | isa = PBXNativeTarget; 78 | buildConfigurationList = 2523167D1EF71675008E3527 /* Build configuration list for PBXNativeTarget "MNISTPrediction" */; 79 | buildPhases = ( 80 | 252316671EF71675008E3527 /* Sources */, 81 | 252316681EF71675008E3527 /* Frameworks */, 82 | 252316691EF71675008E3527 /* Resources */, 83 | ); 84 | buildRules = ( 85 | ); 86 | dependencies = ( 87 | ); 88 | name = MNISTPrediction; 89 | productName = MNISTPrediction; 90 | productReference = 2523166B1EF71675008E3527 /* MNISTPrediction.app */; 91 | productType = "com.apple.product-type.application"; 92 | }; 93 | /* End PBXNativeTarget section */ 94 | 95 | /* Begin PBXProject section */ 96 | 252316631EF71675008E3527 /* Project object */ = { 97 | isa = PBXProject; 98 | attributes = { 99 | LastSwiftUpdateCheck = 0900; 100 | LastUpgradeCheck = 0920; 101 | ORGANIZATIONNAME = "Philipp Gabriel"; 102 | TargetAttributes = { 103 | 2523166A1EF71675008E3527 = { 104 | CreatedOnToolsVersion = 9.0; 105 | }; 106 | }; 107 | }; 108 | buildConfigurationList = 252316661EF71675008E3527 /* Build configuration list for PBXProject "MNISTPrediction" */; 109 | compatibilityVersion = "Xcode 8.0"; 110 | developmentRegion = en; 111 | hasScannedForEncodings = 0; 112 | knownRegions = ( 113 | en, 114 | Base, 115 | ); 116 | mainGroup = 252316621EF71675008E3527; 117 | productRefGroup = 2523166C1EF71675008E3527 /* Products */; 118 | projectDirPath = ""; 119 | projectRoot = ""; 120 | targets = ( 121 | 2523166A1EF71675008E3527 /* MNISTPrediction */, 122 | ); 123 | }; 124 | /* End PBXProject section */ 125 | 126 | /* Begin PBXResourcesBuildPhase section */ 127 | 252316691EF71675008E3527 /* Resources */ = { 128 | isa = PBXResourcesBuildPhase; 129 | buildActionMask = 2147483647; 130 | files = ( 131 | 252316791EF71675008E3527 /* LaunchScreen.storyboard in Resources */, 132 | 252316761EF71675008E3527 /* Assets.xcassets in Resources */, 133 | 252316741EF71675008E3527 /* Main.storyboard in Resources */, 134 | ); 135 | runOnlyForDeploymentPostprocessing = 0; 136 | }; 137 | /* End PBXResourcesBuildPhase section */ 138 | 139 | /* Begin PBXSourcesBuildPhase section */ 140 | 252316671EF71675008E3527 /* Sources */ = { 141 | isa = PBXSourcesBuildPhase; 142 | buildActionMask = 2147483647; 143 | files = ( 144 | 253087DA2035F8D1008F89B4 /* UIImage+PixelBuffer.swift in Sources */, 145 | 256933B51EF81FC2003997BB /* MNIST.mlmodel in Sources */, 146 | 252316711EF71675008E3527 /* ViewController.swift in Sources */, 147 | 2523166F1EF71675008E3527 /* AppDelegate.swift in Sources */, 148 | ); 149 | runOnlyForDeploymentPostprocessing = 0; 150 | }; 151 | /* End PBXSourcesBuildPhase section */ 152 | 153 | /* Begin PBXVariantGroup section */ 154 | 252316721EF71675008E3527 /* Main.storyboard */ = { 155 | isa = PBXVariantGroup; 156 | children = ( 157 | 252316731EF71675008E3527 /* Base */, 158 | ); 159 | name = Main.storyboard; 160 | sourceTree = ""; 161 | }; 162 | 252316771EF71675008E3527 /* LaunchScreen.storyboard */ = { 163 | isa = PBXVariantGroup; 164 | children = ( 165 | 252316781EF71675008E3527 /* Base */, 166 | ); 167 | name = LaunchScreen.storyboard; 168 | sourceTree = ""; 169 | }; 170 | /* End PBXVariantGroup section */ 171 | 172 | /* Begin XCBuildConfiguration section */ 173 | 2523167B1EF71675008E3527 /* Debug */ = { 174 | isa = XCBuildConfiguration; 175 | buildSettings = { 176 | ALWAYS_SEARCH_USER_PATHS = NO; 177 | CLANG_ANALYZER_NONNULL = YES; 178 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 179 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 180 | CLANG_CXX_LIBRARY = "libc++"; 181 | CLANG_ENABLE_MODULES = YES; 182 | CLANG_ENABLE_OBJC_ARC = YES; 183 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 184 | CLANG_WARN_BOOL_CONVERSION = YES; 185 | CLANG_WARN_COMMA = YES; 186 | CLANG_WARN_CONSTANT_CONVERSION = YES; 187 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 188 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 189 | CLANG_WARN_EMPTY_BODY = YES; 190 | CLANG_WARN_ENUM_CONVERSION = YES; 191 | CLANG_WARN_INFINITE_RECURSION = YES; 192 | CLANG_WARN_INT_CONVERSION = YES; 193 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 194 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 195 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 196 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 197 | CLANG_WARN_STRICT_PROTOTYPES = YES; 198 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 199 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 200 | CLANG_WARN_UNREACHABLE_CODE = YES; 201 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 202 | CODE_SIGN_IDENTITY = "iPhone Developer"; 203 | COPY_PHASE_STRIP = NO; 204 | DEBUG_INFORMATION_FORMAT = dwarf; 205 | ENABLE_STRICT_OBJC_MSGSEND = YES; 206 | ENABLE_TESTABILITY = YES; 207 | GCC_C_LANGUAGE_STANDARD = gnu11; 208 | GCC_DYNAMIC_NO_PIC = NO; 209 | GCC_NO_COMMON_BLOCKS = YES; 210 | GCC_OPTIMIZATION_LEVEL = 0; 211 | GCC_PREPROCESSOR_DEFINITIONS = ( 212 | "DEBUG=1", 213 | "$(inherited)", 214 | ); 215 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 216 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 217 | GCC_WARN_UNDECLARED_SELECTOR = YES; 218 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 219 | GCC_WARN_UNUSED_FUNCTION = YES; 220 | GCC_WARN_UNUSED_VARIABLE = YES; 221 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 222 | MTL_ENABLE_DEBUG_INFO = YES; 223 | ONLY_ACTIVE_ARCH = YES; 224 | SDKROOT = iphoneos; 225 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 226 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 227 | }; 228 | name = Debug; 229 | }; 230 | 2523167C1EF71675008E3527 /* Release */ = { 231 | isa = XCBuildConfiguration; 232 | buildSettings = { 233 | ALWAYS_SEARCH_USER_PATHS = NO; 234 | CLANG_ANALYZER_NONNULL = YES; 235 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 236 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 237 | CLANG_CXX_LIBRARY = "libc++"; 238 | CLANG_ENABLE_MODULES = YES; 239 | CLANG_ENABLE_OBJC_ARC = YES; 240 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 241 | CLANG_WARN_BOOL_CONVERSION = YES; 242 | CLANG_WARN_COMMA = YES; 243 | CLANG_WARN_CONSTANT_CONVERSION = YES; 244 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 245 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 246 | CLANG_WARN_EMPTY_BODY = YES; 247 | CLANG_WARN_ENUM_CONVERSION = YES; 248 | CLANG_WARN_INFINITE_RECURSION = YES; 249 | CLANG_WARN_INT_CONVERSION = YES; 250 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 251 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 252 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 253 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 254 | CLANG_WARN_STRICT_PROTOTYPES = YES; 255 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 256 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 257 | CLANG_WARN_UNREACHABLE_CODE = YES; 258 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 259 | CODE_SIGN_IDENTITY = "iPhone Developer"; 260 | COPY_PHASE_STRIP = NO; 261 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 262 | ENABLE_NS_ASSERTIONS = NO; 263 | ENABLE_STRICT_OBJC_MSGSEND = YES; 264 | GCC_C_LANGUAGE_STANDARD = gnu11; 265 | GCC_NO_COMMON_BLOCKS = YES; 266 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 267 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 268 | GCC_WARN_UNDECLARED_SELECTOR = YES; 269 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 270 | GCC_WARN_UNUSED_FUNCTION = YES; 271 | GCC_WARN_UNUSED_VARIABLE = YES; 272 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 273 | MTL_ENABLE_DEBUG_INFO = NO; 274 | SDKROOT = iphoneos; 275 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 276 | VALIDATE_PRODUCT = YES; 277 | }; 278 | name = Release; 279 | }; 280 | 2523167E1EF71675008E3527 /* Debug */ = { 281 | isa = XCBuildConfiguration; 282 | buildSettings = { 283 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 284 | DEVELOPMENT_TEAM = Q5MZT8NG66; 285 | INFOPLIST_FILE = MNISTPrediction/Info.plist; 286 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 287 | PRODUCT_BUNDLE_IDENTIFIER = io.gbrl.MNIST; 288 | PRODUCT_NAME = "$(TARGET_NAME)"; 289 | SWIFT_VERSION = 4.0; 290 | TARGETED_DEVICE_FAMILY = "1,2"; 291 | }; 292 | name = Debug; 293 | }; 294 | 2523167F1EF71675008E3527 /* Release */ = { 295 | isa = XCBuildConfiguration; 296 | buildSettings = { 297 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 298 | DEVELOPMENT_TEAM = Q5MZT8NG66; 299 | INFOPLIST_FILE = MNISTPrediction/Info.plist; 300 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 301 | PRODUCT_BUNDLE_IDENTIFIER = io.gbrl.MNIST; 302 | PRODUCT_NAME = "$(TARGET_NAME)"; 303 | SWIFT_VERSION = 4.0; 304 | TARGETED_DEVICE_FAMILY = "1,2"; 305 | }; 306 | name = Release; 307 | }; 308 | /* End XCBuildConfiguration section */ 309 | 310 | /* Begin XCConfigurationList section */ 311 | 252316661EF71675008E3527 /* Build configuration list for PBXProject "MNISTPrediction" */ = { 312 | isa = XCConfigurationList; 313 | buildConfigurations = ( 314 | 2523167B1EF71675008E3527 /* Debug */, 315 | 2523167C1EF71675008E3527 /* Release */, 316 | ); 317 | defaultConfigurationIsVisible = 0; 318 | defaultConfigurationName = Release; 319 | }; 320 | 2523167D1EF71675008E3527 /* Build configuration list for PBXNativeTarget "MNISTPrediction" */ = { 321 | isa = XCConfigurationList; 322 | buildConfigurations = ( 323 | 2523167E1EF71675008E3527 /* Debug */, 324 | 2523167F1EF71675008E3527 /* Release */, 325 | ); 326 | defaultConfigurationIsVisible = 0; 327 | defaultConfigurationName = Release; 328 | }; 329 | /* End XCConfigurationList section */ 330 | }; 331 | rootObject = 252316631EF71675008E3527 /* Project object */; 332 | } 333 | -------------------------------------------------------------------------------- /MNISTPrediction.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /MNISTPrediction/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // MNISTPrediction 4 | // 5 | // Created by Philipp Gabriel on 18.06.17. 6 | // Copyright © 2017 Philipp Gabriel. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 17 | return true 18 | } 19 | 20 | func applicationWillResignActive(_ application: UIApplication) {} 21 | 22 | func applicationDidEnterBackground(_ application: UIApplication) {} 23 | 24 | func applicationWillEnterForeground(_ application: UIApplication) {} 25 | 26 | func applicationDidBecomeActive(_ application: UIApplication) {} 27 | 28 | func applicationWillTerminate(_ application: UIApplication) {} 29 | } 30 | -------------------------------------------------------------------------------- /MNISTPrediction/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /MNISTPrediction/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /MNISTPrediction/Assets.xcassets/eight.imageset/14978199835997.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ph1ps/MNIST-CoreML/8aea319ae0fb26f3567f83b4bda50c3e33cb4657/MNISTPrediction/Assets.xcassets/eight.imageset/14978199835997.png -------------------------------------------------------------------------------- /MNISTPrediction/Assets.xcassets/eight.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "14978199835997.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /MNISTPrediction/Assets.xcassets/four.imageset/14978202165336.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ph1ps/MNIST-CoreML/8aea319ae0fb26f3567f83b4bda50c3e33cb4657/MNISTPrediction/Assets.xcassets/four.imageset/14978202165336.png -------------------------------------------------------------------------------- /MNISTPrediction/Assets.xcassets/four.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "14978202165336.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /MNISTPrediction/Assets.xcassets/one.imageset/14978178986780.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ph1ps/MNIST-CoreML/8aea319ae0fb26f3567f83b4bda50c3e33cb4657/MNISTPrediction/Assets.xcassets/one.imageset/14978178986780.png -------------------------------------------------------------------------------- /MNISTPrediction/Assets.xcassets/one.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "14978178986780.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /MNISTPrediction/Assets.xcassets/three.imageset/1497820050162.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ph1ps/MNIST-CoreML/8aea319ae0fb26f3567f83b4bda50c3e33cb4657/MNISTPrediction/Assets.xcassets/three.imageset/1497820050162.png -------------------------------------------------------------------------------- /MNISTPrediction/Assets.xcassets/three.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "1497820050162.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /MNISTPrediction/Assets.xcassets/two.imageset/14978214768760.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ph1ps/MNIST-CoreML/8aea319ae0fb26f3567f83b4bda50c3e33cb4657/MNISTPrediction/Assets.xcassets/two.imageset/14978214768760.png -------------------------------------------------------------------------------- /MNISTPrediction/Assets.xcassets/two.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "14978214768760.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /MNISTPrediction/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /MNISTPrediction/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /MNISTPrediction/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /MNISTPrediction/MNIST.mlmodel: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ph1ps/MNIST-CoreML/8aea319ae0fb26f3567f83b4bda50c3e33cb4657/MNISTPrediction/MNIST.mlmodel -------------------------------------------------------------------------------- /MNISTPrediction/UIImage+PixelBuffer.swift: -------------------------------------------------------------------------------- 1 | // 2 | // UIImage+PixelBuffer.swift 3 | // MNISTPrediction 4 | // 5 | // Created by Philipp Gabriel on 15.02.18. 6 | // Copyright © 2018 Philipp Gabriel. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | extension UIImage { 12 | 13 | func resize(to newSize: CGSize) -> UIImage? { 14 | 15 | guard self.size != newSize else { return self } 16 | 17 | UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0) 18 | self.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)) 19 | 20 | defer { UIGraphicsEndImageContext() } 21 | return UIGraphicsGetImageFromCurrentImageContext() 22 | } 23 | 24 | func pixelBuffer() -> CVPixelBuffer? { 25 | var pixelBuffer: CVPixelBuffer? = nil 26 | 27 | let width = Int(self.size.width) 28 | let height = Int(self.size.height) 29 | 30 | CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_OneComponent8, nil, &pixelBuffer) 31 | CVPixelBufferLockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue:0)) 32 | 33 | let colorspace = CGColorSpaceCreateDeviceGray() 34 | let bitmapContext = CGContext(data: CVPixelBufferGetBaseAddress(pixelBuffer!), width: width, height: height, bitsPerComponent: 8, bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer!), space: colorspace, bitmapInfo: 0)! 35 | 36 | guard let cg = self.cgImage else { 37 | return nil 38 | } 39 | 40 | bitmapContext.draw(cg, in: CGRect(x: 0, y: 0, width: width, height: height)) 41 | 42 | return pixelBuffer 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /MNISTPrediction/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // MNISTPrediction 4 | // 5 | // Created by Philipp Gabriel on 18.06.17. 6 | // Copyright © 2017 Philipp Gabriel. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController { 12 | 13 | override func viewDidLoad() { 14 | super.viewDidLoad() 15 | 16 | let size = CGSize(width: 28, height: 28) 17 | 18 | guard let image = #imageLiteral(resourceName: "eight").resize(to: size)?.pixelBuffer() else { 19 | fatalError() 20 | } 21 | 22 | guard let result = try? MNIST().prediction(image: image) else { 23 | fatalError() 24 | } 25 | 26 | print(result.classLabel) 27 | } 28 | 29 | override func didReceiveMemoryWarning() { 30 | super.didReceiveMemoryWarning() 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MNIST for CoreML (CNN) 2 | 3 | ### Description 4 | This is the MNIST dataset implemented in Apple's new framework CoreML. The MNIST dataset can predict handwritten (drawn) digits from an image and outputs a prediction from 0-9. The model was built with Keras 1.2.2. 5 | 6 | To test this model you can open the `MNISTPrediction.xcodeproj` and run it on your device (iOS 11 and Xcode 9 is required). To test further images just add them to the project and replace my testing with yours. 7 | 8 | An example of a handdrawn digit would look like this: 9 | ![Digit 4](MNISTPrediction/Assets.xcassets/four.imageset/14978202165336.png) 10 | 11 | **Be aware that your images have to have a black background and white line color!** 12 | 13 | Furthermore your images resolution has to be 28x28px. If yours is bigger just use my `UIImage` rescaling extension I wrote. The line width has to be thick enough to be recognized as a digit. 14 | 15 | ### Information about the model 16 | This CNN model achieves up to 99.5% of accuracy and the structure is as follows: 17 | 18 | ![CNN Model](model.png) 19 | -------------------------------------------------------------------------------- /model.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ph1ps/MNIST-CoreML/8aea319ae0fb26f3567f83b4bda50c3e33cb4657/model.png --------------------------------------------------------------------------------