philipperemy/keract

Cannot find a way to feed a single image to InceptionV3

lamba92 opened this issue · 2 comments

My InceptionV3 layer has an input of [?, 250, 150, 3]. If I call get_activations() with an x of shape (250, 150, 3), Keras will complaint that you need 4 dimensions, not 3.
Fair enough, the model needs a dynamic batch dimension (wait, wasn't supposed to use that dimension for training only?).

But if I go for a reshape like np.reshape(image, (1,250, 150, 3)) the error becomes:

InvalidArgumentError:  You must feed a value for placeholder tensor 'input_4' with dtype float and shape [?,250,150,3]
	 [[node input_4 (defined at /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:3009) ]] [Op:__inference_keras_scratch_graph_83846]

Function call stack:
keras_scratch_graph

What's that about?

@lamba92 do you have a code snippet that I can execute to reproduce your error? Which version of TF and keras are you using?

I tried it and it worked well.

Model recognises the cat as tabby (57.99463987350464) and it looks correct.

tensorflow           2.1.0
Keras                2.3.1
from io import BytesIO

import requests
from PIL import Image
from keras.applications.inception_v3 import InceptionV3
from keras.applications.inception_v3 import decode_predictions
from keras.applications.inception_v3 import preprocess_input
from keras.preprocessing.image import img_to_array

import keract

model = InceptionV3()

url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Gatto_europeo4.jpg/250px-Gatto_europeo4.jpg'
response = requests.get(url)
image = Image.open(BytesIO(response.content))
image = image.crop((0, 0, 299, 299))
image = img_to_array(image)
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
image = preprocess_input(image)
yhat = model.predict(image)
label = decode_predictions(yhat)
label = label[0][0]
print('{} ({})'.format(label[1], label[2] * 100))  # a tabby is a cat!

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
activations = keract.get_activations(model, image)
keract.display_activations(activations)

image

image