1

I use the follwoing code to load the net and set up it, the parameters of layers are stored in deploy.prototxt.

net = caffe.Net(deploy.prototxt, caffemodel, caffe.TEST)

However, what I want to do is to modify the parameters (e.g. kernel_size, or pad, etc.) of layers dynamically instead of modifying the prototxt file and reload it. Is there any way to do so?

zbqv
  • 79
  • 8

3 Answers3

0

You can write your own get/set methods and expose them to python. In layer.hpp:

virtual float GetParameter(const std::string param_name) {return -1;}
virtual void SetParameter(const std::string param_name, float val) {}

Then redefine these methods in layers where you would like to get/set parameters dynamically.

The last step is to expose the methods to python. In _caffe.cpp add this for bp::class_<Layer...:

.def("get_parameter", &Layer<Dtype>::GetParameter)
.def("set_parameter", &Layer<Dtype>::SetParameter)
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
sbond
  • 168
  • 1
  • 8
  • Thanks for your answer. I have took another solution by directory change the .prototxt file dynamically, I think your solution will be more flexible. – zbqv Apr 11 '17 at 01:19
0

I would suggest changing the way you think of this problem. What does it depend on for what you mentioned "dynamically modified parameter"? A most commonly used variable (which I was facing) is the current iteration times. For example I want to reduce the parameter value in every 10000 times. Based on that, in the layer when you are using that parameter, apply the function to modify it. That's the same as modifying the prototxt file.

To get the iteration times in specific layer, I just put one other's solution here. It is quite straightforward and could probably reduce your work load significantly compared to modifying prototxt file. Hopefully you could get inspired from this solution and apply it in your case.

https://stackoverflow.com/a/38386603/6591990

Anthony Yu
  • 26
  • 3
0


[Solution at the end of the post]
I needed to finetune a model and hence wanted to change the lr_mult parameters of individual layers programmatically. My search for help started from the title of this thread and thankfully ended in the below mentioned link titled 'How to modify a prototxt programmatically?'.
https://github.com/BVLC/caffe/issues/4878
The parameters can be accessed and modified after loading the model definition prototxt file in google/protobuf in text_format. The modified protobuf can be written as a file.

sbanik
  • 1
  • 1