您的位置:首页 > Web前端

caffe:用自己的图像数据训练模型(图片分类)

2015-08-04 15:43 961 查看
本文参考“学习笔记 3 用自己的数据训练和测试“CaffeNet(薛开宇)”和caffe官网点这里

主要讲述——如何用自己准备的图片数据并使用caffe来训练CNN模型,用于图片分类,类似于caffe官网例程ImageNet。

本文主要分为以下几部分:

数据准备
计算图像均值
定义网络
模型训练

1.数据准备

在 caffe_master/data 中新建文件夹 myself,在myself/train/cat文件里存放训练猫的图片 40 张(名称为cat1~cat40),在myself/train/dog文件里存放训练狗的图片 40 张(名称为cat1~cat40),在myself/val文件里测试狗的图片
10 张和测试猫的图片10张。

根据训练和测试的图片生成train.txt文件和val.txt文件,此文件里包含文件名和分类标签(标签从0开始,0,1,2....)。

可在train文件夹路径下,使用find -name *.jpeg |cut -d '/' -f2-3 >train.txt脚本命令来生成train.txt文件。val.txt的生成方法类似。

train.txt如图所示:





我们还需要把图片大小变成256*256的,我们可以使用lightweight mincepie package ,也可以使用如下的shell命令:

for name in /path/to/imagenet/val/*.JPEG; do

convert -resize 256x256\! $name $name

done

我们更可以不在这一步缩放,在下一步创建leveldb数据时,将
examples/imagenet/create_imagenet.sh
的RESIZE设置为true来更改大小。

生成leveldb格式数据:在caffe-master/examples里创建文件夹myself,将imagenet文件夹里的create_imagenet.sh复制到该文件夹下进行修改,主要修改训练和测试路径的位置,然后在命令行的caffe-master路径下,运行 ./examples/myself/create_imagenet.sh
。运行完毕,将在myself里生成myself_train_leveldb和myself_val_leveldb。

2.计算图像均值

模型需要从每张图片里减去均值,所以我们必须提前获得图像的均值,用tools/compute_image_mean.cpp实现,这个cpp是一个很好的例子去熟悉如何操作多个组件,例如协议缓冲区,leveldb, 登录等。我们直接复制Imagenet的make_imagenet_mean,并修改路径即可。然后在命令行的caffe-master路径下,运行
./examples/myself/make_imagenet_mean.sh ,运行结束后,将会在caffe-master/data/myself里生成myself_mean.binaryproto。

3.定义网络

这一步生成myself_train.prototxt,myself_val.prototxt和myself_solver.prototxt等网络配置文件。

方法一:

将训练和测试的模型定义文件prototxt分开写成两个文件,即myself_train.prototxt和myself_val.prototxt文件,这两个文件由imagenet的imagenet_train.prototxt和imagenet_val.prototxt修改而来。但是在imagenet文件夹下没有这两个文件,我们可以从这里下载。下载完毕后,一般不能直接用,因为版本不一致,需要更新版本。在caffe_ROOT路径下,./build/tools/upgrade_net_proto_text
myself/imagenet_train.prototxt new_version_train.prototxt , val也类似)。

下载完后,修改imagenet_train.prototxt, 修改数据层的路径,如下:

mean_file: "data/myself/myself_mean.binaryproto"

source: "examples/myself/myself_train_leveldb"

并在data层的data param里加入backend:LMDB,这个很重要,否则训练数据时,将报错——“leveldb数据不存在”。还有最后一层的num_output改为2,因为只有两类。

修改完毕后,将命名为myself_train.prototxt。

imagenet_val.prototxt的修改方法与之类似。imagenet_val.prototxt和imagenet_train.prototxt的网络结构几乎一样,区别在于input
layer和output layer。

input layer difference:The training network’s
data
input
layer draws its data from
examples/myself/myself_train_leveldb
and randomly mirrors the input image. The testing network’s
data
layer takes data from
examples/
myself/myself
_val_leveldb

and does not perform random mirroring.


output layer difference:
Both networks output the
softmax_loss
layer, which in training is used to compute the loss function and to initialize the backpropagation, while in validation this loss is simply reported. The testing network also
has a second output layer,
accuracy
, which is used to report the accuracy on the test set. In the process of training, the test network will occasionally be instantiated and tested on the test set, producing lines like
Test score #0: xxx

and
Test score #1: xxx
. In this case score 0 is the accuracy (which will start around 1/1000 = 0.001 for an untrained network) and score 1 is the loss (which will start around 7 for an untrained network).



方法二:


将train和test的网络写在一个文件夹里,粘贴
models/bvlc_reference_caffenet/train_val.prototxt到examples/myself文件夹里,然后更改相应路径和num_output=2即可。




生成myself_solver.prototxt文件:


粘贴
models/bvlc_reference_caffenet/solver.prototxt
,然后根据数据更改参数,具体更改如下:

test_iter:
1000 是指测试的批次,我们就 10 张照片,设置 10 就可以了。

test_interval: 1000 是指每 1000 次迭代测试一次,我们改成 500 次测试一次。

base_lr: 0.01 是基础学习率,因为数据量小,0.01 就会下降太快了,因此改成 0.001

lr_policy: "step"学习率变化

gamma: 0.1 学习率变化的比率

stepsize: 100000 每 100000 次迭代减少学习率

display: 20 每 20 层显示一次

max_iter: 450000 最大迭代次数,

momentum: 0.9 学习的参数,不用变

weight_decay: 0.0005 学习的参数,不用变

snapshot: 10000 每迭代 10000 次显示状态,这里改为 2000 次

solver_mode: GPU 末尾加一行,代表用 GPU 进行

在上面三步都完成后,examples/myself文件夹如下:



4.训练

命令行运行./build/tools/caffe
train --solver=examples/myself/myself_solver.prototxt

如果对计算时间感兴趣,还可以运行./build/tools/caffe time --model=examples/myself//train_val.prototxt

5.恢复数据(继续训练)

如果断电,由于snapshots里存储中间结果(参数和历史),我们可以使用如下命令从snapshots里回复训练:

./build/tools/caffe train --solver=examples/myself/solver.prototxt

--snapshot=examples/myself/caffenet_train_iter_10000.solverstate

6.用模型分类图片

参见这里
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: