I want go generate the output of 'fc6' layer of Caffenet for my image and use it as feature vector. But, when I compare the output of two different images, I get exactly the same output. Please, tell me what is the issue. I have attached for your reference.
I am savingnet.blobs['fc6'].data[0]
import numpy as np
import matplotlib.pyplot as plt
import time
from array import array
import sys
caffe_root = '/home/iplab_apu/Desktop/caffe-master/'
sys.path.insert(0,caffe_root+'python')
import caffe
caffe.set_mode_cpu()
model_def = caffe_root + 'models/bvlc_reference_caffenet/deploy.prototxt'
model_weights = caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'
net = caffe.Net(model_def, # defines the structure of the model
model_weights, # contains the trained weights
caffe.TEST) # use test mode (e.g., don't perform dropout)
# load the mean ImageNet image (as distributed with Caffe) for subtraction
mu = np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy')
print mu
mu = mu.mean(1).mean(1) # average over pixels to obtain the mean (BGR) pixel values
print 'mean-subtracted values:', zip('BGR', mu)
net.blobs['data'].reshape(1, # batch size
3, # 3-channel (BGR) images
227, 227) # image size is 227x227
# create transformer for the input called 'data'
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1)) # move image channels to outermost dimension
transformer.set_mean('data', mu) # subtract the dataset-mean value in each channel
transformer.set_raw_scale('data', 255) # rescale from [0, 1] to [0, 255]
transformer.set_channel_swap('data', (2,1,0)) # swap channels from RGB to BGR
# set the size of the input (we can skip this if we're happy
# with the default; we can also change it later, e.g., for different batch sizes)
image = caffe.io.load_image('/home/iplab_apu/Desktop/ankit/drive-download-20160823T093738Z/ISBI2016_ISIC_Part1_Training_Data/ISIC_0000001.jpg')
image = caffe.io.resize(image, (227,227))
transformed_image=[]
transformed_image = transformer.preprocess('data', image)
net.blobs['data'].data[...] = transformed_image
t=time.clock()
output = net.forward()
t2=time.clock()
output_prob = output['prob'][0]
np.save('feature_vec_arr_fc6_1.npy',net.blobs['fc6'].data[0])
When you call
output = net.forward()
it replaces _all_ layers by the input, hence your data gets overwritten, basically making your call to
net.blobs['data'].data[...] = transformed_image
redundant.
You should replace those lines with:
output = net.forward(data=np.asarray(transformed_image))
This should work. Please refer to the caffe-users group for further queries.
It gave the following error:
Traceback (most recent call last):
File "compare.py", line 52, in <module>
output = net.forward(data=np.asarray(transformed_image))
File "/home/iplab_apu/Desktop/caffe-master/python/caffe/pycaffe.py", line 118, in _Net_forward
raise Exception('Input is not batch sized')
Exception: Input is not batch sized
Please set the batch size to 1 in deploy.prototxt.
Still the same error
Apologies, replace the line mentioned earlier with:
output = net.forward(data=np.asarray([transformed_image]))
Thats okay. It runs but generates the same output of 'fc6' layer for two different images.
Replace your line:
np.save('feature_vec_arr_fc6_1.npy',net.blobs['fc6'].data[0])
with:
np.save('feature_vec_arr_fc6_1.npy',output['fc6'].data[0])
And the call to forward() with forward_all(). For further queries, please use the Google group.
Yes, I would use Google group but just to update, output does not have 'fc6' key as it only has 'prob' key in its dictionary.
Thanks :) I'll go through the code.
But, why is it thatnet.blobs['fc6'].data[0] gives the same value for two different images?
I think it is relevant to this forum because data blobs are supposed to give the output of layer which is supposed to be different for different images.
Please ask usage questions on the mailing list.
From https://github.com/BVLC/caffe/blob/master/CONTRIBUTING.md:
_Please do not post usage, installation, or modeling questions, or other requests for help to Issues._
Use the caffe-users list instead. This helps developers maintain a clear, uncluttered, and efficient view of the state of Caffe.
Most helpful comment
But, why is it that
net.blobs['fc6'].data[0]gives the same value for two different images?I think it is relevant to this forum because data blobs are supposed to give the output of layer which is supposed to be different for different images.