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)

Commits related to this issue

Most upvoted comments

Hi @ruddyscent ,

TFHub has dependency on tf_keras package (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.

def hub_layer_fn(x):
    return hub.KerasLayer(MODEL_URL, trainable=False)(x)
import tensorflow as tf
import tensorflow_hub as hub

# Create a function which builds a Keras model
def create_model(input_shape=INPUT_SHAPE, output_shape=OUTPUT_SHAPE, model_url=MODEL_URL):
    print("Building model with:", model_url)

    # Setup the model layers
    model = tf.keras.Sequential([
        tf.keras.layers.Lambda(hub_layer_fn, name="hub_lambda"),  # Wrap the hub.KerasLayer in Lambda
        tf.keras.layers.Dense(units=output_shape, activation="softmax")  # Output layer with softmax activation
    ])

    # Compile the model
    model.compile(
        loss=tf.keras.losses.CategoricalCrossentropy(),
        optimizer=tf.keras.optimizers.Adam(),
        metrics=["accuracy"]
    )

    # Build the model
    model.build((None, *input_shape))  # Ensure input shape is unpacked correctly

    return model

This function create_model takes input_shape, output_shape, and model_url as parameters and constructs a tf.keras.Sequential model. It uses a tf.keras.layers.Lambda layer to wrap the TensorFlow Hub layer (represented by hub_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, and hub_layer_fn defined according to your specific use case.

Another Example:

resnet_url = "https://www.kaggle.com/models/google/resnet-v2/TensorFlow2/50-feature-vector/2"

efficientnet_url = "https://www.kaggle.com/models/tensorflow/efficientnet/TensorFlow2/b0-feature-vector/1"
# Import dependencies
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras import layers
def create_model(model_url, num_classes=10):
  """
  Takes a TensorFlow Hub URL and creates a Keras Sequential model with it.

  Args:
    model_url (str): A TensorFlow Hub feature extraction URL.
    num_classes (int): Number of output neurons in the output layer,
    should be equal to number of target classes, default 10.
  
  Returns:
    An uncompiled keras Sequential model with model_url as feature extarctor
    layer and Dense output layer with num_classes output neurons.
  """
  # Download the pretrained model and save it as a keras layer
  feature_extractor_layer = hub.KerasLayer(model_url,
                                           trainable=False, # Freeze the already learned patterns
                                           name="feature_extraction_layer",
                                           input_shape=IMAGE_SHAPE+(3,)
                                           )
  
  # Create our own model
  model = tf.keras.Sequential([
      layers.Lambda(feature_extractor_layer, name="hub_lambda"),
      layers.Dense(num_classes, activation="softmax", name="output_layer")
  ])

  model.build((None, *(IMAGE_SHAPE+(3,))))

  return model
# Create Resnet model
resnet_model = create_model(resnet_url,
                            num_classes=train_data_10_percent.num_classes
                            )
resnet_model.summary()

Simply put this in your notebook’s top cell if you don’t want to do any of these tasks:

import os
os.environ["TF_USE_LEGACY_KERAS"] = "1"

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