CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-keras-hub

Pretrained models for Keras with multi-framework compatibility.

Pending
Overview
Eval results
Files

generative-models.mddocs/

Generative Models

Advanced generative models for text-to-image synthesis, image-to-image transformation, and image inpainting. Keras Hub provides implementations of state-of-the-art diffusion models including Stable Diffusion 3 and Flux.

Capabilities

Base Classes

Foundation classes for different types of generative models.

class TextToImage(Task):
    """Base class for text-to-image generation models."""
    def __init__(
        self,
        backbone: Backbone,
        preprocessor: Preprocessor = None,
        **kwargs
    ): ...
    
    def generate(
        self,
        inputs,
        num_steps: int = 50,
        guidance_scale: float = 7.5,
        **kwargs
    ): ...

class ImageToImage(Task):
    """Base class for image-to-image generation models."""
    def __init__(
        self,
        backbone: Backbone,
        preprocessor: Preprocessor = None,
        **kwargs
    ): ...

class Inpaint(Task):
    """Base class for image inpainting models."""
    def __init__(
        self,
        backbone: Backbone,
        preprocessor: Preprocessor = None,
        **kwargs
    ): ...

Stable Diffusion 3

Stable Diffusion 3 is an advanced text-to-image diffusion model with improved quality and prompt adherence.

class StableDiffusion3Backbone(Backbone):
    """Stable Diffusion 3 backbone architecture."""
    def __init__(
        self,
        height: int = 1024,
        width: int = 1024,
        num_train_timesteps: int = 1000,
        shift: float = 3.0,
        **kwargs
    ): ...

class StableDiffusion3TextToImage(TextToImage):
    """Stable Diffusion 3 model for text-to-image generation."""
    def __init__(
        self,
        backbone: StableDiffusion3Backbone,
        preprocessor: Preprocessor = None,
        **kwargs
    ): ...

class StableDiffusion3ImageToImage(ImageToImage):
    """Stable Diffusion 3 model for image-to-image generation."""
    def __init__(
        self,
        backbone: StableDiffusion3Backbone,
        preprocessor: Preprocessor = None,
        **kwargs
    ): ...

class StableDiffusion3Inpaint(Inpaint):
    """Stable Diffusion 3 model for image inpainting."""
    def __init__(
        self,
        backbone: StableDiffusion3Backbone,
        preprocessor: Preprocessor = None,
        **kwargs
    ): ...

class StableDiffusion3TextToImagePreprocessor:
    """Preprocessor for Stable Diffusion 3 text-to-image generation."""
    def __init__(
        self,
        height: int = 1024,
        width: int = 1024,
        **kwargs
    ): ...

Flux

Flux is a high-quality text-to-image diffusion model with excellent prompt following capabilities.

class FluxBackbone(Backbone):
    """Flux diffusion model backbone."""
    def __init__(
        self,
        height: int = 1024,
        width: int = 1024,
        max_sequence_length: int = 512,
        **kwargs
    ): ...

class FluxTextToImage(TextToImage):
    """Flux model for text-to-image generation."""
    def __init__(
        self,
        backbone: FluxBackbone,
        preprocessor: Preprocessor = None,
        **kwargs
    ): ...

class FluxTextToImagePreprocessor:
    """Preprocessor for Flux text-to-image generation."""
    def __init__(
        self,
        height: int = 1024,
        width: int = 1024,
        max_sequence_length: int = 512,
        **kwargs
    ): ...

Preprocessor Base Classes

Base classes for generative model preprocessing.

class TextToImagePreprocessor(Preprocessor):
    """Base preprocessor for text-to-image models."""
    def __init__(
        self,
        height: int = 512,
        width: int = 512,
        **kwargs
    ): ...

Usage Examples

Text-to-Image Generation with Stable Diffusion 3

import keras_hub

# Load pretrained Stable Diffusion 3 model
model = keras_hub.models.StableDiffusion3TextToImage.from_preset("stable_diffusion_3_medium")

# Generate image from text
prompt = "A serene landscape with mountains and a lake at sunset"
generated_image = model.generate(
    prompt,
    num_steps=50,
    guidance_scale=7.5,
    height=1024,
    width=1024
)

