Deep Learning-converting Matlab to C ++-testing CNN manual recognition and ioscnn on iOS
1 Preface
In the previous Blog, I introduced some methods for running CNN on iOS. However, in general, we need a machine with strong performance to run CNN, but we just need to apply the results to mobile terminals. Previously, after using UFLDL code modification in Matlab, we ran the 3-layer CNN for manual recognition. Here we will consider converting Matlab to C and then porting it to xcode.
Step 1: Convert Matlab to c
First, make sure that the code can be run and run. For example, we will test the cnn recognition manual as follows:
>> parameters = load('./opt_parameters/opttheta_8epoches_cnn.mat');cnnPredict(imread('./data/test_five1 (1).bmp'),parameters.opttheta)ans = 5
As you can see, I have identified five fingers. OK. There is no problem with CNN. Now we want to convert the cnnPredict function to c. here we can see that this function contains input data and trained parameters.
function labels = cnnPredict(images,opttheta)
The basic method is to use the tool that comes with Matlab: coder.
Enter coder in the Command window:
Create a project:
Here I have imported the cnnPredict file to be transferred. m. There are two input variables. I need to define the variable type. Here I use autodefine types to write a script to run this function. That is, the code I posted at the beginning is identified as follows:
Here, we can see that there are not many CNN parameters, that is, 0.19 million parameters.
The next step is build. Here, select c/c ++ static library and only output c code:
The build result is as follows:
It is possible that you will fail to build. In this case, it may be a data type problem. You can modify it as needed until it is successful.
The generated code is in the codegen folder of the folder:
Step 2: Export .mate8 to the. txt format
Region format.
The export method is very simple, with a piece of code;
> Save('opttheta.txt ', 'opttheta', '-ASCII'); % save opt thetaworkflow as opttheta.txt
Step 3: Create an iOS project and import the cnnPredict code
This step is very simple. It is OK to pull the entire folder in.
Note the cnnPredict. h code. We will use the following functions:
/* * File: cnnPredict.h * * MATLAB Coder version : 2.7 * C/C++ source code generated on : 16-Jul-2015 16:22:01 */#ifndef __CNNPREDICT_H__#define __CNNPREDICT_H__/* Include Files */#include <math.h>#include <stddef.h>#include <stdlib.h>#include <string.h>#include "rt_nonfinite.h"#include "rtwtypes.h"#include "cnnPredict_types.h"/* Function Declarations */extern double cnnPredict(const double images[9216], const double opttheta[195245]);#endif/* * File trailer for cnnPredict.h * * [EOF] */
Note that an interface folder in the import operation will cause the operation to fail and should be deleted without affecting other operations.
Step 4 Import parameters in Xcode
This step is to read the data in the txt file and store it as an array of double, directly paste the Code:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"opttheta" ofType:@"txt"]; NSString *testString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; NSMutableArray *thetaString = (NSMutableArray *)[testString componentsSeparatedByString:@"\n"]; [thetaString removeLastObject]; NSLog(@"Theta1 count:%lu",(unsigned long)thetaString.count); for (int i = 0; i < thetaString.count; i++) { NSString *data = [thetaString objectAtIndex:i]; theta[i] = [data doubleValue]; }
From the code, we can see that data is separated by '\ n', which is very simple.
Step 5 convert an image to a double Array
To use the function, we must convert the image into an array. Here we use grayscale images. The conversion code is as follows:
UIImage *image = [UIImage imageNamed:@"one.bmp"];CGImageRef imageRef = [image CGImage];CGDataProviderRef provider = CGImageGetDataProvider(imageRef);NSData *data = (id)CFBridgingRelease(CGDataProviderCopyData(provider));NSLog(@"image:%lu",(unsigned long)data.length);const uint8_t *bytes = [data bytes];
Here we will convert it to the uint8 array. Next we will transpose the grayscale matrix of the image as needed:
double newBytes[9216]; for (int y = 0; y < 96 ; y++) { for (int x = 0; x < 96; x++) { newBytes[x*96 + y] = bytes[y*96 + x]; } }
Step 6: Run cnn
With the above processing, this step runs cnnPredict directly
double result = cnnPredict(newBytes, theta);NSLog(@"result:%f",result);
The result is output directly:
Have you seen this? The result is 1, which means a thumbs up.
In fact, I am a little excited here. Isn't it really nice that the CNN running on iOS directly recognizes gestures, although the pictures here are black and white, it's a little simpler.
Summary
This article summarizes how to convert CNN's MATLAB code to C ++ code and then run it directly on iOS. Hope to inspire the same person!
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.