Keras Hub (Legacy)

0.27.1 · abandoned · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This example illustrates how `KerasLayer` from `keras-hub` was intended to be used to integrate TensorFlow Hub modules into Keras models. **Please note: This code block is for historical context and is not runnable with Python 3.8+ or Keras 3.x due to the library's abandonment and dependency incompatibilities.** For a working example with modern Keras, refer to the 'warnings' section below which uses `tensorflow_hub` directly.

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

view raw JSON →