Caffe学习笔记3——制作并训练自己的数据集
Caffe學習筆記3
本文為原創作品,未經本人同意,禁止轉載,禁止用于商業用途!本人對博客使用擁有最終解釋權
歡迎關注我的博客:http://blog.csdn.net/hit2015spring和http://www.cnblogs.com/xujianqing
http://caffe.berkeleyvision.org/gathered/examples/feature_extraction.html
這篇博客主要是用imagenet的一個網絡模型來對自己的圖片進行訓練和測試
圖片下載網址:http://download.csdn.net/detail/hit2015spring/9704947
參考文章:
http://caffe.berkeleyvision.org/gathered/examples/imagenet.html
?
1、準備數據,生成樣本標簽
在caffe/data 文件夾下新建文件夾myself
這篇文章主要是幫助你怎么準備你的數據集,怎么訓練你自己的模型尺度,在這個筆記中主要是對自己網上下載的車,馬,恐龍,花,進行訓練和測試,訓練2類各80張,測試各20張,放在/data/myself 目錄下的train和val文件夾下,這些圖片分類好了
這里面的圖像的大小全部為的,可以在終端用命令行,調整圖像大小,訓練和測試的圖像均為
for name in data/myself/val/val_dinosar/*.JPEG; do
convert -resize 256x256\! $name $name
done
給這些圖片制作索引標簽,生成訓練和測試的txt文件,用批量處理工具對這些圖片進行處理:在data/myself/ 文件夾下面建立一個label.py的python腳本文件
| #<pre name="code"class="python"> #coding:utf-8 ''''' Created on Jul 29, 2016 ? @author: sgg ''' ? "<span style=""font-family:Arial;font-size:18px;"">" "<span style=""font-size:18px;"">" "<span style=""font-size:18px;"">" import os ? def IsSubString(SubStrList,Str): flag=True for substr in SubStrList: if not(substr in Str): flag=False ? return flag ? #掃面文件 def 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 FileList ? ? ? train_txt=open('train.txt','w') #制作標簽數據,如果是狗的,標簽設置為0,如果是貓的標簽為1 imgfile=GetFileList('train/train_dinosar')#將數據集放在與.py文件相同目錄下 for img in imgfile: str1=img+' '+'4'+'\n' #用空格代替轉義字符 \t train_txt.writelines(str1) ? ? imgfile=GetFileList('train/train_ele') for img in imgfile: str2=img+' '+'3'+'\n' train_txt.writelines(str2) ? ? #imgfile=GetFileList('train/train_flower')#將數據集放在與.py文件相同目錄下 #for img in imgfile: # str3=img+' '+'2'+'\n' #用空格代替轉義字符 \t # train_txt.writelines(str3) ? ? #imgfile=GetFileList('train/train_horse') #for img in imgfile: # str4=img+' '+'1'+'\n' # train_txt.writelines(str4) ? ? #imgfile=GetFileList('train/train_truck') #for img in imgfile: # str5=img+' '+'0'+'\n' # train_txt.writelines(str5) train_txt.close() ? #測試集文件列表 test_txt=open('val.txt','w') #制作標簽數據,如果是男的,標簽設置為0,如果是女的標簽為1 imgfile=GetFileList('val/val_dinosar')#將數據集放在與.py文件相同目錄下 for img in imgfile: str6=img+' '+'4'+'\n' test_txt.writelines(str6) ? ? imgfile=GetFileList('val/val_ele') for img in imgfile: str7=img+' '+'3'+'\n' test_txt.writelines(str7) ? ? #imgfile=GetFileList('val/val_flower')#將數據集放在與.py文件相同目錄下 #for img in imgfile: # str8=img+' '+'2'+'\n' # test_txt.writelines(str8) ? ? #imgfile=GetFileList('val/val_horse') #for img in imgfile: # str9=img+' '+'1'+'\n' # test_txt.writelines(str9) ? ? #imgfile=GetFileList('val/val_truck') #for img in imgfile: # str10=img+' '+'0'+'\n' # test_txt.writelines(str10) test_txt.close() ? print("成功生成文件列表") |
在終端運行該腳本
python label.py
可以在data/myself/ 文件夾下生成兩個txt文件,train.txt和val.txt
2、生成lmdb文件
在caffe/ 文件夾下新建myself文件夾,
從/home/xxx/caffe/examples/imagenet下復制create_imagenet.sh文件到caffe/myself
修改create_imagenet.sh里面的路徑設置
其中:
EXAMPLE =/home/wangshuo/caffe/myself
表示生成的LMDB文件存放的位置
DATA=/home/wangshuo/caffe/data/myself
表示數據標簽存放的位置
TRAIN_DATA_ROOT=/home/wangshuo/caffe/data/myself/
VAL_DATA_ROOT=/home/wangshuo/caffe/data/myself/
表示訓練和測試數據的位置,注意這里只填到myself這一級的目錄。
| GLOG_logtostderr=1 $TOOLS/convert_imageset \ --resize_height=$RESIZE_HEIGHT \ --resize_width=$RESIZE_WIDTH \ --shuffle \ $TRAIN_DATA_ROOT \ $DATA/train.txt \ $EXAMPLE/ilsvrc12_train_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.txt \ $EXAMPLE/ilsvrc12_val_lmdb |
EXAMPLE/ilsvrc12_val_lmdb
表示生成文件名為ilsvrc12_train_lmdb 和ilsvrc12_val_lmdb
在caffe根目錄下運行create_imagenet.sh
./myself/create_imagenet.sh
在caffe/myself文件夾下生成lmdb文件
3、生成均值文件
從caffe/ examples/imagenet/ 拷貝make_imagenet_mean.sh文件到caffe/myself 文件夾下
修改該文件
EXAMPLE=/home/wangshuo/caffe/myself
##上面生成的lmdb文件目錄
DATA=/home/wangshuo/caffe/data/myself
###生成文件所要存放的目錄
TOOLS=/home/wangshuo/caffe/build/tools
?
在caffe根目錄下運行該文件
./myself/make_imagenet_mean.sh
在caffe/data/myself 下生成imagenet_mean.binaryproto文件
4、模型定義
復制models/bvlc_reference_caffenet/train_val.prototxt到caffe/myself文件夾,并修改路徑
| mean_file: "data/ilsvrc12/imagenet_mean.binaryproto" source: "examples/imagenet/ilsvrc12_train_lmdb" mean_file: "data/ilsvrc12/imagenet_mean.binaryproto" source: "examples/imagenet/ilsvrc12_val_lmdb" |
?
| mean_file: "data/myself/imagenet_mean.binaryproto" source: "myself/ilsvrc12_train_lmdb" mean_file: "data/myself/imagenet_mean.binaryproto" source: "myself/ilsvrc12_val_lmdb" |
?
這里還有一個bitch_size的參數,該參數如果過大,會提示GPU內存不夠,在這里我設置為8
?
?
復制models/bvlc_reference_caffenet/solver.prototxt到caffe/myself
文件夾下,并修改文件路徑
| net: "myself/train_val.prototxt" ##模型所在目錄 snapshot_prefix: "myself/caffenet_train"##生成的模型參數 |
test_iter: 1000 是指測試的批次,我們就 20 張照片,設置20就可以了。
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 進行
5、訓練
在caffe根目錄下運行
| ./build/tools/caffe time --model=myself/train_val.prototxt |
總結
以上是生活随笔為你收集整理的Caffe学习笔记3——制作并训练自己的数据集的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu16.04 +cuda8.0
- 下一篇: Android textedit 背景,