0

I'm trying to use a python script to extract a feature value using a pre-trained network. Here's my script:

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import caffe

caffe.set_device(0)
caffe.set_mode_gpu()
net=caffe.Net('blobs-Test-Eval.prototxt', 'best_snapshot_iter_4553.caffemodel', caffe.TEST)
im=np.array(Image.open('Test/D_4051.png'))
im_input = im[np.newaxis, np.newaxis, :, :]
net.blobs['data'].reshape(*im_input.shape)
net.blobs['data'].data[...]=im_input
out=net.forward()
print out['fc8']

Now the image D_4051 should return a score somewhere between 0.002 - 0.006, but the python script returns [[ 2.99469399]] for ANY image from my test set. It's clear my code isn't doing what I want it to do, but from the documentation and examples I have found, I'm unsure what is wrong.

Thanks!

SHB11
  • 375
  • 5
  • 14

1 Answers1

1

When you use python interface, you must pay attention to that the format of test image should be the same as the images you use for training. For example, pixel values are [0..1] or [0..256], RGB order or BGR order, mean subtraction and so on. Only if you preprocess the test image exact the same as the training images, you can get reasonable results.

Some useful examples: http://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/00-classification.ipynb

kli_nlpr
  • 894
  • 2
  • 11
  • 25
  • Thanks for the reply. The image I'm running it with at the moment is actually one of the test images which had it's fc8 feature successfully extracted from the command line interface. I'm just trying to do the same thing in python 1 image at a time. From what I can tell all of the pre-processing is taken care of in the .prototxt file in the first layer: – SHB11 Jan 27 '16 at 22:16
  • layer { name: "data" type: "ImageData" top: "data" top: "label" include { phase: TEST } transform_param { mirror: false crop_size: 96 mean_file: "trainingMean.binary" } image_data_param { source: "Test.txt" batch_size: 1 } } – SHB11 Jan 27 '16 at 22:16