Diffusers 文档
带有 Hunyuan-DiT 的 ControlNet
并获得增强的文档体验
开始使用
带有 Hunyuan-DiT 的 ControlNet
HunyuanDiTControlNetPipeline 是 ControlNet 对于 Hunyuan-DiT 的一种实现。
ControlNet 由 Lvmin Zhang、Anyi Rao 和 Maneesh Agrawala 在《Adding Conditional Control to Text-to-Image Diffusion Models》中提出。
通过 ControlNet 模型,您可以提供额外的控制图像来调节和控制 Hunyuan-DiT 的生成。例如,如果您提供深度图,ControlNet 模型将生成一个保留深度图空间信息的图像。这是一种更灵活和准确的方式来控制图像生成过程。
论文摘要如下:
我们提出了 ControlNet,一种神经网络架构,用于为大型预训练的文本到图像扩散模型添加空间条件控制。ControlNet 锁定已准备好生产的大型扩散模型,并重用其经过数十亿图像预训练的深度和鲁棒的编码层,作为学习各种条件控制的强大骨干。该神经架构与“零卷积”(零初始化的卷积层)连接,这些卷积层逐渐从零开始增长参数,并确保没有有害噪声会影响微调。我们使用 Stable Diffusion 测试了各种条件控制,例如,边缘、深度、分割、人体姿势等,使用单个或多个条件,有或没有提示。我们表明,ControlNet 的训练对于小型(<50k)和大型(>1m)数据集都是稳健的。广泛的结果表明,ControlNet 可以促进更广泛的应用来控制图像扩散模型。
此代码由腾讯混元团队实现。您可以在 Tencent Hunyuan 上找到 Hunyuan-DiT ControlNet 的预训练检查点。
HunyuanDiTControlNetPipeline
class diffusers.HunyuanDiTControlNetPipeline
< source >( vae: AutoencoderKL text_encoder: BertModel tokenizer: BertTokenizer transformer: HunyuanDiT2DModel scheduler: DDPMScheduler safety_checker: StableDiffusionSafetyChecker feature_extractor: CLIPImageProcessor controlnet: typing.Union[diffusers.models.controlnets.controlnet_hunyuan.HunyuanDiT2DControlNetModel, typing.List[diffusers.models.controlnets.controlnet_hunyuan.HunyuanDiT2DControlNetModel], typing.Tuple[diffusers.models.controlnets.controlnet_hunyuan.HunyuanDiT2DControlNetModel], diffusers.models.controlnets.controlnet_hunyuan.HunyuanDiT2DMultiControlNetModel] text_encoder_2 = <class 'transformers.models.t5.modeling_t5.T5EncoderModel'> tokenizer_2 = <class 'transformers.models.mt5.tokenization_mt5.MT5Tokenizer'> requires_safety_checker: bool = True )
参数
- vae (AutoencoderKL) — 变分自动编码器 (VAE) 模型,用于将图像编码和解码为潜在表示形式以及从潜在表示形式解码图像。我们使用
sdxl-vae-fp16-fix
。 - text_encoder (Optional[
~transformers.BertModel
,~transformers.CLIPTextModel
]) — 冻结的文本编码器 (clip-vit-large-patch14)。 混元 DiT 使用微调的 [双语 CLIP]。 - tokenizer (Optional[
~transformers.BertTokenizer
,~transformers.CLIPTokenizer
]) — 用于标记文本的BertTokenizer
或CLIPTokenizer
。 - transformer (HunyuanDiT2DModel) — 腾讯混元设计的 HunyuanDiT 模型。
- text_encoder_2 (
T5EncoderModel
) — mT5 嵌入器。具体来说,它是 ‘t5-v1_1-xxl’。 - tokenizer_2 (
MT5Tokenizer
) — mT5 嵌入器的 tokenizer。 - scheduler (DDPMScheduler) — 调度器,与 HunyuanDiT 结合使用,以对编码的图像潜在空间进行去噪。
- controlnet (HunyuanDiT2DControlNetModel 或
List[HunyuanDiT2DControlNetModel]
或 HunyuanDiT2DControlNetModel) — 在去噪过程中,为unet
提供额外的条件控制。如果将多个 ControlNet 设置为列表,则每个 ControlNet 的输出将加在一起,以创建一个组合的额外条件控制。
使用 HunyuanDiT 进行英语/中文到图像生成的 Pipeline。
此模型继承自 DiffusionPipeline。查看超类文档,了解库为所有 pipeline 实现的通用方法(例如,下载或保存、在特定设备上运行等)。
HunyuanDiT 使用两个文本编码器:mT5 和 [双语 CLIP](由我们自己微调)。
__call__
< source >( prompt: typing.Union[str, typing.List[str]] = None height: typing.Optional[int] = None width: typing.Optional[int] = None num_inference_steps: typing.Optional[int] = 50 guidance_scale: typing.Optional[float] = 5.0 control_image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor]] = None controlnet_conditioning_scale: typing.Union[float, typing.List[float]] = 1.0 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None num_images_per_prompt: typing.Optional[int] = 1 eta: typing.Optional[float] = 0.0 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None prompt_embeds: typing.Optional[torch.Tensor] = None prompt_embeds_2: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds_2: typing.Optional[torch.Tensor] = None prompt_attention_mask: typing.Optional[torch.Tensor] = None prompt_attention_mask_2: typing.Optional[torch.Tensor] = None negative_prompt_attention_mask: typing.Optional[torch.Tensor] = None negative_prompt_attention_mask_2: typing.Optional[torch.Tensor] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True callback_on_step_end: typing.Union[typing.Callable[[int, int, typing.Dict], NoneType], diffusers.callbacks.PipelineCallback, diffusers.callbacks.MultiPipelineCallbacks, NoneType] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] guidance_rescale: float = 0.0 original_size: typing.Optional[typing.Tuple[int, int]] = (1024, 1024) target_size: typing.Optional[typing.Tuple[int, int]] = None crops_coords_top_left: typing.Tuple[int, int] = (0, 0) use_resolution_binning: bool = True ) → StableDiffusionPipelineOutput 或 tuple
参数
- prompt (
str
或List[str]
, 可选) — 用于引导图像生成的 prompt 或 prompts。如果未定义,则需要传递prompt_embeds
。 - height (
int
) — 生成图像的高度像素。 - width (
int
) — 生成图像的宽度像素。 - num_inference_steps (
int
, 可选, 默认为 50) — 去噪步骤的数量。更多去噪步骤通常会以较慢的推理速度为代价,从而生成更高质量的图像。此参数由strength
调制。 - guidance_scale (
float
, 可选, 默认为 7.5) — 较高的 guidance scale 值会鼓励模型生成与文本prompt
紧密相关的图像,但会牺牲较低的图像质量。当guidance_scale > 1
时,将启用 Guidance scale。 - control_guidance_start (
float
或List[float]
, 可选, 默认为 0.0) — ControlNet 开始应用的起始总步数的百分比。 - control_guidance_end (
float
或List[float]
, 可选, 默认为 1.0) — ControlNet 停止应用的起始总步数的百分比。 - control_image (
torch.Tensor
,PIL.Image.Image
,np.ndarray
,List[torch.Tensor]
,List[PIL.Image.Image]
,List[np.ndarray]
, —List[List[torch.Tensor]]
,List[List[np.ndarray]]
或List[List[PIL.Image.Image]]
): ControlNet 输入条件,为unet
生成提供指导。如果类型指定为torch.Tensor
,则按原样传递给 ControlNet。PIL.Image.Image
也可以接受为图像。输出图像的尺寸默认为image
的尺寸。如果传递了 height 和/或 width,则会相应地调整image
的大小。如果在init
中指定了多个 ControlNet,则必须将图像作为列表传递,以便可以正确批量处理列表中的每个元素,以输入到单个 ControlNet。 - controlnet_conditioning_scale (
float
或List[float]
, 可选, 默认为 1.0) — ControlNet 的输出在添加到原始unet
中的残差之前,会乘以controlnet_conditioning_scale
。如果在init
中指定了多个 ControlNet,则可以将相应的比例设置为列表。 - negative_prompt (
str
或List[str]
, 可选) — 用于引导图像生成中不应包含的内容的 prompt 或 prompts。如果未定义,则需要改为传递negative_prompt_embeds
。当不使用 guidance 时(guidance_scale < 1
),将被忽略。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个 prompt 生成的图像数量。 - eta (
float
, 可选, 默认为 0.0) — 对应于 DDIM 论文中的参数 eta (η)。仅适用于 DDIMScheduler,在其他调度器中将被忽略。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 用于使生成具有确定性的torch.Generator
。 - prompt_embeds (
torch.Tensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入(提示词权重)。如果未提供,则文本嵌入将从prompt
输入参数生成。 - prompt_embeds_2 (
torch.Tensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入(提示词权重)。如果未提供,则文本嵌入将从prompt
输入参数生成。 - negative_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负面文本嵌入。可用于轻松调整文本输入(提示词权重)。如果未提供,则negative_prompt_embeds
将从negative_prompt
输入参数生成。 - negative_prompt_embeds_2 (
torch.Tensor
, 可选) — 预生成的负面文本嵌入。可用于轻松调整文本输入(提示词权重)。如果未提供,则negative_prompt_embeds
将从negative_prompt
输入参数生成。 - prompt_attention_mask (
torch.Tensor
, 可选) — 提示词的注意力掩码。当直接传递prompt_embeds
时是必需的。 - prompt_attention_mask_2 (
torch.Tensor
, 可选) — 提示词的注意力掩码。当直接传递prompt_embeds_2
时是必需的。 - negative_prompt_attention_mask (
torch.Tensor
, 可选) — 负面提示词的注意力掩码。当直接传递negative_prompt_embeds
时是必需的。 - negative_prompt_attention_mask_2 (
torch.Tensor
, 可选) — 负面提示词的注意力掩码。当直接传递negative_prompt_embeds_2
时是必需的。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。在PIL.Image
或np.array
之间选择。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回 StableDiffusionPipelineOutput 而不是普通元组。 - callback_on_step_end (
Callable[[int, int, Dict], None]
,PipelineCallback
,MultiPipelineCallbacks
, 可选) — 在每个去噪步骤结束时调用的回调函数或回调函数列表。 - callback_on_step_end_tensor_inputs (
List[str]
, 可选) — 应传递给回调函数的张量输入列表。如果未定义,则将传递所有张量输入。 - guidance_rescale (
float
, 可选, 默认为 0.0) — 根据guidance_rescale
重新缩放 noise_cfg。基于 Common Diffusion Noise Schedules and Sample Steps are Flawed 的发现。请参阅第 3.4 节 - original_size (
Tuple[int, int]
, 可选, 默认为(1024, 1024)
) — 图像的原始尺寸。用于计算时间 ID。 - target_size (
Tuple[int, int]
, 可选) — 图像的目标尺寸。用于计算时间 ID。 - crops_coords_top_left (
Tuple[int, int]
, 可选, 默认为(0, 0)
) — 裁剪区域的左上角坐标。用于计算时间 ID。 - use_resolution_binning (
bool
, 可选, 默认为True
) — 是否使用分辨率分箱。如果为True
,输入分辨率将映射到最接近的标准分辨率。支持的分辨率为 1024x1024、1280x1280、1024x768、1152x864、1280x960、768x1024、864x1152、960x1280、1280x768 和 768x1280。建议将其设置为True
。
返回值
StableDiffusionPipelineOutput 或 tuple
如果 return_dict
为 True
,则返回 StableDiffusionPipelineOutput,否则返回一个 tuple
,其中第一个元素是包含生成图像的列表,第二个元素是 bool
列表,指示相应的生成图像是否包含“不适合工作场所”(nsfw)内容。
调用该 pipeline 函数以使用 HunyuanDiT 进行生成。
示例
from diffusers import HunyuanDiT2DControlNetModel, HunyuanDiTControlNetPipeline
import torch
controlnet = HunyuanDiT2DControlNetModel.from_pretrained(
"Tencent-Hunyuan/HunyuanDiT-v1.1-ControlNet-Diffusers-Canny", torch_dtype=torch.float16
)
pipe = HunyuanDiTControlNetPipeline.from_pretrained(
"Tencent-Hunyuan/HunyuanDiT-v1.1-Diffusers", controlnet=controlnet, torch_dtype=torch.float16
)
pipe.to("cuda")
from diffusers.utils import load_image
cond_image = load_image(
"https://huggingface.co/Tencent-Hunyuan/HunyuanDiT-v1.1-ControlNet-Diffusers-Canny/resolve/main/canny.jpg?download=true"
)
## You may also use English prompt as HunyuanDiT supports both English and Chinese
prompt = "在夜晚的酒店门前,一座古老的中国风格的狮子雕像矗立着,它的眼睛闪烁着光芒,仿佛在守护着这座建筑。背景是夜晚的酒店前,构图方式是特写,平视,居中构图。这张照片呈现了真实摄影风格,蕴含了中国雕塑文化,同时展现了神秘氛围"
# prompt="At night, an ancient Chinese-style lion statue stands in front of the hotel, its eyes gleaming as if guarding the building. The background is the hotel entrance at night, with a close-up, eye-level, and centered composition. This photo presents a realistic photographic style, embodies Chinese sculpture culture, and reveals a mysterious atmosphere."
image = pipe(
prompt,
height=1024,
width=1024,
control_image=cond_image,
num_inference_steps=50,
).images[0]
encode_prompt
< source >( prompt: str device: device = None dtype: dtype = None num_images_per_prompt: int = 1 do_classifier_free_guidance: bool = True negative_prompt: typing.Optional[str] = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None prompt_attention_mask: typing.Optional[torch.Tensor] = None negative_prompt_attention_mask: typing.Optional[torch.Tensor] = None max_sequence_length: typing.Optional[int] = None text_encoder_index: int = 0 )
参数
- prompt (
str
或List[str]
, 可选) — 要编码的提示词 - device — (
torch.device
): torch 设备 - dtype (
torch.dtype
) — torch 数据类型 - num_images_per_prompt (
int
) — 每个提示词应生成的图像数量 - do_classifier_free_guidance (
bool
) — 是否使用无分类器引导 - negative_prompt (
str
或List[str]
, 可选) — 不用于引导图像生成的提示词。如果未定义,则必须传递negative_prompt_embeds
。当不使用引导时将被忽略(即,如果guidance_scale
小于1
则忽略)。 - prompt_embeds (
torch.Tensor
, optional) — 预生成的文本嵌入 (Pre-generated text embeddings)。 可用于轻松调整文本输入,例如 prompt 权重 (prompt weighting)。 如果未提供,则将从prompt
输入参数生成文本嵌入。 - negative_prompt_embeds (
torch.Tensor
, optional) — 预生成的负面文本嵌入 (Pre-generated negative text embeddings)。 可用于轻松调整文本输入,例如 prompt 权重 (prompt weighting)。 如果未提供,则将从negative_prompt
输入参数生成 negative_prompt_embeds。 - prompt_attention_mask (
torch.Tensor
, optional) — prompt 的注意力掩码 (Attention mask for the prompt)。 当直接传递prompt_embeds
时,此参数为必需项 (Required)。 - negative_prompt_attention_mask (
torch.Tensor
, optional) — negative prompt 的注意力掩码 (Attention mask for the negative prompt)。 当直接传递negative_prompt_embeds
时,此参数为必需项 (Required)。 - max_sequence_length (
int
, optional) — 用于 prompt 的最大序列长度 (maximum sequence length to use for the prompt)。 - text_encoder_index (
int
, optional) — 要使用的文本编码器的索引 (Index of the text encoder to use)。0
代表 clip,1
代表 T5。
将 prompt 编码为文本编码器隐藏状态 (Encodes the prompt into text encoder hidden states)。