上一篇( mlp模型参数提取 : //www.greatytc.com/p/2e5b2ff068eb )中我们得到了mlp训练模型的参数。
这篇介绍如何将这个模型应用在c++工程中进行银行卡图片预测。
首先需要介绍一个c++矩阵计算库。Eigen,开源的。可以去官网上下载最新版本编译安装。
官网地址:http://eigen.tuxfamily.org/index.php?title=Main_Page
下载后解压:
进入目录,在源码目录下新建一个build文件夹(执行下面三个命令):
mkdir buildrelease
cd buildrelease
cmake -DEIGEN_TEST_NO_OPENGL=1 ..
再执行命令
make
sudo make install
坐等它安装成功即可。
下一步,打开ide,配置环境变量,导入头文件,定义需要计算的矩阵
#include <eigen3/Eigen/Core>
#include <eigen3/Eigen/Dense>
typedef Eigen::Matrix<float, 1, 513, Eigen::RowMajor> FullyNumberImage;//输入矩阵
typedef Eigen::Matrix<float, 1, 513, Eigen::RowMajor> ModelInputImage;
typedef Eigen::Matrix<float, 513, 128, Eigen::RowMajor> ModelCWeight1Type;//模型参数中weight1的矩阵
typedef Eigen::Matrix<float, 128, 32, Eigen::RowMajor> ModelCWeight2Type; //模型参数中weight2的矩阵
typedef Eigen::Matrix<float, 32, 10, Eigen::RowMajor> ModelCWeight3Type; //模型参数中weight3的矩阵
typedef Eigen::Matrix<float, 1, 128, Eigen::RowMajor> ModelCB1; //模型参数中b1的矩阵
typedef Eigen::Matrix<float, 1, 32, Eigen::RowMajor> ModelCB2; //模型参数中b2的矩阵
typedef Eigen::Matrix<float, 1, 10, Eigen::RowMajor> ModelCB3; //模型参数中b3的矩阵
typedef Eigen::Matrix<float, 27, 19, Eigen::RowMajor> NumberImage;
typedef Eigen::Matrix<float, 1, 10, Eigen::RowMajor> SingleNumberScores;
typedef Eigen::Matrix<float, 1, 513, Eigen::RowMajor> FullyNumberImage;
定义数据,由于数据太多了,我就只写一部分意思下:
static float data_513_128[65664] ={-0.047784225994872205, -0.07284609638534617, -0.041955766457765144,.........};
static float data_128_32[4096]={-0.17730805116015405, 0.2448316226022322, 0.07461592356579184,........};
static float data_32_10[320]={-0.014385057734315638,.......};
static float data_1_128[128]={-0.0012434375941485945,....};
static float data_1_32[32]={-0.013492870061084548,......};
static float data_1_10[10]={0.04521966,.......};
下面是矩阵计算部分,还是那个公式,上一步得到的结果作为下一步的输入:
QunarModelCOutput_5c241121 apply_qunarmodel_2719(const QunarModelCInput_5c241121& input){
Eigen::Map<ModelCWeight1Type, Eigen::Aligned> weight1((float *)data_513_128);
Eigen::Map<ModelCWeight2Type, Eigen::Aligned> weight2((float *)data_128_32);
Eigen::Map<ModelCWeight3Type, Eigen::Aligned> weight3((float *)data_32_10);
Eigen::Map<ModelCB1, Eigen::Aligned> b1((float *)data_1_128);
Eigen::Map<ModelCB2, Eigen::Aligned> b2((float *)data_1_32);
Eigen::Map<ModelCB3, Eigen::Aligned> b3((float *)data_1_10);
//Eigen::Map<ModelCB3, Eigen::Aligned> r12((float *)data_10_1);
ModelCB1 output1 = input*weight1+b1;
ModelCB2 output2 = output1*weight2+b2;
ModelCB3 output3 = output2*weight3+b3;
//Eigen::Map<QunarModelCOutput_5c241121, Eigen::Aligned> r1((float *)data_10_1);
return output3;
}
使用测试数据还是和上一篇中用到的一个数字一样:
static float data_test_image[513]={228, 207, 220, 227, 223, 222, 211, 213, 224, 215, 213, 221, 223, 218, 223, 234, 227, 224, 222, 219, 215, 226, 213, 205, 220, 223, 227, 211, 211, 209, 208, 213, 219, 216, 208, 210, 206, 203, 220, 215, 228, 226, 223, 213, 181, 169, 162, 181, 186, 175, 176, 196, 207, 201, 160, 154, 144, 238, 212, 209, 205, 186, 148, 123, 148, 197, 212, 200, 150, 103, 90, 99, 109, 122, 124, 123, 216, 211, 209, 174, 120, 74, 81, 149, 163, 166, 157, 122, 72, 44, 55, 79, 110, 122, 131, 215, 223, 200, 129, 74, 48, 56, 111, 139, 128, 122, 113, 81, 40, 31, 45, 85, 106, 123, 214, 195, 134, 55, 32, 39, 50, 96, 119, 110, 113, 119, 95, 52, 29, 33, 56, 88, 118, 128, 111, 74, 34, 34, 34, 41, 96, 121, 123, 131, 128, 94, 46, 21, 22, 45, 81, 113, 122, 85, 55, 29, 25, 31, 48, 96, 117, 128, 118, 115, 90, 42, 27, 29, 37, 65, 106, 121, 83, 52, 26, 21, 25, 42, 90, 116, 128, 118, 115, 90, 41, 25, 26, 32, 59, 100, 120, 82, 51, 28, 23, 27, 44, 95, 116, 128, 119, 116, 90, 40, 22, 22, 26, 52, 91, 115, 75, 46, 27, 25, 30, 49, 102, 116, 128, 119, 117, 90, 39, 20, 19, 25, 48, 83, 109, 68, 39, 22, 21, 25, 44, 99, 116, 128, 119, 117, 90, 38, 19, 18, 26, 46, 78, 112, 69, 40, 24, 21, 22, 40, 96, 118, 129, 119, 116, 90, 39, 21, 20, 26, 45, 75, 115, 70, 41, 26, 23, 23, 41, 98, 119, 130, 119, 116, 90, 40, 23, 23, 29, 46, 74, 107, 63, 35, 21, 21, 20, 40, 98, 120, 130, 119, 115, 89, 40, 25, 26, 31, 47, 73, 116, 73, 36, 18, 22, 20, 38, 105, 121, 131, 120, 116, 90, 42, 29, 32, 34, 59, 82, 117, 80, 44, 23, 27, 27, 41, 99, 124, 133, 121, 118, 93, 45, 31, 33, 43, 71, 98, 119, 91, 57, 29, 29, 32, 43, 94, 123, 133, 122, 120, 97, 50, 35, 35, 46, 78, 108, 121, 103, 73, 34, 26, 30, 45, 96, 119, 131, 122, 122, 99, 50, 34, 33, 50, 83, 113, 123, 114, 89, 44, 26, 27, 45, 100, 119, 132, 124, 121, 95, 45, 29, 31, 64, 94, 121, 126, 122, 104, 63, 41, 33, 46, 101, 121, 134, 124, 118, 89, 41, 34, 42, 82, 106, 126, 129, 125, 116, 88, 69, 49, 47, 95, 116, 130, 119, 111, 83, 46, 54, 74, 103, 118, 129, 131, 126, 123, 107, 92, 64, 49, 88, 109, 123, 112, 104, 81, 54, 75, 105, 122, 131, 134, 128, 127, 127, 124, 116, 105, 98, 95, 106, 111, 113, 107, 99, 99, 112, 124, 127, 127, 127, 129, 130, 131, 131, 127, 122, 119, 117, 114, 116, 117, 117, 116, 118, 123, 128, 127, 127, 127, 127, 128, 129, 130, 130, 130, 130, 129, 125, 123, 123, 127, 132, 133, 130, 125, 127, 127, 127};
运行程序:
debug模式下查看程序每一步的输出:
和上一篇中用python实现的结果是一致的。