<p dir="auto"><strong>System information</strong></p>
<ul dir="auto">
<li>Have I written custom code (as opposed to using a stock example script provided in TensorFlow): yes, found here (<a href="https://github.com/viaboxxsystems/deeplearning-showcase/blob/tensorflow_2.0/flaskApp.py">https://github.com/viaboxxsystems/deeplearning-showcase/blob/tensorflow_2.0/flaskApp.py</a>)</li>
<li>OS Platform and Distribution (e.g., Linux Ubuntu 16.04): MAC OSX 10.14.4</li>
<li>TensorFlow version (use command below): 2.0.0-alpha0</li>
<li>Python version: Python 3.6.5</li>
</ul>
<p dir="auto">You can collect some of this information using our environment capture<br>
python -c "import tensorflow as tf; print(tf.version.GIT_VERSION, tf.version.VERSION)"<br>
v1.12.0-9492-g2c319fb415 2.0.0-alpha0</p>
<p dir="auto"><strong>Describe the current behavior</strong><br>
when running "flaskApp.py", After loading the model and trying to classify an image using "predict", it fails with the error:</p>
<blockquote>
<p dir="auto">tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable softmax/kernel from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/softmax/kernel/N10tensorflow3VarE does not exist.</p>
</blockquote>
<p dir="auto"><strong>Describe the expected behavior</strong><br>
a result of image classification should be returned.</p>
<p dir="auto"><strong>Code to reproduce the issue</strong><br>
Steps to reproduce:</p>
<ul dir="auto">
<li><code class="notranslate">git clone https://github.com/viaboxxsystems/deeplearning-showcase.git</code></li>
<li><code class="notranslate">git checkout tensorflow_2.0</code></li>
<li>(if needed) <code class="notranslate">pip3 install -r requirements.txt</code></li>
<li><code class="notranslate">export FLASK_APP=flaskApp.py</code></li>
<li>start the app with <code class="notranslate">flask run</code></li>
<li>using Postman or curl send any image of a dog or cat to the app<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/38561624/56967975-93abfd00-6b62-11e9-9de0-ef99356f8db4.png"><img src="https://user-images.githubusercontent.com/38561624/56967975-93abfd00-6b62-11e9-9de0-ef99356f8db4.png" alt="Screenshot 2019-04-30 at 16 10 57" style="max-width: 100%;"></a><br>
OR</li>
</ul>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="curl -X POST \
  http://localhost:5000/net/MobileNet \
  -H 'Postman-Token: ea35b79b-b34d-4be1-a80c-505c104050ec' \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -F image=@/Users/haitham.b/Projects/ResearchProjects/CNNs/deeplearning-showcase/data/sample/valid/dogs/dog.1008.jpg"><pre class="notranslate"><code class="notranslate">curl -X POST \
  http://localhost:5000/net/MobileNet \
  -H 'Postman-Token: ea35b79b-b34d-4be1-a80c-505c104050ec' \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -F image=@/Users/haitham.b/Projects/ResearchProjects/CNNs/deeplearning-showcase/data/sample/valid/dogs/dog.1008.jpg
</code></pre></div>
<p dir="auto"><strong>Other info / logs</strong></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="E0430 13:36:10.374372 123145501933568 app.py:1761] Exception on /net/MobileNet [POST]
Traceback (most recent call last):
  File &quot;/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/flask/app.py&quot;, line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File &quot;/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/flask/app.py&quot;, line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File &quot;/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/flask/app.py&quot;, line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File &quot;/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/flask/_compat.py&quot;, line 35, in reraise
    raise value
  File &quot;/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/flask/app.py&quot;, line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File &quot;/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/flask/app.py&quot;, line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File &quot;/Users/haitham.b/Projects/Virtualenvs/deeplearning-showcase/flaskApp.py&quot;, line 97, in use_net_to_classify_image
    prediction, prob = predict(net_name, image)
  File &quot;/Users/haitham.b/Projects/Virtualenvs/deeplearning-showcase/flaskApp.py&quot;, line 59, in predict
    output_probability = net_models[cnn_name].predict(post_processed_input_images)
  File &quot;/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py&quot;, line 1167, in predict
    callbacks=callbacks)
  File &quot;/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_arrays.py&quot;, line 352, in model_iteration
    batch_outs = f(ins_batch)
  File &quot;/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/tensorflow/python/keras/backend.py&quot;, line 3096, in __call__
    run_metadata=self.run_metadata)
  File &quot;/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/tensorflow/python/client/session.py&quot;, line 1440, in __call__
    run_metadata_ptr)
  File &quot;/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py&quot;, line 548, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable softmax/kernel from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/softmax/kernel/N10tensorflow3VarE does not exist.
	 [[{{node softmax/MatMul/ReadVariableOp}}]]"><pre class="notranslate"><code class="notranslate">E0430 13:36:10.374372 123145501933568 app.py:1761] Exception on /net/MobileNet [POST]
Traceback (most recent call last):
  File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/haitham.b/Projects/Virtualenvs/deeplearning-showcase/flaskApp.py", line 97, in use_net_to_classify_image
    prediction, prob = predict(net_name, image)
  File "/Users/haitham.b/Projects/Virtualenvs/deeplearning-showcase/flaskApp.py", line 59, in predict
    output_probability = net_models[cnn_name].predict(post_processed_input_images)
  File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1167, in predict
    callbacks=callbacks)
  File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_arrays.py", line 352, in model_iteration
    batch_outs = f(ins_batch)
  File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/tensorflow/python/keras/backend.py", line 3096, in __call__
    run_metadata=self.run_metadata)
  File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1440, in __call__
    run_metadata_ptr)
  File "/Users/haitham.b/venv/tensorflow2.0alpha/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 548, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable softmax/kernel from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/softmax/kernel/N10tensorflow3VarE does not exist.
	 [[{{node softmax/MatMul/ReadVariableOp}}]]
