model.predict keeps predicting the same, wrong, class

I have a simple oxford102 (102 UK flowers) tensorflow model and Android app from here.

I'd like to see if I can add recognition (model.predict) to the python code in the subproject FlowerML which is python code that generates the model in the first place.

Added the following to FlowerML2.py to recognize a single flower:

# sequence of calls adapting image then calling predict:
# https://datascience.stackexchange.com/questions/31167/how-to-predict-an-image-using-saved-model
test_image = image.load_img('d:/ML/images/CallaLily-1.jpg', target_size=(224, 224))
print("test_image PIL size " + str(test_image.size))
test_image_arr = image.img_to_array(test_image)
print(test_image_arr.shape)
# double-check visually
image.save_img('d:/ml/images/WhatIsIt-1.jpg', test_image_arr)
test_image = np.expand_dims(test_image_arr, axis=0)
print(test_image.shape)
result2 = model.predict(tf.convert_to_tensor(test_image), batch_size=1, verbose=2)
print("\nnp.where result2: " + str(np.where(result2 == result2.max())) + " max = " + str(result2.max()) + "\n")


The model correctly identifies Calla Lily in the Android app however when I query the model as above it keeps telling me the wrong class. 'Keeps' refers to many modifications not above - and even with a different flower. It's like the model, in this context (but not in the Android app), gets stuck with an output to its head layer of n=1.

Since it works in Android, my assumption is that I'm not calling predict properly - although exactly what goes wrong I can't say and I have little experience, so far, digging into how models work which is maybe what you need to do when they don't function correctly.

1 Answer

Figured it out. Need to normalize the image I'm going to predict to $$[-1,1]$$ instead of what-I-assume-is-the-standard $$[0,1]$$. It seems this is model-dependent. The project I'm working off of uses transfer learning and imports this model.

It's not immediately clear to me though just how this model needs inputs normalized to $$[-1,1]$$.