I am just trying to use pre-trained vgg16 to make prediction in Keras like this.
from scipy import ndimage
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
im = scipy.misc.imread("cat_dog/validation/cats/cat.1362.jpg").astype(np.float32)
im = scipy.misc.imresize(im, (224, 224)).astype(np.float32)
#im /= 255.0
#im = im - np.mean(im, axis=2, keepdims=True)
im = np.expand_dims(im, axis=0)
im = preprocess_input(im)
out = vgg16_model.predict(im)
np.argmax(out)
It seemed that im /= 255.0 give very bad prediction. I commented it out and it started making good prediction. I also added preprocess_input(...) but that doesn't seem to affect prediction for the few random trials I did.
The question is that according to this great blog:
https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html
under "Using the bottleneck features of a pre-trained network: 90% accuracy in a minute", pre-trained VGG16 is in a transfer learning context. And if you look at this gist, you see this line of code:
def save_bottlebeck_features():
datagen = ImageDataGenerator(rescale=1. / 255)
etc.
The preprocessing of input seemed to be 1/255.0 during caching of features from the last conv layer. This is sort of puzzling. I further also looked up how preprocess_input(...) is defined in the code I have, and found for 'tf':
x /= 127.5
x -= 1.
Which you can check here.