</code></pre></div>
<p dir="auto"><em>Please make sure that this is a bug. As per our <a href="https://github.com/tensorflow/tensorflow/blob/master/ISSUES.md">GitHub Policy</a>, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template</em></p>
<p dir="auto"><strong>System information</strong></p>
<ul dir="auto">
<li>Have I written custom code (as opposed to using a stock example script provided in TensorFlow): yes</li>
<li>OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 18.04</li>
<li>Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device:</li>
<li>TensorFlow installed from (source or binary): pip nightly</li>
<li>TensorFlow version (use command below): 2.0.0-dev20190415</li>
<li>Python version: 3.6</li>
<li>Bazel version (if compiling from source):</li>
<li>GCC/Compiler version (if compiling from source):</li>
<li>CUDA/cuDNN version:</li>
<li>GPU model and memory:</li>
</ul>
<p dir="auto">You can collect some of this information using our environment capture <a href="https://github.com/tensorflow/tensorflow/tree/master/tools/tf_env_collect.sh">script</a><br>
You can also obtain the TensorFlow version with<br>
python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)"</p>
<p dir="auto"><strong>Describe the current behavior</strong><br>
The two following layers give different number of variable in <code class="notranslate">model.variables</code>. Keras duplicates shared variables in the variable list:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import tensorflow as tf
import numpy as np

class MLayer(tf.Module):
    def __init__(self):
        super(MLayer, self).__init__()
        conv = tf.keras.layers.Conv2D(3, 3, 1, padding='SAME')
        self.convs = [conv] * 7

    def __call__(self, x):
        for conv in self.convs:
            x = conv(x)
        return x

class KLayer(tf.keras.models.Model):
    def __init__(self):
        super(KLayer, self).__init__()
        conv = tf.keras.layers.Conv2D(3, 3, 1, padding='SAME')
        self.convs = [conv] * 7

    def call(self, x):
        for conv in self.convs:
            x = conv(x)
        return x

xnp = np.random.rand(1, 224, 224, 3)
x = tf.constant(xnp, tf.float32)

model = MLayer()
y = model(x)
v = [variable.name for variable in model.variables]
print(v, len(v))

model = KLayer()
y = model(x)
v = [variable.name for variable in model.variables]
print(v, len(v))"><pre class="notranslate"><code class="notranslate">import tensorflow as tf
import numpy as np

class MLayer(tf.Module):
    def __init__(self):
        super(MLayer, self).__init__()
        conv = tf.keras.layers.Conv2D(3, 3, 1, padding='SAME')
        self.convs = [conv] * 7

    def __call__(self, x):
        for conv in self.convs:
            x = conv(x)
        return x

class KLayer(tf.keras.models.Model):
    def __init__(self):
        super(KLayer, self).__init__()
        conv = tf.keras.layers.Conv2D(3, 3, 1, padding='SAME')
        self.convs = [conv] * 7

    def call(self, x):
        for conv in self.convs:
            x = conv(x)
        return x

xnp = np.random.rand(1, 224, 224, 3)
x = tf.constant(xnp, tf.float32)

model = MLayer()
y = model(x)
v = [variable.name for variable in model.variables]
print(v, len(v))

model = KLayer()
y = model(x)
v = [variable.name for variable in model.variables]
print(v, len(v))
</code></pre></div>
<p dir="auto">The first print:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="['conv2d/kernel:0', 'conv2d/bias:0'] 2"><pre class="notranslate"><code class="notranslate">['conv2d/kernel:0', 'conv2d/bias:0'] 2
</code></pre></div>
<p dir="auto">The second prints:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="['k_layer/conv2d_1/kernel:0', 'k_layer/conv2d_1/bias:0', 'k_layer/conv2d_1/kernel:0', 'k_layer/conv2d_1/bias:0', 'k_layer/conv2d_1/kernel:0', 'k_layer/conv2d_1/bias:0', 'k_layer/conv2d_1/kernel:0', 'k_layer/conv2d_1/bias:0', 'k_layer/conv2d_1/kernel:0', 'k_layer/conv2d_1/bias:0', 'k_layer/conv2d_1/kernel:0', 'k_layer/conv2d_1/bias:0', 'k_layer/conv2d_1/kernel:0', 'k_layer/conv2d_1/bias:0'] 14"><pre class="notranslate"><code class="notranslate">['k_layer/conv2d_1/kernel:0', 'k_layer/conv2d_1/bias:0', 'k_layer/conv2d_1/kernel:0', 'k_layer/conv2d_1/bias:0', 'k_layer/conv2d_1/kernel:0', 'k_layer/conv2d_1/bias:0', 'k_layer/conv2d_1/kernel:0', 'k_layer/conv2d_1/bias:0', 'k_layer/conv2d_1/kernel:0', 'k_layer/conv2d_1/bias:0', 'k_layer/conv2d_1/kernel:0', 'k_layer/conv2d_1/bias:0', 'k_layer/conv2d_1/kernel:0', 'k_layer/conv2d_1/bias:0'] 14
</code></pre></div>
<p dir="auto"><strong>Describe the expected behavior</strong></p>
<p dir="auto">I would expect two layers have the same number of variables.</p>
<p dir="auto"><strong>Code to reproduce the issue</strong><br>
Provide a reproducible test case that is the bare minimum necessary to generate the problem.</p>
<p dir="auto"><strong>Other info / logs</strong><br>
Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.</p>