0

我想做一个简单的 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

我很确定,我的 train- 和 labelstack 的结构不正确。有谁知道,我做错了什么?

我试图按照这个例子并将其翻译成我的案例: http ://www.mlpack.org/docs/mlpack-git/doxygen/cnetutorial.html

这是视觉工作室的图片,显示了我的火车组的结构: 在此处输入图像描述

这是视觉工作室的图片,显示了我的标签的结构: 在此处输入图像描述



感谢您的任何帮助。

你的,扬

这是我的代码:

#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
4

2 回答 2

1

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

于 2018-09-18T22:16:04.030 回答
0

好的,感谢马库斯,我发现了错误。不允许使用标签“0”。我只是将标签更改为:
1 ==> 零
2 ==> 一
3 ==> 二
...
10 => 九

有效。

于 2018-09-19T15:07:05.237 回答