【问题标题】:Training CNN for NIST digits using tiny-dnn使用 tiny-dnn 为 NIST 数字训练 CNN
【发布时间】:2018-04-12 06:50:39
【问题描述】:

我一直在尝试使用 tiny-dnn 库来训练 CNN 进行数字识别。使用的数据库是 NIST 19。每类的样本数为 1000 个用于训练,30 个用于测试。所以训练的样本总数是 1000*10=10000。 OpenCV用于图像处理。

获得的最大准确率为 40%。这是因为样本数量少吗? 如何提高准确率?

代码如下

ConvolutionalNN::train()
{
    network<sequential> net;

    // add layers
    net << conv(32, 32, 5, 1, 6) << tiny_dnn::activation::tanh()  // in:32x32x1, 5x5conv, 6fmaps
        << ave_pool(28, 28, 6, 2) << tiny_dnn::activation::tanh() // in:28x28x6, 2x2pooling
        << fc(14 * 14 * 6, 120) << tiny_dnn::activation::tanh()   // in:14x14x6, out:120
        << fc(120, 10);                     // in:120,     out:10

    assert(net.in_data_size() == 32 * 32);
    assert(net.out_data_size() == 10);

    DatabaseReader db;
    db.readTrainingFiles();

    // hold labels -> training filenames
    std::vector<int> labels = db.getTrainLabels();
    std::vector<std::string> trainingFilenames = db.getTrainFileNames();

    std::vector<label_t> train_labels;
    std::vector<vec_t> train_images;

    // loop over training files
    for(int index=0; index<trainingFilenames.size(); index++)
    {
        // output on which file we are training
        std::cout << "Analyzing label -> file: " <<  labels[index] << "|" <<  trainingFilenames[index] << std::endl;

        // read image file (grayscale)
        cv::Mat imgMat = cv::imread(trainingFilenames[index], 0);

        Mat nonZero;
        Mat invert = 255 - imgMat;
        findNonZero(invert, nonZero);
        Rect bb = boundingRect(nonZero);
        Mat img = invert(bb);

        int w=32, h=32,scale=1;
        cv::Mat resized;
        cv::resize(img, resized, cv::Size(w, h));

        imshow("img", resized);
        waitKey(30);
        //convert to float

        resized.convertTo(resized, CV_32FC1);
        cv::normalize(resized,resized, -1, 1, NORM_MINMAX);

        //convert to vec_t

        vec_t d;
        tiny_dnn::float_t *ptr = resized.ptr<tiny_dnn::float_t>(0);
        d = tiny_dnn::vec_t(ptr, ptr + resized.cols * resized.rows );

        train_images.push_back(d);
        train_labels.push_back(labels[index]);


    }

    // declare optimization algorithm
    adagrad optimizer;

    cout << "Training Started" << endl;

    // train (50-epoch, 30-minibatch)
    net.train<mse, adagrad>(optimizer, train_images, train_labels, 30, 50);

    cout << "Training Completed" << endl;



    // save
    net.save("net");

}

谢谢 阿马尔

【问题讨论】:

    标签: machine-learning neural-network ocr tiny-dnn


    【解决方案1】:

    您为什么只尝试预测 10 个类别?如果我没记错的话,NIST 19 有 62 个类,所以你应该创建一个带有 62 个神经元的 softmax 激活的输出层。顺便说一句,tanh 激活方法太老了,我建议你使用 ReLU。

    您也可以尝试做一些数据增强。在继续训练 CNN 时,尝试为输入图像生成随机扰动。

    【讨论】:

    • 只是先尝试​​数字。稍后将尝试所有 62 个课程。我也会尝试 ReLU。但问题在于学习率高。
    【解决方案2】:

    问题在于高学习率。当学习率从默认值 0.01 更改为 0.001 时,准确率从 40% 提高到 90%

    更改的行是

    optimizer.alpha = static_cast<tiny_dnn::float_t>(0.001);
    

    谢谢

    阿马尔

    【讨论】:

      猜你喜欢
      • 2021-01-26
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      • 2018-07-01
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多