• MNIST字符识别(C++)


    构建网络

    采用官方示例的的lenet网络

    训练

    相关文件都已编译好,下载后执行命令即可

    .\caffe-bin.exe  train --solver .\lenet_solver.prototxt

    识别

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. using namespace caffe;
    8. using std::string;
    9. int main(int argc, char** argv)
    10. {
    11. fLI::FLAGS_minloglevel = 2;
    12. string model_file = "lenet_deploy.prototxt";
    13. string trained_file = "lenet_iter_10000.caffemodel"; //for visualization
    14. string img_file = "Fmnist_images/test/test_0_7.jpg";
    15. Caffe::set_mode(Caffe::CPU);
    16. shared_ptrfloat> > net;
    17. /* Load the network. */
    18. net.reset(new Net<float>(model_file, caffe::TEST));
    19. net->CopyTrainedLayersFrom(trained_file);
    20. CHECK_EQ(net->num_inputs(), 1) << "Network should have exactly one input.";
    21. CHECK_EQ(net->num_outputs(), 1) << "Network should have exactly one output.";
    22. //net->set_debug_info(true);
    23. Blob<float>* input_layer = net->input_blobs()[0];
    24. int num_channels = input_layer->channels();
    25. int input_height = input_layer->height();
    26. int input_width = input_layer->width();
    27. CHECK(num_channels == 3 || num_channels == 1) << "Input layer should have 1 or 3 channels.";
    28. input_layer->Reshape(1, num_channels, input_height, input_width);
    29. /* Forward dimension change to all layers. */
    30. net->Reshape();
    31. std::vector input_channels;
    32. float* input_data = input_layer->mutable_cpu_data();
    33. for (int i = 0; i < input_layer->channels(); ++i) {
    34. cv::Mat channel(input_height, input_width, CV_32FC1, input_data);
    35. input_channels.push_back(channel);
    36. input_data += input_height * input_width;
    37. }
    38. // Input Data
    39. cv::Mat img = cv::imread(img_file, 1);
    40. CHECK(!img.empty()) << "Unable to decode image " << img_file;
    41. cv::Mat sample_resized;
    42. cv::resize(img, sample_resized, cv::Size(input_width, input_height));
    43. cv::Mat sample_float;
    44. if (num_channels == 3)
    45. sample_resized.convertTo(sample_float, CV_32FC3);
    46. else
    47. sample_resized.convertTo(sample_float, CV_32FC1);
    48. sample_float *= 0.00390625;
    49. /* This operation will write the separate BGR planes directly to the
    50. * input layer of the network because it is wrapped by the cv::Mat
    51. * objects in input_channels. */
    52. cv::split(sample_float, input_channels);
    53. CHECK(reinterpret_cast<float*>(input_channels.at(0).data) == net->input_blobs()[0]->cpu_data())
    54. << "Input channels are not wrapping the input layer of the network.";
    55. // predict
    56. net->Forward();
    57. /* Copy the output layer to a std::vector */
    58. Blob<float>* output_layer = net->output_blobs()[0];
    59. std::cout << "output_blob(n,c,h,w) = " << output_layer->num() << ", " << output_layer->channels() << ", "
    60. << output_layer->height() << ", " << output_layer->width() << std::endl;
    61. for (int n = 0; nnum(); n++) {
    62. for (int c = 0; c < output_layer->channels(); c++) {
    63. for (int h = 0; hheight(); h++) {
    64. for (int w = 0; wwidth(); w++) {
    65. std::cout << "output_blob(n,c,h,w) = " << n << ", " << c << ", "
    66. << h << ", " << w<<":"<< output_layer->data_at(n, c, h, w) << std::endl;
    67. }
    68. }
    69. }
    70. }
    71. }

    下载地址

  • 相关阅读:
    林浩然与杨凌芸的Scala奇遇记:从Java王国到函数式编程乐园
    从语言层面了解线程(std::thread)使用的里里外外
    【Quarto】Markdown导出PPT
    Linux 文本操作指令
    python如何设置单线程爬虫
    ACA云助理计算知识笔记
    springboot如何接入netty,实现在线统计人数?
    Ant Design表单之labelCol 和wrapperCol的实际开发笔记
    java毕业设计颜如玉图书销售网站的设计与实现Mybatis+系统+数据库+调试部署
    Aptos 生态项目秀第一期路演精彩回顾
  • 原文地址:https://blog.csdn.net/u012594175/article/details/133713967