4

I'm using Keras pretrained model 'Xception' to do image recognition. However, no matter what picture I give Xception, the predictions are always:

Predicted: [[('n04179913', 'sewing_machine', 1.0), ('n15075141, toilet_tissue', 0.0), ('n02317335', 'starfish', 0.0), ('n02389026, sorrel', 0.0), ('n02364673', 'guinea_pig', 0.0)]]

Is there anything wrong with my code?

My code is:

from tensorflow.contrib.keras import applications as app
from tensorflow.contrib.keras import preprocessing as pp
import numpy as np

model = app.Xception(weights='imagenet', include_top=True)
img_path = 'test123.jpg'
img = pp.image.load_img(path=img_path, target_size=(299, 299))
x = pp.image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = app.xception.preprocess_input(x)

preds = model.predict(x)
print('Predicted:', app.xception.decode_predictions(preds))

Marcin Możejko
  • 39,542
  • 10
  • 109
  • 120
sunnwmy
  • 143
  • 2
  • 9
  • Weird. Could you print out `x.max()` and `x.min()`? – Marcin Możejko Sep 14 '17 at 05:29
  • This code works perfectly for me. Are you on the newest versions of keras and tf? – McLawrence Sep 14 '17 at 06:23
  • 1
    I can reproduce the problem with TF 1.3.0. Changing `from tensorflow.contrib.keras import applications` into `from keras import applications` fixes the problem for me. Maybe there are some issues in the TF-keras code base. You can try to install keras instead of using it from TF. – Yu-Yang Sep 14 '17 at 17:05
  • @Yu-Yang This seems to be a bug of keras in tensorflow, I have changed the import statement to 'from keras import applications' and everything works fine now. Thanks – sunnwmy Sep 15 '17 at 05:19
  • @McLawrence I'm using the newest version of tensorflow on windows platform. This seems to be a bug. – sunnwmy Sep 15 '17 at 05:21
  • Just in case someone really needs to use TF-Keras, this problem has already been fixed in [this commit](https://github.com/tensorflow/tensorflow/commit/17f26f81bfaf8ee03e330b98f4297cb754676c35). It's not yet included in the latest released TF 1.3.0 on pip, but the [nightly build](https://github.com/tensorflow/tensorflow#installation) already has it. – Yu-Yang Sep 15 '17 at 07:23

1 Answers1

-1

Normalize the image by x/255. just before predict function call As per my understanding, Xception module is trained on normalized intensities. I faced the same problem. So, i normalized the pixel intensities by dividing them by 255. You can try the same. I hope it helps.