caffe下跑自己的数据
来源:互联网 发布:灵界基友网络剧第一集 编辑:程序博客网 时间:2024/06/06 13:22
学习完mnsit和cifar10样例后,基本了解了caffe框架的使用方法,可以试试跑自己的数据了。
前几周处理完的一批图像这次排上了用场。前景图训练集和验证集各7000多张,背景图训练集和验证集各20000多张。师兄让我们用这个数据用AlexNet做一个简单的二分类。
一.准备数据
1. train和val,两个文件夹的图片准备好。
2. 生成train.txt 和 val.txt两个文档,文档中列出所有图片文件名和它所属的标签。 (这里我的前景图label为0, 背景图label为1)
如图:
3. 这里我在网上找了一个python脚本来生成这两个txt
import osdef ListFilesToTxt(dir,file,wildcard,recursion): exts = wildcard.split(" ") files = os.listdir(dir) for name in files: fullname=os.path.join(dir,name) if(os.path.isdir(fullname) & recursion): ListFilesToTxt(fullname,file,wildcard,recursion) else: for ext in exts: if(name.endswith(ext)): file.write(name + " 1\n") #此处可以更改标签 breakdef Test():#设置目录和格式 dir="bg/train_bg/" outfile="train.txt" wildcard = ".jpg" file = open(outfile,"w") if not file: print ("cannot open the file %s for writing" % outfile) ListFilesToTxt(dir,file,wildcard, 1) file.close()Test()
4.处理完后,目录结构如下:
二.处理数据
1.在caffe-master/examples目录下,新建文件夹命名为自己的项目名,我的是binary,这就是自己项目的工作目录。
因为我用的是AlexNet,可以把caffe-master/module/bvlc_alexnet/中的网络结构文件都拷贝到我的binary文件夹。
caffe-master/examples/imagenet/下有几个处理写好的脚本,也全都拷贝到binary文件夹中,改一下就能使用。
2. create_imagenet.sh
这个脚本会读取图片信息,生成lmdb的数据格式。当然,也可以修改脚本的命名。
Note:脚本中的相对路径是在caffe-master目录下
#!/usr/bin/env sh# Create the imagenet lmdb inputs# N.B. set the path to the imagenet train + val data dirsEXAMPLE=examples/binary #项目所在目录DATA=/media/clay/学习/HelloWorld/work/VOCdevkit/dataset/ #txt文件所在目录TOOLS=build/tools TRAIN_DATA_ROOT=/media/clay/学习/HelloWorld/work/VOCdevkit/dataset/train/ #train图片所在目录VAL_DATA_ROOT=/media/clay/学习/HelloWorld/work/VOCdevkit/dataset/val/ #val图片所在目录#此处可以设置调整图片的size# Set RESIZE=true to resize the images to 256x256. Leave as false if images have# already been resized using another tool.RESIZE=falseif $RESIZE; then RESIZE_HEIGHT=256 RESIZE_WIDTH=256else RESIZE_HEIGHT=227 RESIZE_WIDTH=227fiif [ ! -d "$TRAIN_DATA_ROOT" ]; then echo "Error: TRAIN_DATA_ROOT is not a path to a directory: $TRAIN_DATA_ROOT" echo "Set the TRAIN_DATA_ROOT variable in create_imagenet.sh to the path" \ "where the ImageNet training data is stored." exit 1fiif [ ! -d "$VAL_DATA_ROOT" ]; then echo "Error: VAL_DATA_ROOT is not a path to a directory: $VAL_DATA_ROOT" echo "Set the VAL_DATA_ROOT variable in create_imagenet.sh to the path" \ "where the ImageNet validation data is stored." exit 1fiecho "Creating train lmdb..."GLOG_logtostderr=1 $TOOLS/convert_imageset \ --resize_height=$RESIZE_HEIGHT \ --resize_width=$RESIZE_WIDTH \ --shuffle \ $TRAIN_DATA_ROOT \ $DATA/train_data.txt \ #train.txt文件 $EXAMPLE/binary_train_lmdb #生成的lmdb数据文件夹echo "Creating val lmdb..."GLOG_logtostderr=1 $TOOLS/convert_imageset \ --resize_height=$RESIZE_HEIGHT \ --resize_width=$RESIZE_WIDTH \ --shuffle \ $VAL_DATA_ROOT \ $DATA/val_data.txt \ #val.txt文件 $EXAMPLE/binary_val_lmdb #lmdbecho "Done."
运行脚本:./examples/binary/create_imagenet.sh
可以检查下工作目录中是否生成了两个lmdb文件夹。
3. 计算图像均值,目的是做归一化处理。
(至于为什么train和val的data layer都使用train set生成的mean,师兄说是因为test set的数据比较少)
make_imagenet_mean.sh脚本
EXAMPLE=examples/binaryDATA=data/binaryTOOLS=build/tools$TOOLS/compute_image_mean $EXAMPLE/binary_train_lmdb \ #train的lmdb文件夹 $DATA/imagenet_mean.binaryproto echo "Done."
$: ./examples/binary/make_imagenet_mean.sh
三、定义网络
这里使用的是AlexNet做分类问题,网络结构已经不需要再修改,只需要改一下data layer数据的路径就好。
train_val.prototxt
note: 显存不够的话,可以修改减少batch_size
name: "AlexNet"layer { name: "data" type: "Data" top: "data" top: "label" include { phase: TRAIN } transform_param { mirror: true crop_size: 227 mean_file: "data/binary/imagenet_mean.binaryproto" } data_param { source: "examples/binary/binary_train_lmdb"batch_size: 1 backend: LMDB }}layer { name: "data" type: "Data" top: "data" top: "label" include { phase: TEST } transform_param { mirror: false crop_size: 227mean_file: "data/binary/imagenet_mean.binaryproto" } data_param { source: "examples/binary/binary_val_lmdb" batch_size: 1 backend: LMDB }}
solver.prototxt
各个参数的含义可以参考cifar10的solver.prototxt
net: "examples/binary/train_val.prototxt"test_iter: 1000test_interval: 1000base_lr: 0.1lr_policy: "step"gamma: 0.1stepsize: 100000display: 20max_iter: 450000momentum: 0.9weight_decay: 0.0005snapshot: 10000snapshot_prefix: "examples/binary/binary_alexnet_train"solver_mode: GPU
test_iter: 1000是指测试的批次
test_interval: 1000是指每1000次迭代测试一次
base_lr: 0.01是基础学习率
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次显示状态
solver_mode: GPU末尾加一行,代表用GPU进行
四、训练
train_caffenet.sh
./build/tools/caffe train \ --solver=examples/binary/solver.prototxt
caffe-master目录下:
$: ./examples/binary/train_caffenet.sh
不出意外的话,可以在终端下看到训练过程了。
五、恢复数据
resume_training.sh
到这里,相信大家都知道怎么用了。
binary目录:
可以开心地跑实验啦~~
然并卵,,,我的电脑把batch_size设置为1还是会out of memory。显存太低,没办法啦。
祝大家玩的开心。。。
- caffe下跑自己的数据
- caffe跑自己的数据
- Windows下caffe训练自己的数据
- 用caffe跑自己的数据,基于WINDOWS的caffe
- 在caffe上跑自己的数据
- 在caffe上跑自己的数据
- 在caffe上跑自己的数据
- 在caffe上跑自己的数据
- Caffe 跑自己的数据和训练
- 在caffe上跑自己的数据
- 在caffe上跑自己的数据
- 利用caffe跑自己的数据
- 在caffe上跑自己的数据
- Caffe下自己的数据训练和测试
- Caffe下自己的数据训练和测试
- windows下使用caffe finetune自己的数据
- 在caffe环境下制作自己的数据集
- windows10下利用caffe model训练自己的数据
- Web Service修炼之二Xfire+Spring
- Android自动化测试:Robotium在Android Studio中的使用
- 数据采集仪器-界面呈现1
- C语言和C++的区别与对比
- BP单隐层神经网络 代码实现 以及 详细步骤
- caffe下跑自己的数据
- ajax的datatype选项的值
- Xcode 中放置图片的位置,Images.xcassets 和直接放在源文件目录中的区别
- 南邮 OJ 1058 Tom and Jerry
- hide app icon
- bootstrap(菜单、按钮、导航)
- HTML自定义的列表,
- HDU 2680(spfa)
- 多手柄输入(20150515、20150610、20150827)