Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename Caffe layers through pycaffe

Tags:

caffe

pycaffe

Is there a simple way of renaming layers in a caffe 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.

like image 437
Igor Ševo Avatar asked Jan 03 '16 21:01

Igor Ševo


1 Answers

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')
like image 118
Tu Bui Avatar answered Nov 07 '22 05:11

Tu Bui