caffe之快速上手简明简明教程

来源:互联网 发布:电脑编程用什么软件 编辑:程序博客网 时间:2024/05/09 13:57

   

记住caffe 所有的个人工作 包括训练集、验证集、网络定义、等都可以放在examples下的个人文件夹下


学习深度学习框架 caffe之前,你必须需要训练集,比如我的这套图片集。


 examples下个人的所有文件都在百度网盘地址:http://pan.baidu.com/s/1skG4Q5J  提取码:jt5v

接着 你就可以使用shell脚本来给你的训练集和验证集打上标签。

shell脚本如下

rename.sh
#!/bin/bashDATA=./zhaoresMY=./echo "Create train.txt..."rm -rf $MY/train.txtfor i in 3 4 5 6 7 do    find $DATA/train -name $i*.jpg | cut -d '/' -f4-5 | sed "s/$/ $i/">>$MY/train.txtdoneecho "Create val.txt..."rm -rf $MY/test.txtfor i in 3 4 5 6 7do    find $DATA/test -name $i*.jpg | cut -d '/' -f4-5 | sed "s/$/ $i/">>$MY/val.txtdoneecho "All done"
</pre><pre name="code" class="html">成功的话,你就可以发现生成了train.txt和val.txt
<img src="http://img.blog.csdn.net/20160706143049679?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
接着再编写一个脚本文件,调用convert_imageset命令来转换数据格式。
#!/usr/bin/env shMY=./echo "Create train lmdb.."rm -rf $MY/img_train_lmdb/home/ist/caffe/build/tools/convert_imageset \    --shuffle \    --resize_height=256 \    --resize_width=256 \    /home/ist/caffe/examples/zhaoimgnet/zhaores/train/ \    $MY/train.txt \    $MY/img_train_lmdbecho "Create test lmdb.."rm -rf $MY/img_val_lmdb/home/ist/caffe/build/tools/convert_imageset \    --shuffle \    --resize_width=256 \    --resize_height=256 \    /home/ist/caffe/examples/zhaoimgnet/zhaores/val/ \    $MY/val.txt \    $MY/img_val_lmdb
会在 examples/myfile下面生成两个文件夹img_train_lmdb和img_test_lmdb,分别用于保存图片转换后的lmdb文件。


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

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

也可以写个脚本进行生成均值。

#!/usr/bin/env sh# Compute the mean image from the imagenet training lmdb# N.B. this is available in data/ilsvrc12MY=/home/ist/caffeEXAMPLE=$MY/examples/zhaoimgnetDATA=$EXAMPLETOOLS=$MY/build/tools$TOOLS/compute_image_mean $EXAMPLE/img_train_lmdb \  $DATA/imagenet_mean.binaryprotoecho "Done."
会出现如下:




模型就用程序自带的caffenet模型,位置在 models/bvlc_reference_caffenet/文件夹下, 将需要的两个配置文件,复制到个人文件夹内

# sudo cp models/bvlc_reference_caffenet/solver.prototxt examples/zhaoimgnet/# sudo cp models/bvlc_reference_caffenet/train_val.prototxt examples/zhaoimgnet/
修改模型参数
<pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; line-height: 18px; font-family: "Courier New" !important;"><span style="margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);"> sudo vim examples/zhaoimgnet/solver.prototxt</span>

net: "/home/ist/caffe/examples/zhaoimgnet/train_val.prototxt"test_iter: 2test_interval: 50base_lr: 0.001lr_policy: "step"gamma: 0.1stepsize: 100display: 20max_iter: 500momentum: 0.9weight_decay: 0.005snapshot: 10000snapshot_prefix: "/home/ist/caffe/examples/zhaoimgnet/caffenet_train"solver_mode: CPU

100个测试数据,batch_size为50,因此test_iter设置为2,就能全cover了。在训练过程中,调整学习率,逐步变小。

修改train_val.protxt,只需要修改两个阶段的data层就可以了,其它可以不用管。

name: "CaffeNet"layer {  name: "data"  type: "Data"  top: "data"  top: "label"  include {    phase: TRAIN  }  transform_param {    mirror: true    crop_size: 227    mean_file: "/home/ist/caffe/examples/zhaoimgnet/imagenet_mean.binaryproto"  }# mean pixel / channel-wise mean instead of mean image#  transform_param {#    crop_size: 227#    mean_value: 104#    mean_value: 117#    mean_value: 123#    mirror: true#  }  data_param {    source: "/home/ist/caffe/examples/zhaoimgnet/img_train_lmdb"    batch_size: 256    backend: LMDB  }}layer {  name: "data"  type: "Data"  top: "data"  top: "label"  include {    phase: TEST  }  transform_param {    mirror: false    crop_size: 227    mean_file: "/home/ist/caffe/examples/zhaoimgnet/imagenet_mean.binaryproto"  }# mean pixel / channel-wise mean instead of mean image#  transform_param {#    crop_size: 227#    mean_value: 104#    mean_value: 117#    mean_value: 123#    mirror: true#  }
到此你的准备工作都配置完毕 ,接下来就是训练你的模型了。使用caffe进行训练。

sudo build/tools/caffe train -solver examples/zhaoimgnet/solver.prototxt





0 0