I'm trying to manually assign new weights to my pytorch model. I can assign new weights like this:
import scipy.io as sio
import torch
caffe_params = sio.loadmat('export_conv1_1.mat')
net.conv1_1.weight = torch.nn.Parameter(torch.from_numpy(caffe_params['w']))
net.conv1_1.bias = torch.nn.Parameter(torch.from_numpy(caffe_params['b']))
caffe_params = sio.loadmat('export_conv2_1.mat')
net.conv2_1.weight = torch.nn.Parameter(torch.from_numpy(caffe_params['w']))
net.conv2_1.bias = torch.nn.Parameter(torch.from_numpy(caffe_params['b']))
Since I have a lot of layers I don't want to manually assign every layer via it's name. Instead I want rather to loop over a list of layer names and assign them automatically. Something like this:
varList = ['conv2_1','conv2_2']
for name in varList:
caffe_params = sio.loadmat(rootDir + 'export_' + name + '.mat')
setattr(net, name + '.weight' ,torch.nn.Parameter(torch.from_numpy(caffe_params['w'])))
setattr(net, name + '.bias' ,torch.nn.Parameter(torch.from_numpy(caffe_params['b'])))
Unfortunately this doesn't work. I guess setattr
doesn't work with either pytorch weigths or with attributes of type layername.weight
, which means the attribute to assign has depth 2 with respect to net
.