caffe的python接口生成配置文件學(xué)習(xí)
引言
caffe是C++語言寫的,可能很多人不太熟悉,因此想用更簡單的腳本語言來實(shí)現(xiàn)。caffe提供matlab接口和python接口,這兩種語言就非常簡單,而且非常容易進(jìn)行可視化,使得學(xué)習(xí)更加快速,理解更加深入。
半年前,我在學(xué)習(xí)CAFFE的時(shí)候,為了加深理解,因此寫下了隨筆,有了一系列的caffe學(xué)習(xí)文章。半年過去,很多人問到關(guān)于python接口和可視化的一些問題,現(xiàn)在有點(diǎn)空閑時(shí)間,就再次寫下一些隨筆,大家一起來學(xué)習(xí)。有些重復(fù)的內(nèi)容,我就不再多講,如果大家有興趣可移步:
如何計(jì)算訓(xùn)練數(shù)據(jù)的均值文件?
以上這些操作都是訓(xùn)練之前的預(yù)處理操作,不管是用什么接口,都要用到。
如何寫配置文件
首先,我們需要掌握的,就是如何寫配置文件,通過下面的代碼來學(xué)習(xí):
# -*- coding: utf-8 -*- """ Spyder Editor """ from caffe import layers as L,params as P,to_proto path='/home/xxx/data/' #保存數(shù)據(jù)和配置文件的路徑 train_lmdb=path+'train_db' #訓(xùn)練數(shù)據(jù)LMDB文件的位置 val_lmdb=path+'val_db' #驗(yàn)證數(shù)據(jù)LMDB文件的位置 mean_file=path+'mean.binaryproto' #均值文件的位置 train_proto=path+'train.prototxt' #生成的訓(xùn)練配置文件保存的位置 val_proto=path+'val.prototxt' #生成的驗(yàn)證配置文件保存的位置 #編寫一個(gè)函數(shù),用于生成網(wǎng)絡(luò) def create_net(lmdb,batch_size,include_acc=False): #創(chuàng)建第一層:數(shù)據(jù)層。向上傳遞兩類數(shù)據(jù):圖片數(shù)據(jù)和對(duì)應(yīng)的標(biāo)簽 data, label = L.Data(source=lmdb, backend=P.Data.LMDB, batch_size=batch_size, ntop=2, transform_param=dict(crop_size=40,mean_file=mean_file,mirror=True)) #創(chuàng)建第二屋:卷積層 conv1=L.Convolution(data, kernel_size=5, stride=1,num_output=16, pad=2,weight_filler=dict(type='xavier')) #創(chuàng)建激活函數(shù)層 relu1=L.ReLU(conv1, in_place=True) #創(chuàng)建池化層 pool1=L.Pooling(relu1, pool=P.Pooling.MAX, kernel_size=3, stride=2) conv2=L.Convolution(pool1, kernel_size=3, stride=1,num_output=32, pad=1,weight_filler=dict(type='xavier')) relu2=L.ReLU(conv2, in_place=True) pool2=L.Pooling(relu2, pool=P.Pooling.MAX, kernel_size=3, stride=2) #創(chuàng)建一個(gè)全連接層 fc3=L.InnerProduct(pool2, num_output=1024,weight_filler=dict(type='xavier')) relu3=L.ReLU(fc3, in_place=True) #創(chuàng)建一個(gè)dropout層 drop3 = L.Dropout(relu3, in_place=True) fc4 = L.InnerProduct(drop3, num_output=10,weight_filler=dict(type='xavier')) #創(chuàng)建一個(gè)softmax層 loss = L.SoftmaxWithLoss(fc4, label) if include_acc: #在訓(xùn)練階段,不需要accuracy層,但是在驗(yàn)證階段,是需要的 acc = L.Accuracy(fc4, label) return to_proto(loss, acc) else: return to_proto(loss) def write_net(): #將以上的設(shè)置寫入到prototxt文件 with open(train_proto, 'w') as f: f.write(str(create_net(train_lmdb,batch_size=64))) #寫入配置文件 with open(val_proto, 'w') as f: f.write(str(create_net(val_lmdb,batch_size=32, include_acc=True))) if __name__ == '__main__': write_net()
通過上面這個(gè)文件的執(zhí)行,我們就會(huì)得到兩個(gè)配置文件:train.prototxt和val.prototxt,分別用于訓(xùn)練階段和驗(yàn)證階段。
圖片轉(zhuǎn)換成LMDB文件
這種方式生成配置文件,必須有個(gè)前提,就是要先把原始圖片轉(zhuǎn)換成LMDB文件才行。如果我們已經(jīng)把原始圖片做成了一個(gè)列表清單(txt文件,一行一張圖片),則可以不用LMDB格式作為輸入數(shù)據(jù),可以用ImageData作為數(shù)據(jù)源輸入,代碼如下:
# -*- coding: utf-8 -*- from caffe import layers as L,params as P,to_proto path='/home/xxx/data/' train_list=path+'train.txt' val_list=path+'val.txt' train_proto=path+'train.prototxt' val_proto=path+'val.prototxt' def create_net(img_list,batch_size,include_acc=False): data,label=L.ImageData(source=img_list,batch_size=batch_size,new_width=48,new_height=48,ntop=2, transform_param=dict(crop_size=40,mirror=True)) conv1=L.Convolution(data, kernel_size=5, stride=1,num_output=16, pad=2,weight_filler=dict(type='xavier')) relu1=L.ReLU(conv1, in_place=True) pool1=L.Pooling(relu1, pool=P.Pooling.MAX, kernel_size=3, stride=2) conv2=L.Convolution(pool1, kernel_size=53, stride=1,num_output=32, pad=1,weight_filler=dict(type='xavier')) relu2=L.ReLU(conv2, in_place=True) pool2=L.Pooling(relu2, pool=P.Pooling.MAX, kernel_size=3, stride=2) conv3=L.Convolution(pool2, kernel_size=53, stride=1,num_output=32, pad=1,weight_filler=dict(type='xavier')) relu3=L.ReLU(conv3, in_place=True) pool3=L.Pooling(relu3, pool=P.Pooling.MAX, kernel_size=3, stride=2) fc4=L.InnerProduct(pool3, num_output=1024,weight_filler=dict(type='xavier')) relu4=L.ReLU(fc4, in_place=True) drop4 = L.Dropout(relu4, in_place=True) fc5 = L.InnerProduct(drop4, num_output=7,weight_filler=dict(type='xavier')) loss = L.SoftmaxWithLoss(fc5, label) if include_acc: acc = L.Accuracy(fc5, label) return to_proto(loss, acc) else: return to_proto(loss) def write_net(): # with open(train_proto, 'w') as f: f.write(str(create_net(train_list,batch_size=64))) # with open(val_proto, 'w') as f: f.write(str(create_net(val_list,batch_size=32, include_acc=True))) if __name__ == '__main__': write_net()
即第一層由原來的Data類型,變成了ImageData類型,不需要LMDB文件和均值文件,但需要一個(gè)txt文件。
以上就是caffe的python接口生成配置文件學(xué)習(xí)的詳細(xì)內(nèi)容,更多關(guān)于caffe python接口生成配置文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
如何使用draw.io插件在vscode中一體化導(dǎo)出高質(zhì)量圖片
這篇文章主要介紹了draw.io插件在vscode中一體化導(dǎo)出高質(zhì)量圖片需要的工具是vscode,?draw.io擴(kuò)展,draw.io桌面版?、python,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒,需要的朋友可以參考下2022-08-08python3中dict.keys().sort()用不了的解決方法
本文主要介紹了python3中dict.keys().sort()用不了的解決方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12Python實(shí)現(xiàn)電腦喚醒后自動(dòng)拍照截屏并發(fā)郵件通知
這篇文章主要為大家詳細(xì)介紹了Python如何實(shí)現(xiàn)電腦喚醒后自動(dòng)拍照截屏并發(fā)郵件通知的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-01-01使用python serial 獲取所有的串口名稱的實(shí)例
今天小編就為大家分享一篇使用python serial 獲取所有的串口名稱的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07Pyqt清空某一個(gè)QTreeewidgetItem下的所有分支方法
今天小編就為大家分享一篇Pyqt清空某一個(gè)QTreeewidgetItem下的所有分支方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06