申明:此教程加工于caffe 如何训练自己的数据图片

一、准备数据

有条件的同学,可以去imagenet的官网http://www.image-net.org/download-images,下载imagenet图片来训练。但是我没有下载,一个原因是注册账号的时候,验证码始终出不来(听说是google网站的验证码,而我是上不了google的)。第二个原因是数据太大了。。。

我去网上找了一些其它的图片来代替,共有500张图片,分为大巴车、恐龙、大象、鲜花和马五个类,每个类100张。需要的同学,可到我的网盘下载:http://pan.baidu.com/s/1nuqlTnN(已保存至个人网盘)

编号分别以3,4,5,6,7开头,各为一类。我从其中每类选出20张作为测试,其余80张作为训练。因此最终训练图片400张,测试图片100张,共5类。我将图片放在caffe根目录下的data文件夹下面。即训练图片目录:data/re/train/ ,测试图片目录: data/re/test/

二、转换为lmdb格式

具体的转换过程,可参见我的前一篇博文:Caffe学习系列(11):图像数据转换成db(leveldb/lmdb)文件

首先,在examples下面创建一个myfile的文件夹,来用存放配置文件和脚本文件。然后编写一个脚本create_filelist.sh,用来生成train.txt和test.txt清单文件

# sudo mkdir examples/myfile
# sudo vi examples/myfile/create_filelist.sh

编辑此文件,写入如下代码,并保存

#!/usr/bin/env sh
DATA=data/re/
MY=examples/myfile
echo "Create train.txt..."
rm -rf $MY/train.txt #删除原有数据
for i in 3 4 5 6 7
do
find $DATA/train -name $i*.jpg | cut -d '/' -f4-5 | sed "s/$/ $i/">>$MY/train.txt
done
echo "Create test.txt..."
rm -rf $MY/test.txt
for i in 3 4 5 6 7
do
find $DATA/test -name $i*.jpg | cut -d '/' -f4-5 | sed "s/$/ $i/">>$MY/test.txt
done
echo "All done"

然后,运行此脚本

# sudo sh examples/myfile/create_filelist.sh

成功的话,就会在examples/myfile/ 文件夹下生成train.txt和test.txt两个文本文件,里面就是图片的列表清单。

接着再编写一个脚本文件,调用convert_imageset命令来转换数据格式。

# sudo vi examples/myfile/create_lmdb.sh

插入:

#!/usr/bin/env sh
MY=examples/myfile # 这里说明下:一共有6处需要修改
# 原数据为256×256 现在为了优化训练速度改成32×32,精确度降低可忽略(4个)
# /home/xxx/caffe/data/re/,修改xxx为自己用户的目录,这里为undo (2个) echo "Create train lmdb.."
rm -rf $MY/img_train_lmdb
build/tools/convert_imageset \
--shuffle \
--resize_height=32 \
--resize_width=32 \
/home/undo/caffe/data/re/ \
$MY/train.txt \
$MY/img_train_lmdb #转换成lmdb格式的训练数据 echo "Create test lmdb.."
rm -rf $MY/img_test_lmdb
build/tools/convert_imageset \
--shuffle \
--resize_width=32 \
--resize_height=32 \
/home/undo/caffe/data/re/ \
$MY/test.txt \
$MY/img_test_lmdb echo "All Done.."

因为图片大小不一,因此我统一转换成256*256(32×32速度更快,精度影响不大)大小。运行成功后,会在 examples/myfile下面生成两个文件夹img_train_lmdb和img_test_lmdb,分别用于保存图片转换后的lmdb文件。

三、计算均值并保存

图片减去均值再训练,会提高训练速度和精度。因此,一般都会有这个操作。

caffe程序提供了一个计算均值的文件compute_image_mean.cpp,我们直接使用就可以了

# sudo build/tools/compute_image_mean examples/myfile/img_train_lmdb examples/myfile/mean.binaryproto
compute_image_mean带两个参数,第一个参数是lmdb训练数据位置,第二个参数设定均值文件的名字及保存路径。
运行成功后,会在 examples/myfile/ 下面生成一个mean.binaryproto的均值文件。 四、创建模型并编写配置文件(使用原作者的配置文件,训练不成功,后参考其他教程该用cifar-10的配置文件成功训练)

  由于在32*32位框架中,cifar-10有现成的学习框架,我们只需要进行一些关键数据的改写就可以快速的训练进行我们的训练了,以下时详细的说明。
