caffe的python接口生成solver文件詳解學習
solver.prototxt的文件參數(shù)設(shè)置
caffe在訓練的時候,需要一些參數(shù)設(shè)置,我們一般將這些參數(shù)設(shè)置在一個叫solver.prototxt的文件里面,如下:
base_lr: 0.001
display: 782
gamma: 0.1
lr_policy: “step”
max_iter: 78200
momentum: 0.9
snapshot: 7820
snapshot_prefix: “snapshot”
solver_mode: GPU
solver_type: SGD
stepsize: 26067
test_interval: 782
test_iter: 313
test_net: “/home/xxx/data/val.prototxt”
train_net: “/home/xxx/data/proto/train.prototxt”
weight_decay: 0.0005
有一些參數(shù)需要計算的,也不是亂設(shè)置。
假設(shè)我們有50000個訓練樣本,batch_size為64,即每批次處理64個樣本,那么需要迭代50000/64=782次才處理完一次全部的樣本。我們把處理完一次所有的樣本,稱之為一代,即epoch。所以,這里的test_interval設(shè)置為782,即處理完一次所有的訓練數(shù)據(jù)后,才去進行測試。如果我們想訓練100代,則需要設(shè)置max_iter為78200.
同理,如果有10000個測試樣本,batch_size設(shè)為32,那么需要迭代10000/32=313次才完整地測試完一次,所以設(shè)置test_iter為313.
學習率變化規(guī)律我們設(shè)置為隨著迭代次數(shù)的增加,慢慢變低??偣驳?8200次,我們將變化lr_rate三次,所以stepsize設(shè)置為78200/3=26067,即每迭代26067次,我們就降低一次學習率。
生成solver文件
下面是生成solver文件的python代碼,比較簡單:
# -*- coding: utf-8 -*- """ Created on Sun Jul 17 18:20:57 2016 @author: root """ path='/home/xxx/data/' solver_file=path+'solver.prototxt' #solver文件保存位置 sp={} sp['train_net']=‘“'+path+'train.prototxt”' # 訓練配置文件 sp['test_net']=‘“'+path+'val.prototxt”' # 測試配置文件 sp['test_iter']='313' # 測試迭代次數(shù) sp['test_interval']='782' # 測試間隔 sp['base_lr']='0.001' # 基礎(chǔ)學習率 sp['display']='782' # 屏幕日志顯示間隔 sp['max_iter']='78200' # 最大迭代次數(shù) sp['lr_policy']='“step”' # 學習率變化規(guī)律 sp['gamma']='0.1' # 學習率變化指數(shù) sp['momentum']='0.9' # 動量 sp['weight_decay']='0.0005' # 權(quán)值衰減 sp['stepsize']='26067' # 學習率變化頻率 sp['snapshot']='7820' # 保存model間隔 sp['snapshot_prefix']=‘"snapshot"' # 保存的model前綴 sp['solver_mode']='GPU' # 是否使用gpu sp['solver_type']='SGD' # 優(yōu)化算法 def write_solver(): #寫入文件 with open(solver_file, 'w') as f: for key, value in sorted(sp.items()): if not(type(value) is str): raise TypeError('All solver parameters must be strings') f.write('%s: %s\n' % (key, value)) if __name__ == '__main__': write_solver()
執(zhí)行上面的文件,我們就會得到一個solver.prototxt文件,有了這個文件,我們下一步就可以進行訓練了。
當然,如果你覺得上面這種鍵值對的字典方式,寫起來容易出錯,我們也可以使用另外一種比較簡便的方法,沒有引號,不太容易出錯,如下:
簡便的方法
# -*- coding: utf-8 -*- from caffe.proto import caffe_pb2 s = caffe_pb2.SolverParameter() path='/home/xxx/data/' solver_file=path+'solver1.prototxt' s.train_net = path+'train.prototxt' s.test_net.append(path+'val.prototxt') s.test_interval = 782 s.test_iter.append(313) s.max_iter = 78200 s.base_lr = 0.001 s.momentum = 0.9 s.weight_decay = 5e-4 s.lr_policy = 'step' s.stepsize=26067 s.gamma = 0.1 s.display = 782 s.snapshot = 7820 s.snapshot_prefix = 'shapshot' s.type = “SGD” s.solver_mode = caffe_pb2.SolverParameter.GPU with open(solver_file, 'w') as f: f.write(str(s))
訓練模型(training)
如果不進行可視化,只想得到一個最終的訓練model, 那么代碼非常簡單,如下 :
import caffe caffe.set_device(0) caffe.set_mode_gpu() solver = caffe.SGDSolver('/home/xxx/data/solver.prototxt') solver.solve()
以上就是caffe的python接口生成solver文件詳解學習的詳細內(nèi)容,更多關(guān)于caffe python接口生成solver文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python利用代理ip實現(xiàn)自動化爬蟲任務(wù)管理
本文主要介紹了Python利用代理ip實現(xiàn)自動化爬蟲任務(wù)管理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06詳解用python -m http.server搭一個簡易的本地局域網(wǎng)
這篇文章主要介紹了詳解用python -m http.server搭一個簡易的本地局域網(wǎng),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09Python利用邏輯回歸模型解決MNIST手寫數(shù)字識別問題詳解
這篇文章主要介紹了Python利用邏輯回歸模型解決MNIST手寫數(shù)字識別問題,結(jié)合實例形式詳細分析了Python MNIST手寫識別問題原理及邏輯回歸模型解決MNIST手寫識別問題相關(guān)操作技巧,需要的朋友可以參考下2020-01-01python+numpy+matplotalib實現(xiàn)梯度下降法
這篇文章主要為大家詳細介紹了python+numpy+matplotalib實現(xiàn)梯度下降法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08