print(f"Generated image shape: {generated_image.shape}")
# Save or display the generated image

Image-to-Image with Stable Diffusion 3

import keras_hub
import numpy as np

# Load image-to-image model
model = keras_hub.models.StableDiffusion3ImageToImage.from_preset("stable_diffusion_3_medium")

# Prepare input image and prompt
input_image = np.random.random((1024, 1024, 3))  # Example input image
prompt = "Transform this into a painting in the style of Van Gogh"

# Generate transformed image
generated_image = model.generate(
    [input_image, prompt],
    num_steps=50,
    guidance_scale=7.5,
    strength=0.8  # How much to transform the input image
)

print(f"Transformed image shape: {generated_image.shape}")

Image Inpainting with Stable Diffusion 3

import keras_hub
import numpy as np

# Load inpainting model
model = keras_hub.models.StableDiffusion3Inpaint.from_preset("stable_diffusion_3_medium")

# Prepare input image, mask, and prompt
input_image = np.random.random((1024, 1024, 3))  # Example input image
mask = np.zeros((1024, 1024, 1))  # Mask where 1 indicates areas to inpaint
mask[400:600, 400:600] = 1  # Square region to inpaint
prompt = "A beautiful flower in the center"

# Generate inpainted image
inpainted_image = model.generate(
    [input_image, mask, prompt],
    num_steps=50,
    guidance_scale=7.5
)

print(f"Inpainted image shape: {inpainted_image.shape}")

Text-to-Image with Flux

import keras_hub

# Load Flux model
model = keras_hub.models.FluxTextToImage.from_preset("flux_1_dev")

# Generate high-quality image
prompt = "A photorealistic portrait of a wise old wizard with a long white beard, wearing a pointed hat, in a mystical forest setting"

generated_image = model.generate(
    prompt,
    num_steps=50,
    guidance_scale=3.5,  # Flux typically uses lower guidance scales
    height=1024,
    width=1024
)

print(f"Generated image shape: {generated_image.shape}")

Batch Generation

import keras_hub

# Load model
model = keras_hub.models.StableDiffusion3TextToImage.from_preset("stable_diffusion_3_medium")

# Generate multiple images from different prompts
prompts = [
    "A cat sitting on a windowsill",
    "A futuristic cityscape at night",
    "A peaceful garden with flowers"
]

# Generate batch of images
generated_images = model.generate(
    prompts,
    num_steps=50,
    guidance_scale=7.5,
    height=512,
    width=512
)

print(f"Generated batch shape: {generated_images.shape}")
# Shape will be (3, 512, 512, 3) for 3 images

Custom Generation Pipeline

import keras_hub

# Create custom backbone
backbone = keras_hub.models.StableDiffusion3Backbone(
    height=768,
    width=768,
    num_train_timesteps=1000
)

# Create preprocessor
preprocessor = keras_hub.models.StableDiffusion3TextToImagePreprocessor(
    height=768,
    width=768
)

# Create custom model
model = keras_hub.models.StableDiffusion3TextToImage(
    backbone=backbone,
    preprocessor=preprocessor
)

# Use for generation
generated_image = model.generate(
    "A custom prompt",
    num_steps=30,
    guidance_scale=8.0
)

Controlling Generation Parameters

import keras_hub

# Load model
model = keras_hub.models.StableDiffusion3TextToImage.from_preset("stable_diffusion_3_medium")

# Fine-tune generation parameters
generated_image = model.generate(
    "A detailed digital artwork of a dragon",
    num_steps=75,  # More steps for higher quality
    guidance_scale=10.0,  # Higher guidance for stronger prompt adherence
    height=1024,
    width=1024,
    seed=42  # Set seed for reproducible results
)

print("Generated image with custom parameters")

Install with Tessl CLI

npx tessl i tessl/pypi-keras-hub

docs

audio-models.md

evaluation-metrics.md

generative-models.md

image-models.md

index.md

layers-components.md

multimodal-models.md

text-generation-sampling.md

text-models.md

tokenizers.md

utilities-helpers.md

tile.json