Caffe: Python interface: get output from layer X, given the output of layer Y

Created on 23 Sep 2014  Â·  7Comments  Â·  Source: BVLC/caffe

Hi,

If I have the output from layer X ('pool5' for example), and want to get the output of layer Y ('fc7' for example), what should I do?

I tried to solve this by my own, but I couldn't get it right and I guess it must be something very simple. Currently I was trying to do something with the "forward" function, but I could find the correct arguments.

Cheers,
Stratis

Most helpful comment

You can insert your features into the pool5 layer, then call forward with the start kwarg to begin computation at the next layer. (You can also use end to avoid extra computation beyond the last layer you want.)

net.blobs['pool5'].data[...] = your data
net.forward(start='fc6', end='fc7')
my data = net.blobs['fc7'].data

(Also note that the caffe-users list exists for usage questions.)

All 7 comments

You can insert your features into the pool5 layer, then call forward with the start kwarg to begin computation at the next layer. (You can also use end to avoid extra computation beyond the last layer you want.)

net.blobs['pool5'].data[...] = your data
net.forward(start='fc6', end='fc7')
my data = net.blobs['fc7'].data

(Also note that the caffe-users list exists for usage questions.)

Oh, thanx for the reply, I will try it out.

And also thanx for the list ;)

Ok, assuming that the prototxt files I edited are alright, when I run the following code I should get the same features feat_fc7 and for feat_fc7_new, after doing what you suggested. When I run the code, I get the same numbers for the positive elements. However, I also get negative elements, which where not there in the feat_fc7 (no negative elements at all in feat_fc7 actually). Obviously there is either a mistake when I get the "original" fc7 or when I reconstruct it via pool5. I guess it is the former, since having only positive values in the feature is kind of weird I suppose.

The code I run is

##
crop_height = 227
crop_width = 227

input_image = caffe.io.load_image(IMAGE_FILE)
input_image = caffe.io.resize_image(input_image, [crop_height, crop_width])
input_image = net_p5.preprocess('data', input_image)

batch = np.zeros( (1, input_image.shape[0], input_image.shape[1], input_image.shape[2]) )
batch[0, :, :, :] = input_image

# 1: Extract the features from fc7 directly
feat_fc7 = net_fc7_2.forward(data=batch)
feat_fc7 = feat_fc7['fc7']
feat_fc7 = np.reshape(feat_fc7, (feat_fc7.shape[0], -1))

# 2.1: First extract the features from pool5
feat_p5 = net_p5.forward(data=batch)
feat_p5 = feat_p5['pool5']

# 2.2: Then pass them to the new network and get features via pool5
net_fc7.blobs['pool5'].data[...] = feat_p5
net_fc7.forward(start='fc6', end='fc7')
feat_fc7_new = net_fc7.blobs['fc7'].data
feat_fc7_new = np.reshape(feat_fc7_new, (feat_fc7_new.shape[0], -1))

feat_fc7_new_pos = feat_fc7_new.copy()
feat_fc7_new_pos[feat_fc7_new_pos < 0] = 0

The plots I get from feat_fc7, feat_fc7_new and feat_fc7_new_pos are the following

feat
feat_new
feat_new_pos

This is because there is a Relu relu7 after fc7 that 0 negative values. The
first call run the network to the last layer, which is relu7, while the
second only runs until fc7.

On Wednesday, October 1, 2014, stratisgavves [email protected]
wrote:

Ok, assuming that the prototxt files I edited are alright, when I run the
following code I should get the same features feat_fc7 and for
feat_fc7_new, after doing what you suggested. When I run the code, I get
the same numbers for the positive elements. However, I also get negative
elements, which where not there in the feat_fc7 (no negative elements at
all in feat_fc7 actually). Obviously there is either a mistake when I get
the "original" fc7 or when I reconstruct it via pool5. I guess it is the
former, since having only positive values in the feature is kind of weird I
suppose.

The code I run is

crop_height = 227crop_width = 227

input_image = caffe.io.load_image(IMAGE_FILE)input_image = caffe.io.resize_image(input_image, [crop_height, crop_width])input_image = net_p5.preprocess('data', input_image)
batch = np.zeros( (1, input_image.shape[0], input_image.shape[1], input_image.shape[2]) )batch[0, :, :, :] = input_image

1: Extract the features from fc7 directlyfeat_fc7 = net_fc7_2.forward(data=batch)feat_fc7 = feat_fc7['fc7']feat_fc7 = np.reshape(feat_fc7, (feat_fc7.shape[0], -1))

2.1: First extract the features from pool5feat_p5 = net_p5.forward(data=batch)feat_p5 = feat_p5['pool5']

2.2: Then pass them to the new network and get features via pool5net_fc7.blobs['pool5'].data[...] = feat_p5net_fc7.forward(start='fc6', end='fc7')feat_fc7_new = net_fc7.blobs['fc7'].datafeat_fc7_new = np.reshape(feat_fc7_new, (feat_fc7_new.shape[0], -1))

feat_fc7_new_pos = feat_fc7_new.copy()feat_fc7_new_pos[feat_fc7_new_pos < 0] = 0

The plots I get from feat_fc7, feat_fc7_new and feat_fc7_new_pos are the
following

[image: feat]
https://cloud.githubusercontent.com/assets/1257485/4472761/2892fc96-494b-11e4-9efb-0c971b87456a.png
[image: feat_new]
https://cloud.githubusercontent.com/assets/1257485/4472760/2891a77e-494b-11e4-921f-bd08a27cd66e.png
[image: feat_new_pos]
https://cloud.githubusercontent.com/assets/1257485/4472759/28899d9a-494b-11e4-8e77-a53e8dd89f05.png

—
Reply to this email directly or view it on GitHub
https://github.com/BVLC/caffe/issues/1146#issuecomment-57438190.

Sergio

Of course, how dump... :S

Hi, I am trying to get the feature, but i fail. Could you tell me what is the variable 'net_p5' and 'net_fc7' in the article above?
Thank you very much!

Hi, I am trying to get the feature, but i fail. Could you tell me what is the variable 'net_p5' and 'net_fc7' in the article above?
Thank you very much!

Did you know how to get these two?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

serimp picture serimp  Â·  3Comments

malreddysid picture malreddysid  Â·  3Comments

weather319 picture weather319  Â·  3Comments

shiorioxy picture shiorioxy  Â·  3Comments

lixin7895123 picture lixin7895123  Â·  3Comments