2

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 
Community
  • 1
  • 1

1 Answers1

4

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

  • How's the future of caffe looking? – Shai Dec 28 '15 at 19:39
  • 3
    It looks pretty good to me. Documentation is practically nonexistant/scattered to the four corners of the galaxy if you're looking to do python implementations, but caffe itself is very nice. GoogLeNet was implemented in it pretty succinctly, as well as several other great convnets. The learning curve is pretty steep though. – Tyler Balsam Dec 28 '15 at 20:27