Tyler Balsam
Tyler Balsam

Reputation: 61

Pycaffe: How to create custom weights in a python layer?

I've been searching the net and the caffe source code for a while without any solutions to speak of, but in a custom application neural net, I am building a few custom layers in python. Forward passes and backward passes are functionally working well, and I can create custom weight parameters in my setup routine, but try as I might I cannot get caffe to set up "official" weights for my layer. This would of course allow better snapshotting, easier solver implementation, etc.

Any idea what I am missing here?

[EDIT: Code from layer shown below. Removed some things for brevity. The purpose of this layer is to add color to the flattened, activated filters from a convolutional layer]

def setup(self, bottom, top):
    global weights
    self.weights = np.random.random((CHANNELS))

def reshape(self, bottom, top):
    top[0].reshape(1,2*XDIM,2*YDIM)

def forward(self, bottom, top):
    arrSize = bottom[0].data.shape
    #Note: speed up w/ numpy ops for this later...
    for j in range(0, 2*arrSize[1]):
            for k in range(0, 2*arrSize[2]):
                    # Set hue/sat from hueSat table.
                    top[0].data[0,j,k] = self.weights[bottom[0].data[0,int(j/2),int(k/2)]]*239

def backward(self, top, propagate_down, bottom):
    diffs = np.zeros((CHANNELS))
    for i in range(0,300):
            for j in range(0,360):
                    diffs[bottom[0].data[0,i/2,j/2]] = top[0].diff[0,i,j]

    #stand in for future scaling
    self.weights[...] += diffs[...]/4 

Upvotes: 2

Views: 2233

Answers (1)

Tyler Balsam
Tyler Balsam

Reputation: 61

It's me from the future! Here's how to solve your question:

Recently blob adding was implemented Python in Caffe. Here's an example layer that does that:

class Param(caffe.Layer):
    def setup(self, bottom, top):
        self.blobs.add_blob(1,2,3)
        self.blobs[0].data[...] = 0

    def reshape(self, bottom, top):
        top[0].reshape(10)

    def forward(self, bottom, top):
        print(self.blobs[0].data)
        self.blobs[0].data[...] += 1

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

To access the diffs, just use self.blobs[0].diff[...] and you'll be all set. The solver will take care of the rest. For more info, see https://github.com/BVLC/caffe/pull/2944

Upvotes: 4

Related Questions