Home Trying to deploy Tensorflow model with Flaskr. Internal server Erro (Variable already exists)
Reply: 0

Trying to deploy Tensorflow model with Flaskr. Internal server Erro (Variable already exists)

Marcel
1#
Marcel Published in 2017-12-06 20:05:38Z

I am trying to deploy a tensorflow char-rnn model with Flaskr.

The server is running and basically working excpet for one problem. From my index function at localhost:500/, I am sending a string to my sample function at localhost:500/sampler and I get the output I expect. As soon as I try to go back to localhost:500/and send another string I get an "Internal Server Error" and the terminal prints the following:

[2017-12-06 17:35:01,029] ERROR in app: Exception on /render [POST]
Traceback (most recent call last):
  File "/Users/marcel/.pyenv/versions/3.5.3/envs/keras/lib/python3.5/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/marcel/.pyenv/versions/3.5.3/envs/keras/lib/python3.5/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/marcel/.pyenv/versions/3.5.3/envs/keras/lib/python3.5/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/marcel/.pyenv/versions/3.5.3/envs/keras/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/Users/marcel/.pyenv/versions/3.5.3/envs/keras/lib/python3.5/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/marcel/.pyenv/versions/3.5.3/envs/keras/lib/python3.5/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/marcel/tensorflow/WebApp/sampleWeb.py", line 57, in sample
    model = Model(saved_args, training=False)
  File "/Users/marcel/tensorflow/WebApp/model.py", line 45, in __init__
    [args.rnn_size, args.vocab_size])
  File "/Users/marcel/.pyenv/versions/3.5.3/envs/keras/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 1203, in get_variable
    constraint=constraint)
  File "/Users/marcel/.pyenv/versions/3.5.3/envs/keras/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 1092, in get_variable
    constraint=constraint)
  File "/Users/marcel/.pyenv/versions/3.5.3/envs/keras/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 425, in get_variable
    constraint=constraint)
  File "/Users/marcel/.pyenv/versions/3.5.3/envs/keras/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 394, in _true_getter
    use_resource=use_resource, constraint=constraint)
  File "/Users/marcel/.pyenv/versions/3.5.3/envs/keras/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 742, in _get_single_variable
    name, "".join(traceback.format_list(tb))))
ValueError: Variable rnnlm/softmax_w already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:

  File "/Users/marcel/tensorflow/WebApp/model.py", line 45, in __init__
    [args.rnn_size, args.vocab_size])
  File "/Users/marcel/tensorflow/WebApp/sampleWeb.py", line 57, in sample
    model = Model(saved_args, training=False)
  File "/Users/marcel/.pyenv/versions/3.5.3/envs/keras/lib/python3.5/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)

In my model.py I tried both return= true and return=tf.AUTO_REUSE, I've also tried using tf.get_variable_scope().reuse_variables() but none of it solved the problem. I can't really see where the problem is, can anyone point me there?

My code:

@app.route('/')
def index():
    return render_template('text.html')

@app.route('/render', methods = ['POST', 'GET'])
def sample():
  if request.method == 'POST':
    sample = request.form['text']

    args = request.cookies.get('args')
    with open(os.path.join('save', 'config.pkl'), 'rb') as f:
        saved_args = cPickle.load(f)
    with open(os.path.join('save', 'chars_vocab.pkl'), 'rb') as f:
        chars, vocab = cPickle.load(f)
    model = Model(saved_args, training=False)
    with tf.Session() as sess:
        tf.global_variables_initializer().run()
        saver = tf.train.Saver(tf.global_variables())
        ckpt = tf.train.get_checkpoint_state('save')
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)

            text = model.sample(sess, chars, vocab, 500, sample + " \n\n",
                                1).encode('utf-8')
            verse = text.decode(encoding='UTF-8')

            with open('output/output.txt', 'a') as file:
              file.write(datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")+ ":\n"+ verse+ "\n\n\n")
            return render_template('verse.html', name = verse)

  return render_template('404.html')            

and the code snippet from the model.py

  with tf.variable_scope('rnnlm'):
            softmax_w = tf.get_variable("softmax_w",
                                        [args.rnn_size, args.vocab_size])
            softmax_b = tf.get_variable("softmax_b", [args.vocab_size])

        embedding = tf.get_variable("embedding", [args.vocab_size, args.rnn_size])
        inputs = tf.nn.embedding_lookup(embedding, self.input_data)

        # dropout beta testing: double check which one should affect next line
        if training and args.output_keep_prob:
            inputs = tf.nn.dropout(inputs, args.output_keep_prob)

        inputs = tf.split(inputs, args.seq_length, 1)
        inputs = [tf.squeeze(input_, [1]) for input_ in inputs]
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.313187 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO