Pretrained models for Keras with multi-framework compatibility.
npx @tessl/cli install tessl/pypi-keras-hub@0.22.00
# Keras Hub
1
2
Keras 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.
3
4
## Package Information
5
6
- **Package Name**: keras-hub
7
- **Language**: Python
8
- **Installation**: `pip install keras-hub` (for NLP models: `pip install keras-hub[nlp]`)
9
- **License**: Apache-2.0
10
- **Documentation**: https://keras.io/keras_hub/
11
12
## Core Imports
13
14
```python
15
import keras_hub
16
```
17
18
Common pattern for specific components:
19
20
```python
21
# Models - most commonly loaded with from_preset()
22
from keras_hub.models import BertTextClassifier, GPT2CausalLM
23
from keras_hub.models import ImageClassifier
24
25
# Tokenizers
26
from keras_hub.tokenizers import BertTokenizer
27
28
# Layers
29
from keras_hub.layers import TransformerEncoder
30
31
# Metrics
32
from keras_hub.metrics import Bleu
33
34
# Utils
35
from keras_hub.utils import upload_preset
36
```
37
38
## Basic Usage
39
40
```python
41
import keras_hub
42
43
# Load a pretrained model for text classification
44
classifier = keras_hub.models.BertTextClassifier.from_preset("bert_base_en")
45
46
# Classify text
47
result = classifier.predict(["This is a great movie!", "I didn't like this film."])
48
print(result)
49
50
# Load a causal language model for text generation
51
generator = keras_hub.models.GPT2CausalLM.from_preset("gpt2_base_en")
52
53
# Generate text
54
generated = generator.generate("The weather today is", max_length=50)
55
print(generated)
56
57
# Use tokenizers directly
58
tokenizer = keras_hub.tokenizers.BertTokenizer.from_preset("bert_base_en")
59
tokens = tokenizer(["Hello world!", "How are you?"])
60
print(tokens)
61
```
62
63
## Architecture
64
65
Keras Hub is organized around several key architectural patterns:
66
67
- **Backbones**: Core model architectures without task-specific heads (e.g., `BertBackbone`, `GPT2Backbone`)
68
- **Task Models**: Complete models with task-specific heads (e.g., `BertTextClassifier`, `GPT2CausalLM`)
69
- **Preprocessors**: Handle data preprocessing for specific models and tasks
70
- **Tokenizers**: Convert text to tokens for model input
71
- **Layers**: Reusable neural network components for building custom models
72
- **Samplers**: Text generation strategies for controlling output
73
74
The library follows consistent naming patterns: `{Architecture}{Task}` for task models, `{Architecture}Backbone` for backbones, and `{Architecture}{Task}Preprocessor` for preprocessors.
75
76
## Capabilities
77
78
### Text Models
79
80
Complete implementations of transformer models for natural language processing tasks including classification, masked language modeling, causal language modeling, and sequence-to-sequence tasks.
81
82
```python { .api }
83
# Base classes
84
class CausalLM: ...
85
class MaskedLM: ...
86
class Seq2SeqLM: ...
87
class TextClassifier: ...
88
89
# Example architectures
90
class BertTextClassifier: ...
91
class GPT2CausalLM: ...
92
class BartSeq2SeqLM: ...
93
```
94
95
[Text Models](./text-models.md)
96
97
### Image Models
98
99
Vision models for image classification, object detection, and image segmentation tasks with popular architectures like ResNet, Vision Transformer, and EfficientNet.
100
101
```python { .api }
102
# Base classes
103
class ImageClassifier: ...
104
class ObjectDetector: ...
105
class ImageSegmenter: ...
106
107
# Example architectures
108
class ResNetImageClassifier: ...
109
class ViTImageClassifier: ...
110
class RetinaNetObjectDetector: ...
111
```
112
113
[Image Models](./image-models.md)
114
115
### Audio Models
116
117
Audio processing models for speech recognition and audio-to-text conversion.
118
119
```python { .api }
120
class WhisperBackbone: ...
121
class MoonshineAudioToText: ...
122
```
123
124
[Audio Models](./audio-models.md)
125
126
### Multimodal Models
127
128
Models that process multiple modalities like text and images together for advanced AI capabilities.
129
130
```python { .api }
131
class CLIPBackbone: ...
132
class PaliGemmaCausalLM: ...
133
class SigLIPBackbone: ...
134
```
135
136
[Multimodal Models](./multimodal-models.md)
137
138
### Generative Models
139
140
Advanced generative models for text-to-image synthesis and image manipulation.
141
142
```python { .api }
143
class StableDiffusion3TextToImage: ...
144
class FluxTextToImage: ...
145
class StableDiffusion3Inpaint: ...
146
```
147
148
[Generative Models](./generative-models.md)
149
150
### Tokenizers
151
152
Text tokenization utilities supporting various algorithms including byte-pair encoding, WordPiece, and SentencePiece.
153
154
```python { .api }
155
class Tokenizer: ...
156
class BytePairTokenizer: ...
157
class WordPieceTokenizer: ...
158
class SentencePieceTokenizer: ...
159
```
160
161
[Tokenizers](./tokenizers.md)
162
163
### Layers and Components
164
165
Reusable neural network layers and components for building custom models or extending existing architectures.
166
167
```python { .api }
168
class TransformerEncoder: ...
169
class TransformerDecoder: ...
170
class CachedMultiHeadAttention: ...
171
class PositionEmbedding: ...
172
```
173
174
[Layers and Components](./layers-components.md)
175
176
### Text Generation Sampling
177
178
Sampling strategies for controlling text generation behavior in language models.
179
180
```python { .api }
181
class Sampler: ...
182
class GreedySampler: ...
183
class TopKSampler: ...
184
class BeamSampler: ...
185
```
186
187
[Text Generation Sampling](./text-generation-sampling.md)
188
189
### Evaluation Metrics
190
191
Metrics for evaluating model performance on various tasks including text generation and classification.
192
193
```python { .api }
194
class Bleu: ...
195
class RougeL: ...
196
class Perplexity: ...
197
```
198
199
[Evaluation Metrics](./evaluation-metrics.md)
200
201
### Utilities and Helpers
202
203
Utility functions for dataset processing, model hub integration, and common operations.
204
205
```python { .api }
206
def upload_preset(uri: str, preset: str) -> None: ...
207
def imagenet_id_to_name(class_id: int) -> str: ...
208
def coco_id_to_name(class_id: int) -> str: ...
209
```
210
211
[Utilities and Helpers](./utilities-helpers.md)
212
213
## Version Information
214
215
```python { .api }
216
__version__: str = "0.22.1"
217
218
def version() -> str:
219
"""Return the current version string."""
220
...
221
```