I want to add a python layer that calculates probability matrix H
for INfoGainLoss Layer
on-the-fly. I have already wrote the program for creating this matrix by calculating probabilities of each class of one image and saving it into .binaryproto
file. I really appreciate if you give me some hints that how can I write a python layer that creates this matrix and send it as third parameter to InfoGainLoss layer? I drew a schematic here, Is it correct? If yes, how to write the python layer for this? I already read some codes online but still have confusion on setup
,reshape
,forward
functions.
Asked
Active
Viewed 190 times
-1
1 Answers
1
Your Python layer is quite similar to an input layer: It has no backward propagation, which makes it easy to implement. See this thread for more details.
Your layer expects "label"
bottom, and produced "H"
matrix as top:
layer { name: "classWeightH" bottom: "label" top: "H" type: "Python" python_param { module: # file name where python code is layer: "classWeightHLayer" } }
The python code should look something like:
import sys, os, numpy as np
sys.path.insert(0, os.environ['CAFFE_ROOT']+'/python')
import caffe
class classWeightHLayer(caffe.Layer):
def setup(self,bottom,top):
assert len(bottom)==1, "expecting exactly one input"
assert len(top)==1, "producing exactly one output"
# you might want to get L - the number of labels as a parameter...
def reshape(self,bottom,top):
top[0].reshape(1,1,L,L) # reshape the output to the size of H
def forward(self,bottom,top):
labels = bottom[0].data
H = np.zeros((1,1,L,L), dtype='f4')
# do your magic here...
top[0].data[...] = H
def backward(self, top, propagate_down, bottom):
# no back-prop for input layers
pass
-
1Thank you very much for all your supports and helps here. You are always helpful. – S.EB May 22 '17 at 10:38