Kamil Saitov
Kamil Saitov

Reputation: 175

Feeding keras with one image (predicting)

I trained a model in Keras (binary image classification, retrained Xception model). Now if I feed it

model.predict(np.random.rand(1, 300, 300, 3))

I get output

array([[0.68225867, 0.3177413 ]], dtype=float32)

which is what i'm trying to get with real images. However, when I feed the real image like that:

from scipy.misc import imread,imresize
x=imread('processed_dataset/test/EM/bull_212.jpg',mode='RGB')
x=imresize(x,(300,300))
x=np.invert(x)
x=x.reshape(-1,300,300,3)
model.predict(x)

I always get the same output:

array([[1., 0.]], dtype=float32)

the model outputs [1., 0] regardless of the input image. It is the same if I feed the image this way:

img = image.load_img('processed_dataset/test/EM/bull_212.jpg', target_size=(img_width, img_height))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)


images = np.vstack([x])
model.predict(images, batch_size=1)

My thinking is: if feeding a np.random 'image' gives the desired result, the problem is how I feed the real images. How to do this properly so that it gives me the desired result?

Upvotes: 0

Views: 717

Answers (1)

Dr. Snoopy
Dr. Snoopy

Reputation: 56357

It seems you are not applying the normalization that was used to train the model, if you don't do it, then then inputs will be completely different and this will saturate neurons, producing inconsistent outputs.

Upvotes: 1

Related Questions