日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人工智能 > Caffe >内容正文

Caffe

Caffe学习笔记3——制作并训练自己的数据集

發(fā)布時間:2025/3/15 Caffe 60 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Caffe学习笔记3——制作并训练自己的数据集 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Caffe學(xué)習(xí)筆記3

本文為原創(chuàng)作品,未經(jīng)本人同意,禁止轉(zhuǎn)載,禁止用于商業(yè)用途!本人對博客使用擁有最終解釋權(quán)

歡迎關(guān)注我的博客:http://blog.csdn.net/hit2015spring和http://www.cnblogs.com/xujianqing

http://caffe.berkeleyvision.org/gathered/examples/feature_extraction.html

這篇博客主要是用imagenet的一個網(wǎng)絡(luò)模型來對自己的圖片進(jìn)行訓(xùn)練和測試

圖片下載網(wǎng)址:http://download.csdn.net/detail/hit2015spring/9704947

參考文章:

http://caffe.berkeleyvision.org/gathered/examples/imagenet.html

?

1、準(zhǔn)備數(shù)據(jù),生成樣本標(biāo)簽

在caffe/data 文件夾下新建文件夾myself

這篇文章主要是幫助你怎么準(zhǔn)備你的數(shù)據(jù)集,怎么訓(xùn)練你自己的模型尺度,在這個筆記中主要是對自己網(wǎng)上下載的車,馬,恐龍,花,進(jìn)行訓(xùn)練和測試,訓(xùn)練2類各80張,測試各20張,放在/data/myself 目錄下的train和val文件夾下,這些圖片分類好了

這里面的圖像的大小全部為的,可以在終端用命令行,調(diào)整圖像大小,訓(xùn)練和測試的圖像均為

for name in data/myself/val/val_dinosar/*.JPEG; do

convert -resize 256x256\! $name $name

done

給這些圖片制作索引標(biāo)簽,生成訓(xùn)練和測試的txt文件,用批量處理工具對這些圖片進(jìn)行處理:在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')

#制作標(biāo)簽數(shù)據(jù),如果是狗的,標(biāo)簽設(shè)置為0,如果是貓的標(biāo)簽為1

imgfile=GetFileList('train/train_dinosar')#將數(shù)據(jù)集放在與.py文件相同目錄下

for img in imgfile:

str1=img+' '+'4'+'\n' #用空格代替轉(zhuǎ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')#將數(shù)據(jù)集放在與.py文件相同目錄下

#for img in imgfile:

# str3=img+' '+'2'+'\n' #用空格代替轉(zhuǎ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')

#制作標(biāo)簽數(shù)據(jù),如果是男的,標(biāo)簽設(shè)置為0,如果是女的標(biāo)簽為1

imgfile=GetFileList('val/val_dinosar')#將數(shù)據(jù)集放在與.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')#將數(shù)據(jù)集放在與.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("成功生成文件列表")

在終端運(yùn)行該腳本

python label.py

可以在data/myself/ 文件夾下生成兩個txt文件,train.txt和val.txt

2、生成lmdb文件

在caffe/ 文件夾下新建myself文件夾,

從/home/xxx/caffe/examples/imagenet下復(fù)制create_imagenet.sh文件到caffe/myself

修改create_imagenet.sh里面的路徑設(shè)置

其中:

EXAMPLE =/home/wangshuo/caffe/myself

表示生成的LMDB文件存放的位置

DATA=/home/wangshuo/caffe/data/myself

表示數(shù)據(jù)標(biāo)簽存放的位置

TRAIN_DATA_ROOT=/home/wangshuo/caffe/data/myself/

VAL_DATA_ROOT=/home/wangshuo/caffe/data/myself/

表示訓(xùn)練和測試數(shù)據(jù)的位置,注意這里只填到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根目錄下運(yùn)行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根目錄下運(yùn)行該文件

./myself/make_imagenet_mean.sh

在caffe/data/myself 下生成imagenet_mean.binaryproto文件

4、模型定義

復(fù)制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的參數(shù),該參數(shù)如果過大,會提示GPU內(nèi)存不夠,在這里我設(shè)置為8

?

?

復(fù)制models/bvlc_reference_caffenet/solver.prototxt到caffe/myself

文件夾下,并修改文件路徑

net: "myself/train_val.prototxt" ##模型所在目錄

snapshot_prefix: "myself/caffenet_train"##生成的模型參數(shù)

test_iter: 1000 是指測試的批次,我們就 20 張照片,設(shè)置20就可以了。

test_interval: 1000 是指每 1000 次迭代測試一次,我們改成 500 次測試一次。

base_lr: 0.01 是基礎(chǔ)學(xué)習(xí)率,因?yàn)閿?shù)據(jù)量小, 0.01 就會下降太快了,因此改成 0.001

lr_policy: "step"學(xué)習(xí)率變化

gamma: 0.1 學(xué)習(xí)率變化的比率

stepsize: 100000 每 100000 次迭代減少學(xué)習(xí)率

display: 20 每 20 層顯示一次

max_iter: 450000 最大迭代次數(shù),

momentum: 0.9 學(xué)習(xí)的參數(shù),不用變

weight_decay: 0.0005 學(xué)習(xí)的參數(shù),不用變

snapshot: 10000 每迭代 10000 次顯示狀態(tài),這里改為 2000 次

solver_mode: GPU 末尾加一行,代表用 GPU 進(jìn)行

5、訓(xùn)練

在caffe根目錄下運(yùn)行

./build/tools/caffe time --model=myself/train_val.prototxt

總結(jié)

以上是生活随笔為你收集整理的Caffe学习笔记3——制作并训练自己的数据集的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。