在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 ):

从网上找了10幅较标准的图像,如下:

测试代码如下:

#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

使用Caffe基于cifar10进行物体识别相关推荐

  1. 基于图像处理的物体识别与分类系统--2021研究生电子设计大赛总结

    基于图像处理的物体识别与分类系统 -2021研究生电子设计大赛总结 1. 赛题  我们组选的是TI企业命题第三题:基于图像处理的物体识别与分类系统. 摄像机采集图像,通过图像处理算法实时检测识别出目标 ...

  2. 基于神经网络的物体识别方法2020[计算机视觉]

    文章主要介绍基于神经网络的object detection方法,包括single stage detectors和two stage detectors.目标在于了解这个领域内的发展情况,并没有对某个 ...

  3. Keras CIFAR-10彩色图像物体识别 卷积神经网络

    参考书籍<Tensorflow+Keras 深度学习人工智能实践应用>林大贵著 第九章,第十章. 这是一本很通俗易懂的入门实践书,所有代码,事无巨细地进行了解释 CIFAR-10数据集是6 ...

  4. 基于ROS机器人的3D物体识别与三维重建(一) 介绍篇

    基于ROS机器人的3D物体识别与三维重建(一) 介绍篇 由来:清理电脑硬盘,发现了当时做毕设的一些资料,所以打算整理一下资料和代码写成专栏,记录下当时的暗金岁月,尽管现在实验室的做的项目已经不是这个方 ...

  5. 基于ROS机器人的3D物体识别与三维重建(三)基于ROS的3D物体识别

    Kinect2相机标定与点云数据获取 1.介绍 2 基于Gazebo搭建物体识别仿真环境 2.1 Gazebo简介 2.2 创建仿真环境 3 三维物体识别 3.1 基于模板匹配的物体识别流程 3.2 ...

  6. 深度学习-服务端训练+android客户端物体识别实战(caffe入门教程+mobilenet+ncnn+android)

    文章目录 背景 物体识别简介 自动驾驶 淘宝京东使用物体识别技术 公司业务需求 深度学习简介 深度学习的位置 深度学习概念 深度学习优势 深度学习基础知识 感知机 激活函数 多层感知机 卷积神经网络 ...

  7. 深度学习-服务端训练+android客户端物体识别实战(caffe+mobilenet+ncnn+android)

    文章目录 背景 物体识别简介 自动驾驶 淘宝京东使用物体识别技术 公司业务需求 深度学习简介 深度学习的位置 深度学习概念 深度学习优势 深度学习基础知识 感知机 激活函数 多层感知机 卷积神经网络 ...

  8. 基于结构光测量技术和3D物体识别技术开发的机器人3D视觉引导系统

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达本文转自|新机器视觉 基于结构光测量技术和3D物体识别技术开发的机器 ...

  9. DeepID:Python基于Caffe的DeepID2实现人脸识别的简介、实现之详细攻略

    DeepID:Python基于Caffe的DeepID2实现人脸识别的简介.实现之详细攻略 目录 基于Caffe的DeepID2实现人脸识别 DeepID2实现人脸识别的实现 基于Caffe的Deep ...

最新文章

  1. GridView正反双向排序
  2. android mp3 lrc歌词文件utf-8歌词显示为乱码,Android读取本地json文件的方法(解决显示乱码问题)...
  3. ORACLE not available如何解决
  4. Javascript设计模式之中介者模式
  5. linux的 vi编辑器在哪,Linux Vi编辑器的使用
  6. 深扒支点的梦起与破灭-千氪
  7. WordPress登陆插件Erphplogin Pro QQ登陆/微博/微信登录/弹窗登录
  8. 厦门大学c语言模拟考试题,厦门大学《C语言》模拟试卷(10级).doc
  9. php中html写法,细致说明注解三种PHP嵌套HTML的写法_后端开发
  10. Django2.0——实现简易登陆、注册
  11. clodop 打印插件打印不显示问题
  12. 购物直播系统搭建 新型电商开发方案
  13. Excel格式的SNP数据怎么变为plink格式
  14. OBD(On-Board-Diagnose)
  15. 考研英语(二)——简单句
  16. 移动Web开发基础-Viewport
  17. Python3 使用科大讯飞 API 接口实现音频文件转写
  18. Xilinx ISERDESE2应用笔记及仿真实操
  19. 二叉树的前、中、后序遍历
  20. 自己写的实用VBA代码合集√

热门文章

  1. HDU - 3078 Network 倍增LCA
  2. 【神经网络】(11) 轻量化网络MobileNetV1代码复现、解析,附Tensorflow完整代码
  3. 强化学习(九)- 策略梯度方法 - 梯度上升,黑箱优化,REINFORCE算法及CartPole实例
  4. 在Mac上通过VMware Fushion 15.1配置静态IP虚拟机实录
  5. 在Ubuntu 14.04 64bit上安装Markdown和绘图软件Haroopad
  6. 在Linux上利用python获取本机ip
  7. javascript之namespace模式
  8. MyRocks: 为facebool 的社交图谱服务的LSM-tree存储引擎
  9. 使用内存盘 格式化文件系统以及部署ceph-osd
  10. vim编辑文章后不能修改