6

Is there a simple way of renaming layers in a network by using the pycaffe interface?

I have looked through the net surgery example, but I cannot find an example of what I need.

For example, I would like to load a trained Caffe model and change the name of conv1 layer and its corresponding blob to new-conv1.

Igor Ševo
  • 5,459
  • 3
  • 35
  • 80
  • see: https://github.com/BVLC/caffe/blob/master/python/caffe/test/test_net_spec.py – Shai Jan 04 '16 at 07:58
  • 1
    Also the following question/answer may be what you need. http://stackoverflow.com/questions/35423309/how-reconstruct-the-caffe-net-by-using-pycaffe – Ray Apr 06 '16 at 09:43

1 Answers1

10

I don't know a direct way to do it, but here is a workaround:

Given a pretrained Caffe model my_model.caffemodel and its net architecture net.prototxt. Make a copy of net.prototxt (say net_new.prototxt), and change the name of conv1 layer to new-conv1 (you can change the names of bottom and top if you want).

import caffe
net_old = caffe.Net('net.prototxt','my_model.caffemodel',caffe.TEST)
net_new = caffe.Net('net_new.prototxt','my_model.caffemodel',caffe.TEST)
net_new.params['new-conv1'][0].data[...] = net_old.params['conv1'][0].data[...]  #copy filter across 2 nets
net_new.params['new-conv1'][1].data[...] = net_old.params['conv1'][1].data[...]  #copy bias
net_new.save('my_model_new.caffemodel')
Tu Bui
  • 1,660
  • 5
  • 26
  • 39