首先拷贝文件。

sudo cp examples/cifar10/cifar10_quick_solver.prototxt  examples/myfile/
sudo cp examples/cifar10/cifar10_quick_train_test.prototxt examples/myfile/
sudo mv examples/myfile/cifar10_quick_solver.prototxt examples/myfile/quick_solver.prototxt
sudo mv examples/myfile/cifar10_quick_train_test.prototxt examples/myfile/quick_train_test.prototxt
sudo vi examples/myfile/quick_train_test.prototxt

修改的地方:(name之类自己随便修改)

1.
transform_param {
mean_file: “examples/myfile/mean.binaryproto”#修改
}
data_param {
source: “examples/myfile/img_train_lmdb”#修改
batch_size: 50#修改
backend: LMDB
}
2.
transform_param {
mean_file: “examples/myfile/mean.binaryproto”#修改
}
data_param {
source: “examples/myfile/img_test_lmdb”#修改
batch_size: 50 #修改
backend: LMDB

这里直接贴上我个人的配置文件

name: "selfimg_quick"
layer {
name: "selfimg"
type: "Data"
top: "data"
top: "label"
include {
phase: TRAIN
}
transform_param {
mean_file: "examples/myfile/mean.binaryproto"
}
data_param {
source: "examples/myfile/img_train_lmdb"
batch_size: 50
backend: LMDB
}
}
layer {
name: "selfimg"
type: "Data"
top: "data"
top: "label"
include {
phase: TEST
}
transform_param {
mean_file: "examples/myfile/mean.binaryproto"
}
data_param {
source: "examples/myfile/img_test_lmdb"
batch_size: 50
backend: LMDB
}
}
layer {
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 32
pad: 2
kernel_size: 5
stride: 1
weight_filler {
type: "gaussian"
std: 0.0001
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "pool1"
type: "Pooling"
bottom: "conv1"
top: "pool1"
pooling_param {
pool: MAX
kernel_size: 3
stride: 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: 32
pad: 2
kernel_size: 5
stride: 1
weight_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: AVE
kernel_size: 3
stride: 2
}
}
layer {
name: "conv3"
type: "Convolution"
bottom: "pool2"
top: "conv3"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 64
pad: 2
kernel_size: 5
stride: 1
weight_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: AVE
kernel_size: 3
stride: 2
}
}
layer {
name: "ip1"
type: "InnerProduct"
bottom: "pool3"
top: "ip1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 64
weight_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: 10
weight_filler {
type: "gaussian"
std: 0.1
}
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"
}

quick_train

$ sudo vi examples/myfile/quick_train_test.prototxt 

只需要修改几句,其它参数默认即可。想要调优的话,可以试试其它的参数。

这里直接贴上我的配置

# The train/test net protocol buffer definition
net: "examples/myfile/quick_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 100 testing images.
test_iter: 2
# Carry out testing every 50 training iterations.
test_interval: 50
# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.001
momentum: 0.9
weight_decay: 0.004
# The learning rate policy
lr_policy: "fixed"
# Display every 2 iterations
display: 2
# The maximum number of iterations
max_iter: 300
# snapshot intermediate results
snapshot: 100
snapshot_prefix: "examples/myfile/selfimg_quick"
# solver mode: CPU or GPU
solver_mode: CPU

step 5 训练

caffe工具箱下,提供了专门训练的函数。

sudo build/tools/caffe train -solver examples/myfile/quick_solver.prototxt 

在cpu模式下,大约训练几分钟即可迭代完300次,以下是我的训练结果。

训练时间3分钟(设置32×32)。精确度0.93(还行。。)

 

caffe 如何训练自己的数据图片的更多相关文章

  1. caffe(12) 训练自己的数据

    学习caffe的目的,不是简单的做几个练习,最终还是要用到自己的实际项目或科研中.因此,本文介绍一下,从自己的原始图片到lmdb数据,再到训练和测试模型的整个流程. 一.准备数据 有条件的同学,可以去 ...

  2. caffe学习三:使用Faster RCNN训练自己的数据

    本文假设你已经完成了安装,并可以运行demo.py 不会安装且用PASCAL VOC数据集的请看另来两篇博客. caffe学习一:ubuntu16.04下跑Faster R-CNN demo (基于c ...

  3. caffe 用faster rcnn 训练自己的数据 遇到的问题

    1 . 怎么处理那些pyx和.c .h文件 在lib下有一些文件为.pyx文件,遇到不能import可以cython 那个文件,然后把lib文件夹重新make一下. 遇到.c 和 .h一样的操作. 2 ...

  4. 在caffe中用训练好的 caffemodel 来分类新的图片所遇到的问题

    结合之前的博客: http://www.cnblogs.com/Allen-rg/p/5834551.html#3949333 用caffemodel去测试单通道的图像(mnist数据集)时,出现了问 ...

  5. caffe绘制训练过程的loss和accuracy曲线

    转自:http://blog.csdn.net/u013078356/article/details/51154847 在caffe的训练过程中,大家难免想图形化自己的训练数据,以便更好的展示结果.如 ...

  6. CAFFE中训练与使用阶段网络设计的不同

    神经网络中,我们通过最小化神经网络来训练网络,所以在训练时最后一层是损失函数层(LOSS), 在测试时我们通过准确率来评价该网络的优劣,因此最后一层是准确率层(ACCURACY). 但是当我们真正要使 ...

  7. py-faster-rcnn 训练自己的数据

    转载:http://blog.csdn.net/sinat_30071459/article/details/51332084  Faster-RCNN+ZF用自己的数据集训练模型(Python版本) ...

  8. YOLO2解读,训练自己的数据及相关转载以供学习

    https://pjreddie.com/darknet/yolo/ 具体安装及使用可以参考官方文档https://github.com/pjreddie/darknet https://blog.c ...

  9. faster r-cnn 在CPU配置下训练自己的数据

    因为没有GPU,所以在CPU下训练自己的数据,中间遇到了各种各样的坑,还好没有放弃,特以此文记录此过程. 1.在CPU下配置faster r-cnn,参考博客:http://blog.csdn.net ...

随机推荐

  1. IIS8.5关于“ 配置错误 不能在此路径中使用此配置节”的解决办法

    今天刚安装好IIS8.5, 我的系统是win8.1 enterprise版本. 建了一个简单的页面准备调试,却发现了这个错误: 详细错误信息模块 IIS Web Core 通知 BeginReques ...

  2. android 低功耗蓝牙使用

    参考链接:http://blog.csdn.net/xubin341719/article/details/38584469 1.android 手机的低功耗蓝牙,又称BLE :BLE在andriod ...

  3. 监控Elasticsearch的插件【check_es_system】

    监控Elasticsearch的插件推荐  强大的shell脚本 #!/bin/bash ####################################################### ...

  4. Elasticsearch技术解析与实战(三)文档的聚合

    1.计算每个tag下的商品数量 PUT /database/_mapping/product { "properties": { "tags": { " ...

  5. Spring容器事件、自定义事件

    Spring容器内置事件,如容器的启动.停止.关闭.销毁等事件 <bean name="contextStartedHandler" class="com.nuts ...

  6. 大数据测试之ETL测试工具和面试常见的问题及答案

    转载自: http://www.51testing.com/html/87/n-3722487.html 概述 商业信息和数据对于任何一个企业而言都是至关重要的.现在很多公司都投入了大量的人力.资金和 ...

  7. 【leetcode 简单】 第七十四题 缺失数字

    给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: [9,6,4,2 ...

  8. 22、Vector简介

    Vector是在jdk1.0版本中就存在的,当时的集合体系还没有现在这么多,在jdk1.2中Vector才实现了Collection接口,不过随着jdk的不断更新,这个类已经逐渐被ArrayList所 ...

  9. opencv 摄像头

    VideoCapture cap(); if(!cap.isOpened()) ; Mat frame, edges; namedWindow(); for(;;) { cap >> fr ...

  10. C# WebClient、 jsonp实现跨域

    WebClient 无传输数据获取 Uri uri = new Uri(allURL); WebClient wc = new WebClient(); wc.Encoding = System.Te ...