1 Introduction
As an iOS developer, there is always an idea to run deep learning on the iphone, whether it's training on a mobile phone or testing with well-trained data.
Because the iOS development environment supports C + +, you can essentially run on iOS as long as your code is c/s + +.
How can you run CNN faster and better on iOS?
2 Method 1: transcoding via matlab
Matlab comes with tools to turn to C, if you have studied UFLDL's deep learning tutorial, you know how to use the CNN in Matlab, then, converted to C, put in the iOS development environment, and then save the parameters into the TXT format and then read the segmentation, that is, can be achieved.
If you have converted Matlab code to c after the result of the import:
Open the Predict.h file to see the interfaces that can be called:
/* Function Declarations */extern real_T predict(const real_T Theta1[10025const real_T Theta2[260const real_T X[400]);
This is a neural network for training mnist, and my side uses the recognition of handwritten numbers for testing.
Therefore, the next step is to process the image and convert it to a vector format of x[400].
This can be converted as long as the pixel of the image can be read. You can consider using OPENCV to achieve this.
Here my method is to draw the number by hand, the picture is converted to 20*20 pixels, as shown in the lower right corner, and then the lower right corner of the picture into an array of 400, input predict results.
3 Method 2: Use DEEPBELIEFSDK
Https://github.com/jetpacapp/DeepBeliefSDK
This is an SDK for deep learning for iOS, written by someone else. Can be used, but the problem is that it is very limited if you want to train yourself.
4 Method 3: Use TINYCNN
Https://github.com/nyanp/tiny-cnn
This is very good, it is compared with the Caffe,theano and other framework of the biggest feature is not need to install, as long as the use of C + + 11. Then the example used in the Boost library. So, in order to run it, we need to install iOS boost library in Xcode.
A compiled boost library was found on the Web:
Https://github.com/danoli3/ofxiOSBoost
The way to import the boost library is simple:
In Xcode Build Settings forYour Project:add toLibrary Search Paths (library_search_paths) $ (srcroot)/. /.. /.. /addons/ofxiosboost/libs/boost/lib/iosadd toHeader Search Paths (header_search_paths) $ (srcroot)/. /.. /.. /addons/ofxiosboost/libs/boost/includeInch theTarget under Build phasesadd to ' Link Binary with Libraries ' theBoost.aFoundinch theOfxiosboost/libs/boost/lib/iosDirectory. If notOpenframeworks justAdd thelibs/boost/include toHeader Search Paths and theLibs/boost/ios toLibrary Search Paths
So when creating iOS apps, here are examples of training mnist provided by the author, so be aware that when working with data, you change the path:
NSString*trainlabels = [[NSBundleMainbundle] pathforresource:@"Train-labels"oftype:@"Idx1-ubyte"];NSString*trainimages = [[NSBundleMainbundle] pathforresource:@"Train-images"oftype:@"Idx3-ubyte"];NSString*t10klabels = [[NSBundleMainbundle] pathforresource:@"T10k-labels"oftype:@"Idx1-ubyte"];NSString*t10kimages = [[NSBundleMainbundle] pathforresource:@"T10k-images"oftype:@"Idx3-ubyte"]; Parse_mnist_labels ([Trainlabels cstringusingencoding:nsutf8stringencoding], &train_labels); Parse_mnist_images ([Trainimages cstringusingencoding:nsutf8stringencoding], &train_images); Parse_mnist_labels ([T10klabels cstringusingencoding:nsutf8stringencoding], &test_labels); Parse_mnist_images ([T10kimages cstringusingencoding:nsutf8stringencoding], &test_images);
Basically, you can run and start training.
If you want to train on a Mac, you also need to install the boost library. This is only available on the website to download boost, I use version 1.58. Then install the CD to the path in terminal, then./boostrap.sh then./B2 installation is possible. Then introduce the path in Xcode:
The Boost C++ Libraries were successfully built!The following directory should be added to compiler include paths: /Users/.../.../...library paths: /Users/.../.../.../boost/stage/lib
The path is initially the folder address of its own boost.
4 Summary
It says there are some handy ways to run CNN under iOS. Of course, the more we need is the recognition of images. I'm sure you'll find it interesting to test yourself.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Deep learning to run CNN on iOS