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

ValueError: Only instances of keras.Layer can be added to a Sequential model. Received: <tensorflow_hub.keras_layer.KerasLayer object at 0x7a9a39daf130> (of type <class 'tensorflow_hub.keras_layer.KerasLayer'>) #668

Open
moatednorth opened this issue Aug 14, 2024 Discussed in #662 · 3 comments

Comments

@moatednorth
Copy link

Discussed in #662

Originally posted by moatednorth July 31, 2024
Sequential api can't handle hub.KerasLayer

`

Create a Keras Layer using the USE pretrained layer from Kaggle

sentence_encoder_layer = hub.KerasLayer("https://www.kaggle.com/models/google/universal-sentence-encoder/TensorFlow2/universal-sentence-encoder/2",
input_shape=[],
dtype=tf.string,
trainable=False,
name="USE")

Create model using the Sequential API

model_6 = tf.keras.Sequential([
sentence_encoder_layer,
layers.Dense(64, activation="relu"),
layers.Dense(1, activation="sigmoid", name="ouput_layer")
], name="model_6_USE")
`

@AnilKamath27
Copy link

Hi, moatednorth. This is an issue that is occurring in the latest version of tensorflow 2.17. This won't be an issue if you try with tf 2.15.0. Below is a screenshot of my notebook where I was able to use hub.KerasLayers as part of Sequential API.

image

@ctournas
Copy link

ctournas commented Nov 2, 2024

I just stuck in this for an hour, for anyone out there that stuck on this too... just run this: pip install tensorflow==2.15.0 tensorflow-hub keras==2.15.0

@GoJo-Rika
Copy link

GoJo-Rika commented Feb 12, 2025

I am getting same issue with TensorFlow == 2.18.0

  IMAGE_SHAPE = (224, 224)
  # Download the pretrained model and save it as a Keras layer
  resnet_url = "https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/4"
  feature_extractor_layer = hub.KerasLayer(resnet_url,
                                            trainable=False, # freeze the already learned patterns
                                            name="feaure_extraction_layer",
                                            input_shape=IMAGE_SHAPE+(3,))
  
  # Create our own model
  resnet_model = tf.keras.Sequential([
      feature_extractor_layer,
      layers.Dense(10, activation="softmax", name="output_layer")
  ])

Getting a ValueError, here is a detailed explanaion:

ValueError: Only instances of `keras.Layer` can be added to a Sequential model. Received: <tensorflow_hub.keras_layer.KerasLayer object at 0x7d6a32dac1d0> (of type <class 'tensorflow_hub.keras_layer.KerasLayer'>)

I was able to resolve the above issue by wrapping the hub layer in the Lambda layer like below:

IMAGE_SHAPE = (224, 224)
# Download the pretrained model and save it as a Keras layer
resnet_url = "https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/4"
feature_extractor_layer = hub.KerasLayer(resnet_url,
                                          trainable=False, # freeze the already learned patterns
                                          name="feaure_extraction_layer",
                                          input_shape=IMAGE_SHAPE+(3,))

# Create our own model
resnet_model = tf.keras.Sequential([
    tf.keras.layers.Lambda(lambda x: feature_extractor_layer(x)),
    layers.Dense(10, activation="softmax", name="output_layer")
])

But model.summary() doesn't give any details from feature_extractor_layer

Image

Some URLS that I found useful:

Read a couple of GitHub issues discussions and also a Medium article on the same and they resolved the above problem using the 2nd option. But I didn't tried it myself as the first option worked for me.

Here are the version of tensorflow, tensorflow_hub and keras that I was using:

tensorflow == 2.18.0
tensorflow_hub == 0.16.1
keras == 3.8.0
tf_keras == 2.18.0 (2nd solution)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants