前面已经介绍了使用使用mnist数据集进行训练lenet,并使用opencv加在caffemodel进行预测。更进一步也是最终的目的,还是要学会使用自己的数据集训练caffemodel并进行预测。这里先以训练lenet为例进行说明。

1、数据格式的转换(images to lmdb)

通过前面几篇介绍,我们已经知道,可直接用于caffe训练的数据格式有lmdb和leveldb两种,但lmdb的效率更高,因此这里需要先将原始图像数据转换为lmdb。

1.1 在工作目录细新建一个文件夹,这里命名为images_to_lmdb,图像格式的转换将在这个文件夹下进行。将分好类的训练数据(这里包括前景(车辆)和背景数据,二者合适的比例为1:3,一部分用于训练,一部分测试)也拷贝到这个文件夹下,分别创建train.txt和test.txt两个文本文件,指定源图像的路径(如果不是剪切好的图像,需要指定目标所在的区域)和类别(这里前景设为0, 背景设为1),如下图所示。

1.2 在当前目录下新建两个文本文件,分别重命名为images_to_train_lmdb.bat 和 images_to_test_lmdb.bat.分别输入以下内容(注意路径输入正确):

D:\Libraries\caffe\msvc2013_64\bin\convert_imageset.exe --shuffle --gray --resize_width=28 --resize_height=28  images\ train.txt train_lmdb -backend=lmdb
pause 
D:\Libraries\caffe\msvc2013_64\bin\convert_imageset.exe --shuffle --gray --resize_width=28 --resize_height=28  images\ test.txt test_lmdb -backend=lmdb
pause 

1.3 双击images_to_train_lmdb.bat执行,可以得到训练数据,保存在文件夹train_lmdb中。执行过程如下:

同样,双击images_to_test_lmdb.bat可得到训练数据。

2、计算均值

在当前文件夹下新建文本文件,并命名为computer_image_mean.bat,并输入如下内容:

D:\Libraries\caffe\msvc2013_64\bin\compute_image_mean.exe train_lmdb mean.binaryproto --backend=lmdb
pause

保存,双击执行,得到文件mean.binaryproto。

至此,训练数据准备完毕,下面进行训练。

3、设置训练参数

3.1 在工程目录下新建文件夹,并命名为glnet_train_test,训练工作将在这个文件夹下进行,将上两步得到的训练数据,即两个文件夹train_lmdb和test_lmdb,以文件mean.binaryproto拷贝到当前文件夹下。

3.2 新建两个文本文件,分别命名为lenet_solver.prototxt和lenet_train_test.prototxt。

向lenet_solver.prototxt输入如下内容,并保存。

# The train/test net protocol buffer definition
net: "lenet_train_test.prototxt"
# test_iter specifies how many forward passes the test should carry out.
# In the case of MNIST, we have test batch size 100 and 100 test iterations,
# covering the full 10,000 testing images.
test_iter: 100
# Carry out testing every 500 training iterations.
test_interval: 500
# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.01
momentum: 0.9
weight_decay: 0.0005
# The learning rate policy
lr_policy: "inv"
gamma: 0.0001
power: 0.75
# Display every 100 iterations
display: 100
# The maximum number of iterations
max_iter: 10000
# snapshot intermediate results
snapshot: 5000
snapshot_prefix: "lenet"
# solver mode: CPU or GPU
solver_mode: GPU

向lenet_train_test.prototxt输入如下内容,并保存

name: "LeNet"
layer {name: "myNet"type: "Data"top: "data"top: "label"include {phase: TRAIN}transform_param {#mean_file:"mean.binaryproto"scale: 0.00390625}data_param {source: "train_lmdb"batch_size: 64backend: LMDB}
}
layer {name: "myNet"type: "Data"top: "data"top: "label"include {phase: TEST}transform_param {scale: 0.00390625#mean_file:"mean.binaryproto"}data_param {source: "test_lmdb"batch_size: 100backend: LMDB}
}
layer {name: "conv1"type: "Convolution"bottom: "data"top: "conv1"param {lr_mult: 1}param {lr_mult: 2}convolution_param {num_output: 20kernel_size: 5stride: 1weight_filler {type: "xavier"}bias_filler {type: "constant"}}
}
layer {name: "pool1"type: "Pooling"bottom: "conv1"top: "pool1"pooling_param {pool: MAXkernel_size: 2stride: 2}
}
layer {name: "conv2"type: "Convolution"bottom: "pool1"top: "conv2"param {lr_mult: 1}param {lr_mult: 2}convolution_param {num_output: 50kernel_size: 5stride: 1weight_filler {type: "xavier"}bias_filler {type: "constant"}}
}
layer {name: "pool2"type: "Pooling"bottom: "conv2"top: "pool2"pooling_param {pool: MAXkernel_size: 2stride: 2}
}
layer {name: "ip1"type: "InnerProduct"bottom: "pool2"top: "ip1"param {lr_mult: 1}param {lr_mult: 2}inner_product_param {num_output: 500weight_filler {type: "xavier"}bias_filler {type: "constant"}}
}
layer {name: "relu1"type: "ReLU"bottom: "ip1"top: "ip1"
}
layer {name: "ip2"type: "InnerProduct"bottom: "ip1"top: "ip2"param {lr_mult: 1}param {lr_mult: 2}inner_product_param {num_output: 2     ####根据训练的目标种类进行设定,这里设置为2weight_filler {type: "xavier"}bias_filler {type: "constant"}}
}
layer {name: "accuracy"type: "Accuracy"bottom: "ip2"bottom: "label"top: "accuracy"include {phase: TEST}
}
layer {name: "loss"type: "SoftmaxWithLoss"bottom: "ip2"bottom: "label"top: "loss"
}

参数设置完毕,下面进行训练和测试。

4、训练和测试

4.1 在当前文件夹下新建文本文件,命名为train.bat,输入如下内容,并保存。

D:\Libraries\caffe\msvc2013_64\bin\caffe.exe train --solver=lenet_solver.prototxt
pause

双击执行,过程如下:

最终得到如下训练模型:

4.2 下面进行测试,新建文本文件并命名为test.bat,并输入如下内容并保存。

D:\Libraries\caffe\msvc2013_64\bin\caffe.exe test --model lenet_train_test.prototxt -weights=lenet_iter_10000.caffemodel
pause

双击执行,测试结果如下:

5、使用opencv加在caffemodel

5.1上面已经得到了caffemodel,在使用opencv进行预测前,需要先新建一个文本文件并命名为lenet.prototxt,输入如下内容,并保存。

name: "LeNet"
input: "data"
input_dim: 64 #训练的bacth_size
input_dim: 1  #通道数
input_dim: 28 #长
input_dim: 28 #宽
layer {name: "conv1"type: "Convolution"bottom: "data"top: "conv1"param {lr_mult: 1}param {lr_mult: 2}convolution_param {num_output: 20kernel_size: 5stride: 1weight_filler {type: "xavier"}bias_filler {type: "constant"}}
}
layer {name: "pool1"type: "Pooling"bottom: "conv1"top: "pool1"pooling_param {pool: MAXkernel_size: 2stride: 2}
}
layer {name: "conv2"type: "Convolution"bottom: "pool1"top: "conv2"param {lr_mult: 1}param {lr_mult: 2}convolution_param {num_output: 50kernel_size: 5stride: 1weight_filler {type: "xavier"}bias_filler {type: "constant"}}
}
layer {name: "pool2"type: "Pooling"bottom: "conv2"top: "pool2"pooling_param {pool: MAXkernel_size: 2stride: 2}
}
layer {name: "ip1"type: "InnerProduct"bottom: "pool2"top: "ip1"param {lr_mult: 1}param {lr_mult: 2}inner_product_param {num_output: 500weight_filler {type: "xavier"}bias_filler {type: "constant"}}
}
layer {name: "relu1"type: "ReLU"bottom: "ip1"top: "ip1"
}
layer {name: "ip2"type: "InnerProduct"bottom: "ip1"top: "ip2"param {lr_mult: 1}param {lr_mult: 2}inner_product_param {num_output: 2  #和lenet_train_test.prototxt中保持一致。weight_filler {type: "xavier"}bias_filler {type: "constant"}}
}
layer {name: "prob"type: "Softmax"bottom: "ip2"top: "prob"
}

5.2 使用opencv加在模型进行预测(具体参考 OpenCV Load caffe model)

这里直接附上代码:

#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>  #include <fstream>
#include <iostream>
#include <cstdlib>  /* Find best class for the blob (i. e. class with maximal probability) */
void getMaxClass(cv::dnn::Blob &probBlob, int *classId, double *classProb)
{cv::Mat probMat = probBlob.matRefConst().reshape(1, 1); //reshape the blob to 1x1000 matrix  cv::Point classNumber;cv::minMaxLoc(probMat, NULL, classProb, NULL, &classNumber);*classId = classNumber.x;
}std::vector<cv::String> readClassNames(const char *filename = "label.txt")
{std::vector<cv::String> classNames;std::ifstream fp(filename);if (!fp.is_open()){std::cerr << "File with classes labels not found: " << filename << std::endl;exit(-1);}std::string name;while (!fp.eof()){std::getline(fp, name);if (name.length())classNames.push_back(name.substr(name.find(' ') + 1));}fp.close();return classNames;
}int main(int argc, char **argv)
{void cv::dnn::initModule();cv::String modelTxt = "lenet.prototxt";cv::String modelBin = "lenet_iter_10000.caffemodel";cv::String imageFile = "0001.png";cv::dnn::Net net = cv::dnn::readNetFromCaffe(modelTxt, modelBin);if (net.empty()){std::cerr << "Can't load network by using the following files: " << std::endl;std::cerr << "prototxt:   " << modelTxt << std::endl;std::cerr << "caffemodel: " << modelBin << std::endl;exit(-1);}//! [Prepare blob]  cv::Mat img = cv::imread(imageFile, cv::IMREAD_GRAYSCALE);if (img.empty()){std::cerr << "Can't read image from the file: " << imageFile << std::endl;exit(-1);}cv::resize(img, img, cv::Size(28, 28));//cv::dnn::Blob inputBlob = cv::dnn::Blob(img);   //Convert Mat to dnn::Blob image batch  cv::dnn::Blob inputBlob = cv::dnn::Blob::fromImages(img);//! [Prepare blob]  //! [Set input blob]  net.setBlob(".data", inputBlob);        //set the network input  //! [Set input blob]  //! [Make forward pass]  net.forward();                          //compute output  //! [Make forward pass]  //! [Gather output]  cv::dnn::Blob prob = net.getBlob("prob");   //gather output of "prob" layer  int classId;double classProb;getMaxClass(prob, &classId, &classProb);//find the best class  //! [Gather output]  //! [Print results]  std::vector<cv::String> classNames = readClassNames();std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl;std::cout << "Probability: " << classProb * 100 << "%" << std::endl;//! [Print results]  return 0;
} //main

测试结果如下:

DONE!

2017.08.01

【caffe】使用自己的图像数据训练lenet并用opencv进行预测相关推荐

  1. 在caffe上跑自己的数据

    本文介绍如何使用caffe对自己的图像数据进行分类. 1 图片数据库准备 由于图片数据收集比较费时,为了简单说明,我用了两类,dog和bird,每种约300张.train200张,val100张. 新 ...

  2. 奔跑吧Caffe(在MNIST手写体数字集上用Caffe框架训练LeNet模型)

    数据集背景: MNIST 是一个大型的手写体数字数据库,广泛应用于机器学习领域的训练和测试,由纽约大学Yann LeCun教授整理. MNIST包括60000个训练集和10000测试集,图片固定尺寸为 ...

  3. Caffe下自己的数据训练和测试

    在caffe提供的例程当中,例如mnist与cifar10中,数据集的准备均是通过调用代码自己完成的,而对于ImageNet1000类的数据库,对于高校实验室而言,常常面临电脑内存不足的尴尬境地.而对 ...

  4. 利用Caffe实现mnist的数据训练

    阿里云的参考文档:https://help.aliyun.com/document_detail/49571.html 在文档里提供了caffe的一个案例,利用Caffe实现mnist的数据训练.准备 ...

  5. scipy笔记—scipy.misc.imresize用法(方便训练图像数据)

    scipy.misc.imresize 不同于普通的reshape, imresize不是单纯的改变图像矩阵的维度,而是能将图片重采样为指定像素,这样给深度学习中训练图像数据带来方便. import ...

  6. Caffe 在自己的数据库上训练步骤

    回忆ImageNet的步骤:http://caffe.berkeleyvision.org/gathered/examples/imagenet.html Brewing ImageNet This ...

  7. caffe HDF5Data 层使用及数据生成

    有些时候,我们的输入不是标准的图像,而是其它一些格式,比如:频谱图.特征向量等等,这种情况下LMDB.Leveldb以及ImageData layer等就不好使了,这时候我们就需要一个新的输入接口-- ...

  8. 使用Keras训练Lenet网络来进行手写数字识别

    使用Keras训练Lenet网络来进行手写数字识别 这篇博客将介绍如何使用Keras训练Lenet网络来进行手写数字识别. LeNet架构是深度学习中的一项开创性工作,演示了如何训练神经网络以端到端的 ...

  9. 如何在Tensorflow.js中处理MNIST图像数据

    by Kevin Scott 凯文·斯科特(Kevin Scott) 如何在Tensorflow.js中处理MNIST图像数据 (How to deal with MNIST image data i ...

最新文章

  1. 没有学不会的C++:用户自定义的隐式类型转换
  2. TYVJ1427 小白逛公园
  3. Caffe部署中的几个train-test-solver-prototxt-deploy等说明三
  4. spingmvc 通过xml配置redis jedispol 有密码 通过xml配置redis中的 jedispool(有密码)
  5. Mysql索引使用情况_介绍mysql索引失效的情况
  6. 【今日CV 视觉论文速览】Tue 21 Mar 2019
  7. linux socket closeconnection,求助:socket的Connection refused
  8. Leetcode 286.墙与门
  9. ASP.NET MVC3书店--第二节 控制器(转)
  10. 阶段3 3.SpringMVC·_03.SpringMVC常用注解_8 SessionAttributes注解
  11. c语言设计数字增量pi控制器,PI控制器的工作原理是什么?
  12. 复工复产到欧洲,深兰科技环卫产品亮相国际舞台
  13. vue获取微信登陆权限_vue微信授权登录
  14. windows命令行工具连接mysql数据库报ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost:3306‘ (10061)
  15. Autowired注解起什么作用呢?
  16. 移动端音视频从零到上手
  17. otg usb 定位_教你简单认识OTG与OTG线
  18. 【论文泛读148】ChineseBERT:通过字形和拼音信息增强的中文预训练
  19. linux如何解除密码锁屏图案大全,手机锁屏图案(锁屏密码)忘记了怎么办?四种方法帮你轻松搞定...
  20. spring cloud -- 5.分布式配置中心(Spring Cloud Config)

热门文章

  1. java自定义jsp标签_Javaweb自定义jsp标签
  2. Spring Cloud Alibaba - 07 Ribbon 应用篇及内置的负载均衡算法
  3. Java Review - 并发编程_读写锁ReentrantReadWriteLock的原理源码剖析
  4. Apache ZooKeeper - 使用原生的API操作ZK
  5. 白话Elasticsearch02- 结构化搜索之filter执行原理bitset机制与caching机制
  6. 并发编程-03线程安全性之原子性(Atomic包)及原理分析
  7. Spring-AOP @AspectJ切点函数之@within()和@target
  8. html圆角兼容jq,IE兼容css3圆角的htc解决方法
  9. mysql 之后,装完MySQL之后的一些操作
  10. C语言之动态内存管理与动态内存函数