Skip to content

Adding TensorFlow Hub KerasLayer to Sequential Model Raises ValueError #63849

Open
@ruddyscent

Description

@ruddyscent

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'>)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions