Forums

Tensorflow operation fails in Web App, but works from console

I'm struggling with a Tensorflow operation that fails when executed in a Web App, but runs without problems from a pythonanywhere console.

Here is the code (simplified):

path_to_img = 'static/images/content.jpg'
max_dim = 512
img = tf.io.read_file(path_to_img)
img = tf.image.decode_image(img, channels=3)
logging.info('checkpoint 1')
img = tf.image.convert_image_dtype(img, tf.float32)
logging.info('checkpoint 2')

From the logging I can see that checkpoint 1 is reached, but checkpoint 2 is not. So the execution hangs at

img = tf.image.convert_image_dtype(img, tf.float32)

Locally, this code runs fine (also in a Flask app). Moreover, in a pythonanywhere console I can run it without issues. Only from within a pythonanywhere Web App it fails. What is the issue here?

http://help.pythonanywhere.com/pages/MachineLearningInWebsiteCode/ explains the issue. You can work around it by moving the tensorflow work to a scheduled task or an always on task: http://help.pythonanywhere.com/pages/AsyncInWebApps/

The provided workaround isn't satisfactory for my use case, where a tensorflow model is being executed with input provided by the Web App user. Is there any other way to resolve this?

Unfortunately not.

the website says using a theano backend can resolve this issue. can anyone tell me how to use theano backend in my web app?

The advice to use a Theano backend applies if you're using Keras, which is a wrapper library that allows you to write ML code that will then be run using Tensorflow or Theano as its backend. If you're using Keras, then this is the part of the documentation where they explain how to set the backend. If your code uses Tensorflow directly, then you would need to rewrite it to use Theano.