Deep Learning: Running CNN on iOS, deep learning ioscnn
1 Introduction
As an iOS developer, when studying deep learning, I always thought that I would run deep learning on the iPhone, whether on a mobile phone or using trained data for testing.
Because the iOS development environment supports C ++, as long as your code is C/C ++, you can basically run it on iOS.
How can we run CNN on iOS faster and better?
2 Method 1: Transcoding Using Matlab
Matlab comes with a tool to convert it to c. If you have studied the UFLDL deep learning tutorial, you will know how to use CNN on Matlab. Then, convert it to c and put it in the iOS development environment, save the parameters in txt format and then read and split them, that is, they can be implemented.
For example, the import result after converting the matlab code to c:
Open the predict. h file and you can see the called interface:
/* Function Declarations */extern real_T predict(const real_T Theta1[10025], const real_T Theta2[260], const real_T X[400]);
This is a neural network used to train MNIST. I used a test to identify handwritten numbers.
Therefore, we need to process the image and convert it to the vector format of x [400.
As long as the pixels of the image can be read and converted. We can consider using opencv for implementation.
Here, my method is to convert the image to a 20*20 pixel image after drawing a number by hand, as shown in the lower right corner, and then convert the image in the lower right corner to an array of 400, enter the result of predict.
3 Method 2: Use DeepBeliefSDK
Https://github.com/jetpacapp/DeepBeliefSDK
This is a deep learning SDK specially written by someone else for iOS. Yes, but the problem is that it is very limited if you need to train yourself.
4 method 3: tinyCNN
Https://github.com/nyanp/tiny-cnn
This is quite good. Compared with Caffe, Theano, and other frameworks, the biggest feature is that they do not need to be installed, as long as they can use C ++ 11. Then the example inside uses the boost library. Therefore, to run it, we need to install the ios boost library in xcode.
A compiled boost library is found on the Internet:
Https://github.com/danoli3/ofxiOSBoost
The method for importing boost libraries is very simple:
In Xcode Build Settings for your project:Add to Library Search Paths ( LIBRARY_SEARCH_PATHS ) $(SRCROOT)/../../../addons/ofxiOSBoost/libs/boost/lib/iosAdd to Header Search Paths ( HEADER_SEARCH_PATHS )$(SRCROOT)/../../../addons/ofxiOSBoost/libs/boost/includeIn the Target under Build PhasesAdd to 'Link Binary With Libraries' the boost.a found in the ofxiOSBoost/libs/boost/lib/ios directory.If not openFrameworks just add the libs/boost/include to Header Search Paths and the libs/boost/ios to Library Search Paths
When creating an iOS application, we use the MNIST training example provided by the author. Note that you must change the path when using data:
NSString *trainLabels = [[NSBundle mainBundle] pathForResource:@"train-labels" ofType:@"idx1-ubyte"]; NSString *trainImages = [[NSBundle mainBundle] pathForResource:@"train-images" ofType:@"idx3-ubyte"]; NSString *t10kLabels = [[NSBundle mainBundle] pathForResource:@"t10k-labels" ofType:@"idx1-ubyte"]; NSString *t10kImages = [[NSBundle mainBundle] 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);
In this way, you can start training.
If you want to train on Mac, you also need to install the boost library. I only need to download boost from the official website and use version 1.58. Then install it in terminal, cd to the path, and then./boostrap. sh and install./b2. Then introduce the path in xcode:
The Boost C++ Libraries were successfully built!The following directory should be added to compiler include paths: /Users/.../.../.../boostThe following directory should be added to linker library paths: /Users/.../.../.../boost/stage/lib
The path is initially the boost folder address.
4. Summary
The above describes some convenient methods to run CNN in iOS. Of course, we need to recognize images. I believe everyone will find it interesting to test it on their own.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.