Diffusers 文档
ControlNet 与 Hunyuan-DiT
并获得增强的文档体验
开始使用
ControlNet with Hunyuan-DiT
HunyuanDiTControlNetPipeline 是 Hunyuan-DiT 的 ControlNet 实现。
ControlNet 由 Lvmin Zhang、Anyi Rao 和 Maneesh Agrawala 在 《为文本到图像扩散模型添加条件控制》 中提出。
使用 ControlNet 模型,您可以提供额外的控制图像来调整和控制 Hunyuan-DiT 的生成。例如,如果您提供一张深度图,ControlNet 模型将生成一张保留深度图空间信息的图像。这是一种更灵活、更精确的控制图像生成过程的方法。
论文摘要如下:
我们提出了 ControlNet,一种用于为大型预训练文本到图像扩散模型添加空间条件控制的神经网络架构。ControlNet 锁定生产就绪的大型扩散模型,并重用它们在数十亿图像上预训练的深度且鲁棒的编码层作为强大的骨干网络,以学习各种条件控制。该神经网络架构通过“零卷积”(零初始化卷积层)连接,这些卷积层从零开始逐步增长参数,并确保不会有有害噪声影响微调。我们使用 Stable Diffusion 测试了各种条件控制,例如,边缘、深度、分割、人体姿态等,使用单一或多个条件,有无提示词。我们表明 ControlNet 的训练在小型(<50k)和大型(>1m)数据集上都具有鲁棒性。大量结果表明 ControlNet 可能有助于控制图像扩散模型的更广泛应用。
此代码由腾讯混元团队实现。您可以在 Tencent Hunyuan 上找到 Hunyuan-DiT ControlNets 的预训练检查点。
HunyuanDiTControlNetPipeline
class diffusers.HunyuanDiTControlNetPipeline
< 来源 >( 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: typing.Optional[transformers.models.t5.modeling_t5.T5EncoderModel] = None tokenizer_2: typing.Optional[transformers.models.mt5.tokenization_mt5.MT5Tokenizer] = None requires_safety_checker: bool = True )
参数
- vae (AutoencoderKL) — 用于将图像编码和解码为潜在表示的变分自编码器 (VAE) 模型。我们使用
sdxl-vae-fp16-fix
。 - text_encoder (Optional[
~transformers.BertModel
,~transformers.CLIPTextModel
]) — 冻结的文本编码器(clip-vit-large-patch14)。HunyuanDiT 使用经过微调的 [双语 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 嵌入器的分词器。 - scheduler (DDPMScheduler) — 与 HunyuanDiT 结合使用以对编码图像潜在表示去噪的调度器。
- controlnet (HunyuanDiT2DControlNetModel 或
List[HunyuanDiT2DControlNetModel]
或 HunyuanDiT2DControlNetModel) — 在去噪过程中为unet
提供额外的条件。如果将多个 ControlNet 设置为列表,则每个 ControlNet 的输出将被相加以创建组合的额外条件。
用于使用 HunyuanDiT 生成英语/中文图像的管道。
此模型继承自 DiffusionPipeline。请查看超类文档,了解库为所有管道实现的通用方法(例如下载或保存、在特定设备上运行等)。
HunyuanDiT 使用两个文本编码器:mT5 和 [双语 CLIP](我们自己微调的)
__call__
< 来源 >( 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 or tuple
参数
- prompt (
str
或List[str]
, 可选) — 用于引导图像生成的提示词或提示词列表。如果未定义,您需要传递prompt_embeds
。 - height (
int
) — 生成图像的高度(像素)。 - width (
int
) — 生成图像的宽度(像素)。 - num_inference_steps (
int
, 可选, 默认为 50) — 去噪步数。更多的去噪步数通常会带来更高的图像质量,但推理速度会更慢。此参数受strength
调制。 - guidance_scale (
float
, 可选, 默认为 7.5) — 较高的引导比例值鼓励模型生成与文本prompt
紧密相关的图像,但图像质量会降低。当guidance_scale > 1
时启用引导比例。 - 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]]
): 用于为unet
提供生成指导的 ControlNet 输入条件。如果类型指定为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]
, 可选) — 用于引导图像生成中不包含内容的提示词或提示词列表。如果未定义,您需要传递negative_prompt_embeds
。当不使用引导(guidance_scale < 1
)时忽略。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个提示词生成的图像数量。 - 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` 输入参数生成 `negative_prompt_embeds`。 - negative_prompt_embeds_2 (
torch.Tensor
, 可选) — 预生成的负向文本嵌入。可用于轻松调整文本输入(提示词权重)。如果未提供,将从 `negative_prompt` 输入参数生成 `negative_prompt_embeds`。 - 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)内容。
使用 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
< 来源 >( 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
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如提示词权重。如果未提供,将从 `prompt` 输入参数生成文本嵌入。 - negative_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负向文本嵌入。可用于轻松调整文本输入,例如提示词权重。如果未提供,`negative_prompt_embeds` 将从 `negative_prompt` 输入参数生成。 - prompt_attention_mask (
torch.Tensor
, 可选) — 提示词的注意力掩码。当直接传入 `prompt_embeds` 时必须提供。 - negative_prompt_attention_mask (
torch.Tensor
, 可选) — 负向提示词的注意力掩码。当直接传入 `negative_prompt_embeds` 时必须提供。 - max_sequence_length (
int
, 可选) — 提示词使用的最大序列长度。 - text_encoder_index (
int
, 可选) — 要使用的文本编码器索引。`0` 表示 clip,`1` 表示 T5。
将提示编码为文本编码器隐藏状态。