Resize Kernel-Blobs

47 views
Skip to first unread message

Martin S.

unread,
May 20, 2017, 7:44:39 AM5/20/17
to Caffe Users
Hi,
i tried to use an existing net architecture with caffemodel, which was trained with grayscale images ( 1 inputchannel) with my RGB-images(3 inputchannels).
Therefore i have to modify the kernels of the first layer.

The 64 kernels of the first layer are 1x3x3 tensors.
I want to replicate the 3x3-matrix, such that one kernel is 3x3x3 instead of 1x3x3.
Unfortunately i was neither able to resize conv_1_weights (ValueError: cannot resize this array: it does not own its data
) nor to use np.copy(a) (ValueError: could not broadcast input array from shape (3,3,3) into shape (1,3,3)).

import numpy as np
import caffe

net = caffe.Net('./phseg_v5.prototxt', '../../Net.caffemodel', caffe.TEST)

conv_1_weights = net.params['conv_d0a-b'][0].data
conv_1_biases = net.params['conv_d0a-b'][1].data

for i in range(conv_1_weights.shape[0]):
  a = np.append(conv_1_weights[i], conv_1_weights[i], axis=0)
  a = np.append(conv_1_weights[i], a, axis=0)
  net.params['conv_d0a-b'][0].data[i] = np.copy(a)


Is there a way to resize the kernels, such that i get a correct .caffemodel for my purpose?

Thank you,
Martin

Martin S.

unread,
May 22, 2017, 3:49:15 AM5/22/17
to Caffe Users
I think i managed to do so:
I loaded two nets, one with a single input channel and one with three input channels(without a caffemodel file).
Then i transformed the kernels from the first layer as i written above and simply copied the kernels from each conv- and upconv-layer.
This should be all parameters of the net, since no fully connected layer is used.

Thank you,
Martin

Przemek D

unread,
May 22, 2017, 9:31:16 AM5/22/17
to Caffe Users
Simpler way would be to use numpy's stacking, if the only thing you want to do is replicate the 1x3x3 filter in 3 channels. You indeed need to load two nets, let's call the grayscale one original_net and RGB my_net. Then you would have something like:
w = original_net.params['conv_d0a-b'][0].data
my_net
.params['conv_d0a-b'][...] = np.vstack((w,w,w))
Reply all
Reply to author
Forward
0 new messages