Caffe_Windows学习笔记(二)用自己的数据训练和测试CaffeNet
来源:互联网 发布:网络基础架构设计方案 编辑:程序博客网 时间:2024/05/22 05:15
0、参考文献
[1]薛开宇 《学习笔记3 用自己的数据训练和测试“CaffeNet”》
[2]悠望南山 http://www.cnblogs.com/NanShan2016/p/5532589.html
1、准备数据集
1)全景网下载的50张鸟、50张猫,批量重命名,放在新建文件夹中,如下。
bird
cat
2)全景网下载的10张鸟、10张猫,批量重命名,放在新建文件夹中,如下。
3)制作train.txt(注意要指明子文件夹)、val.txt,放在data/myself下。生成标签代码如下:
import osdef IsSubString(SubStrList,Str): flag=True for substr in SubStrList: if not(substr in Str): flag=False return flagdef GetFileList(FindPath,FlagStr=[]): FileList=[] FileNames=os.listdir(FindPath) if len(FileNames)>0: for fn in FileNames: if len(FlagStr)>0: if IsSubString(FlagStr,fn): fullfilename=os.path.join(FindPath,fn) FileList.append(fullfilename) else: fullfilename=os.path.join(FindPath,fn) FileList.append(fullfilename) if len(FileList)>0: FileList.sort() return FileListtrain_txt=open('train.txt','w')imgfile=GetFileList('train/train_dhj/')for img in imgfile: str1=img+' '+'1'+'\n' train_txt.writelines(str1) imgfile=GetFileList('train/train_zzz/')for img in imgfile: str2=img+' '+'0'+'\n' train_txt.writelines(str2)train_txt.close()test_txt=open('val.txt','w')imgfile=GetFileList('val/test_dhj/')for img in imgfile: str3=img+' '+'1'+'\n' test_txt.writelines(str3) imgfile=GetFileList('val/test_zzz/')for img in imgfile: str4=img+' '+'0'+'\n' test_txt.writelines(str4)test_txt.close()print("succeed")
2、将训练集与测试集转成leveldb格式
1)将examples/imagenet的create_imagenet.sh复制到examples/myself文件夹下,代码修改如下
#!/usr/bin/env sh# Create the imagenet lmdb inputs# N.B. set the path to the imagenet train + val data dirsset -eEXAMPLE=examples/myselfDATA=data/myselfTOOLS=Build/x64/ReleaseTRAIN_DATA_ROOT=data/myself/train/VAL_DATA_ROOT=data/myself/val/# Set RESIZE=true to resize the images to 256x256. Leave as false if images have# already been resized using another tool.RESIZE=trueif $RESIZE; then RESIZE_HEIGHT=256 RESIZE_WIDTH=256else RESIZE_HEIGHT=0 RESIZE_WIDTH=0fiif [ ! -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 \--backend=leveldb \ --shuffle \ $TRAIN_DATA_ROOT \ $DATA/train.txt \ $DATA/myself_train_leveldbecho "Creating val lmdb..."GLOG_logtostderr=1 $TOOLS/convert_imageset \ --resize_height=$RESIZE_HEIGHT \ --resize_width=$RESIZE_WIDTH \--backend=leveldb \ --shuffle \ $VAL_DATA_ROOT \ $DATA/val.txt \ $DATA/myself_val_leveldbecho "Done."
2)在cygwin64中运行create_imagenet.sh,得到
3、计算图像均值
1)将examples/imagenet的make_imagenet_mean.sh复制到examples/myself文件夹下,代码修改如下
#!/usr/bin/env sh# Compute the mean image from the imagenet training lmdb# N.B. this is available in data/ilsvrc12EXAMPLE=examples/myselfDATA=data/myselfTOOLS=Build/x64/Release$TOOLS/compute_image_mean $DATA/myself_train_leveldb $DATA/imagenet_mean.binaryproto --backend=leveldbecho "Done."
2)在cygwin64中运行make_imagenet_mean.sh,得到均值文件
4、网络定义
1)复制models/bvlc_reference_caffenet的train_val.prototxt和solver.prototxt到examples/myself
2)设置train_val.prototxt文件,对数据层进行修改。
name: "CaffeNet"layer { name: "data" type: "Data" top: "data" top: "label" include { phase: TRAIN } transform_param { mirror: true crop_size: 227 mean_file: "D:/caffe_test/caffe-master/data/myself/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: "D:/caffe_test/caffe-master/data/myself/myself_train_leveldb" batch_size: 5 backend: LEVELDB }}layer { name: "data" type: "Data" top: "data" top: "label" include { phase: TEST } transform_param { mirror: false crop_size: 227 mean_file: "D:/caffe_test/caffe-master/data/myself/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: false# } data_param { source: "D:/caffe_test/caffe-master/data/myself/myself_val_leveldb" batch_size: 5 backend: LEVELDB }}
3)设置solver.prototxt文件
net: "D:/caffe_test/caffe-master/examples/myself/train_val.prototxt"test_iter: 10test_interval: 500base_lr: 0.001lr_policy: "step"gamma: 0.1stepsize: 100000display: 20max_iter: 450000momentum: 0.9weight_decay: 0.0005snapshot: 2000snapshot_prefix: "D:/caffe_test/caffe-master/data/myself/result"solver_mode: GPU
4)复制examples/imagenet的train_caffenet.sh到examples/myself下,修改
#!/usr/bin/env shset -e./Build/x64/Release/caffe.exe train \ --solver=examples/myself/solver.prototxt $@
5、训练网络
在cygwin64中运行train_caffenet.sh,就完成了。
阅读全文
0 0
- Caffe_Windows学习笔记(二)用自己的数据训练和测试CaffeNet
- 用自己的数据训练和测试“caffenet”
- windows 训练、微调caffenet 训练测试自己的数据
- caffe示例实现之5用CaffeNet训练与测试自己的数据集
- caffe示例实现之5用CaffeNet训练与测试自己的数据集
- caffe示例实现之5用CaffeNet训练与测试自己的数据集
- Caffe_Windows学习笔记(三)搭建自己的网络mnist在caffe上进行训练与学习
- Caffe_Windows学习笔记(五)用训练好的caffemodel来进行分类
- [caffe学习笔记]用自己的数据进行训练和测试
- Windows Caffe 学习笔记(三)在Caffe上训练和测试自己的数据
- caffe利用caffenet训练自己的图片数据
- caffe学习笔记3:使用caffe对自己的图像数据进行训练和测试
- caffe----训练自己的图片caffenet模型
- caffe学习笔记12:训练和测试自己的图片
- Tiny_cnn用自己的数据训练和测试
- Tiny_cnn用自己的数据训练和测试
- Windows Caffe 学习笔记(四)搭建自己的网络,训练和测试MNIST手写字体库
- caffe学习笔记:mnist数据集的训练和测试
- DC-DC电路Layout设计注意事项
- Spring事务管理
- 织梦tag标签按照设定的栏目id获取相应的内容
- RPC communication--portmapper
- android 通过代理activity的方式实现插件化
- Caffe_Windows学习笔记(二)用自己的数据训练和测试CaffeNet
- Android 开发之录音与播放
- Spring JDBC-混合框架的事务管理
- 数据库锁总结
- [LeetCode]21. Merge Two Sorted Lists
- 道路改建
- Java 打包 jar 运行
- Eclipse导入Java项目时“No projects are found to import”错误的处理,
- frameworks/av/media/CedarX-Projects/CedarAndroidLib/LIB_KK44_/Android.mk: No such file or directory