Pretrained models for Keras with multi-framework compatibility.
npx @tessl/cli install tessl/pypi-keras-hub@0.22.0Keras Hub is a comprehensive pretrained modeling library providing Keras 3 implementations of popular model architectures for text, image, and audio data. It offers state-of-the-art models including BERT, ResNet, BART, BLOOM, DeBERTa, DistilBERT, GPT-2, Llama, Mistral, OPT, RoBERTa, Whisper, and XLM-RoBERTa with pretrained checkpoints available on Kaggle Models. The library supports multi-framework compatibility with JAX, TensorFlow, and PyTorch backends and enables fine-tuning on GPUs and TPUs with built-in PEFT techniques.
pip install keras-hub (for NLP models: pip install keras-hub[nlp])import keras_hubCommon pattern for specific components:
# Models - most commonly loaded with from_preset()
from keras_hub.models import BertTextClassifier, GPT2CausalLM
from keras_hub.models import ImageClassifier
# Tokenizers
from keras_hub.tokenizers import BertTokenizer
# Layers
from keras_hub.layers import TransformerEncoder
# Metrics
from keras_hub.metrics import Bleu
# Utils
from keras_hub.utils import upload_presetimport keras_hub
# Load a pretrained model for text classification
classifier = keras_hub.models.BertTextClassifier.from_preset("bert_base_en")
# Classify text
result = classifier.predict(["This is a great movie!", "I didn't like this film."])
print(result)
# Load a causal language model for text generation
generator = keras_hub.models.GPT2CausalLM.from_preset("gpt2_base_en")
# Generate text
generated = generator.generate("The weather today is", max_length=50)
print(generated)
# Use tokenizers directly
tokenizer = keras_hub.tokenizers.BertTokenizer.from_preset("bert_base_en")
tokens = tokenizer(["Hello world!", "How are you?"])
print(tokens)Keras Hub is organized around several key architectural patterns:
BertBackbone, GPT2Backbone)BertTextClassifier, GPT2CausalLM)The library follows consistent naming patterns: {Architecture}{Task} for task models, {Architecture}Backbone for backbones, and {Architecture}{Task}Preprocessor for preprocessors.
Complete implementations of transformer models for natural language processing tasks including classification, masked language modeling, causal language modeling, and sequence-to-sequence tasks.
# Base classes
class CausalLM: ...
class MaskedLM: ...
class Seq2SeqLM: ...
class TextClassifier: ...
# Example architectures
class BertTextClassifier: ...
class GPT2CausalLM: ...
class BartSeq2SeqLM: ...Vision models for image classification, object detection, and image segmentation tasks with popular architectures like ResNet, Vision Transformer, and EfficientNet.
# Base classes
class ImageClassifier: ...
class ObjectDetector: ...
class ImageSegmenter: ...
# Example architectures
class ResNetImageClassifier: ...
class ViTImageClassifier: ...
class RetinaNetObjectDetector: ...Audio processing models for speech recognition and audio-to-text conversion.
class WhisperBackbone: ...
class MoonshineAudioToText: ...Models that process multiple modalities like text and images together for advanced AI capabilities.
class CLIPBackbone: ...
class PaliGemmaCausalLM: ...
class SigLIPBackbone: ...Advanced generative models for text-to-image synthesis and image manipulation.
class StableDiffusion3TextToImage: ...
class FluxTextToImage: ...
class StableDiffusion3Inpaint: ...Text tokenization utilities supporting various algorithms including byte-pair encoding, WordPiece, and SentencePiece.
class Tokenizer: ...
class BytePairTokenizer: ...
class WordPieceTokenizer: ...
class SentencePieceTokenizer: ...Reusable neural network layers and components for building custom models or extending existing architectures.
class TransformerEncoder: ...
class TransformerDecoder: ...
class CachedMultiHeadAttention: ...
class PositionEmbedding: ...Sampling strategies for controlling text generation behavior in language models.
class Sampler: ...
class GreedySampler: ...
class TopKSampler: ...
class BeamSampler: ...Metrics for evaluating model performance on various tasks including text generation and classification.
class Bleu: ...
class RougeL: ...
class Perplexity: ...Utility functions for dataset processing, model hub integration, and common operations.
def upload_preset(uri: str, preset: str) -> None: ...
def imagenet_id_to_name(class_id: int) -> str: ...
def coco_id_to_name(class_id: int) -> str: ...__version__: str = "0.22.1"
def version() -> str:
"""Return the current version string."""
...