在TensorRT 2.1.2中提供了MNIST的model,这里拿来用Caffe的代码调用实现,原始的mnist_mean.binaryproto文件调整为了纯二进制文件mnist_tensorrt_mean.binary,测试结果与使用TensorRT调用(http://blog.csdn.net/fengbingchun/article/details/78552908)一致:

测试代码如下:

#include "funset.hpp"
#include <memory>
#include <fstream>
#include <tuple>
#include "common.hpp"int mnist_tensorrt_predict()
{
#ifdef CPU_ONLYcaffe::Caffe::set_mode(caffe::Caffe::CPU);
#elsecaffe::Caffe::set_mode(caffe::Caffe::GPU);
#endifconst std::string deploy_file{ "E:/GitCode/Caffe_Test/test_data/model/mnist/mnist_tensorrt.prototxt" };const std::string model_filename{ "E:/GitCode/Caffe_Test/test_data/model/mnist/mnist_tensorrt.caffemodel" };const std::string mean_file{ "E:/GitCode/Caffe_Test/test_data/model/mnist/mnist_tensorrt_mean.binary" };const std::string image_path{ "E:/GitCode/Caffe_Test/test_data/images/handwritten_digits/" };caffe::Net<float> caffe_net(deploy_file, caffe::TEST);caffe_net.CopyTrainedLayersFrom(model_filename);// print net infofprintf(stdout, "input blob num: %d, output blob num: %d\n", caffe_net.num_inputs(), caffe_net.num_outputs());const boost::shared_ptr<caffe::Blob<float> > blob_by_name = caffe_net.blob_by_name("data");int image_num = blob_by_name->num();int image_channel = blob_by_name->channels();int image_height = blob_by_name->height();int image_width = blob_by_name->width();fprintf(stdout, "inpub blob shape(num, channels, height, width): %d, %d, %d, %d\n",image_num, image_channel, image_height, image_width);fprintf(stdout, "layer names: ");for (int i = 0; i < caffe_net.layer_names().size(); ++i) {fprintf(stdout, "  %s  ", caffe_net.layer_names()[i].c_str());}fprintf(stdout, "\nblob names: ");for (int i = 0; i < caffe_net.blob_names().size(); ++i) {fprintf(stdout, "  %s  ", caffe_net.blob_names()[i].c_str());}fprintf(stdout, "\nlayer types: ");for (int i = 0; i < caffe_net.layers().size(); ++i) {fprintf(stdout, "  %s  ", caffe_net.layers()[i]->type());}const std::vector<caffe::Blob<float>*> output_blobs = caffe_net.output_blobs();fprintf(stdout, "\noutput blobs num: %d, blob(num, channel, heihgt, width): %d, %d, %d, %d\n",output_blobs.size(), output_blobs[0]->num(), output_blobs[0]->channels(), output_blobs[0]->height(), output_blobs[0]->width());const int image_size{ image_num * image_channel * image_height * image_width };std::unique_ptr<float[]> mean_values(new float[image_size]);std::ifstream in(mean_file.c_str(), std::ios::in | std::ios::binary);if (!in.is_open()) {fprintf(stderr, "read mean file fail: %s\n", mean_file.c_str());return -1;}in.read((char*)mean_values.get(), image_size * sizeof(float));in.close();const std::vector<int> target{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };typedef std::tuple<int, float> result;std::vector<result> results;for (const auto& num : target) {std::string str = std::to_string(num);str += ".png";str = image_path + str;cv::Mat mat = cv::imread(str.c_str(), 0);if (!mat.data) {fprintf(stderr, "load image error: %s\n", str.c_str());return -1;}cv::resize(mat, mat, cv::Size(image_width, image_height));mat.convertTo(mat, CV_32FC1);float* p = (float*)mat.data;for (int i = 0; i < image_size; ++i) {p[i] -= mean_values.get()[i];}const std::vector<caffe::Blob<float>*>& blob_input = caffe_net.input_blobs();blob_input[0]->set_cpu_data((float*)mat.data);const std::vector<caffe::Blob<float>*>& output_blob_ = caffe_net.Forward(nullptr);const float* output = output_blob_[0]->cpu_data();float tmp{ -1.f };int pos{ -1 };for (int j = 0; j < output_blobs[0]->count(); j++) {if (tmp < output[j]) {pos = j;tmp = output[j];}}result ret = std::make_tuple(pos, tmp);results.push_back(ret);}for (auto i = 0; i < target.size(); i++)fprintf(stdout, "actual digit is: %d, result digit is: %d, probability: %f\n",target[i], std::get<0>(results[i]), std::get<1>(results[i]));fprintf(stdout, "predict finish\n");return 0;
}

测试图像如下:

执行结果如下:

GitHub: https://github.com/fengbingchun/Caffe_Test

在Caffe中调用TensorRT提供的MNIST model相关推荐

  1. Java相对路径调用dll文件,VS项目中调用他人提供的.lib和.dll文件的用法(绝对路径和相对路径)...

     开发平台:vs2013 他人提供的动态库文件,包含 .lib 文件和 .dll 文件 在VS的工程中常常要设置头文件的包含路径和库文件的包含路径,当然你可以使用绝对路径,但是如果你这样设置了你只 ...

  2. 在caffe中调用python层的问题和解决方案

    之前很早以前在caffe中用过python写的loss layer,很久不用后在新的服务器上使用就各种"No module named WeightedEuclideanLossLayer2 ...

  3. Caffe中对MNIST执行train操作执行流程解析

    之前在 http://blog.csdn.net/fengbingchun/article/details/49849225 中简单介绍过使用Caffe train MNIST的文章,当时只是仿照ca ...

  4. 【Caffe学习01】在Caffe中trian MNIST

    在上次搭建好Caffe环境的基础上我们进行第一次实验,将Caffe自带的一个Mnist example跑一跑,对其在处理图像方面的能力有个初步了解. 如果还没有搭建好环境的朋友可以看看我的上一篇文章: ...

  5. caffe中mnist数据集的运行

    1.首先进入caffe的安装目录 CAFFE_ROOT='/home/lxc/caffe/' 2.运行脚本文件,数据集 cd CAFFE_ROOT ./data/mnist/gte_mnist.sh ...

  6. caffe中的layer

    layer是神经网络搭建的脚手架,理解了layer,才能盖好神经网络这座摩天大楼. 下图是一张关于layer的思维导图,在功力到达一定程度的时候才可练此功,到时一定会有不一样的收获. 1. Outli ...

  7. caffe中各层的作用

    caffe中各层的作用: 关于caffe中的solver: cafffe中的sover的方法都有: Stochastic Gradient Descent (type: "SGD" ...

  8. 多线程 python layer_在Caffe中加Python Layer的方法

    Author: Zongwei Zhou | 周纵苇 Weibo: @MrGiovanni Email: zongweiz@asu.edu Acknowledgement: Md Rahman Sid ...

  9. 在caffe中使用hdf5的数据

    caffe默认使用的数据格式为lmdb文件格式,它提供了把图片转为lmdb文件格式的小程序,但是呢,我的数据为一维的数据,我也要分类啊,那我怎么办?肯定有办法可以转为lmdb文件格式的,我也看了一些源 ...

最新文章

  1. 我使用过的Linux命令之trap - 在脚本中处理信号
  2. Asp.net生成Excel文件并下载(解决使用迅雷下载页面而不是文件的问题)
  3. 新版appium 支持name定位的方法(没试 记录再此)
  4. like左匹配索引失效_Mysql索引失效的情况
  5. Nacos Spring Cloud 快速开始
  6. python 字典排序成绩_集体备课第四章 python基础与顺序结构
  7. Mobx | 强大的状态管理工具 | 可以用Mobx来替代掉redux
  8. HTML5手机开发——滚动和惯性缓动
  9. 萌新学习笔记之线性表
  10. 拓端tecdat|【视频】Lasso回归、岭回归等正则化回归数学原理及R语言实例
  11. js 调用 php,利用js调用后台php进行数据处理原码
  12. SOTA级发丝抠图模型PP-Matting开源,支持多场景精细化分割
  13. 评估指标【簇内误差平方和】-轮廓系数
  14. MySQL中通过经纬度查询地址
  15. 利用vim编辑器创建和编辑正文文件
  16. 支付宝php sdk如何使用,支付宝SDK怎么用啊?
  17. 连行为艺术家都开始直播带货了
  18. JAVA版我的世界如何开第三方服务器
  19. Appium学习:雷电模拟器的使用
  20. 关于AD18中Board information的位置更改

热门文章

  1. Java8 新的 try-with-resources 语句,自动资源释放
  2. 回归分析中的“回归”
  3. python基础知识整理 第三节 :函数
  4. java 连nosql_浅谈 Java 中 MongoDB NoSQL数据库使用指南
  5. 【fiveKeyPress】2秒内五次点击键盘任意键(或组合键)触发自定义事件(以Pause/Break键为例)
  6. 复制已有的Tomcat作为新的Tomcat,只需修改三个配置文件,五步操作,保证能正常运行!
  7. 在CentOS 6.3 64bit上安装ActiveMQ 5.15.9实录
  8. Python3和Raspberry Pi最全面最直接的课程
  9. 嵌入式系统开发过程中遇到的——volatile
  10. ceph-deploy rpm包的制作