说明:
caffe通过配置文件prototxt来描设置训练参数,通过Python接口来生成solver配置文件比较简单。
步骤:
1.生成配置文件
touch create_solver_prototxt.py
spyder create_solver_prototxt.py
1 #!/usr/bin/env python 2 # coding: utf-8 3 """ 4 yuandanfei Editor 5 6 This is a temporary script file. 7 """ 8 path = '/home/yuandanfei/work/caffe/mnist/out/' #solver prototxt path 9 solver_file = path + 'solver.prototxt' 10 11 sp = {} 12 sp['train_net'] = '"' + path + 'train.prototxt"' #train prototxt path 13 sp['test_net'] = '"' + path + 'test.prototxt"' #test prototxt path 14 15 sp['test_interval'] = '938' #test interval = train sample numbers/train batch numbers 16 sp['test_iter'] = '313' #test iter = test sample numbers/test batch numbers 17 sp['max_iter'] = '93800' #train numbers = max iter/test interval 18 19 sp['lr_policy'] = '"step"' #learning rate change strategy = base_lr*gamma^(floor(iter/stepsize)) 20 sp['base_lr'] = '0.001' #base learning rate 21 sp['gamma'] = '0.1' #learning rate change index 22 sp['stepsize'] = '31267' #learning rate change frequece = max iter/stepsize 23 sp['momentum'] = '0.9' #learnig momentum 24 sp['weight_decay'] = '0.0005' #weight decay 25 26 sp['display'] = '938' #display log interval = display/test interval 27 sp['snapshot'] = '9380' #save model interval = snapshot/test interval 28 sp['snapshot_prefix'] = '"snapshot"' #save model prefix 29 30 sp['solver_type'] = 'SGD' #optimization algorithm 31 sp['solver_mode'] = 'GPU' #use GPU 32 33 def write_solver(): 34 with open(solver_file, 'w') as f: 35 for key, value in sorted(sp.items()): 36 if not(type(value) is str): 37 raise TypeError('All solver parameters must be strings') 38 f.write('%s: %s ' % (key, value)) 39 40 if __name__ == '__main__': 41 write_solver()
参考资料:
https://www.cnblogs.com/denny402/p/5679154.html
https://www.cnblogs.com/denny402/p/5074049.html
https://www.cnblogs.com/denny402/p/5074212.html