a new layer with weights?

24 views
Skip to first unread message

izhang...@gmail.com

unread,
Jun 3, 2017, 10:51:38 PM6/3/17
to Caffe Users
Hey guys, I am trying to add a new layer, with weights, I have successfully add a new layer(basically do nothing just copy data now), but having trouble in how to add weight parameter into this new layer. any idea? thanks in advance

Hieu Do Trung

unread,
Jun 4, 2017, 10:37:01 PM6/4/17
to Caffe Users
Take a look at network surgery, below is an example for adding weights to a convolution layer:
https://github.com/BVLC/caffe/blob/master/examples/net_surgery.ipynb


ksize = net.params['conv'][0].data.shape[2:]
# make Gaussian blur
sigma = 1.
y, x = np.mgrid[-ksize[0]//2 + 1:ksize[0]//2 + 1, -ksize[1]//2 + 1:ksize[1]//2 + 1]
g = np.exp(-((x**2 + y**2)/(2.0*sigma**2)))
gaussian = (g / g.sum()).astype(np.float32)
net.params['conv'][0].data[0] = gaussian
# make Sobel operator for edge detection
net.params['conv'][0].data[1:] = 0.
sobel = np.array((-1, -2, -1, 0, 0, 0, 1, 2, 1), dtype=np.float32).reshape((3,3))
net.params['conv'][0].data[1, 0, 1:-1, 1:-1] = sobel  # horizontal
net.params['conv'][0].data[2, 0, 1:-1, 1:-1] = sobel.T  # vertical
show_filters(net)
Reply all
Reply to author
Forward
0 new messages