C + + uses MATLAB convolutional neural network library matconvnet for handwritten digit recognition

Source: Internet
Author: User
Tags microsoft c

Environment: WIN10 (+vs2010) +matlab2015b (+ bit)


Introduction to Matconvnet Reference: http://www.vlfeat.org/matconvnet/

Github is: https://github.com/vlfeat/matconvnet/


Our goal is to migrate Matconvnet's handwritten digital recognition demo to a simple WIN32 demo, with the following steps in the main process:

(1) Configure the matconvnet and then compile the handwritten digital recognition demo into the C + + shared library.

(2) write the calling code and test the result.

Configuring compilation matconvnet and Mnist examples

First from the above address download matconvnet source and unzip, and then open matlab2015b, the source of the Matlab folder to the current path, as shown in:


Executes the Mex-setup command, at which point MATLAB displays the available compilers, with the following effects:


Then choose the compiler we want to use Microsoft Visual C + + (directly with the mouse point), and then enter the Compile command: Vl_compilenn, execute, after completion can see a folder called Mex, as shown in:


So the configuration is complete, let's compile the mnist example under examples, switch the current path of Matlab to Mnist folder, open the Cnn_mnist.m file and execute it directly, as shown in:


The above is actually downloading the corresponding training data (data stored in the Data\mnist directory, a total of 4 files: T10k-images-idx3-ubyte,t10k-labels-idx1-ubyte, Train-images-idx3-ubyte and Train-labels-idx1-ubyte). Of course, the corresponding data can be downloaded first, and then put in the specified directory, you can skip this step.

After waiting for a while (the default is 20 epochs, you can modify the value of Net.meta.trainOpts.numEpochs in the cnn_mnist_init.m file to reduce the number of epochs and wait time), after the training is complete, open the Data\ The Mnist-baseline-simplenn folder can see the results of the training as follows:


Then execute the following two commands:

[NET, ~]=cnn_mnist (); Save ('.. \\.. \\data\\mnist-baseline-simplenn\\net.mat ', '-struct ', ' net ');

After execution can see the Mnist-baseline-simplenn directory more than a Net.mat file, we need to use Imdb.mat and Net.mat later.

Next we need to write a test_mnist.m file of our own, and then use it to generate the C + + shared library.

It is important to note here that the VL_SETUPNN function is to add the path of all subfolders under the Matlab folder to the path variable of MATLAB, similar to the concept of Set header file directory in VS, so for the sake of simplicity, we directly put Matlab under the. m file and matlab Copy the. mex64 file under \mex to a folder, I placed it directly under the Test_mnist folder under D, and created a new empty file test_mnist.m, as shown in:


Copy the following code into the TEST_MNIST.M file:

function numeric = Test_mnist (IMG, Net_path, imdb_path) net = load (Net_path); IMDB = Load (imdb_path); Net.layers{1, end}. Type = ' Softmax ';%resize inuput imgimg = Imresize (IMG, [], ' bicubic '); img = single (IMG); img = Bsxfun (@minus, IMG, IMD B.images.data_mean); res = Vl_simplenn (NET, IMG); [~, ~, n] = size (res (end). x (1, 1,:)) tmp = 0.0;for i = 1:n    res (end). x (1, 1, i)    if tmp < res (end). x (1, 1, i) 
   numeric = i-1;        TMP = RES (end). x (1, 1, i);    EndEnd

Note: The Vl_simplenn function returns a multidimensional array, and the last dimension holds the probability values identified as 0~9 respectively, and the maximum probability value is the final result.

Now start compiling the C + + shared library, there are two ways, the first is to compile with the command, and the other is a visual tool with Matlab library Compiler, we use the first way, enter the following command:

Mcc-w cpplib:dnrecognize-t Link:lib test_mnist

Dnrecognize indicates that the generated library name,test_mnist the MATLAB file specified to be compiled.

After compiling, a lot of files are generated in the current directory, we focus on three (Dnrecognize.dll,dnrecognize.h,dnrecognize.lib):

Well, the library we need to call in C + + is ready, and now we're going to create a new WIN32 engineering testmnist to test.

The configuration is as follows:

Include header File directory:

. \test_mnist; C:\Program Files\matlab\r2015b\extern\include; C:\Program Files\matlab\r2015b\extern\include\win64

Library directory:

. \test_mnist; C:\Program Files\matlab\r2015b\extern\lib\win64\microsoft


Write The code for the WIN32 section

This part can be built according to their own ideas, I simply realized a background black window, and then you can use the mouse to draw a number, left-click can be handwritten a number, right-click, the middle key to recognize handwritten numbers, interface and effects are as follows:


When the Cnn_mnist function parameter is passed in, it is important to note that in MATLAB The matrix is stored in column-First, remember.

The following are interested in my inferior code:

http://download.csdn.net/detail/jieleiping/9500490


Problems that exist:

(1) It seems that the handwritten number 8 is not correct? I don't know what the reason is.

(2) in the process of running the program, there are a lot of abnormal information, do not know why, but the program is executed correctly. If anyone knows why, be sure to tell me, thank you . The error message is as follows:

0x00007ffaf3531f28 most likely exception in TestMnist.exe: Microsoft C + + exception: Std::logic_error at memory location 0x154feb18. 0x00007ffaf3531f28 most likely exception in TestMnist.exe: Microsoft C + + exception: Matrix::serialize::endoffile at memory location 0x0b4ec570. Most likely exceptions in TestMnist.exe 0x00007ffaf3531f28: Microsoft C + + exception: Cryptopp::aes_phm_decryption::i at memory location 0x0b4e7d60 Nvalidciphertextorkey. 0x00007ffaf3531f28 most likely exception in TestMnist.exe: Microsoft C + + exception: Fl::filesystem::P athnotfound at memory location 0x0014e218. 0x00007ffaf3531f28 most likely exception in TestMnist.exe: Microsoft C + + exception: Xsd_binder::malformeddocumenterror at memory location 0X0014CF10.

Off-topic, if you need to publish the program, it seems particularly troublesome.

At first I thought it would be possible to bring the DLL (including the VS Library, Test_mnist.dll, and Mclmcrrt9_0.dll in the R2015b\runtime\win64 directory), but it would not work on other computers that did not have a corresponding MATLAB environment. 100 degrees is said to be in the computer without MATLAB to perform MATLAB, you need to install the MCR on the corresponding computer.

Details visible:http://cn.mathworks.com/products/compiler/mcr/index.html?refresh=true

In fact, the Matlab\r2015b\toolbox\compiler\deploy\win64 directory MCRInstaller.exe this file is the corresponding MCR installation files. (You can actually specify an additional MCR when compiling the C + + shared library with Matlab compiler, which is not detailed here.)





C + + uses MATLAB convolutional neural network library matconvnet for handwritten digit recognition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.