Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: The layer has never been called and thus has no defined output shape. #44857

Closed
PratsBhatt opened this issue Nov 13, 2020 · 8 comments
Assignees
Labels
stale This label marks the issue/pr stale - to be closed automatically if no activity stat:awaiting response Status - Awaiting response from author TF 2.3 Issues related to TF 2.3 type:bug Bug

Comments

@PratsBhatt
Copy link
Contributor

Please make sure that this is a bug. As per our
GitHub Policy,
we only address code/doc bugs, performance issues, feature requests and
build/installation issues on GitHub. tag:bug_template

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow):
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 18.04
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device:
  • TensorFlow installed from (source or binary): Used Google Colab
  • TensorFlow version (use command below): 2.3.0
  • Python version: 3.6.9
  • Bazel version (if compiling from source):
  • GCC/Compiler version (if compiling from source):
  • CUDA/cuDNN version: Doesn't matter
  • GPU model and memory: Doesn't matter

You can collect some of this information using our environment capture
script
You can also obtain the TensorFlow version with:

  1. TF 1.0: python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)"
  2. TF 2.0: python -c "import tensorflow as tf; print(tf.version.GIT_VERSION, tf.version.VERSION)"

Describe the current behavior
I used to run below mentioned code in the tensorflow version 1.15 but since the time I have upgraded it fails to run.
I have already gone through similar bugs, but they all mention that I should have provided the input shape, I have done that, so I am not sure where the problem lies.

model = DenseNet121(input_shape=(810, 1440, 3),
                                         include_top=False,
                                         weights='imagenet'
                                         )

depth = model.get_output_shape_at(0)[-1]

Describe the expected behavior
Expected behaviour should be that I should be able to get the output shape of the last layer.

Standalone code to reproduce the issue
Provide a reproducible test case that is the bare minimum necessary to generate
the problem. If possible, please share a link to Colab/Jupyter/any notebook.

Everyone can recreate the issue using the below colab link.
https://colab.research.google.com/drive/1TPYkETG8szsCvut6SdzY345jEnqOrgsg?usp=sharing

Other info / logs 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.

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-4-c67805c58fe0> in <module>()
----> 1 depth = model.get_output_shape_at(0)[-1]

1 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in get_output_shape_at(self, node_index)
   2028     """
   2029     return self._get_node_attribute_at_index(node_index, 'output_shapes',
-> 2030                                              'output shape')
   2031 
   2032   @doc_controls.do_not_doc_inheritable

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _get_node_attribute_at_index(self, node_index, attr, attr_name)
   2601     if not self._inbound_nodes:
   2602       raise RuntimeError('The layer has never been called '
-> 2603                          'and thus has no defined ' + attr_name + '.')
   2604     if not len(self._inbound_nodes) > node_index:
   2605       raise ValueError('Asked to get ' + attr_name + ' at node ' +

RuntimeError: The layer has never been called and thus has no defined output shape.
@bhack
Copy link
Contributor

bhack commented Nov 13, 2020

Can you unlock the Colab?

@PratsBhatt
Copy link
Contributor Author

@bhack
Copy link
Contributor

bhack commented Nov 13, 2020

You need to call the model with an input:
E.g.

model(tf.keras.Input((810, 1440, 3)))
depth = model.get_output_shape_at(0)

@PratsBhatt
Copy link
Contributor Author

But, if you have observed, when I am initializing the densenet model, I am providing the input shape.

Then why do I need to provide it two times?

@bhack
Copy link
Contributor

bhack commented Nov 14, 2020

The layer need to be called. See the difference between call and build https://keras.io/api/layers/base_layer/

@Saduf2019 Saduf2019 added TF 2.3 Issues related to TF 2.3 stat:awaiting response Status - Awaiting response from author labels Nov 17, 2020
@google-ml-butler
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you.

@google-ml-butler google-ml-butler bot added the stale This label marks the issue/pr stale - to be closed automatically if no activity label Nov 24, 2020
@google-ml-butler
Copy link

Closing as stale. Please reopen if you'd like to work on this further.

@google-ml-butler
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale This label marks the issue/pr stale - to be closed automatically if no activity stat:awaiting response Status - Awaiting response from author TF 2.3 Issues related to TF 2.3 type:bug Bug
Projects
None yet
Development

No branches or pull requests

3 participants