use pre-trained layers

40 views
Skip to first unread message

Farhad Dalirani

unread,
Aug 25, 2019, 12:44:20 PM8/25/19
to Caffe Users
I want to use the first five layers plus the RoI layer of caffenet in fast R-CNN(https://github.com/rbgirshick/fast-rcnn). And then add my new neural branch to it.
But I don't know how to do that.


Corvus Corax

unread,
Aug 26, 2019, 2:01:08 AM8/26/19
to Caffe Users

You need to make sure the layers you want to keep are named and sized identically in each network definition.
Then you load both networks in python, and copy the weights over from one to the other, something like this:


rcnn = caffe.Net('fastrcnn.prototxt',caffe.TEST,weights='fastrcnn.caffemodel')
mine
= caffe.Net('mynetwork.prototxt',caffe.TRAIN)

def copy_weights(from_net,to_net):
   
for key in from_net.params.keys():
       
try:            
           
for i in range(len(from_net.params[key])):
               
#print i,"from", np.array(from_net.params[key][i].shape), "to", np.array(to_net.params[key][i].shape)
                to_net
.params[key][i].data.flat = from_net.params[key][i].data.flat
       
except Exception as e:
           
print("copying data for layer ",key, " failed with ",e)
           
if key in to_net.params.keys():
               
if (len(from_net.params[key])>len(to_net.params[key])):
                   
print("target layer has no bias weights")
               
else:
                   
print("copying from shape",np.array(from_net.params[key][i].shape), "to", np.array(to_net.params[key][i].shape))
           
else:
               
print "layer not defined in destination network"


copy_weights
(rcnn,mine)



Reply all
Reply to author
Forward
0 new messages