You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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.
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
IMAGE_SHAPE= (224, 224)
# Download the pretrained model and save it as a Keras layerresnet_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 patternsname="feaure_extraction_layer",
input_shape=IMAGE_SHAPE+(3,))
# Create our own modelresnet_model=tf.keras.Sequential([
feature_extractor_layer,
layers.Dense(10, activation="softmax", name="output_layer")
])
Getting a ValueError, here is a detailed explanaion:
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 layerresnet_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 patternsname="feaure_extraction_layer",
input_shape=IMAGE_SHAPE+(3,))
# Create our own modelresnet_model=tf.keras.Sequential([
tf.keras.layers.Lambda(lambdax: feature_extractor_layer(x)),
layers.Dense(10, activation="softmax", name="output_layer")
])
But model.summary() doesn't give any details from feature_extractor_layer
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:
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")
`
The text was updated successfully, but these errors were encountered: