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

Title: TensorFlow/Keras Integration Error: A KerasTensor cannot be used as input to a TensorFlow function #69340

Closed
ShoumitroC opened this issue Jun 6, 2024 · 4 comments
Assignees
Labels
comp:keras Keras related issues 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.13 For issues related to Tensorflow 2.13 type:bug Bug

Comments

@ShoumitroC
Copy link

Issue type

Build/Install

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

binary

TensorFlow version

2.13.0

Custom code

Yes

OS platform and distribution

Mac OS x86-64

Mobile device

Macbook

Python version

3.9

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

I'm working on an image processing project using InsightFace and OpenNSFW2. I'm encountering an error with TensorFlow and Keras integration. When I try to use tf.pad on a tensor, I get the following error:

ValueError: A KerasTensor cannot be used as input to a TensorFlow function. A KerasTensor is a symbolic placeholder for a shape and dtype, used when constructing Keras Functional models or Keras Functions. You can only use it as input to a Keras layer or a Keras operation (from the namespaces keras.layers and keras.operations). You are likely doing something like:

Standalone code to reproduce the issue

ValueError: A KerasTensor cannot be used as input to a TensorFlow function. A KerasTensor is a symbolic placeholder for a shape and dtype, used when constructing Keras Functional models or Keras Functions. You can only use it as input to a Keras layer or a Keras operation (from the namespaces `keras.layers` and `keras.operations`). You are likely doing something like:

x = Input(...)
...
tf_fn(x)  # Invalid.


I've tried wrapping tf.pad in a custom layer as suggested, but I'm still getting the error. Here's the relevant code snippet:

class MyLayer(Layer):
    def call(self, x):
        return tf.pad(x, [[0, 0], [3, 3], [3, 3], [0, 0]], "CONSTANT")

# Usage
x = MyLayer()(x)


Could someone please help me understand what I'm doing wrong and how to fix it?

Thank you!

Relevant log output

No response

@google-ml-butler google-ml-butler bot added the type:build/install Build and install issues label Jun 6, 2024
@sushreebarsa sushreebarsa added subtype:macOS macOS Build/Installation issues TF 2.13 For issues related to Tensorflow 2.13 type:bug Bug comp:keras Keras related issues and removed type:build/install Build and install issues subtype:macOS macOS Build/Installation issues labels Jun 10, 2024
@sushreebarsa
Copy link
Contributor

@ShoumitroC Could you try to use keras.layers.Lambda layer that allows you to define a custom function using a Python lambda expression that can operate on KerasTensors within the model building process?
Please upgrade to the latest TF version and if the issue still persists then kindly provide the standalone code to replicate this issue?
Thank you!

@sushreebarsa sushreebarsa added the stat:awaiting response Status - Awaiting response from author label Jun 10, 2024
Copy link

This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you.

@github-actions github-actions bot added the stale This label marks the issue/pr stale - to be closed automatically if no activity label Jun 18, 2024
Copy link

This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further.

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
comp:keras Keras related issues 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.13 For issues related to Tensorflow 2.13 type:bug Bug
Projects
None yet
Development

No branches or pull requests

2 participants