OCR神经网络(仅限数字)

时间:2014-06-04 11:42:15

标签: opencv neural-network ocr

一直致力于神经网络,让OCR识别数字。对于培训,我使用了从在线课程中获得的一组(来自MNIST数据库)。训练值在-1.28到1.28范围内(我不知道为什么会这样,这可能是问题的根源,但我正在努力从其他地方获得训练集,有什么建议吗?)。原因是,预测函数的'output'矩阵对于所有10个条目都是相同的值。基本上,神经网络不起作用..

void neuralNet(Mat M, Mat label, Mat test) {
//M is the training set of dimension 5000 X 400  (the image size is 20 X 20 and there are 5000 samples
        //label is 5000 X 10 , where the columns correspond to the represented number. i.e. ( if the number is 5 
         //  label[x][5]=1 and all other elements of the row are 0.
         // test has dimension 1 X 400

Mat layerSizes = new Mat(3,1,CvType.CV_32S); //Setting up the layers
layerSizes.put(0,0,400);
layerSizes.put(1, 0, 25);
layerSizes.put(2, 0, 10);
//Creating the Neural Network
CvANN_MLP nnet = new CvANN_MLP(layerSizes,CvANN_MLP.SIGMOID_SYM,0.6,1);
//Create criteria
TermCriteria criteria = new TermCriteria(TermCriteria.COUNT+TermCriteria.EPS,1000,0.000001);
//Create training parameters
CvANN_MLP_TrainParams param = new CvANN_MLP_TrainParams();
param.set_term_crit(criteria);
param.set_train_method(CvANN_MLP_TrainParams.BACKPROP);
param.set_bp_dw_scale(0.1);
param.set_bp_moment_scale(0.1);

//Training
int iter = nnet.train(M, label,new Mat(), new Mat() ,param, 0);
System.out.println("Number of iteratins: "+ iter);

System.out.println("Running test...");
Mat testOut = new Mat(1,10,CvType.CV_32FC1);

nnet.predict(test,testOut);
System.out.println(testOut.dump());
double []t = null;
System.out.println("The recognised symbols are:");
double max=0;
int loc=-1;
for(int j=0;j<10;j++)
{
    t=testOut.get(0, j);

    if(max < t[0])
    {
        max=t[0];
        loc=j;
    }
}
System.out.println(loc);    }

testOut.dump()导致[0 0 0 0 0 0 0 0 0 0]我对此非常陌生,但我对神经网络有一个“体面”的理解。我将不胜感激任何帮助。

1 个答案:

答案 0 :(得分:0)

这行代码

CvANN_MLP nnet = new CvANN_MLP(layerSizes,CvANN_MLP.SIGMOID_SYM,0.6,1);

给我一​​个印象,你使用sigmoid。如果你在-1.28和1.28之间输入它可能是一个很大的问题来源,因为sigmoid建议用于值[0,1]。你最好从sigmoig切换到tanh,但也可以将数据从[-1.28,1.28]标准化为[-1,1],或者如果你想将sigmoid保持为[0,1]。