0

I would like to obtain the output of the 6th layer of a pre-built caffe model and train an SVM on top of it. My intention is to build a custom image classifier, where the user can create custom image classes, and classify input images among those classes, instead of the imagenet classes.Here is the pseudo code:

#input
file='cat.jpg'
image=caffe.io.load_image(file)

#model
net = caffe.Classifier('deploy.prototxt','model.caffemodel')

#compute activation at layer 6 --- Need help here. Will net.forward help? will the activation be retained? 

#extract features from layer 6:

features = net.blobs['fc6'].data[4][:,0, 0]


#SVM
category=svm.predict(features)
print get_category_name(category)
4

1 回答 1

5

您应该使用Net类,而不是Classifier. 因此,您只需要调用net.forward().

需要注意两点:

  1. 预处理您的输入图像。请参阅此处Transformer的课程以供参考。
  2. 如果您仅使用

    features = net.blobs['fc6'].data
    

    您的数组将被下一次forward()调用覆盖。确保您正在执行深层复制,例如

    features = net.blobs['fc6'].data.copy()
    
于 2015-10-05T16:23:42.207 回答