rename a layer?

31 views
Skip to first unread message

G Voysey

unread,
Aug 30, 2018, 5:42:23 PM8/30/18
to Caffe Users
I have an existing *.prototext and *.caffemodel. 

Unfortunately, the final layer's name is "class", which causes all sorts of trouble with pycaffe (because "class" is a reserved word).

I'd like to rename this to "class_".

Here's how i'm trying it now:
  def convert(prototext, new_prototext, model, newmodel):
      net_old
= caffe.Net(prototext, model, caffe.TEST)
      net_new
= caffe.Net(new_prototext, model, caffe.TEST)
      net_new
= update_key(net_new)

     
for i in range(0,2):
          net_new
.params['class_'][i].data[...] = net_old.params['class'][i].data[...]
      net_new
.save(newmodel)


 
def update_key(net):
      net
.params['class_'] = net.params['class']
      net
.params.pop('class')
     
return net

This executes cleanly.  But, the newly saved prototext has not changed the name of "class" to "class_".  I can inspect the state of net_new at the end of convert() and determine that the key in net_new.params is really different... what's going on?

Przemek D

unread,
Aug 31, 2018, 3:33:32 AM8/31/18
to Caffe Users
I don't think you can do this with your net instantiated. Editing the params list does not modify the layers - that information is held in layers and _layer_names, but I'm not sure right now if you can modify them.
The simplest approach here would be to manually change "class" to "class_" in the prototext, and then continue with the weight transplant like you already do. If you want to automate this though, I suppose you should work with the NetSpec. Unfortunately the documentation is scarce, maybe this snippet will guide you onto the right direction.
Reply all
Reply to author
Forward
0 new messages