使用Caffe基于cifar10进行物体识别
在http://blog.csdn.net/fengbingchun/article/details/72953284中对cifar10进行train,这里通过train得到的model,对图像进行识别。cifar10数据集共包括10类,按照0到9的顺序依次为airplane(飞机)、automobile(轿车)、bird(鸟)、cat(猫)、deer(鹿)、dog(狗)、frog(青蛙)、horse(房子)、ship(船)、truck(卡车)。
在识别前需要对原有的cifar10_quick_train_test.prototxt文件进行调整,调整后的内容如下:
name: "CIFAR10_quick"
layer {name: "data"type: "MemoryData"top: "data"top: "label"memory_data_param {batch_size: 1channels: 3height: 32width: 32}
}
layer {name: "conv1"type: "Convolution"bottom: "data"top: "conv1"param {lr_mult: 1}param {lr_mult: 2}convolution_param {num_output: 32pad: 2kernel_size: 5stride: 1weight_filler {type: "gaussian"std: 0.0001}bias_filler {type: "constant"}}
}
layer {name: "pool1"type: "Pooling"bottom: "conv1"top: "pool1"pooling_param {pool: MAXkernel_size: 3stride: 2}
}
layer {name: "relu1"type: "ReLU"bottom: "pool1"top: "pool1"
}
layer {name: "conv2"type: "Convolution"bottom: "pool1"top: "conv2"param {lr_mult: 1}param {lr_mult: 2}convolution_param {num_output: 32pad: 2kernel_size: 5stride: 1weight_filler {type: "gaussian"std: 0.01}bias_filler {type: "constant"}}
}
layer {name: "relu2"type: "ReLU"bottom: "conv2"top: "conv2"
}
layer {name: "pool2"type: "Pooling"bottom: "conv2"top: "pool2"pooling_param {pool: AVEkernel_size: 3stride: 2}
}
layer {name: "conv3"type: "Convolution"bottom: "pool2"top: "conv3"param {lr_mult: 1}param {lr_mult: 2}convolution_param {num_output: 64pad: 2kernel_size: 5stride: 1weight_filler {type: "gaussian"std: 0.01}bias_filler {type: "constant"}}
}
layer {name: "relu3"type: "ReLU"bottom: "conv3"top: "conv3"
}
layer {name: "pool3"type: "Pooling"bottom: "conv3"top: "pool3"pooling_param {pool: AVEkernel_size: 3stride: 2}
}
layer {name: "ip1"type: "InnerProduct"bottom: "pool3"top: "ip1"param {lr_mult: 1}param {lr_mult: 2}inner_product_param {num_output: 64weight_filler {type: "gaussian"std: 0.1}bias_filler {type: "constant"}}
}
layer {name: "ip2"type: "InnerProduct"bottom: "ip1"top: "ip2"param {lr_mult: 1}param {lr_mult: 2}inner_product_param {num_output: 10weight_filler {type: "gaussian"std: 0.1}bias_filler {type: "constant"}}
}
layer {name: "prob"type: "Softmax"bottom: "ip2"top: "prob"
}
可视化结果如下图(https://ethereon.github.io/netscope/quickstart.html ):测试代码如下:
#include "funset.hpp"
#include "common.hpp"int cifar10_predict()
{
#ifdef CPU_ONLYcaffe::Caffe::set_mode(caffe::Caffe::CPU);
#elsecaffe::Caffe::set_mode(caffe::Caffe::GPU);
#endifconst std::string param_file{ "E:/GitCode/Caffe_Test/test_data/model/cifar10/cifar10_quick_train_test_.prototxt" };const std::string trained_filename{ "E:/GitCode/Caffe_Test/test_data/model/cifar10/cifar10_quick_iter_4000.caffemodel.h5" };const std::string image_path{ "E:/GitCode/Caffe_Test/test_data/images/object_recognition/" };const std::string mean_file{"E:/GitCode/Caffe_Test/test_data/model/cifar10/mean.binaryproto"};caffe::Net<float> caffe_net(param_file, caffe::TEST);caffe_net.CopyTrainedLayersFromHDF5(trained_filename);const boost::shared_ptr<caffe::Blob<float> > blob_by_name = caffe_net.blob_by_name("data");int image_channel = blob_by_name->channels();int image_height = blob_by_name->height();int image_width = blob_by_name->width();int num_outputs = caffe_net.num_outputs();const std::vector<caffe::Blob<float>*>& output_blobs = caffe_net.output_blobs();int require_blob_index{ -1 };const int digit_category_num{ 10 };for (int i = 0; i < output_blobs.size(); ++i) {if (output_blobs[i]->count() == digit_category_num)require_blob_index = i;}if (require_blob_index == -1) {fprintf(stderr, "ouput blob don't match\n");return -1;}std::vector<int> target{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };std::vector<int> result;// read mean datacaffe::BlobProto image_mean; // storage order: rr..rrgg..ggbb..bbif (!caffe::ReadProtoFromBinaryFile(mean_file, &image_mean)) {fprintf(stderr, "parse mean file fail\n");return -1;}if (image_channel != image_mean.channels() || image_height != image_mean.height() || image_width != image_mean.width() ||image_channel != 3) {fprintf(stderr, "their dimension dismatch\n");return -1;}cv::Mat mat_mean(image_height, image_width, CV_32FC3, const_cast<float*>(image_mean.data().data()));for (auto num : target) {std::string str = std::to_string(num);str += ".jpg";str = image_path + str;cv::Mat mat = cv::imread(str.c_str(), 1);if (!mat.data) {fprintf(stderr, "load image error: %s\n", str.c_str());return -1;}if (image_channel == 1)cv::cvtColor(mat, mat, CV_BGR2GRAY);else if (image_channel == 4)cv::cvtColor(mat, mat, CV_BGR2BGRA);cv::resize(mat, mat, cv::Size(image_width, image_height));mat.convertTo(mat, CV_32FC3);// Note: need to subtract meanstd::vector<cv::Mat> mat_tmp2; //b,g,rcv::split(mat, mat_tmp2);cv::Mat mat_tmp3(image_height, image_width, CV_32FC3);float* p = (float*)mat_tmp3.data;memcpy(p, mat_tmp2[2].data, image_height * image_width * sizeof(float));memcpy(p + image_height * image_width, mat_tmp2[1].data, image_height * image_width * sizeof(float));memcpy(p + image_height * image_width * 2, mat_tmp2[0].data, image_height * image_width * sizeof(float));cv::subtract(mat_tmp3, mat_mean, mat_tmp3);boost::shared_ptr<caffe::MemoryDataLayer<float> > memory_data_layer =boost::static_pointer_cast<caffe::MemoryDataLayer<float>>(caffe_net.layer_by_name("data"));float dummy_label[1] {0};memory_data_layer->Reset((float*)(mat_tmp3.data), dummy_label, 1); // rr..rrgg..ggbb..bbfloat loss{ 0.0 };const std::vector<caffe::Blob<float>*>& results = caffe_net.ForwardPrefilled(&loss); // Net forwardconst float* output = results[require_blob_index]->cpu_data();float tmp{ -1 };int pos{ -1 };fprintf(stderr, "actual digit is: %d\n", target[num]);for (int j = 0; j < 10; j++) {printf("Probability to be Number %d is: %.3f\n", j, output[j]);if (tmp < output[j]) {pos = j;tmp = output[j];}}result.push_back(pos);}for (auto i = 0; i < 10; i++)fprintf(stderr, "actual digit is: %d, result digit is: %d\n", target[i], result[i]);fprintf(stderr, "predict finish\n");return 0;
}
测试结果如下:其中鹿和青蛙识别错误。
GitHub:https://github.com/fengbingchun/Caffe_Test
相关文章:

SoJpt Boot 2.3-3.8 发布,Spring Boot 使用 Jfinal 特性极速开发
SoJpt Boot 2.3-3.8 发布了。SoJpt Boot 基于 JFinal 与 Spring Boot制作, 实现了 Spring Boot 与 Jfinal 的混合双打,使 Spring Boot 下的开发者能够体验 Jfinal 的极速开发特性。新版更新内容如下: SoJpt-Boot-2.3-3.8 changelog 1、加入事务注解,Tx(value"c…

PL/SQL程序设计 第七章 包的创建和应用
7.1 引言包是一组相关过程、函数、变量、常量和游标等PL/SQL程序设计元素的组合,它具有面向对象程序设计语言的特点,是对这些PL/SQL 程序设计元素的封装。包类似于C和JAVA语言中的类,其中变量相当于类中的成员变量,过程和函数相当…

C++11中头文件chrono的使用
在C11中,<chrono>是标准模板库中与时间有关的头文件。该头文件中所有函数与类模板均定义在std::chrono命名空间中。 std::chrono是在C11中引入的,是一个模板库,用来处理时间和日期的Time library。要使用chrono库,需要incl…

为什么平头哥做芯片如此迅猛?
作者 | 胡巍巍 发自杭州云栖大会责编 | 唐小引来源 | CSDN(ID:CSDNnews)2018年10月31日,阿里旗下的平头哥半导体有限公司成立。如今,平头哥成立不到一年,就已成绩斐然。2019年9月25日,阿里巴巴旗…

Vue 组件库 heyui@1.18.0 发布,新增地址选择、图片预览组件
开发四年只会写业务代码,分布式高并发都不会还做程序员? 新增 CategoryPicker 新增组件 CategoryPicker,地址级联组件的最佳方案。 <CategoryPicker :option"option" v-model"value"/> 相关文档 ImagePreview 新…

HTML5 Dashboard – 那些让你激动的 Web 技术
HTML5 Dashboard 是一个 Mozilla 推出的项目,里面展示了最前沿的 HTML5,CSS3,JavaScript 技术。每一项技术都有简洁,在线演示以及详细的文档链接。这些技术将成为未来一段时间 Web 开发的顶尖技术,如果不想落伍的话就赶…

计算机解决问题没有奇技淫巧,但动态规划还是有点套路
作者 | labuladong来源 | labuladong(ID:labuladong) 【导读】动态规划算法似乎是一种很高深莫测的算法,你会在一些面试或算法书籍的高级技巧部分看到相关内容,什么状态转移方程,重叠子问题,最优子结构等高…

idea下,Jetty采用main方法启动web项目
为什么80%的码农都做不了架构师?>>> 对于maven多模块的spring web项目,本地开发时,启动的方式一般有如下几种: 使用容器(tomcat/jetty/resin等),该方式需要ide支持,而社…
概率论中均值、方差、标准差介绍及C++/OpenCV/Eigen的三种实现
概率论是用于表示不确定性声明(statement)的数学框架。它不仅提供了量化不确定性的方法,也提供了用于导出新的不确定性声明的公理。在人工智能领域,概率论主要有两种用途。首先,概率法则告诉我们AI系统如何推理,据此我们设计一些算…

[转]CentOS 5.5下FTP安装及配置
一、FTP的安装 1、检测是否安装了FTP : [rootlocalhost ~]# rpm -q vsftpd vsftpd-2.0.5-16.el5_5.1 否则显示:[rootlocalhost ~]# package vsftpd is not installed 查看ftp运行状态 service vsftpd status 2、如果没安装FTP,运行yum install vsftpd命令进行安装 如…

C++11中头文件thread的使用
C11中加入了<thread>头文件,此头文件主要声明了std::thread线程类。C11的标准类std::thread对线程进行了封装。std::thread代表了一个线程对象。应用C11中的std::thread便于多线程程序的移值。 <thread>是C标准程序库中的一个头文件,定义了…

python3 urllib 类
urllib模块中的方法 1.urllib.urlopen(url[,data[,proxies]]) 打开一个url的方法,返回一个文件对象,然后可以进行类似文件对象的操作。本例试着打开google >>> import urllib >>> f urllib.urlopen(http://www.google.com.hk/) >&…
阿里飞天大数据飞天AI平台“双生”系统正式发布,9大全新数据产品集中亮相
作者 | 夕颜 责编 | 唐小引 出品 | AI科技大本营(ID:rgznai100) 如今,大数据和 AI 已经成为两个分不开的词汇,没有大数据,AI 就失去了根基;没有 AI,数据不会呈现爆发式的增长。如何将 AI 与大…

关于JavaScript的闭包(closure)
(转载自阮一峰博客) 闭包(closure)是Javascript语言的一个难点,也是它的特色,更是函数式编程的重要思想之一,很多高级应用都要依靠闭包实现。 下面就是我的学习笔记,对于Javascript初…

Vagrant控制管理器——“Hobo”
Hobo是控制Vagrant盒子和在Mac上编辑Vagrantfiles的最佳和最简单的方法。您可以快速启动,停止和重新加载您的Vagrant机器。您可以从头开始轻松创建新的Vagrantfile。点击进入,尽享Hobo for Mac全部功能! Hobo做什么? 启动…

微众银行AI团队开源联邦学习框架,并发布《联邦学习白皮书1.0》
(图片由AI科技大本营付费下载自视觉中国)编辑 | Jane来源 | 《联邦学习白皮书1.0》出品 | AI科技大本营(ID:rgznai100)【导语】2019年,联邦学习成为业界技术研究与应用的焦点。近日,微众银行 AI 项目组编制…

C++11中头文件atomic的使用
原子库为细粒度的原子操作提供组件,允许无锁并发编程。涉及同一对象的每个原子操作,相对于任何其他原子操作是不可分的。原子对象不具有数据竞争(data race)。原子类型对象的主要特点就是从不同线程访问不会导致数据竞争。因此从不同线程访问某个原子对象…

Oracle回收站
回收站是删除对象使用的存储空间。可以使用实例参数recyclebin禁用回收站,默认是on,可以为某个会话或系统设置为off或on。所有模式都有一个回收站。 当表空间不足时可以自动重用回收站对象占用的表空间(此后不可能恢复对象)&#…
协方差矩阵介绍及C++/OpenCV/Eigen的三种实现
函数f(x)关于某分布P(x)的期望(expectation)或者期望值(expected value)是指,当x由P产生,f作用于x时,f(x)的平均值。对于离散型随机变量,这可以通过求和得到:对于连续型随机变量可以通过求积分得到:当概率分…

10分钟搭建你的第一个图像识别模型 | 附完整代码
(图片由AI科技大本营付费下载自视觉中国)作者 | Pulkit Sharma译者 | 王威力来源 | 数据派THU(ID:DatapiTHU)【导读】本文介绍了图像识别的深度学习模型的建立过程,通过陈述实际比赛的问题、介绍模型框架和…

Rancher 2.2.2 发布,优化 Kubernetes 集群运维
开发四年只会写业务代码,分布式高并发都不会还做程序员? >>> Rancher 2.2.2 发布了。Rancher 是一个开源的企业级 Kubernetes 平台,可以管理所有云上、所有发行版、所有 Kubernetes集群,解决了生产环境中企业用户可能面…

EXP/EXPDP, IMP/IMPDP应用
2019独角兽企业重金招聘Python工程师标准>>> EXP/EXPDP, IMP/IMPDP应用 exp name/pwddbname filefilename.dmp tablestablename rowsy indexesn triggersn grantsn $ sqlplus username/passwordhostname:port/SERVICENAME OR $ sqlplus username Enter password:…

微软语音AI技术与微软听听文档小程序实践 | AI ProCon 2019
演讲嘉宾 | 赵晟、张鹏整理 | 伍杏玲来源 | CSDN(ID:CSDNnews)【导语】9 月 7 日,在CSDN主办的「AI ProCon 2019」上,微软(亚洲)互联网工程院人工智能语音团队首席研发总监赵晟、微软࿰…

C++11中std::condition_variable的使用
<condition_variable>是C标准程序库中的一个头文件,定义了C11标准中的一些用于并发编程时表示条件变量的类与方法等。条件变量是并发程序设计中的一种控制结构。多个线程访问一个共享资源(或称临界区)时,不但需要用互斥锁实现独享访问以避免并发错…

docker基础文档(链接,下载,安装)
一、docker相关链接1.docker中国区官网(包含部分中文文档,下载安装包,镜像加速器):https://www.docker-cn.com/2.docker官方镜像仓库:https://cloud.docker.com/3.docker下载:https://www.docker-cn.com/community-edi…

一个JS对话框,可以显示其它页面,
还不能自适应大小 garyBox.js // JavaScript Document// gary 2014-3-27// 加了 px 在google浏览器没加这个发现设置width 和height没有用 //gary 2014-3-27 //实在不会用那些JS框架,自己弄个,我只是想要个可以加载其它页面的对话框而以,这里用了别人的…

只需4秒,这个算法就能鉴别你的LV是真是假
(图片付费下载自视觉中国)导语:假冒奢侈品制造这个屡禁不止的灰色产业,每年给正品商家和消费者造成上千亿的损失,对企业和消费者造成伤害。作为全球奢侈品巨头,LVMH 对假冒奢侈品的打击十分重视。LVMH 其旗…
概率论中伯努利分布(bernoulli distribution)介绍及C++11中std::bernoulli_distribution的使用
Bernoulli分布(Bernoulli distribution):是单个二值随机变量的分布。它由单个参数∈[0,1],给出了随机变量等于1的概率。它具有如下的一些性质:P(x1) P(x0)1-P(xx) x(1-)1-xEx[x] Varx(x) (1-)伯努力分布(Bernoulli distribution,又…

关于View测量中的onMeasure函数
在自定义View中我们通常会重写onMeasure,下面来说说这个onMeasure有什么作用 onMeasure主要用于对于View绘制时进行测量 Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);…

zabbix二次开发之从mysql取值在运维平台js图表展现
前沿:集群控制平台已经要慢慢的灰度上线了,出问题的时候,才找点bug,时间有点空闲。正好看了下zabbix的数据库,产生了自己想做一套能更好的展现zabbix的页面。更多内容请到我的个人的博客站点,blog.xiaorui.…