Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the solver parameters in Caffe through pycaffe

How can I change the solver parameter in Caffe through pycaffe?

E.g. right after calling solver = caffe.get_solver(solver_prototxt_filename) I would like to change the solver's parameters (learning rate, stepsize, gamma, momentum, base_lr, power, etc.), without having to change solver_prototxt_filename.

like image 695
Franck Dernoncourt Avatar asked Aug 05 '15 05:08

Franck Dernoncourt


1 Answers

Maybe you can create a temporary file.

First of all, load your solver parameters with

from caffe.proto import caffe_pb2
from google.protobuf import text_format
solver_config = caffe_pb2.SolverParameter()
with open('/your/solver/path') as f:
    text_format.Merge(str(f.read()), solver_config)

You can modify any solver parameter just setting the desired value in solver_config (e.g. solver_config.test_interval = 15). Then, it's just creating a temp file and load your solver from it:

new_solver_config = text_format.MessageToString(solver_config)
with open('temp.prototxt', 'w') as f:
    f.write(new_solver_config) 
solver = caffe.get_solver('temp.prototxt')
solver.step(1)
like image 194
Flavio Ferrara Avatar answered Oct 05 '22 12:10

Flavio Ferrara