tensorflow: Adding TensorFlow Hub KerasLayer to Sequential Model Raises ValueError
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
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[29], line 1
----> 1 model = tf.keras.Sequential([
2 feature_extractor,
3 tf.keras.layers.Dense(2, activation = 'softmax')
4 ])
6 model.build([None, image_size, image_size, 3])
7 model.summary()
File /usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py:70, in Sequential.__init__(self, layers, trainable, name)
68 if layers:
69 for layer in layers:
---> 70 self.add(layer, rebuild=False)
71 self._maybe_rebuild()
File /usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py:92, in Sequential.add(self, layer, rebuild)
90 layer = origin_layer
91 if not isinstance(layer, Layer):
---> 92 raise ValueError(
93 "Only instances of `keras.Layer` can be "
94 f"added to a Sequential model. Received: {layer} "
95 f"(of type {type(layer)})"
96 )
97 if not self._is_layer_name_unique(layer):
...
101 "the name of a layer in this model. Update the `name` argument "
102 "to pass a unique name."
103 )
ValueError: Only instances of `keras.Layer` can be added to a Sequential model. Received: <tensorflow_hub.keras_layer.KerasLayer object at 0x7a4ac7e30f40> (of type <class 'tensorflow_hub.keras_layer.KerasLayer'>)
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 4
- Comments: 21 (6 by maintainers)
Links to this issue
Commits related to this issue
- Keras fix for Kaggle. See more: https://github.com/tensorflow/tensorflow/issues/63849#issuecomment-2002501172 — committed to piotrsok2/cv-course by deleted user 3 years ago
Hi @ruddyscent ,
TFHub has dependency on
tf_keraspackage (i.e Keras2) as per the setup.py of TFHub.https://github.com/tensorflow/hub/blob/ff72e25fef44bd67d5c14fb5328aa44e303e3404/tensorflow_hub/pip_package/setup.py#L31
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.
win10, no gpu, no colab
tensorflow_hub를 사용하시려면 다음과 같이 해보세요 import tensorflow as tf import tensorflow_hub as hub import tf_keras as keras “”“tensorflow_hub 설치시 자동으로 설치됩니다.”
… … model = keras.Sequential([ “”““tf.keras --> keras””" hub,KerasLayer(url, input_shape=(size, size, 3) … … imgfile = keras.utils.get_file(‘image.jpg’, ‘https://…’) “”““tf.keras --> keras””" 작동이 잘됩니다.
Hi @ruddyscent , @alishhde , @SuryanarayanaY,
Thanks for reporting this issue.
I believe the following code snippet provides a solution or a starting point to address the problem. It outlines how to properly build a Keras model that incorporates a TensorFlow Hub layer.
This function
create_modeltakesinput_shape,output_shape, andmodel_urlas parameters and constructs atf.keras.Sequentialmodel. It uses atf.keras.layers.Lambdalayer to wrap the TensorFlow Hub layer (represented byhub_layer_fn, which you would need to define based on how you are loading the Hub module). This approach can sometimes resolve compatibility or layering issues.Please ensure you have
INPUT_SHAPE,OUTPUT_SHAPE,MODEL_URL, andhub_layer_fndefined according to your specific use case.Another Example:
Simply put this in your notebook’s top cell if you don’t want to do any of these tasks:
For more info check out my Dog Vision Project Notebook: https://github.com/Anand-Velpuri/Dog-Vision/blob/main/dog-vision.ipynb
Let me know if this helps resolve the issue!
Best regards, @Anand-Velpuri