Keras Hub (Legacy)
The `keras-hub` library, version 0.27.1, was a wrapper released in 2019 to provide a convenient Keras API for loading models from TensorFlow Hub. It is now considered abandoned and is not compatible with modern Keras (3.x) or recent TensorFlow versions due to significant API changes and Python version requirements. Users should directly use the `tensorflow_hub` library with modern Keras installations.
Common errors
-
ModuleNotFoundError: No module named 'keras_hub'
cause The `keras-hub` package is not installed, or the current Python environment does not have access to it. This library is largely obsolete.fixFor modern Keras, use `tensorflow_hub` directly. Ensure `pip install tensorflow-hub` is run and then use `from tensorflow_hub import KerasLayer`. Do not attempt to install `keras-hub` for new projects. -
AttributeError: module 'tensorflow.keras.layers' has no attribute 'Dense' (or similar Keras/TF API mismatch errors)
cause Attempting to use `keras-hub` (which relies on very old TensorFlow/Keras APIs) with a modern Keras 3.x or TensorFlow 2.x installation, leading to severe API incompatibility.fixThe `keras-hub` library is incompatible with current versions. Remove `keras-hub` from your project dependencies. Instead, use `tensorflow_hub.KerasLayer` directly, ensuring your `tensorflow-hub` and `keras` installations are up-to-date and compatible with each other.
Warnings
- breaking The `keras-hub` library (0.27.1) is incompatible with Python 3.8+ and modern Keras (3.x) / TensorFlow (2.x) due to outdated dependencies and significant API changes. Attempting to use it will likely result in `ModuleNotFoundError`, `AttributeError`, or runtime crashes.
- deprecated The `keras-hub` package is abandoned, with its last release in 2019. It served as a thin wrapper for `tensorflow_hub.KerasLayer` and is no longer maintained or necessary with current Keras/TensorFlow versions.
- gotcha While `keras-hub.KerasLayer` defaulted `trainable=False`, `tensorflow_hub.KerasLayer` often defaults `trainable=True` for many feature vector modules. This subtle difference can lead to unexpected model training behavior if the `trainable` argument is not explicitly set.
Install
-
pip install keras-hub
Imports
- KerasLayer
from keras_hub import KerasLayer
- url_to_feature_column
from keras_hub import url_to_feature_column
Quickstart
import keras
from keras_hub import KerasLayer
import tensorflow as tf # Required for data types and model building
# This code block demonstrates the intended usage of keras-hub (0.27.1).
# It is NOT runnable with modern Python (3.8+) or Keras 3.x without
# specific, older TensorFlow/Keras versions and their corresponding
# Python environment.
# For a runnable example using modern Keras, see the 'warnings' section below.
try:
# Example: Use a pre-trained image feature vector module from TensorFlow Hub
# Note: The actual module URL would need to be valid for the TensorFlow Hub
# version compatible with keras-hub 0.27.1 (circa 2019).
feature_extractor_url = "https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/feature_vector/4"
# Create a KerasLayer from the Hub module
# keras-hub's KerasLayer defaults trainable=False
hub_layer = KerasLayer(feature_extractor_url, input_shape=(224, 224, 3))
# Build a simple Keras model
model = keras.Sequential([
keras.Input(shape=(224, 224, 3)),
hub_layer,
keras.layers.Dense(2, activation='softmax')
])
model.summary()
print(f"KerasLayer trainable: {hub_layer.trainable}")
except ImportError:
print("\n'keras-hub' or its dependencies not found. This library is abandoned.")
print("Install with 'pip install keras-hub' requires older Python/TensorFlow environment.")
except Exception as e:
print(f"\nAn error occurred during keras-hub usage (likely due to environment incompatibility): {e}")
# --- Modern Keras/TensorFlow Hub approach (RECOMMENDED) ---
# For current environments, use tensorflow_hub directly:
# import tensorflow_hub as hub
# import keras
#
# feature_extractor_url_modern = "https://tfhub.dev/google/efficientnet/b0/feature-vector/1" # Example modern URL
# hub_layer_modern = hub.KerasLayer(feature_extractor_url_modern, trainable=False, input_shape=(224, 224, 3))
# model_modern = keras.Sequential([
# keras.Input(shape=(224, 224, 3)),
# hub_layer_modern,
# keras.layers.Dense(2, activation='softmax')
# ])
# model_modern.summary()