Keras version: 2.0.5
Tensorflow Version: 1.2
Python: 3.5
OS: Windows 10
Trying to use a pretrained InceptionV3 model using tensors as inputs, I'm getting two different results when using model.predict() on the raw numpy array and when using sess.run([model.output], ...).
An example of the problem:
import tensorflow as tf
from keras.preprocessing.image import load_img, img_to_array
from keras.applications.inception_v3 import InceptionV3, decode_predictions, preprocess_input
import numpy as np
img_sample_filename = 'my_image.jpg' #e.g.: https://upload.wikimedia.org/wikipedia/commons/thumb/d/d4/CH_cow_2_cropped.jpg/1024px-CH_cow_2_cropped.jpg
img = img_to_array(load_img(img_sample_filename, target_size=(299,299)))
img = preprocess_input(img)
img_tensor = tf.constant(img[None,:])
# WITH KERAS:
model = InceptionV3()
pred = model.predict(img[None,:])
pred = decode_predictions(np.asarray(pred)) #<------ correct prediction!
print(pred)
# WITH TF:
model = InceptionV3(input_tensor=img_tensor)
init = tf.global_variables_initializer()
with tf.Session() as sess:
from keras import backend as K
K.set_session(sess)
sess.run(init)
pred = sess.run([model.output], feed_dict={K.learning_phase(): 0})
pred = decode_predictions(np.asarray(pred)[0])
print(pred) #<------ wrong prediction!
(I also opened a stackoverflow question just in case this was an implementation issue but from what little information about this use-case I could find this is supposed to work...)
Is there something I'm missing about incompatibilities of pretrained models and tensor-based input? Or am I doing something wrong?
Ok, taking a closer look at it I found the problem: sess.run(init) overwrites the weights loaded in the InceptionV3 constructor.
By adding to the imports:
from keras.applications.inception_v3 import get_file, WEIGHTS_PATH
and in the with tf.Session() as session: block:
sess.run(init)
weights_path = get_file(
'inception_v3_weights_tf_dim_ordering_tf_kernels.h5',
WEIGHTS_PATH,
cache_subdir='models',
md5_hash='9a0d58056eeedaa3f26cb7ebd46da564')
model.load_weights(weights_path)
the prediction is the same as when using model.predict().
Before closing the issue, however, I'd like to ask if this is documented anywhere and if is there a better way to do this than reading through the InceptionV3 code to find which weights file should be loaded.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.
Most helpful comment
Ok, taking a closer look at it I found the problem:
sess.run(init)overwrites the weights loaded in theInceptionV3constructor.By adding to the imports:
and in the
with tf.Session() as session:block:the prediction is the same as when using
model.predict().Before closing the issue, however, I'd like to ask if this is documented anywhere and if is there a better way to do this than reading through the
InceptionV3code to find which weights file should be loaded.