liveBook · Manning

KerasHub

As explained in Deep Learning with Python, Third Edition

A Keras-hosted library providing pretrained models and high-level tasks such as Stable Diffusion and TextToImage.

KerasHub lets you use pretrained Keras models without recreating their architectures and loading their weights by hand. Its `from_preset()` constructors load named presets; high-level task classes also package common inputs and outputs. For example, `TextToImage` wraps tokenization and diffusion behind `generate()`. section 15.4.2 section 17.3

Listing 17.8 Creating a Stable Diffusion text-to-image model
import keras_hub

height, width = 512, 512
task = keras_hub.models.TextToImage.from_preset(
    "stable_diffusion_3_medium",
    image_shape=(height, width, 3),
    dtype="float16",                                                 #1
)
prompt = "A NASA astraunaut riding an origami elephant in New York City"
task.generate(prompt)

Load a Stable Diffusion 3 preset, set the image shape and dtype, then generate an image from a prompt. section 17.3

Choose the interface for the job. A task such as `ImageSegmenter`, `CausalLM`, or `TextToImage` provides a ready-to-use model. A `Backbone` is useful when you need pretrained features but will build your own prediction head. For text, load a tokenizer that matches the preset: different token IDs would prevent the model from using the representations it learned during pretraining. section 11.3.1 section 15.4.2 section 12.2.2

Listing 15.20 Loading the RoBERTa pretrained model with KerasHub
import keras_hub
 
tokenizer = keras_hub.models.Tokenizer.from_preset("roberta_base_en")
backbone = keras_hub.models.Backbone.from_preset("roberta_base_en")

Load the matching RoBERTa tokenizer and backbone from the same preset. section 15.4.2

A practical way to choose a starting point:

  • Use `ImageSegmenter.from_preset("sam_huge_sa1b")` for a pretrained segmentation model; this SAM preset has 641 million parameters. section 11.3.1
  • Use `CausalLM.from_preset("gemma3_1b")` for text generation, then `compile()` with a sampling strategy and call `generate()`. Downloading these weights requires accepting Gemma’s terms and authenticating with Kaggle. section 16.3.1
  • Use a pretrained ResNet `Backbone` and matching `ImageConverter` when attaching a custom YOLO detection head. section 12.2.2
  • Use KerasHub’s `SentencePieceTokenizer` layer when preparing token sequences for language-model training; it supports both tokenization and detokenization. section 16.2

Pretrained does not mean effortless or flawless. Training an effective text-to-image diffusion model from scratch is expensive, while generated images can contain visible artifacts. Treat the preset as a practical starting point, and inspect its outputs. section 17.3

How Deep Learning with Python, Third Edition covers it

How deep it goes
Demonstrates preset loading, matching tokenizers, task-level generation, and backbone reuse; defers training effective text-to-image diffusion models from scratch.
Examples
Loads RoBERTa’s tokenizer and backbone and generates images with a Stable Diffusion 3 preset.
What it assumes
Assumes familiarity with Keras models and tokenization; avoids requiring readers to recreate pretrained architectures or weights.
What you can do after
Apply it via KerasHub to load pretrained task models or backbones and generate text or images.
How it connects
Connects pretrained weights to tokenization, Transformer backbones, image segmentation, object detection, causal language modeling, and diffusion models.

Also explained in

Compare how 2 books explain KerasHub →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.