从C ++代码读取mnist期间发生异常

时间:2019-01-04 06:27:36

标签: opencv visual-studio-2010 mnist

我在此函数中遇到异常-我试图从该函数读取mnist数据集,但我遇到了异常。我认为数据集文件未读取-在调试过程中,读取的值为空白,因此很清楚它无法读取,如何应对这种情况。

 int main(int argc, char** argv)
    {
        cout<<"Start of the main function";


        long start, end;
        start = clock();

        vector<Mat> trainX;
        vector<Mat> testX;
        Mat trainY, testY;

        readData(trainX, trainY, "C:\\Users\sadiq\Documents\Visual Studio 2010\Projects\cnn\myNewOpenCV\myNewOpenCV\mnist\train-images-idx3-ubyte", "C:\\Users\sadiq\Documents\Visual Studio 2010\Projects\cnn\myNewOpenCV\myNewOpenCV\mnist\train-labels-idx1-ubyte", 60000);
        readData(testX, testY, "C:\\Users\sadiq\Documents\Visual Studio 2010\Projects\cnn\myNewOpenCV\myNewOpenCV\mnist\t10k-images-idx3-ubyte", "C:\\Users\sadiq\Documents\Visual Studio 2010\Projects\cnn\myNewOpenCV\myNewOpenCV\mnist\t10k-labels-idx1-ubyte", 10000);

        cout<<"Read trainX successfully, including "<<trainX[0].cols * trainX[0].rows<<" features and "<<trainX.size()<<" samples."<<endl;
        cout<<"Read trainY successfully, including "<<trainY.cols<<" samples"<<endl;
        cout<<"Read testX successfully, including "<<testX[0].cols * testX[0].rows<<" features and "<<testX.size()<<" samples."<<endl;
        cout<<"Read testY successfully, including "<<testY.cols<<" samples"<<endl;

        int nfeatures = trainX[0].rows * trainX[0].cols;
        int imgDim = trainX[0].rows;
        int nsamples = trainX.size();
        Cvl cvl;
        vector<Ntw> HiddenLayers;
        SMR smr;

        ConvNetInitPrarms(cvl, HiddenLayers, smr, imgDim, nsamples);
        // Train network using Back Propogation
        batch = nsamples / 100;
        Mat tpX = concatenateMat(trainX);
        double lrate = getLearningRate(tpX);
        cout<<"lrate = "<<lrate<<endl;
        trainNetwork(trainX, trainY, cvl, HiddenLayers, smr, 3e-3, 200000, lrate);

        if(! G_CHECKING){
            // Save the trained kernels, you can load them into Matlab/GNU Octave to see what are they look like.
            saveWeight(cvl.layer[0].W, "w0");
            saveWeight(cvl.layer[1].W, "w1");
            saveWeight(cvl.layer[2].W, "w2");
            saveWeight(cvl.layer[3].W, "w3");
            saveWeight(cvl.layer[4].W, "w4");
            saveWeight(cvl.layer[5].W, "w5");
            saveWeight(cvl.layer[6].W, "w6");
            saveWeight(cvl.layer[7].W, "w7");

            // Test use test set
            Mat result = resultProdict(testX, cvl, HiddenLayers, smr, 3e-3);
            Mat err(testY);
            err -= result;
            int correct = err.cols;
            for(int i=0; i<err.cols; i++){
                if(err.ATD(0, i) != 0) --correct;
            }
            cout<<"correct: "<<correct<<", total: "<<err.cols<<", accuracy: "<<double(correct) / (double)(err.cols)<<endl;
        }    
        end = clock();
        cout<<"Totally used time: "<<((double)(end - start)) / CLOCKS_PER_SEC<<" second"<<endl;
        return 0;
    }

0 个答案:

没有答案