Pretrained models for Keras with multi-framework compatibility.
—
Utility functions for dataset processing, model hub integration, and common operations. Keras Hub provides helpers for working with standard datasets and uploading models to various hubs.
Functions for uploading and managing model presets on various model hubs.
def upload_preset(uri: str, preset: str) -> None:
"""
Upload a preset directory to a model hub.
Supports uploading to Kaggle Models and other model hosting platforms.
The URI format determines the target platform:
- Kaggle: kaggle://<username>/<model>/<framework>/<variation>
- HuggingFace: hf://<username>/<model>
- Google Cloud Storage: gs://<bucket>/<path>
Args:
uri: The URI identifying where to upload the model.
Format depends on the target platform.
preset: Path to the local preset directory containing model files,
configuration, and metadata.
Raises:
ValueError: If URI format is invalid or unsupported
ConnectionError: If upload fails due to network issues
AuthenticationError: If credentials are invalid or missing
Example:
# Upload to Kaggle Models
keras_hub.upload_preset(
"kaggle://username/bert-sentiment/keras/v1",
"./my_bert_preset"
)
"""
...Helper functions for working with popular computer vision datasets.
def imagenet_id_to_name(class_id: int) -> str:
"""
Convert ImageNet class ID to human-readable class name.
Args:
class_id: Integer class ID (0-999 for ImageNet-1K)
Returns:
Human-readable class name string
Raises:
ValueError: If class_id is outside valid range
Example:
name = keras_hub.utils.imagenet_id_to_name(285)
# Returns: "Egyptian cat"
"""
...
def imagenet_name_to_id(class_name: str) -> int:
"""
Convert ImageNet class name to class ID.
Args:
class_name: Human-readable class name
Returns:
Integer class ID
Raises:
KeyError: If class_name not found in ImageNet classes
Example:
class_id = keras_hub.utils.imagenet_name_to_id("Egyptian cat")
# Returns: 285
"""
...
def decode_imagenet_predictions(predictions, top: int = 5):
"""
Decode ImageNet model predictions to human-readable format.
Args:
predictions: Model prediction array of shape (batch_size, 1000)
or (1000,) for single prediction
top: Number of top predictions to return
Returns:
List of tuples (class_id, class_name, probability) for top predictions
Example:
# predictions shape: (1, 1000)
decoded = keras_hub.utils.decode_imagenet_predictions(predictions, top=3)
# Returns: [(285, "Egyptian cat", 0.85), (281, "tabby cat", 0.12), ...]
"""
...Helper functions for working with the COCO (Common Objects in Context) dataset.
def coco_id_to_name(class_id: int) -> str:
"""
Convert COCO class ID to human-readable class name.
Args:
class_id: Integer class ID (1-80 for COCO)
Returns:
Human-readable class name string
Raises:
ValueError: If class_id is outside valid range
Example:
name = keras_hub.utils.coco_id_to_name(1)
# Returns: "person"
"""
...
def coco_name_to_id(class_name: str) -> int:
"""
Convert COCO class name to class ID.
Args:
class_name: Human-readable class name
Returns:
Integer class ID
Raises:
KeyError: If class_name not found in COCO classes
Example:
class_id = keras_hub.utils.coco_name_to_id("person")
# Returns: 1
"""
...Functions and constants for accessing version information.
def version() -> str:
"""
Return the current Keras Hub version string.
Returns:
Version string in semantic versioning format
Example:
version_str = keras_hub.version()
# Returns: "0.22.1"
"""
...
__version__: str = "0.22.1"
"""Current Keras Hub version as a string constant."""import keras_hub
import numpy as np
# Load an ImageNet-trained model
model = keras_hub.models.ResNetImageClassifier.from_preset("resnet50_imagenet")
# Make predictions (example with random data)
images = np.random.random((2, 224, 224, 3))
predictions = model.predict(images)
# Decode predictions to human-readable format
for i, pred in enumerate(predictions):
decoded = keras_hub.utils.decode_imagenet_predictions(pred, top=3)
print(f"Image {i+1} predictions:")
for class_id, class_name, probability in decoded:
print(f" {class_name} (ID: {class_id}): {probability:.3f}")
print()import keras_hub
# Convert ImageNet class ID to name
class_id = 285
class_name = keras_hub.utils.imagenet_id_to_name(class_id)
print(f"Class {class_id}: {class_name}")
# Convert back to ID
recovered_id = keras_hub.utils.imagenet_name_to_id(class_name)
print(f"Class '{class_name}': ID {recovered_id}")
# COCO dataset utilities
coco_id = 1
coco_name = keras_hub.utils.coco_id_to_name(coco_id)
print(f"COCO class {coco_id}: {coco_name}")
coco_recovered_id = keras_hub.utils.coco_name_to_id(coco_name)
print(f"COCO class '{coco_name}': ID {coco_recovered_id}")import keras_hub
# Train and save a custom model
model = keras_hub.models.BertTextClassifier.from_preset("bert_base_en", num_classes=2)
# Fine-tune the model on your data
# model.fit(train_data, epochs=3)
# Save as preset
model.save_to_preset("./my_custom_bert_sentiment")
# Upload to Kaggle Models
try:
keras_hub.upload_preset(
uri="kaggle://myusername/bert-sentiment-classifier/keras/v1",
preset="./my_custom_bert_sentiment"
)
print("Successfully uploaded to Kaggle Models!")
except Exception as e:
print(f"Upload failed: {e}")import keras_hub
# Get version information
current_version = keras_hub.version()
print(f"Keras Hub version: {current_version}")
# Access version constant
version_constant = keras_hub.__version__
print(f"Version constant: {version_constant}")
# Check version compatibility
required_version = "0.22.0"
if keras_hub.__version__ >= required_version:
print("Version requirements satisfied")
else:
print(f"Please upgrade to version {required_version} or higher")import keras_hub
import numpy as np
def process_imagenet_batch(model, images, top_k=5):
"""Process a batch of images and return decoded predictions."""
predictions = model.predict(images)
results = []
for i, pred in enumerate(predictions):
decoded = keras_hub.utils.decode_imagenet_predictions(pred, top=top_k)
results.append({
'image_index': i,
'predictions': decoded
})
return results
# Example usage
model = keras_hub.models.EfficientNetImageClassifier.from_preset("efficientnet_b0_imagenet")
batch_images = np.random.random((4, 224, 224, 3))
results = process_imagenet_batch(model, batch_images, top_k=3)
for result in results:
print(f"Image {result['image_index']}:")
for class_id, class_name, prob in result['predictions']:
print(f" {class_name}: {prob:.3f}")
print()import keras_hub
import numpy as np
# Load object detection model
detector = keras_hub.models.RetinaNetObjectDetector.from_preset("retinanet_resnet50_pascalvoc")
# Make predictions (example with random data)
images = np.random.random((1, 512, 512, 3))
detections = detector.predict(images)
# Process detections (assuming COCO-format output)
def process_detections(detections, confidence_threshold=0.5):
"""Process detection results and convert class IDs to names."""
processed = []
# Extract detection components (format depends on model)
boxes = detections['boxes'][0] # Bounding boxes
classes = detections['classes'][0] # Class predictions
scores = detections['scores'][0] # Confidence scores
# Filter by confidence
valid_detections = scores > confidence_threshold
for i in range(len(boxes)):
if valid_detections[i]:
class_id = int(classes[i])
try:
class_name = keras_hub.utils.coco_id_to_name(class_id)
except ValueError:
class_name = f"Class_{class_id}"
processed.append({
'box': boxes[i],
'class_id': class_id,
'class_name': class_name,
'score': float(scores[i])
})
return processed
# Process results
results = process_detections(detections)
for detection in results:
print(f"Detected {detection['class_name']} "
f"(score: {detection['score']:.3f}) "
f"at {detection['box']}")import keras_hub
import os
def upload_model_safely(model, preset_name, upload_uri):
"""Safely upload a model with proper error handling."""
# Save model to temporary preset directory
temp_preset_dir = f"./temp_preset_{preset_name}"
try:
# Save model as preset
print("Saving model as preset...")
model.save_to_preset(temp_preset_dir)
# Upload to model hub
print(f"Uploading to {upload_uri}...")
keras_hub.upload_preset(upload_uri, temp_preset_dir)
print("Upload completed successfully!")
return True
except FileNotFoundError:
print("Error: Model files not found")
return False
except ConnectionError:
print("Error: Network connection failed")
return False
except PermissionError:
print("Error: Authentication failed or insufficient permissions")
return False
except Exception as e:
print(f"Unexpected error: {e}")
return False
finally:
# Clean up temporary files
if os.path.exists(temp_preset_dir):
import shutil
shutil.rmtree(temp_preset_dir)
print("Cleaned up temporary files")
# Example usage
model = keras_hub.models.GPT2CausalLM.from_preset("gpt2_base_en")
success = upload_model_safely(
model,
"my_gpt2_model",
"kaggle://username/my-gpt2/keras/v1"
)
if success:
print("Model is now available on Kaggle Models!")Functions for creating and managing tokenizer vocabularies.
def compute_sentence_piece_proto(
data: list,
vocab_size: int,
model_type: str = "unigram",
pad_id: int = 0,
eos_id: int = 1,
unk_id: int = 2,
bos_id: int = -1,
**kwargs
) -> bytes:
"""
Compute a SentencePiece model proto from training data.
Creates a SentencePiece model protocol buffer that can be used
to initialize SentencePiece tokenizers.
Args:
data: List of strings to train the tokenizer on
vocab_size: Target vocabulary size
model_type: SentencePiece model type ("unigram", "bpe", "word", "char")
pad_id: ID for padding token
eos_id: ID for end-of-sequence token
unk_id: ID for unknown token
bos_id: ID for beginning-of-sequence token (-1 to disable)
**kwargs: Additional SentencePiece training parameters
Returns:
bytes: Serialized SentencePiece model proto
Example:
training_data = ["Hello world", "How are you", "Fine thanks"]
proto = keras_hub.tokenizers.compute_sentence_piece_proto(
data=training_data,
vocab_size=1000,
model_type="unigram"
)
"""
...
def compute_word_piece_vocabulary(
data: list,
vocab_size: int,
reserved_tokens: list = None,
**kwargs
) -> dict:
"""
Compute a WordPiece vocabulary from training data.
Creates a WordPiece vocabulary dictionary suitable for use with
WordPieceTokenizer.
Args:
data: List of strings to train the vocabulary on
vocab_size: Target vocabulary size
reserved_tokens: List of tokens to reserve (e.g., ["[PAD]", "[UNK]"])
**kwargs: Additional vocabulary computation parameters
Returns:
dict: Vocabulary mapping from token to ID
Example:
training_data = ["Hello world", "How are you", "Fine thanks"]
vocab = keras_hub.tokenizers.compute_word_piece_vocabulary(
data=training_data,
vocab_size=1000,
reserved_tokens=["[PAD]", "[UNK]", "[CLS]", "[SEP]"]
)
"""
...import keras_hub
import numpy as np
def analyze_image_predictions(model, images, detailed=True):
"""Comprehensive analysis of image classification predictions."""
# Get predictions
predictions = model.predict(images)
analysis = {
'total_images': len(images),
'model_version': keras_hub.version(),
'predictions': []
}
for i, pred in enumerate(predictions):
# Get top predictions
top_preds = keras_hub.utils.decode_imagenet_predictions(pred, top=5)
# Calculate prediction confidence
top_prob = top_preds[0][2]
entropy = -np.sum(pred * np.log(pred + 1e-10))
image_analysis = {
'image_index': i,
'top_prediction': {
'class_id': top_preds[0][0],
'class_name': top_preds[0][1],
'probability': float(top_prob)
},
'confidence_level': 'high' if top_prob > 0.8 else 'medium' if top_prob > 0.5 else 'low',
'entropy': float(entropy)
}
if detailed:
image_analysis['all_top_predictions'] = [
{
'class_id': int(cid),
'class_name': cname,
'probability': float(prob)
}
for cid, cname, prob in top_preds
]
analysis['predictions'].append(image_analysis)
return analysis
# Example usage
model = keras_hub.models.ViTImageClassifier.from_preset("vit_base_patch16_224")
test_images = np.random.random((3, 224, 224, 3))
analysis = analyze_image_predictions(model, test_images, detailed=True)
print(f"Analysis using Keras Hub {analysis['model_version']}")
print(f"Processed {analysis['total_images']} images")
for pred in analysis['predictions']:
top_pred = pred['top_prediction']
print(f"\nImage {pred['image_index']}:")
print(f" Top prediction: {top_pred['class_name']} ({top_pred['probability']:.3f})")
print(f" Confidence: {pred['confidence_level']}")
print(f" Entropy: {pred['entropy']:.3f}")Install with Tessl CLI
npx tessl i tessl/pypi-keras-hub