使用mlpack

时间:2018-09-18 08:48:04

标签: c++ machine-learning mnist mlpack

我想做一个简单的dnn示例,以了解mlpack。 最简单的例子是用MNist Digits训练dnn-经典 ml-hello世界案例:-)

我设法使用opencv-filters准备了所有图像-结果是 单通道灰度opencv :: mat矩阵。

我还设法将像素值转换为犰狳矩阵arma :: mat 并将其标记为“图片”。 但是在过去两个操作中的某个地方,我犯了一个错误。

我有N个例子和M个输入神经元 IN表示“输入神经元”
OL的意思是“ OutputLabel”
Ex表示“示例”
我的火车数据的结构是这样的:

arma::mat TrainSet = {{IN_1/Ex_1,IN_/Ex_2,IN_1/Ex_3,...,IN_1/Ex_N},
                      {IN_2/Ex_1, IN_2/Ex_2, IN_2/Ex_3,...,IN_2/Ex_N},
                      {...},
                      {IN_M/Ex_1, IN_M/Ex_2, IN_M/Ex_3,...,IN_M/Ex_N}}`

arma::mat LabelSet = {OL_Ex_1, OL_Ex_2, ..., OL_Ex_N}

训练我的网络会导致错误。

Error training artificial neural network! Error details: Mat::operator(): index out of bounds

我非常确定,我的火车和标签栈的结构不正确。 有人知道,我做错了吗?

我尝试遵循此示例并将其转换为我的案例: http://www.mlpack.org/docs/mlpack-git/doxygen/cnetutorial.html

这是Visual Studio的图片,显示了我的火车的结构: enter image description here

这是Visual Studio的图片,显示了我的标签结构: enter image description here



谢谢您的帮助。

您, 扬

这是我的代码:

#pragma region Includings
#include <iostream>
#include <stdlib.h>
#include <exception>
#include <string>
#include "opencv2/opencv.hpp"
#include <mlpack\\core.hpp>
#include <mlpack/methods/ann/layer/layer.hpp>
#include <mlpack/methods/ann/ffn.hpp>
#include <mlpack/core/optimizers/cne/cne.hpp>
#pragma endregion

#pragma region Globals
std::string TrainFolder = "C:\\HomeC\\MNist\\MNist\\train-labels\\";
#pragma endregion

#pragma region Structs
typedef struct TInputPair {
    double Value;
    int Index;
};

typedef struct TDigitPairExample {
    TInputPair* InputPairArray;
    int nNonZero;
    char OutputValue;
};
#pragma endregion

#pragma region Identifier
void DisplayImage(cv::Mat* Img, std::string Title = "CV::DefaultForm");
std::vector<TDigitPairExample> GenerateTrainingSet(std::string TrainFolder, int nExamplesPerClass, bool DisplayAtWindow = false);
void DisplayImage(cv::Mat* Img, std::string Title, int Delay = 0);
TInputPair* MatToArray(cv::Mat* img, int* nEntries);
int CharToOutputInt(char c);
void TransferDataToMLPack(std::vector<TDigitPairExample>* ExStack, arma::mat* DataStack, arma::mat* LabelStack, int nInput);
typedef uchar Pixel;
#pragma endregion

int main() {
#pragma region Get training examples from images
std::vector<TDigitPairExample> TrainExamples = GenerateTrainingSet(TrainFolder, 101);
#pragma endregion

#pragma region Convert training vector to armadillo matrix
arma::mat trainset, labels;
TransferDataToMLPack(&TrainExamples, &trainset, &labels, 784);
#pragma endregion

#pragma region Define network
mlpack::ann::FFN<mlpack::ann::NegativeLogLikelihood<> > network;
network.Add<mlpack::ann::Linear<> >(784, 784);
network.Add<mlpack::ann::SigmoidLayer<> >();

network.Add<mlpack::ann::Linear<> >(784, 10);
network.Add<mlpack::ann::LogSoftMax<> >();
#pragma endregion

#pragma region Train network
try {
    network.Train(trainset, labels);
}catch (const std::exception& e) {
    std::cout << "Error training artificial neural network!" << std::endl << "Error details: " << e.what() << std::endl;
}
#pragma endregion

std::cout << "Application finished. Press ENTER to exit..." << std::endl;
std::cin.get();
}

#pragma region Private_regions
void DisplayImage(cv::Mat* Img, std::string Title, int Delay) {

/***************/
/*Define window*/
/***************/
cv:cvNamedWindow(Title.c_str(), cv::WINDOW_AUTOSIZE);
cv::imshow(Title.c_str(), *Img);
cv::waitKey(Delay);
//cv::destroyWindow(Title.c_str());

return;
}

TInputPair* MatToArray(cv::Mat* img, int* nEntries) {
uchar* ptr = nullptr, *dptr = nullptr;
TInputPair* InPairArr = nullptr;
int j = 0;

if (img->isContinuous()) {
    ptr = img->ptr<uchar>();
}else { return nullptr; }

InPairArr = (TInputPair*)malloc((img->cols) * (img->rows) * sizeof(TInputPair));
if (InPairArr == nullptr) { return nullptr; }

for (int i = 0; i < (img->rows)*(img->cols); i++) { 
    //std::cout << "Index_" + std::to_string(i) + "; " + std::to_string(ptr[i]) << std::endl; 
    if (ptr[i] != 255) { InPairArr[j].Index = i; InPairArr[j].Value = (double)(255 - ptr[i]) / 255.0; j++; }
}

InPairArr = (TInputPair*)realloc(InPairArr, j * sizeof(TInputPair));
*nEntries = j;

return InPairArr;
}

std::vector<TDigitPairExample> GenerateTrainingSet(std::string TrainFolder, int nExamplesPerClass, bool DisplayAtWindow) {
/********/
/*Localc*/
/********/
int nEntries = 0;
cv::Mat imgMod, imgGrad, imgInv, ptHull, imgHull, imgResize;
std::vector<std::vector<cv::Point>> contours;
std::vector<TDigitPairExample> TrainExamples;
TDigitPairExample TDPE;

for (int i = 1, j = 0;; i++) {
    /**************/
    /*Reading file*/
    /**************/
    cv::Mat imgOrig = cv::imread(TrainFolder + std::to_string(j) + "_" + std::to_string(i) + ".bmp", cv::IMREAD_GRAYSCALE);
    if (imgOrig.empty() || i > 100) { j++; i = 1; if (j > 9) { break; } continue; }




    /****************/
    /*Build negative*/
    /****************/
    cv::subtract(cv::Scalar::all(255.0), imgOrig, imgMod);





    /*****************/
    /*Cut by treshold*/
    /*****************/
    cv::threshold(imgMod, imgMod, 230.0, 255.0, cv::THRESH_BINARY);





    /**************/
    /*Get contours*/
    /**************/
    //cv::findContours(imgMod, contours, cv::CHAIN_APPROX_NONE, 1);
    //cv::Scalar color = cv::Scalar(255, 0, 0);
    //cv::drawContours(imgMod, contours, -1, color, 1, 8);



    //cv::Laplacian(imgOrig, imgGrad, 16, 1, 1.0, 0.0, cv::BORDER_REFLECT);
    /********************/
    /*Resize and display*/
    /********************/
    cv::resize(imgMod, imgResize, cv::Size(300, 300), .0, .0, cv::INTER_LINEAR);
    TDPE.InputPairArray = MatToArray(&imgMod, &nEntries);
    TDPE.nNonZero = nEntries;
    TDPE.OutputValue = std::to_string(j).c_str()[0];
    TrainExamples.push_back(TDPE);
    if (DisplayAtWindow) { DisplayImage(&imgResize, std::string("After inversion"), 5); }
}

return TrainExamples;
}

int CharToOutputInt(char c) {
switch (c) {
    case '0': return 0;
    case '1': return 1;
    case '2': return 2;
    case '3': return 3;
    case '4': return 4;
    case '5': return 5;
    case '6': return 6;
    case '7': return 7;
    case '8': return 8;
    case '9': return 9;
    default: throw new std::exception();
}
}

void TransferDataToMLPack(std::vector<TDigitPairExample>* ExStack, arma::mat* DataStack, arma::mat* LabelStack, int nInput) {
    *DataStack = arma::zeros(nInput, ExStack->size());
    *LabelStack = arma::zeros(1, ExStack->size()); /*...edit...*/

    TDigitPairExample DPE;
    TInputPair TIP;

    /*Looping all digit examples*/
    for (int i = 0; i < ExStack->size(); i++) {
        DPE = (*ExStack)[i];

        /*Looping all nonZero pixle*/
        for (int j = 0; j < DPE.nNonZero; j++) {
            TIP = DPE.InputPairArray[j];
            try {
                (*DataStack)(TIP.Index, i) = TIP.Value;
            }catch (std::exception& ex) {
                std::cout << "Error adding example[" << std::to_string(j) << "] to training stack!" << std::endl <<
                    "Error details: " << ex.what() << std::endl;
            }
        }

        /*Adding label*/
        try {
            (*LabelStack)(0, i) = CharToOutputInt(DPE.OutputValue); /*...edit...*/
        }catch (std::exception& ex) {
            std::cout << "Error adding example[" << std::to_string(i) << "] to label stack!" << std::endl <<
                "Error details: " << ex.what() << std::endl;
        }
    }

    return;
}
#pragma endregion

2 个答案:

答案 0 :(得分:1)

NegativeLogLikelihood损失函数期望目标在[1, N]范围内,因此您必须增加CharToOutputInt的返回值。如果您还没有看到它,可以举个有趣的例子:mlpack - DigitRecognizerCNN,这也可能会有所帮助。

答案 1 :(得分:0)

好的,感谢Marcus,我找到了错误。不允许标签“ 0”。我只是将标签更改为:
1 ==>零
2 ==>一个
3 ==>两个
...
10 =>九个

可行。