I'm using the VGG19 model from keras application. I was expecting the image to be scaled to [-1, 1]
but instead, it seems that preprocess_input
is doing something else.
To preprocess the input, I use the following 2 lines to first load the image and then scale it:
from keras.preprocessing import image
from keras.applications.vgg19 import preprocess_input
img = image.load_img("./img.jpg", target_size=(256, 256))
img = preprocess_input(np.array(img))
print(img)
>>> array([[[151.061 , 138.22101, 131.32 ],
... ]]]
The output seems to be in the [0,255] interval, however, original 255s were mapped to values around 151 (Likely centering). What is the input that VGG actually requires? I thought it should be in [-1,1] by looking at the source code (for mode='tf'
). Is it pretty flexible and I can use any kind of scaling that I want? (I'm using VGG for extracting mid level features - Conv4 block).
When looking at the source code of preprocess_input
I see:
...
if mode == 'tf':
x /= 127.5
x -= 1.
return x
...
which suggests that for tensorflow backend (which is what keras is using), it should be scaled to [-1,1].
What I need to do is to create a function restore_original_image_from_array()
which will take the img
and reconstruct the original image that I fed in. The problem is I'm not sure how the scaling happens for VGG19.
So in short I would like to do:
img = image.load_img("./img.jpg", target_size=(256, 256))
scaled_img = preprocess_input(np.array(img))
restore_original_image_from_array(scaled_img) == np.array(img)
>>> True