Diffusers documentation
Z-Image
Z-Image
Z-Image 是一个拥有 6B 参数、功能强大且高效能极高的图像生成模型。目前仅发布了一个模型,还有两个模型即将发布:
| 模型 | Hugging Face |
|---|---|
| Z-Image-Turbo | https://huggingface.co/Tongyi-MAI/Z-Image-Turbo |
Z-Image-Turbo
Z-Image-Turbo 是 Z-Image 的蒸馏版本,仅需 8 次 NFE(函数评估次数)即可达到或超越领先竞争对手的水平。它在企业级 H800 GPU 上可实现亚秒级推理延迟,并且能够轻松适配 16G 显存的消费级设备。它在照片级真实感图像生成、双语文本渲染(英文 & 中文)以及稳健的指令遵循方面表现出色。
图像到图像
使用 ZImageImg2ImgPipeline 根据文本提示对现有图像进行转换。
import torch
from diffusers import ZImageImg2ImgPipeline
from diffusers.utils import load_image
pipe = ZImageImg2ImgPipeline.from_pretrained("Tongyi-MAI/Z-Image-Turbo", dtype=torch.bfloat16)
pipe.to("cuda") # or "mps", "xpu", "cpu"
url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
init_image = load_image(url).resize((1024, 1024))
prompt = "A fantasy landscape with mountains and a river, detailed, vibrant colors"
image = pipe(
prompt,
image=init_image,
strength=0.6,
num_inference_steps=8,
guidance_scale=0.0,
generator=torch.Generator("cuda").manual_seed(42),
).images[0]
image.save("zimage_img2img.png")图像修复
使用 ZImageInpaintPipeline 根据文本提示和掩码对图像的特定区域进行修复。
import torch
import numpy as np
from PIL import Image
from diffusers import ZImageInpaintPipeline
from diffusers.utils import load_image
pipe = ZImageInpaintPipeline.from_pretrained("Tongyi-MAI/Z-Image-Turbo", dtype=torch.bfloat16)
pipe.to("cuda") # or "mps", "xpu", "cpu"
url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
init_image = load_image(url).resize((1024, 1024))
# Create a mask (white = inpaint, black = preserve)
mask = np.zeros((1024, 1024), dtype=np.uint8)
mask[256:768, 256:768] = 255 # Inpaint center region
mask_image = Image.fromarray(mask)
prompt = "A beautiful lake with mountains in the background"
image = pipe(
prompt,
image=init_image,
mask_image=mask_image,
strength=1.0,
num_inference_steps=8,
guidance_scale=0.0,
generator=torch.Generator("cuda").manual_seed(42),
).images[0]
image.save("zimage_inpaint.png")ZImagePipeline
class diffusers.ZImagePipeline
< source >( scheduler: FlowMatchEulerDiscreteSchedulervae: AutoencoderKLtext_encoder: PreTrainedModeltokenizer: AutoTokenizertransformer: ZImageTransformer2DModel )
__call__
< source >( prompt: str | list[str] = Noneheight: int | None = Nonewidth: int | None = Nonenum_inference_steps: int = 50sigmas: list[float] | None = Noneguidance_scale: float = 5.0cfg_normalization: bool = Falsecfg_truncation: float = 1.0negative_prompt: str | list[str] | None = Nonenum_images_per_prompt: int | None = 1generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: typing.Optional[torch.FloatTensor] = Noneprompt_embeds: list[torch.FloatTensor] | None = Nonenegative_prompt_embeds: list[torch.FloatTensor] | None = Noneoutput_type: str | None = 'pil'return_dict: bool = Truejoint_attention_kwargs: dict[str, typing.Any] | None = Nonecallback_on_step_end: typing.Optional[typing.Callable[[int, int], NoneType]] = Nonecallback_on_step_end_tensor_inputs: list = ['latents']max_sequence_length: int = 512 ) → ZImagePipelineOutput or tuple
参数
- prompt (
str或list[str],可选) — 用于引导图像生成的提示词或提示词列表。如果未定义,则必须改为传入prompt_embeds。 - height (
int,可选,默认为 1024) — 生成图像的高度(以像素为单位)。 - width (
int,可选,默认为 1024) — 生成图像的宽度(以像素为单位)。 - num_inference_steps (
int,可选,默认为 50) — 去噪步数。更多的去噪步数通常能生成更高质量的图像,但代价是推理速度变慢。 - sigmas (
list[float],可选) — 用于在支持sigmas参数的调度器的set_timesteps方法中,为去噪过程指定自定义的 sigmas。如果未定义,则使用传入num_inference_steps时的默认行为。 - guidance_scale (
float, 可选,默认为 5.0) — 如 Classifier-Free Diffusion Guidance 中定义的引导尺度。guidance_scale定义为 Imagen Paper 中公式 2 的w。通过设置guidance_scale > 1来启用引导尺度。较高的引导尺度会促使生成与文本prompt紧密关联的图像, 通常以牺牲图像质量为代价。 - cfg_normalization (
bool,可选,默认为 False) — 是否应用配置归一化。 - cfg_truncation (
float,可选,默认值为 1.0) — 用于配置的截断值。 - negative_prompt (
str或list[str],可选) — 用于不引导图像生成的提示词。如果未定义,则必须传入negative_prompt_embeds。在未使用引导时会被忽略(即当guidance_scale小于1时忽略)。 - num_images_per_prompt (
int,可选,默认为 1) — 每个提示词生成的图像数量。 - generator (
torch.Generator或list[torch.Generator],可选) — 一个或一组 torch 生成器,用于使生成过程具有确定性。 - latents (
torch.FloatTensor, 可选) — 预先生成的噪声潜变量,从高斯分布中采样,用作图像生成的输入。可用于使用不同的提示词对同一生成过程进行微调。如果未提供,将通过使用提供的随机generator进行采样来生成一个潜变量张量。 - prompt_embeds (
list[torch.FloatTensor], 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 提示词加权。如果未提供,则会根据prompt输入参数生成文本嵌入。 - negative_prompt_embeds (
list[torch.FloatTensor],可选) — 预生成的负向文本嵌入。可用于轻松调整文本输入,例如提示词加权。如果未提供,negative_prompt_embeds 将根据negative_prompt输入参数生成。 - output_type (
str,可选,默认为"pil") — 生成图像的输出格式。可选值为 PIL:PIL.Image.Image或np.array。 - return_dict (
bool,可选,默认为True) — 是否返回~pipelines.stable_diffusion.ZImagePipelineOutput而不是普通元组。 - joint_attention_kwargs (
dict, 可选) — 一个 kwargs 字典,如果指定,将作为参数传递给 diffusers.models.attention_processor 中self.processor下定义的AttentionProcessor。 - callback_on_step_end (
Callable, 可选) — 一个在推理过程中每个去噪步骤结束时调用的函数。该函数以以下参数被调用:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)。callback_kwargs将包含由callback_on_step_end_tensor_inputs指定的所有张量列表。 - callback_on_step_end_tensor_inputs (
List,可选) —callback_on_step_end函数的张量输入列表。列表中指定的张量将作为callback_kwargs参数传入。您只能包含流水线类的._callback_tensor_inputs属性中列出的变量。 - max_sequence_length (
int,可选,默认值为 512) — 与prompt一起使用的最大序列长度。
返回值
ZImagePipelineOutput 或 tuple
ZImagePipelineOutput,如果
return_dict 为 True,否则为 tuple。当返回元组时,第一个元素是一个包含生成图像的列表。
调用生成 pipeline 时调用的函数。
示例:
>>> import torch
>>> from diffusers import ZImagePipeline
>>> pipe = ZImagePipeline.from_pretrained("Z-a-o/Z-Image-Turbo", torch_dtype=torch.bfloat16)
>>> pipe.to("cuda")
>>> # Optionally, set the attention backend to flash-attn 2 or 3, default is SDPA in PyTorch.
>>> # (1) Use flash attention 2
>>> # pipe.transformer.set_attention_backend("flash")
>>> # (2) Use flash attention 3
>>> # pipe.transformer.set_attention_backend("_flash_3")
>>> prompt = "一幅为名为“造相「Z-IMAGE-TURBO」”的项目设计的创意海报。画面巧妙地将文字概念视觉化:一辆复古蒸汽小火车化身为巨大的拉链头,正拉开厚厚的冬日积雪,展露出一个生机盎然的春天。"
>>> image = pipe(
... prompt,
... height=1024,
... width=1024,
... num_inference_steps=8,
... guidance_scale=0.0,
... generator=torch.Generator("cuda").manual_seed(42),
... ).images[0]
>>> image.save("zimage.png")ZImageImg2ImgPipeline
class diffusers.ZImageImg2ImgPipeline
< source >( scheduler: FlowMatchEulerDiscreteSchedulervae: AutoencoderKLtext_encoder: PreTrainedModeltokenizer: AutoTokenizertransformer: ZImageTransformer2DModel )
参数
- scheduler (FlowMatchEulerDiscreteScheduler) —
用于与
transformer配合,对编码后的图像潜变量进行去噪的调度器。 - vae (AutoencoderKL) — 变分自编码器(VAE)模型,用于将图像编码为潜在表示,以及从潜在表示解码回图像。
- text_encoder (
PreTrainedModel) — 用于编码文本提示的文本编码器模型。 - tokenizer (
AutoTokenizer) — 用于对文本提示进行分词的 tokenizer。 - transformer (ZImageTransformer2DModel) — 用于对编码后的图像潜在变量进行去噪的 ZImage transformer 模型。
用于图像到图像生成的 ZImage 流水线。
__call__
< source >( prompt: str | list[str] = Noneimage: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, list[PIL.Image.Image], list[numpy.ndarray], list[torch.Tensor]] = Nonestrength: float = 0.6height: int | None = Nonewidth: int | None = Nonenum_inference_steps: int = 50sigmas: list[float] | None = Noneguidance_scale: float = 5.0cfg_normalization: bool = Falsecfg_truncation: float = 1.0negative_prompt: str | list[str] | None = Nonenum_images_per_prompt: int | None = 1generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: typing.Optional[torch.FloatTensor] = Noneprompt_embeds: list[torch.FloatTensor] | None = Nonenegative_prompt_embeds: list[torch.FloatTensor] | None = Noneoutput_type: str | None = 'pil'return_dict: bool = Truejoint_attention_kwargs: dict[str, typing.Any] | None = Nonecallback_on_step_end: typing.Optional[typing.Callable[[int, int], NoneType]] = Nonecallback_on_step_end_tensor_inputs: list = ['latents']max_sequence_length: int = 512 ) → ZImagePipelineOutput or tuple
参数
- prompt (
str或list[str],可选) — 用于引导图像生成的提示词或提示词列表。如果未定义,则必须改为传入prompt_embeds。 - image (
torch.Tensor、PIL.Image.Image、np.ndarray、list[torch.Tensor]、list[PIL.Image.Image]或list[np.ndarray]) —Image、numpy 数组或张量,表示用作起点的图像批次。对于 numpy 数组和 PyTorch 张量,预期的取值范围为[0, 1]。如果它是张量或张量列表,预期的形状应为(B, C, H, W)或(C, H, W)。如果它是 numpy 数组或数组列表,预期的形状应为(B, H, W, C)或(H, W, C)。 - strength (
float,可选,默认值为 0.6) — 指示对参考image进行变换的程度。取值必须在 0 到 1 之间。image用作起始点,strength越高,添加的噪声越多。去噪步数取决于初始添加的噪声量。当strength为 1 时,添加的噪声达到最大值,去噪过程将运行num_inference_steps中指定的完整迭代次数。值为 1 时,基本上会忽略image。 - height (
int,可选,默认为 1024) — 生成图像的高度(以像素为单位)。如果未提供,则使用输入图像的高度。 - width (
int,可选,默认为 1024) — 生成图像的宽度(以像素为单位)。如果未提供,则使用输入图像的宽度。 - num_inference_steps (
int,可选,默认为 50) — 去噪步数。更多的去噪步数通常能生成更高质量的图像,但代价是推理速度变慢。 - sigmas (
list[float],可选) — 用于在支持sigmas参数的调度器的set_timesteps方法中,为去噪过程指定自定义的 sigmas。如果未定义,则使用传入num_inference_steps时的默认行为。 - guidance_scale (
float, 可选,默认为 5.0) — 如 Classifier-Free Diffusion Guidance 中定义的引导尺度。guidance_scale定义为 Imagen Paper 中公式 2 的w。通过设置guidance_scale > 1来启用引导尺度。较高的引导尺度会促使生成与文本prompt紧密关联的图像, 通常以牺牲图像质量为代价。 - cfg_normalization (
bool,可选,默认为 False) — 是否应用配置归一化。 - cfg_truncation (
float,可选,默认值为 1.0) — 用于配置的截断值。 - negative_prompt (
str或list[str],可选) — 用于不引导图像生成的提示词。如果未定义,则必须传入negative_prompt_embeds。在未使用引导时会被忽略(即当guidance_scale小于1时忽略)。 - num_images_per_prompt (
int,可选,默认为 1) — 每个提示词生成的图像数量。 - generator (
torch.Generator或list[torch.Generator],可选) — 一个或一组 torch 生成器,用于使生成过程具有确定性。 - latents (
torch.FloatTensor, 可选) — 预先生成的噪声潜变量,从高斯分布中采样,用作图像生成的输入。可用于使用不同的提示词对同一生成过程进行微调。如果未提供,将通过使用提供的随机generator进行采样来生成一个潜变量张量。 - prompt_embeds (
list[torch.FloatTensor], 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 提示词加权。如果未提供,则会根据prompt输入参数生成文本嵌入。 - negative_prompt_embeds (
list[torch.FloatTensor],可选) — 预生成的负向文本嵌入。可用于轻松调整文本输入,例如提示词加权。如果未提供,negative_prompt_embeds 将根据negative_prompt输入参数生成。 - output_type (
str,可选,默认为"pil") — 生成图像的输出格式。可选值为 PIL:PIL.Image.Image或np.array。 - return_dict (
bool,可选,默认为True) — 是否返回~pipelines.stable_diffusion.ZImagePipelineOutput而不是普通元组。 - joint_attention_kwargs (
dict, 可选) — 一个 kwargs 字典,如果指定,将作为参数传递给 diffusers.models.attention_processor 中self.processor下定义的AttentionProcessor。 - callback_on_step_end (
Callable, 可选) — 一个在推理过程中每个去噪步骤结束时调用的函数。该函数以以下参数被调用:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)。callback_kwargs将包含由callback_on_step_end_tensor_inputs指定的所有张量列表。 - callback_on_step_end_tensor_inputs (
List,可选) —callback_on_step_end函数的张量输入列表。列表中指定的张量将作为callback_kwargs参数传入。您只能包含流水线类的._callback_tensor_inputs属性中列出的变量。 - max_sequence_length (
int,可选,默认值为 512) — 与prompt一起使用的最大序列长度。
返回值
ZImagePipelineOutput 或 tuple
ZImagePipelineOutput,如果
return_dict 为 True,否则为 tuple。当返回元组时,第一个元素是一个包含生成图像的列表。
调用用于图像到图像生成的 pipeline 时调用的函数。
示例:
>>> import torch
>>> from diffusers import ZImageImg2ImgPipeline
>>> from diffusers.utils import load_image
>>> pipe = ZImageImg2ImgPipeline.from_pretrained("Z-a-o/Z-Image-Turbo", torch_dtype=torch.bfloat16)
>>> pipe.to("cuda")
>>> url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
>>> init_image = load_image(url).resize((1024, 1024))
>>> prompt = "A fantasy landscape with mountains and a river, detailed, vibrant colors"
>>> image = pipe(
... prompt,
... image=init_image,
... strength=0.6,
... num_inference_steps=8,
... guidance_scale=0.0,
... generator=torch.Generator("cuda").manual_seed(42),
... ).images[0]
>>> image.save("zimage_img2img.png")ZImageInpaintPipeline
class diffusers.ZImageInpaintPipeline
< source >( scheduler: FlowMatchEulerDiscreteSchedulervae: AutoencoderKLtext_encoder: PreTrainedModeltokenizer: AutoTokenizertransformer: ZImageTransformer2DModel )
参数
- scheduler (FlowMatchEulerDiscreteScheduler) —
用于与
transformer配合,对编码后的图像潜变量进行去噪的调度器。 - vae (AutoencoderKL) — 变分自编码器(VAE)模型,用于将图像编码为潜在表示,以及从潜在表示解码回图像。
- text_encoder (
PreTrainedModel) — 用于编码文本提示的文本编码器模型。 - tokenizer (
AutoTokenizer) — 用于对文本提示进行分词的 tokenizer。 - transformer (ZImageTransformer2DModel) — 用于对编码后的图像潜在变量进行去噪的 ZImage transformer 模型。
用于图像修复的 ZImage 流水线。
__call__
< source >( prompt: typing.Union[str, typing.List[str]] = Noneimage: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, list[PIL.Image.Image], list[numpy.ndarray], list[torch.Tensor]] = Nonemask_image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, list[PIL.Image.Image], list[numpy.ndarray], list[torch.Tensor]] = Nonemasked_image_latents: typing.Optional[torch.FloatTensor] = Nonestrength: float = 1.0height: int | None = Nonewidth: int | None = Nonenum_inference_steps: int = 50sigmas: list[float] | None = Noneguidance_scale: float = 5.0cfg_normalization: bool = Falsecfg_truncation: float = 1.0negative_prompt: typing.Union[str, typing.List[str], NoneType] = Nonenum_images_per_prompt: typing.Optional[int] = 1generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: typing.Optional[torch.FloatTensor] = Noneprompt_embeds: typing.Optional[typing.List[torch.FloatTensor]] = Nonenegative_prompt_embeds: typing.Optional[typing.List[torch.FloatTensor]] = Noneoutput_type: str = 'pil'return_dict: bool = Truejoint_attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = Nonecallback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = Nonecallback_on_step_end_tensor_inputs: typing.List[str] = ['latents']max_sequence_length: int = 512 ) → ZImagePipelineOutput or tuple
参数
- prompt (
str或List[str],可选) — 用于引导图像生成的提示词或提示词列表。如果未定义,则必须改为传入prompt_embeds。 - image (
torch.Tensor、PIL.Image.Image、np.ndarray、List[torch.Tensor]、List[PIL.Image.Image]或List[np.ndarray]) —Image、numpy 数组或张量,表示用作起点的图像批次。对于 numpy 数组和 PyTorch 张量,预期的取值范围为[0, 1]。如果它是张量或张量列表,预期的形状应为(B, C, H, W)或(C, H, W)。如果它是 numpy 数组或数组列表,预期的形状应为(B, H, W, C)或(H, W, C)。 - mask_image (
torch.Tensor、PIL.Image.Image、np.ndarray、List[torch.Tensor]、List[PIL.Image.Image]或List[np.ndarray]) —Image、numpy 数组或张量,表示用于图像修复(inpainting)的掩码图像。掩码中的白色像素(值为 1)将被修复,黑色像素(值为 0)将保留原始图像的内容。 - masked_image_latents (
torch.FloatTensor, 可选) — 预编码的掩码图像潜变量。如果提供,将跳过掩码图像编码步骤。 - strength (
float,可选,默认为 1.0) — 指示对参考image进行变换的程度。取值必须在 0 到 1 之间。image用作起始点,strength越高,添加的噪声越多。去噪步数取决于初始添加的噪声量。当strength为 1 时,添加的噪声达到最大值,去噪过程将运行num_inference_steps中指定的完整迭代次数。值为 1 时,基本上会忽略掩码区域内的image。 - height (
int,可选,默认为 1024) — 生成图像的高度(以像素为单位)。如果未提供,则使用输入图像的高度。 - width (
int,可选,默认为 1024) — 生成图像的宽度(以像素为单位)。如果未提供,则使用输入图像的宽度。 - num_inference_steps (
int,可选,默认为 50) — 去噪步数。更多的去噪步数通常能生成更高质量的图像,但代价是推理速度变慢。 - sigmas (
List[float], 可选) — 用于支持在set_timesteps方法中接受sigmas参数的调度器的去噪过程的自定义 sigmas。如果未定义,则使用传入num_inference_steps时的默认行为。 - guidance_scale (
float, 可选,默认为 5.0) — 如 Classifier-Free Diffusion Guidance 中定义的引导尺度。guidance_scale定义为 Imagen Paper 中公式 2 的w。通过设置guidance_scale > 1来启用引导尺度。较高的引导尺度会促使生成与文本prompt紧密关联的图像, 通常以牺牲图像质量为代价。 - cfg_normalization (
bool,可选,默认为 False) — 是否应用配置归一化。 - cfg_truncation (
float,可选,默认值为 1.0) — 用于配置的截断值。 - negative_prompt (
str或List[str],可选) — 用于引导图像生成的负面提示词。如果未定义,则必须传入negative_prompt_embeds。当不使用引导时(即guidance_scale小于1时)将被忽略。 - num_images_per_prompt (
int,可选,默认为 1) — 每个提示词生成的图像数量。 - generator (
torch.Generator或List[torch.Generator],可选) — 一个或一组 torch 生成器,用于使生成过程具有确定性。 - latents (
torch.FloatTensor, 可选) — 预先生成的噪声潜变量,从高斯分布中采样,用作图像生成的输入。可用于使用不同的提示词对同一生成过程进行微调。如果未提供,将通过使用提供的随机generator进行采样来生成一个潜变量张量。 - prompt_embeds (
List[torch.FloatTensor], 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 提示词加权。如果未提供,文本嵌入将从prompt输入参数生成。 - negative_prompt_embeds (
List[torch.FloatTensor], 可选) — 预生成的负向文本嵌入。可用于轻松调整文本输入,例如 提示词加权。如果未提供,negative_prompt_embeds 将根据negative_prompt输入参数生成。 - output_type (
str,可选,默认为"pil") — 生成图像的输出格式。可选值为 PIL:PIL.Image.Image或np.array。 - return_dict (
bool,可选,默认为True) — 是否返回~pipelines.stable_diffusion.ZImagePipelineOutput而不是普通元组。 - joint_attention_kwargs (
dict, 可选) — 一个 kwargs 字典,如果指定,将作为参数传递给 diffusers.models.attention_processor 中self.processor下定义的AttentionProcessor。 - callback_on_step_end (
Callable, 可选) — 一个在推理过程中每个去噪步骤结束时调用的函数。该函数以以下参数被调用:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)。callback_kwargs将包含由callback_on_step_end_tensor_inputs指定的所有张量列表。 - callback_on_step_end_tensor_inputs (
List,可选) —callback_on_step_end函数的张量输入列表。列表中指定的张量将作为callback_kwargs参数传入。您只能包含流水线类的._callback_tensor_inputs属性中列出的变量。 - max_sequence_length (
int,可选,默认值为 512) — 与prompt一起使用的最大序列长度。
返回值
ZImagePipelineOutput 或 tuple
ZImagePipelineOutput,如果
return_dict 为 True,否则为 tuple。当返回元组时,第一个元素是一个包含生成图像的列表。
调用用于图像修复(inpainting)的 pipeline 时调用的函数。
示例:
>>> import torch
>>> from diffusers import ZImageInpaintPipeline
>>> from diffusers.utils import load_image
>>> pipe = ZImageInpaintPipeline.from_pretrained("Tongyi-MAI/Z-Image-Turbo", torch_dtype=torch.bfloat16)
>>> pipe.to("cuda")
>>> url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
>>> init_image = load_image(url).resize((1024, 1024))
>>> # Create a mask (white = inpaint, black = preserve)
>>> import numpy as np
>>> from PIL import Image
>>> mask = np.zeros((1024, 1024), dtype=np.uint8)
>>> mask[256:768, 256:768] = 255 # Inpaint center region
>>> mask_image = Image.fromarray(mask)
>>> prompt = "A beautiful lake with mountains in the background"
>>> image = pipe(
... prompt,
... image=init_image,
... mask_image=mask_image,
... strength=1.0,
... num_inference_steps=8,
... guidance_scale=0.0,
... generator=torch.Generator("cuda").manual_seed(42),
... ).images[0]
>>> image.save("zimage_inpaint.png")prepare_latents
< source >( imagetimestepbatch_sizenum_channels_latentsheightwidthdtypedevicegeneratorlatents = None ) → Tuple of (latents, noise, image_latents) where
返回值
(latents, noise, image_latents) 的元组,其中
- latents:用于去噪的加噪图像潜在表示
- noise: 用于混合的噪声张量
- image_latents: 用于混合的干净图像潜在表示
为图像修复准备潜在变量,返回用于混合的噪声和图像潜在变量。
prepare_mask_latents
< source >( maskmasked_imagebatch_sizeheightwidthdtypedevicegenerator )
为图像修复准备掩码和带掩码的图像潜在表示。