Build a custom python layer in caffe to transpose a 2D blob.

257 views
Skip to first unread message

Vijetha Gattupalli

unread,
Apr 17, 2017, 4:07:30 PM4/17/17
to Caffe Users
Hi all, 

I need to perform the transpose operation on a blob(2D matrix) in order to implement a loss function. 
In order to do that, I chose to implement the layer in python. 

Below is the code for my layer.

import caffe
import numpy as np

class transpose(caffe.Layer):

    def setup(self, bottom, top):
        assert len(bottom) == 1,            'requires a single layer.bottom'
        assert bottom[0].data.ndim == 2,    'requires matrix data'
        assert len(top) == 1,               'requires a single layer.top'

    def reshape(self, bottom, top):
        top[0].reshape((bottom[0].data.shape[1], bottom[0].data.shape[0]))

    def forward(self, bottom, top):
        top[0].data = np.transpose(bottom[0].data)

    def backward(self, top, propagate_down, bottom):
        pass

It is throwing the error "terminate called after throwing an instance of 'boost::python::error_already_set' " at Reshape() method. 

Can someone throw some light on what I am doing wrong?

Thank you, 
Vijetha. 

Abhay Mittal

unread,
Oct 15, 2017, 10:04:48 PM10/15/17
to Caffe Users
Hi, were you able to fix it? I also need a layer for transpose and would be great if you could provide me with yours.
Reply all
Reply to author
Forward
0 new messages