-
Notifications
You must be signed in to change notification settings - Fork 74.3k
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
Adding TensorFlow Hub KerasLayer to Sequential Model Raises ValueError #63849
Comments
After investigating the code, I found a potential cause of the issue in # Use Keras 2.
version_fn = getattr(tf.keras, "version", None)
if version_fn and version_fn().startswith("3."):
import tf_keras as keras
else:
keras = tf.keras Depending on the Keras version, the module might either import |
Hi @ruddyscent , TFHub has dependency on Since TF2.16 comes with Keras3 the problem arises. As a workaround, you can install tf_keras package and set environment variable TF_USE_LEGACY_KERAS=1 to ensure Keras2 will be used with tf.keras. |
Hi, When I tried with TF2.16v on Colab environment the error stack seems generated from the _ensure_keras_2_importable() function from |
Even this code seems confusing to me. If version_fn results in Keras3 then it tries to import tf_keras to use Keras2 else it assumes Keras2 is alreday there(i.e TF<=2.15v) hence it takes keras=tf.keras. But |
Actually,
|
From the code above if version_fn is not None and if it starts with "3" (which means Keras 3 found) in that case it is importing tf_keras package as keras. Please note that tf_keras package is for Keras2. This indicates TFHub supports only Keras2 package. The else part it assumes that version_fn doesn't starts with "3" which case it assumes Keras2 installed with TF package and it marks keras=tf.keras. But for any case if version_fn becomes None and if Keras3 installed with TF then tf.keras will become Keras3 which might a problem. @Aloqeely , Could you please import TF2.16 and confirm what will be the version_fn output ? |
Here you go import tensorflow as tf
version_fn = getattr(tf.keras, "version", None)
print("TF Version: " + tf.__version__)
print("TF Keras Version: " + version_fn()) Output:
|
If this workaround fixes ruddyscent's problem, then I think we should mark this issue as resolved, because the problem is tensorflow hub not supporting Keras 3, so it is not relevant to this repository. |
This means when tf.keras version is 3.x then Hub suggesting to import tf_keras as keras which is a Keras2 package. For that we should install with tf_keras package using |
Yep, hub does that. |
win10, no gpu, no colab tensorflow_hub를 사용하시려면 다음과 같이 해보세요 ... |
Thank you, @sj-yoon, for your assistance. The solution provided by @SuryanarayanaY works well for my situation. |
I had the same issue, and I could fix it with the solution provided by @SuryanarayanaY. Thanks Others can check my notebook for steps to fix it at this page. |
Could you please check whether this issue is resolved with the latest tensorflow v2.17 which contains Keras 3.0 version. As Keras3 now built to support multiple backends there are some changes in design. Also this issue is more related to Keras, please try to raise the issue in keras-team/Keras repo for the quick resolution. Thank you! |
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. |
The problem still exists with the latest FROM python:3.12.5
RUN pip3 install tensorflow==2.17.0 tensorflow-hub==0.16.1
RUN echo 'import tensorflow as tf' > main.py
RUN echo 'import tensorflow_hub as hub' >> main.py
RUN echo 'model = tf.keras.Sequential([hub.KerasLayer("https://tfhub.dev/tensorflow/efficientnet/b0/feature-vector/1", input_shape=(224, 224, 3))])' >> main.py
RUN python main.py docker build --rm --progress=plain . Output:
|
Issue type
Bug
Have you reproduced the bug with TensorFlow Nightly?
Yes
Source
source
TensorFlow version
2.16.1
Custom code
No
OS platform and distribution
Ubuntu 22.04.3 LTS
Mobile device
No response
Python version
Python 3.11.0rc1
Bazel version
No response
GCC/compiler version
No response
CUDA/cuDNN version
12.3.0
GPU model and memory
No response
Current behavior?
I can execute the following code without any issues in TensorFlow 2.15.0 and TensorFlow Hub 1.16.1. However, when I upgrade the TensorFlow version to 2.16.0 or above, I encounter an error stating that
KerasLayer
cannot be added to the Sequential model.Standalone code to reproduce the issue
import tensorflow as tf import tensorflow_hub as hub image_size = 224 URL = "https://tfhub.dev/tensorflow/efficientnet/b0/feature-vector/1" model = tf.keras.Sequential([ hub.KerasLayer(URL, input_shape=(image_size, image_size, 3)) ])
Relevant log output
The text was updated successfully, but these errors were encountered: