• 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. }

    下载地址

  • 相关阅读:
    矩阵分析与计算学习记录-矩阵分解
    【前端】Vue+Element UI案例:通用后台管理系统-登陆页面功能:登录权限跳转、路由守卫、退出
    Scala基本语法
    【云原生|探索 Kubernetes 系列 5】简化 Kubernetes 的部署,深入解析其工作流程
    Java Reflect 反射
    django框架ModelForm组件用法详解
    QT+MSVC+Opencv环境配置
    《C语言图形界面-系统开发》专栏介绍 & 专栏目录
    4.7 minio下载文件代码优化
    接口自动化测试框架(pytest+allure+aiohttp+ 用例自动生成)
  • 原文地址:https://blog.csdn.net/u012594175/article/details/133713967