Forums

Keras predict function doesn't work

Hello everyone! I am developing flask application in which I use keras model to predict class of the picture. I have already trained model offline I only load weight in server. Server properly saves image to directory but then it cannot finish evalutaing of predict function.

In Shell I get very quick response from the same program passing the same test picture as argument.

#part of predict_img function
##################################################
test_image = image.load_img(img,target_size=(100, 100)) 
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result  = classifier.predict(test_image) <---------here program stucks
ind = np.where(result[0] !=0)[0][0]
with open('en.json') as json_file:
    classes = load(json_file)

I am currently having the same issue.

My program also hangs when loading a new keras model using:

model.load_weights('{}/model.h5'.format(location))

The location is definitely correct so maybe a problem with Keras running on pythonanywhere?

the thing is that when I run predict function in shell I get response very quickly. While I upload test photo on my web app I cannot see any change in CPU usage although the website is waiting for getting prediction from function. I think that I should notice change in CPU usage. when prediction function is working. Is my way of thinking correct?

This is exactly the same problem I am having. Have you made any progress?

Unfortunately not

Do you know if the classifier is trying to use threads?

This problem is solved by changing the Keras back-end to Theano.

Install Theano using

pip install theano

You can change Keras back-end by modifying the keras.json file in /home/your_name/.keras/keras.json

{
"backend": "theano",
"floatx": "float32",
"image_data_format": "channels_last",
"epsilon": 1e-07
}

Interesting. What backend where you using before?

I got the same problem. I use Tensorflow as my backend

OK. It sounds like we have a problem running Tensorflow inside Flask (and maybe other) websites -- we're looking into a solution, but if you can switch to Theano it sounds like that works as a workaround.

[edit -- I said Keras when I meant Theano]

There may be another fix here:

https://github.com/keras-team/keras/issues/2397#issuecomment-306687500

That sounds promising! We'd love to hear from anyone who tries it out :-)

I'm still facing this same issue, even trying out the suggested fix. I know because I applied that fix on my test webserver from my laptop and it worked, but not on pythonanywhere. Will this be fixed anytime soon?

It's unlikely that we are going to change the things that prevent Tensorflow and Keras from running in a web app any time soon.

So I changed my backend to theano but it still doesn't work. Any suggestions?

What error messages are you getting, if any? There's a link to the error log on the "Web" page, and the most recent error message will be at the bottom of the file.

In keras.json file: Change backend to theano in flask app file: Change tensorflow.keras to only keras everywhere in the file in bash console: pip3 install theano --user (--user is very important otherwise it will not install)

These Changes will make sure your model will start predicting

thanks for helping!