StableDiffusion3TextToImage
类keras_hub.models.StableDiffusion3TextToImage(backbone, preprocessor, **kwargs)
用于文本到图像生成的端到端 Stable Diffusion 3 模型。
此模型有一个 generate()
方法,可以根据提示生成图像。
参数
keras_hub.models.StableDiffusion3Backbone
实例。keras_hub.models.StableDiffusion3TextToImagePreprocessor
实例。示例
使用 generate()
进行图像生成。
text_to_image = keras_hub.models.StableDiffusion3TextToImage.from_preset(
"stable_diffusion_3_medium", image_shape=(512, 512, 3)
)
text_to_image.generate(
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
)
# Generate with batched prompts.
text_to_image.generate(
["cute wallpaper art of a cat", "cute wallpaper art of a dog"]
)
# Generate with different `num_steps` and `guidance_scale`.
text_to_image.generate(
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
num_steps=50,
guidance_scale=5.0,
)
# Generate with `negative_prompts`.
prompt = (
"Astronaut in a jungle, cold color palette, muted colors, "
"detailed, 8k"
)
text_to_image.generate(
{
"prompts": prompt,
"negative_prompts": "green color",
}
)
from_preset
方法StableDiffusion3TextToImage.from_preset(preset, load_weights=True, **kwargs)
从模型预设实例化 keras_hub.models.Task
。
预设是一个包含配置、权重和其他文件资源的目录,用于保存和加载预训练模型。 preset
可以作为以下之一传递
'bert_base_en'
'kaggle://user/bert/keras/bert_base_en'
'hf://user/bert_base_en'
'./bert_base_en'
对于任何 Task
子类,您可以运行 cls.presets.keys()
来列出该类上所有可用的内置预设。
此构造函数可以通过两种方式之一调用。可以从任务特定的基类(如 keras_hub.models.CausalLM.from_preset()
)调用,也可以从模型类(如 keras_hub.models.BertTextClassifier.from_preset()
)调用。如果从基类调用,则返回对象的子类将从预设目录中的配置推断出来。
参数
True
,则保存的权重将加载到模型架构中。如果为 False
,则所有权重都将随机初始化。示例
# Load a Gemma generative task.
causal_lm = keras_hub.models.CausalLM.from_preset(
"gemma_2b_en",
)
# Load a Bert classification task.
model = keras_hub.models.TextClassifier.from_preset(
"bert_base_en",
num_classes=2,
)
预设 | 参数 | 描述 |
---|---|---|
stable_diffusion_3_medium | 2.99B | 30 亿参数,包括 CLIP L 和 CLIP G 文本编码器、MMDiT 生成模型和 VAE 自编码器。由 Stability AI 开发。 |
stable_diffusion_3.5_medium | 3.37B | 30 亿参数,包括 CLIP L 和 CLIP G 文本编码器、MMDiT-X 生成模型和 VAE 自编码器。由 Stability AI 开发。 |
stable_diffusion_3.5_large | 9.05B | 90 亿参数,包括 CLIP L 和 CLIP G 文本编码器、MMDiT 生成模型和 VAE 自编码器。由 Stability AI 开发。 |
stable_diffusion_3.5_large_turbo | 9.05B | 90 亿参数,包括 CLIP L 和 CLIP G 文本编码器、MMDiT 生成模型和 VAE 自编码器。一个时间步蒸馏版本,消除了无分类器指导,并使用更少的步骤进行生成。由 Stability AI 开发。 |
backbone
属性keras_hub.models.StableDiffusion3TextToImage.backbone
具有核心架构的 keras_hub.models.Backbone
模型。
generate
方法StableDiffusion3TextToImage.generate(
inputs, num_steps=28, guidance_scale=7.0, seed=None
)
根据提供的 inputs
生成图像。
通常,inputs
包含用于指导图像生成的文本描述(称为提示)。
一些模型支持 negative_prompts
键,这有助于引导模型远离生成某些样式和元素。要启用此功能,请将 prompts
和 negative_prompts
作为字典传递
prompt = (
"Astronaut in a jungle, cold color palette, muted colors, "
"detailed, 8k"
)
text_to_image.generate(
{
"prompts": prompt,
"negative_prompts": "green color",
}
)
如果 inputs
是 tf.data.Dataset
,则输出将“按批次”生成并连接。否则,所有输入都将作为批次处理。
参数
tf.data.Dataset
。格式必须是以下之一tf.data.Dataset
,带有 "prompts" 和/或 "negative_prompts" 键preprocessor
属性keras_hub.models.StableDiffusion3TextToImage.preprocessor
用于预处理输入的 keras_hub.models.Preprocessor
层。