Perturbed-Attention Guidance (PAG) is a new diffusion sampling guidance that improves sample quality across both unconditional and conditional settings, achieving this without requiring further training or the integration of external modules.
PAG was introduced in Self-Rectifying Diffusion Sampling with Perturbed-Attention Guidance by Donghoon Ahn, Hyoungwon Cho, Jaewon Min, Wooseok Jang, Jungwoo Kim, SeonHwa Kim, Hyun Hee Park, Kyong Hwan Jin and Seungryong Kim.
The abstract from the paper is:
Recent studies have demonstrated that diffusion models are capable of generating high-quality samples, but their quality heavily depends on sampling guidance techniques, such as classifier guidance (CG) and classifier-free guidance (CFG). These techniques are often not applicable in unconditional generation or in various downstream tasks such as image restoration. In this paper, we propose a novel sampling guidance, called Perturbed-Attention Guidance (PAG), which improves diffusion sample quality across both unconditional and conditional settings, achieving this without requiring additional training or the integration of external modules. PAG is designed to progressively enhance the structure of samples throughout the denoising process. It involves generating intermediate samples with degraded structure by substituting selected self-attention maps in diffusion U-Net with an identity matrix, by considering the self-attention mechanisms’ ability to capture structural information, and guiding the denoising process away from these degraded samples. In both ADM and Stable Diffusion, PAG surprisingly improves sample quality in conditional and even unconditional scenarios. Moreover, PAG significantly improves the baseline performance in various downstream tasks where existing guidances such as CG or CFG cannot be fully utilized, including ControlNet with empty prompts and image restoration such as inpainting and deblurring.
PAG can be used by specifying the pag_applied_layers
as a parameter when instantiating a PAG pipeline. It can be a single string or a list of strings. Each string can be a unique layer identifier or a regular expression to identify one or more layers.
down_blocks.2.attentions.0.transformer_blocks.0.attn1.processor
down_blocks.2.(attentions|motion_modules).0.transformer_blocks.0.attn1.processor
down_blocks.2
, or attn1
["blocks.1", "blocks.(14|20)", r"down_blocks\.(2,3)"]
Since RegEx is supported as a way for matching layer identifiers, it is crucial to use it correctly otherwise there might be unexpected behaviour. The recommended way to use PAG is by specifying layers as blocks.{layer_index}
and blocks.({layer_index_1|layer_index_2|...})
. Using it in any other way, while doable, may bypass our basic validation checks and give you unexpected results.
( vae: AutoencoderKL text_encoder: CLIPTextModel tokenizer: CLIPTokenizer unet: Union motion_adapter: MotionAdapter scheduler: KarrasDiffusionSchedulers feature_extractor: CLIPImageProcessor = None image_encoder: CLIPVisionModelWithProjection = None pag_applied_layers: Union = 'mid_block.*attn1' )
Parameters
CLIPTextModel
) —
Frozen text-encoder (clip-vit-large-patch14). CLIPTokenizer
) —
A CLIPTokenizer to tokenize text. MotionAdapter
) —
A MotionAdapter
to be used in combination with unet
to denoise the encoded video latents. unet
to denoise the encoded image latents. Can be one of
DDIMScheduler, LMSDiscreteScheduler, or PNDMScheduler. Pipeline for text-to-video generation using AnimateDiff and Perturbed Attention Guidance.
This model inherits from DiffusionPipeline. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a particular device, etc.).
The pipeline also inherits the following loading methods:
( prompt: Union = None num_frames: Optional = 16 height: Optional = None width: Optional = None num_inference_steps: int = 50 guidance_scale: float = 7.5 negative_prompt: Union = None num_videos_per_prompt: Optional = 1 eta: float = 0.0 generator: Union = None latents: Optional = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None ip_adapter_image: Union = None ip_adapter_image_embeds: Optional = None output_type: Optional = 'pil' return_dict: bool = True cross_attention_kwargs: Optional = None clip_skip: Optional = None callback_on_step_end: Optional = None callback_on_step_end_tensor_inputs: List = ['latents'] decode_chunk_size: int = 16 pag_scale: float = 3.0 pag_adaptive_scale: float = 0.0 ) → AnimateDiffPipelineOutput or tuple
Parameters
str
or List[str]
, optional) —
The prompt or prompts to guide image generation. If not defined, you need to pass prompt_embeds
. int
, optional, defaults to self.unet.config.sample_size * self.vae_scale_factor
) —
The height in pixels of the generated video. int
, optional, defaults to self.unet.config.sample_size * self.vae_scale_factor
) —
The width in pixels of the generated video. int
, optional, defaults to 16) —
The number of video frames that are generated. Defaults to 16 frames which at 8 frames per seconds
amounts to 2 seconds of video. int
, optional, defaults to 50) —
The number of denoising steps. More denoising steps usually lead to a higher quality videos at the
expense of slower inference. float
, optional, defaults to 7.5) —
A higher guidance scale value encourages the model to generate images closely linked to the text
prompt
at the expense of lower image quality. Guidance scale is enabled when guidance_scale > 1
. str
or List[str]
, optional) —
The prompt or prompts to guide what to not include in image generation. If not defined, you need to
pass negative_prompt_embeds
instead. Ignored when not using guidance (guidance_scale < 1
). float
, optional, defaults to 0.0) —
Corresponds to parameter eta (η) from the DDIM paper. Only applies
to the DDIMScheduler, and is ignored in other schedulers. torch.Generator
or List[torch.Generator]
, optional) —
A torch.Generator
to make
generation deterministic. torch.Tensor
, optional) —
Pre-generated noisy latents sampled from a Gaussian distribution, to be used as inputs for video
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
tensor is generated by sampling using the supplied random generator
. Latents should be of shape
(batch_size, num_channel, num_frames, height, width)
. torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not
provided, text embeddings are generated from the prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs (prompt weighting). If
not provided, negative_prompt_embeds
are generated from the negative_prompt
input argument.
ip_adapter_image — (PipelineImageInput
, optional):
Optional image input to work with IP Adapters. List[torch.Tensor]
, optional) —
Pre-generated image embeddings for IP-Adapter. It should be a list of length same as number of
IP-adapters. Each element should be a tensor of shape (batch_size, num_images, emb_dim)
. It should
contain the negative image embedding if do_classifier_free_guidance
is set to True
. If not
provided, embeddings are computed from the ip_adapter_image
input argument. str
, optional, defaults to "pil"
) —
The output format of the generated video. Choose between torch.Tensor
, PIL.Image
or np.array
. bool
, optional, defaults to True
) —
Whether or not to return a TextToVideoSDPipelineOutput instead
of a plain tuple. dict
, optional) —
A kwargs dictionary that if specified is passed along to the AttentionProcessor
as defined in
self.processor
. int
, optional) —
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
the output of the pre-final layer will be used for computing the prompt embeddings. Callable
, optional) —
A function that calls at the end of each denoising steps during the inference. The function is called
with the following arguments: callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
. callback_kwargs
will include a list of all tensors as specified by
callback_on_step_end_tensor_inputs
. List
, optional) —
The list of tensor inputs for the callback_on_step_end
function. The tensors specified in the list
will be passed as callback_kwargs
argument. You will only be able to include variables listed in the
._callback_tensor_inputs
attribute of your pipeline class. float
, optional, defaults to 3.0) —
The scale factor for the perturbed attention guidance. If it is set to 0.0, the perturbed attention
guidance will not be used. float
, optional, defaults to 0.0) —
The adaptive scale factor for the perturbed attention guidance. If it is set to 0.0, pag_scale
is
used. Returns
AnimateDiffPipelineOutput or tuple
If return_dict
is True
, AnimateDiffPipelineOutput is
returned, otherwise a tuple
is returned where the first element is a list with the generated frames.
The call function to the pipeline for generation.
Examples:
>>> import torch
>>> from diffusers import AnimateDiffPAGPipeline, MotionAdapter, DDIMScheduler
>>> from diffusers.utils import export_to_gif
>>> model_id = "SG161222/Realistic_Vision_V5.1_noVAE"
>>> motion_adapter_id = "guoyww/animatediff-motion-adapter-v1-5-2"
>>> motion_adapter = MotionAdapter.from_pretrained(motion_adapter_id)
>>> scheduler = DDIMScheduler.from_pretrained(
... model_id, subfolder="scheduler", beta_schedule="linear", steps_offset=1, clip_sample=False
... )
>>> pipe = AnimateDiffPAGPipeline.from_pretrained(
... model_id,
... motion_adapter=motion_adapter,
... scheduler=scheduler,
... pag_applied_layers=["mid"],
... torch_dtype=torch.float16,
... ).to("cuda")
>>> video = pipe(
... prompt="car, futuristic cityscape with neon lights, street, no human",
... negative_prompt="low quality, bad quality",
... num_inference_steps=25,
... guidance_scale=6.0,
... pag_scale=3.0,
... generator=torch.Generator().manual_seed(42),
... ).frames[0]
>>> export_to_gif(video, "animatediff_pag.gif")
( prompt device num_images_per_prompt do_classifier_free_guidance negative_prompt = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None lora_scale: Optional = None clip_skip: Optional = None )
Parameters
str
or List[str]
, optional) —
prompt to be encoded
device — (torch.device
):
torch device int
) —
number of images that should be generated per prompt bool
) —
whether to use classifier free guidance or not str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. float
, optional) —
A LoRA scale that will be applied to all LoRA layers of the text encoder if LoRA layers are loaded. int
, optional) —
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
the output of the pre-final layer will be used for computing the prompt embeddings. Encodes the prompt into text encoder hidden states.
( vae: AutoencoderKL text_encoder: BertModel tokenizer: BertTokenizer transformer: HunyuanDiT2DModel scheduler: DDPMScheduler safety_checker: Optional = None feature_extractor: Optional = None requires_safety_checker: bool = True text_encoder_2: Optional = None tokenizer_2: Optional = None pag_applied_layers: Union = 'blocks.1' )
Parameters
sdxl-vae-fp16-fix
. ~transformers.BertModel
, ~transformers.CLIPTextModel
]) —
Frozen text-encoder (clip-vit-large-patch14).
HunyuanDiT uses a fine-tuned [bilingual CLIP]. ~transformers.BertTokenizer
, ~transformers.CLIPTokenizer
]) —
A BertTokenizer
or CLIPTokenizer
to tokenize text. T5EncoderModel
) —
The mT5 embedder. Specifically, it is ‘t5-v1_1-xxl’. MT5Tokenizer
) —
The tokenizer for the mT5 embedder. Pipeline for English/Chinese-to-image generation using HunyuanDiT and Perturbed Attention Guidance.
This model inherits from DiffusionPipeline. Check the superclass documentation for the generic methods the library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)
HunyuanDiT uses two text encoders: mT5 and [bilingual CLIP](fine-tuned by ourselves)
( prompt: Union = None height: Optional = None width: Optional = None num_inference_steps: Optional = 50 guidance_scale: Optional = 5.0 negative_prompt: Union = None num_images_per_prompt: Optional = 1 eta: Optional = 0.0 generator: Union = None latents: Optional = None prompt_embeds: Optional = None prompt_embeds_2: Optional = None negative_prompt_embeds: Optional = None negative_prompt_embeds_2: Optional = None prompt_attention_mask: Optional = None prompt_attention_mask_2: Optional = None negative_prompt_attention_mask: Optional = None negative_prompt_attention_mask_2: Optional = None output_type: Optional = 'pil' return_dict: bool = True callback_on_step_end: Union = None callback_on_step_end_tensor_inputs: List = ['latents'] guidance_rescale: float = 0.0 original_size: Optional = (1024, 1024) target_size: Optional = None crops_coords_top_left: Tuple = (0, 0) use_resolution_binning: bool = True pag_scale: float = 3.0 pag_adaptive_scale: float = 0.0 ) → StableDiffusionPipelineOutput or tuple
Parameters
str
or List[str]
, optional) —
The prompt or prompts to guide image generation. If not defined, you need to pass prompt_embeds
. int
) —
The height in pixels of the generated image. int
) —
The width in pixels of the generated image. int
, optional, defaults to 50) —
The number of denoising steps. More denoising steps usually lead to a higher quality image at the
expense of slower inference. This parameter is modulated by strength
. float
, optional, defaults to 7.5) —
A higher guidance scale value encourages the model to generate images closely linked to the text
prompt
at the expense of lower image quality. Guidance scale is enabled when guidance_scale > 1
. str
or List[str]
, optional) —
The prompt or prompts to guide what to not include in image generation. If not defined, you need to
pass negative_prompt_embeds
instead. Ignored when not using guidance (guidance_scale < 1
). int
, optional, defaults to 1) —
The number of images to generate per prompt. float
, optional, defaults to 0.0) —
Corresponds to parameter eta (η) from the DDIM paper. Only applies
to the DDIMScheduler, and is ignored in other schedulers. torch.Generator
or List[torch.Generator]
, optional) —
A torch.Generator
to make
generation deterministic. torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not
provided, text embeddings are generated from the prompt
input argument. torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not
provided, text embeddings are generated from the prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs (prompt weighting). If
not provided, negative_prompt_embeds
are generated from the negative_prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs (prompt weighting). If
not provided, negative_prompt_embeds
are generated from the negative_prompt
input argument. torch.Tensor
, optional) —
Attention mask for the prompt. Required when prompt_embeds
is passed directly. torch.Tensor
, optional) —
Attention mask for the prompt. Required when prompt_embeds_2
is passed directly. torch.Tensor
, optional) —
Attention mask for the negative prompt. Required when negative_prompt_embeds
is passed directly. torch.Tensor
, optional) —
Attention mask for the negative prompt. Required when negative_prompt_embeds_2
is passed directly. str
, optional, defaults to "pil"
) —
The output format of the generated image. Choose between PIL.Image
or np.array
. bool
, optional, defaults to True
) —
Whether or not to return a StableDiffusionPipelineOutput instead of a
plain tuple. Callable[[int, int, Dict], None]
, PipelineCallback
, MultiPipelineCallbacks
, optional) —
A callback function or a list of callback functions to be called at the end of each denoising step. List[str]
, optional) —
A list of tensor inputs that should be passed to the callback function. If not defined, all tensor
inputs will be passed. float
, optional, defaults to 0.0) —
Rescale the noise_cfg according to guidance_rescale
. Based on findings of Common Diffusion Noise
Schedules and Sample Steps are Flawed. See Section 3.4 Tuple[int, int]
, optional, defaults to (1024, 1024)
) —
The original size of the image. Used to calculate the time ids. Tuple[int, int]
, optional) —
The target size of the image. Used to calculate the time ids. Tuple[int, int]
, optional, defaults to (0, 0)
) —
The top left coordinates of the crop. Used to calculate the time ids. bool
, optional, defaults to True
) —
Whether to use resolution binning or not. If True
, the input resolution will be mapped to the closest
standard resolution. Supported resolutions are 1024x1024, 1280x1280, 1024x768, 1152x864, 1280x960,
768x1024, 864x1152, 960x1280, 1280x768, and 768x1280. It is recommended to set this to True
. float
, optional, defaults to 3.0) —
The scale factor for the perturbed attention guidance. If it is set to 0.0, the perturbed attention
guidance will not be used. float
, optional, defaults to 0.0) —
The adaptive scale factor for the perturbed attention guidance. If it is set to 0.0, pag_scale
is
used. Returns
StableDiffusionPipelineOutput or tuple
If return_dict
is True
, StableDiffusionPipelineOutput is returned,
otherwise a tuple
is returned where the first element is a list with the generated images and the
second element is a list of bool
s indicating whether the corresponding generated image contains
“not-safe-for-work” (nsfw) content.
The call function to the pipeline for generation with HunyuanDiT.
Examples:
>>> import torch
>>> from diffusers import AutoPipelineForText2Image
>>> pipe = AutoPipelineForText2Image.from_pretrained(
... "Tencent-Hunyuan/HunyuanDiT-v1.2-Diffusers",
... torch_dtype=torch.float16,
... enable_pag=True,
... pag_applied_layers=[14],
... ).to("cuda")
>>> # prompt = "an astronaut riding a horse"
>>> prompt = "一个宇航员在骑马"
>>> image = pipe(prompt, guidance_scale=4, pag_scale=3).images[0]
( prompt: str device: device = None dtype: dtype = None num_images_per_prompt: int = 1 do_classifier_free_guidance: bool = True negative_prompt: Optional = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None prompt_attention_mask: Optional = None negative_prompt_attention_mask: Optional = None max_sequence_length: Optional = None text_encoder_index: int = 0 )
Parameters
str
or List[str]
, optional) —
prompt to be encoded
device — (torch.device
):
torch device torch.dtype
) —
torch dtype int
) —
number of images that should be generated per prompt bool
) —
whether to use classifier free guidance or not str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. torch.Tensor
, optional) —
Attention mask for the prompt. Required when prompt_embeds
is passed directly. torch.Tensor
, optional) —
Attention mask for the negative prompt. Required when negative_prompt_embeds
is passed directly. int
, optional) — maximum sequence length to use for the prompt. int
, optional) —
Index of the text encoder to use. 0
for clip and 1
for T5. Encodes the prompt into text encoder hidden states.
( vae: AutoencoderKL text_encoder: ChatGLMModel tokenizer: ChatGLMTokenizer unet: UNet2DConditionModel scheduler: KarrasDiffusionSchedulers image_encoder: CLIPVisionModelWithProjection = None feature_extractor: CLIPImageProcessor = None force_zeros_for_empty_prompt: bool = False pag_applied_layers: Union = 'mid' )
Parameters
ChatGLMModel
) —
Frozen text-encoder. Kolors uses ChatGLM3-6B. ChatGLMTokenizer
) —
Tokenizer of class
ChatGLMTokenizer. unet
to denoise the encoded image latents. Can be one of
DDIMScheduler, LMSDiscreteScheduler, or PNDMScheduler. bool
, optional, defaults to "False"
) —
Whether the negative prompt embeddings shall be forced to always be set to 0. Also see the config of
Kwai-Kolors/Kolors-diffusers
. str
or List[str]``, *optional*, defaults to
“mid”`) —
Set the transformer attention layers where to apply the perturbed attention guidance. Can be a string or a
list of strings with “down”, “mid”, “up”, a whole transformer block or specific transformer block attention
layers, e.g.:
[“mid”]["down", "mid"] [“down”, “mid”, “up.block_1”]["down", "mid", "up.block_1.attentions_0",
"up.block_1.attentions_1"] Pipeline for text-to-image generation using Kolors.
This model inherits from DiffusionPipeline. Check the superclass documentation for the generic methods the library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)
The pipeline also inherits the following loading methods:
( prompt: Union = None height: Optional = None width: Optional = None num_inference_steps: int = 50 timesteps: List = None sigmas: List = None denoising_end: Optional = None guidance_scale: float = 5.0 negative_prompt: Union = None num_images_per_prompt: Optional = 1 eta: float = 0.0 generator: Union = None latents: Optional = None prompt_embeds: Optional = None pooled_prompt_embeds: Optional = None negative_prompt_embeds: Optional = None negative_pooled_prompt_embeds: Optional = None ip_adapter_image: Union = None ip_adapter_image_embeds: Optional = None output_type: Optional = 'pil' return_dict: bool = True cross_attention_kwargs: Optional = None original_size: Optional = None crops_coords_top_left: Tuple = (0, 0) target_size: Optional = None negative_original_size: Optional = None negative_crops_coords_top_left: Tuple = (0, 0) negative_target_size: Optional = None callback_on_step_end: Union = None callback_on_step_end_tensor_inputs: List = ['latents'] pag_scale: float = 3.0 pag_adaptive_scale: float = 0.0 max_sequence_length: int = 256 ) → ~pipelines.kolors.KolorsPipelineOutput
or tuple
Parameters
str
or List[str]
, optional) —
The prompt or prompts to guide the image generation. If not defined, one has to pass prompt_embeds
.
instead. int
, optional, defaults to self.unet.config.sample_size * self.vae_scale_factor) —
The height in pixels of the generated image. This is set to 1024 by default for the best results.
Anything below 512 pixels won’t work well for
Kwai-Kolors/Kolors-diffusers and checkpoints
that are not specifically fine-tuned on low resolutions. int
, optional, defaults to self.unet.config.sample_size * self.vae_scale_factor) —
The width in pixels of the generated image. This is set to 1024 by default for the best results.
Anything below 512 pixels won’t work well for
Kwai-Kolors/Kolors-diffusers and checkpoints
that are not specifically fine-tuned on low resolutions. int
, optional, defaults to 50) —
The number of denoising steps. More denoising steps usually lead to a higher quality image at the
expense of slower inference. List[int]
, optional) —
Custom timesteps to use for the denoising process with schedulers which support a timesteps
argument
in their set_timesteps
method. If not defined, the default behavior when num_inference_steps
is
passed will be used. Must be in descending order. List[float]
, optional) —
Custom sigmas to use for the denoising process with schedulers which support a sigmas
argument in
their set_timesteps
method. If not defined, the default behavior when num_inference_steps
is passed
will be used. float
, optional) —
When specified, determines the fraction (between 0.0 and 1.0) of the total denoising process to be
completed before it is intentionally prematurely terminated. As a result, the returned sample will
still retain a substantial amount of noise as determined by the discrete timesteps selected by the
scheduler. The denoising_end parameter should ideally be utilized when this pipeline forms a part of a
“Mixture of Denoisers” multi-pipeline setup, as elaborated in Refining the Image
Output float
, optional, defaults to 5.0) —
Guidance scale as defined in Classifier-Free Diffusion Guidance.
guidance_scale
is defined as w
of equation 2. of Imagen
Paper. Guidance scale is enabled by setting guidance_scale > 1
. Higher guidance scale encourages to generate images that are closely linked to the text prompt
,
usually at the expense of lower image quality. str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). int
, optional, defaults to 1) —
The number of images to generate per prompt. float
, optional, defaults to 0.0) —
Corresponds to parameter eta (η) in the DDIM paper: https://arxiv.org/abs/2010.02502. Only applies to
schedulers.DDIMScheduler, will be ignored for others. torch.Generator
or List[torch.Generator]
, optional) —
One or a list of torch generator(s)
to make generation deterministic. torch.Tensor
, optional) —
Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
tensor will ge generated by sampling using the supplied random generator
. torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting.
If not provided, pooled text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. torch.Tensor
, optional) —
Pre-generated negative pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, pooled negative_prompt_embeds will be generated from negative_prompt
input argument.
ip_adapter_image — (PipelineImageInput
, optional): Optional image input to work with IP Adapters. List[torch.Tensor]
, optional) —
Pre-generated image embeddings for IP-Adapter. It should be a list of length same as number of
IP-adapters. Each element should be a tensor of shape (batch_size, num_images, emb_dim)
. It should
contain the negative image embedding if do_classifier_free_guidance
is set to True
. If not
provided, embeddings are computed from the ip_adapter_image
input argument. str
, optional, defaults to "pil"
) —
The output format of the generate image. Choose between
PIL: PIL.Image.Image
or np.array
. bool
, optional, defaults to True
) —
Whether or not to return a ~pipelines.kolors.KolorsPipelineOutput
instead of a plain tuple. dict
, optional) —
A kwargs dictionary that if specified is passed along to the AttentionProcessor
as defined under
self.processor
in
diffusers.models.attention_processor. Tuple[int]
, optional, defaults to (1024, 1024)) —
If original_size
is not the same as target_size
the image will appear to be down- or upsampled.
original_size
defaults to (height, width)
if not specified. Part of SDXL’s micro-conditioning as
explained in section 2.2 of
https://huggingface.co/papers/2307.01952. Tuple[int]
, optional, defaults to (0, 0)) —
crops_coords_top_left
can be used to generate an image that appears to be “cropped” from the position
crops_coords_top_left
downwards. Favorable, well-centered images are usually achieved by setting
crops_coords_top_left
to (0, 0). Part of SDXL’s micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. Tuple[int]
, optional, defaults to (1024, 1024)) —
For most cases, target_size
should be set to the desired height and width of the generated image. If
not specified it will default to (height, width)
. Part of SDXL’s micro-conditioning as explained in
section 2.2 of https://huggingface.co/papers/2307.01952. Tuple[int]
, optional, defaults to (1024, 1024)) —
To negatively condition the generation process based on a specific image resolution. Part of SDXL’s
micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. For more
information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208. Tuple[int]
, optional, defaults to (0, 0)) —
To negatively condition the generation process based on a specific crop coordinates. Part of SDXL’s
micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. For more
information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208. Tuple[int]
, optional, defaults to (1024, 1024)) —
To negatively condition the generation process based on a target image resolution. It should be as same
as the target_size
for most cases. Part of SDXL’s micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. For more
information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208. Callable
, PipelineCallback
, MultiPipelineCallbacks
, optional) —
A function or a subclass of PipelineCallback
or MultiPipelineCallbacks
that is called at the end of
each denoising step during the inference. with the following arguments: callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
. callback_kwargs
will include a
list of all tensors as specified by callback_on_step_end_tensor_inputs
. List
, optional) —
The list of tensor inputs for the callback_on_step_end
function. The tensors specified in the list
will be passed as callback_kwargs
argument. You will only be able to include variables listed in the
._callback_tensor_inputs
attribute of your pipeline class. float
, optional, defaults to 3.0) —
The scale factor for the perturbed attention guidance. If it is set to 0.0, the perturbed attention
guidance will not be used. float
, optional, defaults to 0.0) —
The adaptive scale factor for the perturbed attention guidance. If it is set to 0.0, pag_scale
is
used. int
defaults to 256) — Maximum sequence length to use with the prompt
. Returns
~pipelines.kolors.KolorsPipelineOutput
or tuple
~pipelines.kolors.KolorsPipelineOutput
if
return_dict
is True, otherwise a tuple
. When returning a tuple, the first element is a list with the
generated images.
Function invoked when calling the pipeline for generation.
Examples:
>>> import torch
>>> from diffusers import AutoPipelineForText2Image
>>> pipe = AutoPipelineForText2Image.from_pretrained(
... "Kwai-Kolors/Kolors-diffusers",
... variant="fp16",
... torch_dtype=torch.float16,
... enable_pag=True,
... pag_applied_layers=["down.block_2.attentions_1", "up.block_0.attentions_1"],
... )
>>> pipe = pipe.to("cuda")
>>> prompt = (
... "A photo of a ladybug, macro, zoom, high quality, film, holding a wooden sign with the text 'KOLORS'"
... )
>>> image = pipe(prompt, guidance_scale=5.5, pag_scale=1.5).images[0]
( prompt device: Optional = None num_images_per_prompt: int = 1 do_classifier_free_guidance: bool = True negative_prompt = None prompt_embeds: Optional = None pooled_prompt_embeds: Optional = None negative_prompt_embeds: Optional = None negative_pooled_prompt_embeds: Optional = None max_sequence_length: int = 256 )
Parameters
str
or List[str]
, optional) —
prompt to be encoded
device — (torch.device
):
torch device int
) —
number of images that should be generated per prompt bool
) —
whether to use classifier free guidance or not str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). torch.FloatTensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting.
If not provided, pooled text embeddings will be generated from prompt
input argument. torch.FloatTensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. torch.Tensor
, optional) —
Pre-generated negative pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, pooled negative_prompt_embeds will be generated from negative_prompt
input argument. int
defaults to 256) — Maximum sequence length to use with the prompt
. Encodes the prompt into text encoder hidden states.
( w: Tensor embedding_dim: int = 512 dtype: dtype = torch.float32 ) → torch.Tensor
Parameters
torch.Tensor
) —
Generate embedding vectors with a specified guidance scale to subsequently enrich timestep embeddings. int
, optional, defaults to 512) —
Dimension of the embeddings to generate. torch.dtype
, optional, defaults to torch.float32
) —
Data type of the generated embeddings. Returns
torch.Tensor
Embedding vectors with shape (len(w), embedding_dim)
.
( vae: AutoencoderKL text_encoder: CLIPTextModel tokenizer: CLIPTokenizer unet: UNet2DConditionModel scheduler: KarrasDiffusionSchedulers safety_checker: StableDiffusionSafetyChecker feature_extractor: CLIPImageProcessor image_encoder: CLIPVisionModelWithProjection = None requires_safety_checker: bool = True pag_applied_layers: Union = 'mid' )
Parameters
CLIPTokenizer
to tokenize text. UNet2DConditionModel
to denoise the encoded image latents. unet
to denoise the encoded image latents. Can be one of
DDIMScheduler, LMSDiscreteScheduler, or PNDMScheduler. StableDiffusionSafetyChecker
) —
Classification module that estimates whether generated images could be considered offensive or harmful.
Please refer to the model card for more details
about a model’s potential harms. CLIPImageProcessor
to extract features from generated images; used as inputs to the safety_checker
. Pipeline for text-to-image generation using Stable Diffusion.
This model inherits from DiffusionPipeline. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a particular device, etc.).
The pipeline also inherits the following loading methods:
.ckpt
files( prompt: Union = None height: Optional = None width: Optional = None num_inference_steps: int = 50 timesteps: List = None sigmas: List = None guidance_scale: float = 7.5 negative_prompt: Union = None num_images_per_prompt: Optional = 1 eta: float = 0.0 generator: Union = None latents: Optional = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None ip_adapter_image: Union = None ip_adapter_image_embeds: Optional = None output_type: Optional = 'pil' return_dict: bool = True cross_attention_kwargs: Optional = None guidance_rescale: float = 0.0 clip_skip: Optional = None callback_on_step_end: Optional = None callback_on_step_end_tensor_inputs: List = ['latents'] pag_scale: float = 3.0 pag_adaptive_scale: float = 0.0 ) → StableDiffusionPipelineOutput or tuple
Parameters
str
or List[str]
, optional) —
The prompt or prompts to guide image generation. If not defined, you need to pass prompt_embeds
. int
, optional, defaults to self.unet.config.sample_size * self.vae_scale_factor
) —
The height in pixels of the generated image. int
, optional, defaults to self.unet.config.sample_size * self.vae_scale_factor
) —
The width in pixels of the generated image. int
, optional, defaults to 50) —
The number of denoising steps. More denoising steps usually lead to a higher quality image at the
expense of slower inference. List[int]
, optional) —
Custom timesteps to use for the denoising process with schedulers which support a timesteps
argument
in their set_timesteps
method. If not defined, the default behavior when num_inference_steps
is
passed will be used. Must be in descending order. List[float]
, optional) —
Custom sigmas to use for the denoising process with schedulers which support a sigmas
argument in
their set_timesteps
method. If not defined, the default behavior when num_inference_steps
is passed
will be used. float
, optional, defaults to 7.5) —
A higher guidance scale value encourages the model to generate images closely linked to the text
prompt
at the expense of lower image quality. Guidance scale is enabled when guidance_scale > 1
. str
or List[str]
, optional) —
The prompt or prompts to guide what to not include in image generation. If not defined, you need to
pass negative_prompt_embeds
instead. Ignored when not using guidance (guidance_scale < 1
). int
, optional, defaults to 1) —
The number of images to generate per prompt. float
, optional, defaults to 0.0) —
Corresponds to parameter eta (η) from the DDIM paper. Only applies
to the DDIMScheduler, and is ignored in other schedulers. torch.Generator
or List[torch.Generator]
, optional) —
A torch.Generator
to make
generation deterministic. torch.Tensor
, optional) —
Pre-generated noisy latents sampled from a Gaussian distribution, to be used as inputs for image
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
tensor is generated by sampling using the supplied random generator
. torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not
provided, text embeddings are generated from the prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs (prompt weighting). If
not provided, negative_prompt_embeds
are generated from the negative_prompt
input argument.
ip_adapter_image — (PipelineImageInput
, optional): Optional image input to work with IP Adapters. List[torch.Tensor]
, optional) —
Pre-generated image embeddings for IP-Adapter. It should be a list of length same as number of
IP-adapters. Each element should be a tensor of shape (batch_size, num_images, emb_dim)
. It should
contain the negative image embedding if do_classifier_free_guidance
is set to True
. If not
provided, embeddings are computed from the ip_adapter_image
input argument. str
, optional, defaults to "pil"
) —
The output format of the generated image. Choose between PIL.Image
or np.array
. bool
, optional, defaults to True
) —
Whether or not to return a StableDiffusionPipelineOutput instead of a
plain tuple. dict
, optional) —
A kwargs dictionary that if specified is passed along to the AttentionProcessor
as defined in
self.processor
. float
, optional, defaults to 0.0) —
Guidance rescale factor from Common Diffusion Noise Schedules and Sample Steps are
Flawed. Guidance rescale factor should fix overexposure when
using zero terminal SNR. int
, optional) —
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
the output of the pre-final layer will be used for computing the prompt embeddings. Callable
, PipelineCallback
, MultiPipelineCallbacks
, optional) —
A function or a subclass of PipelineCallback
or MultiPipelineCallbacks
that is called at the end of
each denoising step during the inference. with the following arguments: callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
. callback_kwargs
will include a
list of all tensors as specified by callback_on_step_end_tensor_inputs
. List
, optional) —
The list of tensor inputs for the callback_on_step_end
function. The tensors specified in the list
will be passed as callback_kwargs
argument. You will only be able to include variables listed in the
._callback_tensor_inputs
attribute of your pipeline class. float
, optional, defaults to 3.0) —
The scale factor for the perturbed attention guidance. If it is set to 0.0, the perturbed attention
guidance will not be used. float
, optional, defaults to 0.0) —
The adaptive scale factor for the perturbed attention guidance. If it is set to 0.0, pag_scale
is
used. Returns
StableDiffusionPipelineOutput or tuple
If return_dict
is True
, StableDiffusionPipelineOutput is returned,
otherwise a tuple
is returned where the first element is a list with the generated images and the
second element is a list of bool
s indicating whether the corresponding generated image contains
“not-safe-for-work” (nsfw) content.
The call function to the pipeline for generation.
Examples:
>>> import torch
>>> from diffusers import AutoPipelineForText2Image
>>> pipe = AutoPipelineForText2Image.from_pretrained(
... "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, enable_pag=True
... )
>>> pipe = pipe.to("cuda")
>>> prompt = "a photo of an astronaut riding a horse on mars"
>>> image = pipe(prompt, pag_scale=0.3).images[0]
( prompt device num_images_per_prompt do_classifier_free_guidance negative_prompt = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None lora_scale: Optional = None clip_skip: Optional = None )
Parameters
str
or List[str]
, optional) —
prompt to be encoded
device — (torch.device
):
torch device int
) —
number of images that should be generated per prompt bool
) —
whether to use classifier free guidance or not str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. float
, optional) —
A LoRA scale that will be applied to all LoRA layers of the text encoder if LoRA layers are loaded. int
, optional) —
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
the output of the pre-final layer will be used for computing the prompt embeddings. Encodes the prompt into text encoder hidden states.
( w: Tensor embedding_dim: int = 512 dtype: dtype = torch.float32 ) → torch.Tensor
Parameters
torch.Tensor
) —
Generate embedding vectors with a specified guidance scale to subsequently enrich timestep embeddings. int
, optional, defaults to 512) —
Dimension of the embeddings to generate. torch.dtype
, optional, defaults to torch.float32
) —
Data type of the generated embeddings. Returns
torch.Tensor
Embedding vectors with shape (len(w), embedding_dim)
.
( vae: AutoencoderKL text_encoder: CLIPTextModel tokenizer: CLIPTokenizer unet: UNet2DConditionModel scheduler: KarrasDiffusionSchedulers safety_checker: StableDiffusionSafetyChecker feature_extractor: CLIPImageProcessor image_encoder: CLIPVisionModelWithProjection = None requires_safety_checker: bool = True pag_applied_layers: Union = 'mid' )
Parameters
CLIPTokenizer
to tokenize text. UNet2DConditionModel
to denoise the encoded image latents. unet
to denoise the encoded image latents. Can be one of
DDIMScheduler, LMSDiscreteScheduler, or PNDMScheduler. StableDiffusionSafetyChecker
) —
Classification module that estimates whether generated images could be considered offensive or harmful.
Please refer to the model card for more details
about a model’s potential harms. CLIPImageProcessor
to extract features from generated images; used as inputs to the safety_checker
. Pipeline for text-guided image-to-image generation using Stable Diffusion.
This model inherits from DiffusionPipeline. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a particular device, etc.).
The pipeline also inherits the following loading methods:
.ckpt
files( prompt: Union = None image: Union = None strength: float = 0.8 num_inference_steps: Optional = 50 timesteps: List = None sigmas: List = None guidance_scale: Optional = 7.5 negative_prompt: Union = None num_images_per_prompt: Optional = 1 eta: Optional = 0.0 generator: Union = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None ip_adapter_image: Union = None ip_adapter_image_embeds: Optional = None output_type: Optional = 'pil' return_dict: bool = True cross_attention_kwargs: Optional = None clip_skip: int = None callback_on_step_end: Union = None callback_on_step_end_tensor_inputs: List = ['latents'] pag_scale: float = 3.0 pag_adaptive_scale: float = 0.0 ) → StableDiffusionPipelineOutput or tuple
Parameters
str
or List[str]
, optional) —
The prompt or prompts to guide image generation. If not defined, you need to pass prompt_embeds
. torch.Tensor
, PIL.Image.Image
, np.ndarray
, List[torch.Tensor]
, List[PIL.Image.Image]
, or List[np.ndarray]
) —
Image
, numpy array or tensor representing an image batch to be used as the starting point. For both
numpy array and pytorch tensor, the expected value range is between [0, 1]
If it’s a tensor or a list
or tensors, the expected shape should be (B, C, H, W)
or (C, H, W)
. If it is a numpy array or a
list of arrays, the expected shape should be (B, H, W, C)
or (H, W, C)
It can also accept image
latents as image
, but if passing latents directly it is not encoded again. float
, optional, defaults to 0.8) —
Indicates extent to transform the reference image
. Must be between 0 and 1. image
is used as a
starting point and more noise is added the higher the strength
. The number of denoising steps depends
on the amount of noise initially added. When strength
is 1, added noise is maximum and the denoising
process runs for the full number of iterations specified in num_inference_steps
. A value of 1
essentially ignores image
. int
, optional, defaults to 50) —
The number of denoising steps. More denoising steps usually lead to a higher quality image at the
expense of slower inference. This parameter is modulated by strength
. List[int]
, optional) —
Custom timesteps to use for the denoising process with schedulers which support a timesteps
argument
in their set_timesteps
method. If not defined, the default behavior when num_inference_steps
is
passed will be used. Must be in descending order. List[float]
, optional) —
Custom sigmas to use for the denoising process with schedulers which support a sigmas
argument in
their set_timesteps
method. If not defined, the default behavior when num_inference_steps
is passed
will be used. float
, optional, defaults to 7.5) —
A higher guidance scale value encourages the model to generate images closely linked to the text
prompt
at the expense of lower image quality. Guidance scale is enabled when guidance_scale > 1
. str
or List[str]
, optional) —
The prompt or prompts to guide what to not include in image generation. If not defined, you need to
pass negative_prompt_embeds
instead. Ignored when not using guidance (guidance_scale < 1
). int
, optional, defaults to 1) —
The number of images to generate per prompt. float
, optional, defaults to 0.0) —
Corresponds to parameter eta (η) from the DDIM paper. Only applies
to the DDIMScheduler, and is ignored in other schedulers. torch.Generator
or List[torch.Generator]
, optional) —
A torch.Generator
to make
generation deterministic. torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not
provided, text embeddings are generated from the prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs (prompt weighting). If
not provided, negative_prompt_embeds
are generated from the negative_prompt
input argument.
ip_adapter_image — (PipelineImageInput
, optional): Optional image input to work with IP Adapters. List[torch.Tensor]
, optional) —
Pre-generated image embeddings for IP-Adapter. It should be a list of length same as number of
IP-adapters. Each element should be a tensor of shape (batch_size, num_images, emb_dim)
. It should
contain the negative image embedding if do_classifier_free_guidance
is set to True
. If not
provided, embeddings are computed from the ip_adapter_image
input argument. str
, optional, defaults to "pil"
) —
The output format of the generated image. Choose between PIL.Image
or np.array
. bool
, optional, defaults to True
) —
Whether or not to return a StableDiffusionPipelineOutput instead of a
plain tuple. dict
, optional) —
A kwargs dictionary that if specified is passed along to the AttentionProcessor
as defined in
self.processor
. int
, optional) —
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
the output of the pre-final layer will be used for computing the prompt embeddings. Callable
, PipelineCallback
, MultiPipelineCallbacks
, optional) —
A function or a subclass of PipelineCallback
or MultiPipelineCallbacks
that is called at the end of
each denoising step during the inference. with the following arguments: callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
. callback_kwargs
will include a
list of all tensors as specified by callback_on_step_end_tensor_inputs
. List
, optional) —
The list of tensor inputs for the callback_on_step_end
function. The tensors specified in the list
will be passed as callback_kwargs
argument. You will only be able to include variables listed in the
._callback_tensor_inputs
attribute of your pipeline class. float
, optional, defaults to 3.0) —
The scale factor for the perturbed attention guidance. If it is set to 0.0, the perturbed attention
guidance will not be used. float
, optional, defaults to 0.0) —
The adaptive scale factor for the perturbed attention guidance. If it is set to 0.0, pag_scale
is
used. Returns
StableDiffusionPipelineOutput or tuple
If return_dict
is True
, StableDiffusionPipelineOutput is returned,
otherwise a tuple
is returned where the first element is a list with the generated images and the
second element is a list of bool
s indicating whether the corresponding generated image contains
“not-safe-for-work” (nsfw) content.
The call function to the pipeline for generation.
Examples:
>>> import torch
>>> from diffusers import AutoPipelineForImage2Image
>>> from diffusers.utils import load_image
>>> pipe = AutoPipelineForImage2Image.from_pretrained(
... "runwayml/stable-diffusion-v1-5",
... torch_dtype=torch.float16,
... enable_pag=True,
... )
>>> pipe = pipe.to("cuda")
>>> url = "https://huggingface.co/datasets/patrickvonplaten/images/resolve/main/aa_xl/000000009.png"
>>> init_image = load_image(url).convert("RGB")
>>> prompt = "a photo of an astronaut riding a horse on mars"
>>> image = pipe(prompt, image=init_image, pag_scale=0.3).images[0]
( prompt device num_images_per_prompt do_classifier_free_guidance negative_prompt = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None lora_scale: Optional = None clip_skip: Optional = None )
Parameters
str
or List[str]
, optional) —
prompt to be encoded
device — (torch.device
):
torch device int
) —
number of images that should be generated per prompt bool
) —
whether to use classifier free guidance or not str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. float
, optional) —
A LoRA scale that will be applied to all LoRA layers of the text encoder if LoRA layers are loaded. int
, optional) —
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
the output of the pre-final layer will be used for computing the prompt embeddings. Encodes the prompt into text encoder hidden states.
( w: Tensor embedding_dim: int = 512 dtype: dtype = torch.float32 ) → torch.Tensor
Parameters
torch.Tensor
) —
Generate embedding vectors with a specified guidance scale to subsequently enrich timestep embeddings. int
, optional, defaults to 512) —
Dimension of the embeddings to generate. torch.dtype
, optional, defaults to torch.float32
) —
Data type of the generated embeddings. Returns
torch.Tensor
Embedding vectors with shape (len(w), embedding_dim)
.
( vae: AutoencoderKL text_encoder: CLIPTextModel tokenizer: CLIPTokenizer unet: UNet2DConditionModel controlnet: Union scheduler: KarrasDiffusionSchedulers safety_checker: StableDiffusionSafetyChecker feature_extractor: CLIPImageProcessor image_encoder: CLIPVisionModelWithProjection = None requires_safety_checker: bool = True pag_applied_layers: Union = 'mid' )
Parameters
CLIPTokenizer
to tokenize text. UNet2DConditionModel
to denoise the encoded image latents. List[ControlNetModel]
) —
Provides additional conditioning to the unet
during the denoising process. If you set multiple
ControlNets as a list, the outputs from each ControlNet are added together to create one combined
additional conditioning. unet
to denoise the encoded image latents. Can be one of
DDIMScheduler, LMSDiscreteScheduler, or PNDMScheduler. StableDiffusionSafetyChecker
) —
Classification module that estimates whether generated images could be considered offensive or harmful.
Please refer to the model card for more details
about a model’s potential harms. CLIPImageProcessor
to extract features from generated images; used as inputs to the safety_checker
. Pipeline for text-to-image generation using Stable Diffusion with ControlNet guidance.
This model inherits from DiffusionPipeline. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a particular device, etc.).
The pipeline also inherits the following loading methods:
.ckpt
files( prompt device num_images_per_prompt do_classifier_free_guidance negative_prompt = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None lora_scale: Optional = None clip_skip: Optional = None )
Parameters
str
or List[str]
, optional) —
prompt to be encoded
device — (torch.device
):
torch device int
) —
number of images that should be generated per prompt bool
) —
whether to use classifier free guidance or not str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. float
, optional) —
A LoRA scale that will be applied to all LoRA layers of the text encoder if LoRA layers are loaded. int
, optional) —
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
the output of the pre-final layer will be used for computing the prompt embeddings. Encodes the prompt into text encoder hidden states.
( w: Tensor embedding_dim: int = 512 dtype: dtype = torch.float32 ) → torch.Tensor
Parameters
torch.Tensor
) —
Generate embedding vectors with a specified guidance scale to subsequently enrich timestep embeddings. int
, optional, defaults to 512) —
Dimension of the embeddings to generate. torch.dtype
, optional, defaults to torch.float32
) —
Data type of the generated embeddings. Returns
torch.Tensor
Embedding vectors with shape (len(w), embedding_dim)
.
( vae: AutoencoderKL text_encoder: CLIPTextModel tokenizer: CLIPTokenizer unet: UNet2DConditionModel controlnet: Union scheduler: KarrasDiffusionSchedulers safety_checker: StableDiffusionSafetyChecker feature_extractor: CLIPImageProcessor image_encoder: CLIPVisionModelWithProjection = None requires_safety_checker: bool = True pag_applied_layers: Union = 'mid' )
Parameters
CLIPTokenizer
to tokenize text. UNet2DConditionModel
to denoise the encoded image latents. List[ControlNetModel]
) —
Provides additional conditioning to the unet
during the denoising process. If you set multiple
ControlNets as a list, the outputs from each ControlNet are added together to create one combined
additional conditioning. unet
to denoise the encoded image latents. Can be one of
DDIMScheduler, LMSDiscreteScheduler, or PNDMScheduler. StableDiffusionSafetyChecker
) —
Classification module that estimates whether generated images could be considered offensive or harmful.
Please refer to the model card for more details
about a model’s potential harms. CLIPImageProcessor
to extract features from generated images; used as inputs to the safety_checker
. Pipeline for image inpainting using Stable Diffusion with ControlNet guidance.
This model inherits from DiffusionPipeline. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a particular device, etc.).
The pipeline also inherits the following loading methods:
.ckpt
filesThis pipeline can be used with checkpoints that have been specifically fine-tuned for inpainting (runwayml/stable-diffusion-inpainting) as well as default text-to-image Stable Diffusion checkpoints (runwayml/stable-diffusion-v1-5). Default text-to-image Stable Diffusion checkpoints might be preferable for ControlNets that have been fine-tuned on those, such as lllyasviel/control_v11p_sd15_inpaint.
( prompt: Union = None image: Union = None mask_image: Union = None control_image: Union = None height: Optional = None width: Optional = None padding_mask_crop: Optional = None strength: float = 1.0 num_inference_steps: int = 50 guidance_scale: float = 7.5 negative_prompt: Union = None num_images_per_prompt: Optional = 1 eta: float = 0.0 generator: Union = None latents: Optional = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None ip_adapter_image: Union = None ip_adapter_image_embeds: Optional = None output_type: Optional = 'pil' return_dict: bool = True cross_attention_kwargs: Optional = None controlnet_conditioning_scale: Union = 0.5 control_guidance_start: Union = 0.0 control_guidance_end: Union = 1.0 clip_skip: Optional = None callback_on_step_end: Union = None callback_on_step_end_tensor_inputs: List = ['latents'] pag_scale: float = 3.0 pag_adaptive_scale: float = 0.0 ) → StableDiffusionPipelineOutput or tuple
Parameters
str
or List[str]
, optional) —
The prompt or prompts to guide image generation. If not defined, you need to pass prompt_embeds
. torch.Tensor
, PIL.Image.Image
, np.ndarray
, List[torch.Tensor]
, —
List[PIL.Image.Image]
, or List[np.ndarray]
):
Image
, NumPy array or tensor representing an image batch to be used as the starting point. For both
NumPy array and PyTorch tensor, the expected value range is between [0, 1]
. If it’s a tensor or a
list or tensors, the expected shape should be (B, C, H, W)
or (C, H, W)
. If it is a NumPy array or
a list of arrays, the expected shape should be (B, H, W, C)
or (H, W, C)
. It can also accept image
latents as image
, but if passing latents directly it is not encoded again. torch.Tensor
, PIL.Image.Image
, np.ndarray
, List[torch.Tensor]
, —
List[PIL.Image.Image]
, or List[np.ndarray]
):
Image
, NumPy array or tensor representing an image batch to mask image
. White pixels in the mask
are repainted while black pixels are preserved. If mask_image
is a PIL image, it is converted to a
single channel (luminance) before use. If it’s a NumPy array or PyTorch tensor, it should contain one
color channel (L) instead of 3, so the expected shape for PyTorch tensor would be (B, 1, H, W)
, (B, H, W)
, (1, H, W)
, (H, W)
. And for NumPy array, it would be for (B, H, W, 1)
, (B, H, W)
, (H, W, 1)
, or (H, W)
. torch.Tensor
, PIL.Image.Image
, List[torch.Tensor]
, List[PIL.Image.Image]
, —
List[List[torch.Tensor]]
, or List[List[PIL.Image.Image]]
):
The ControlNet input condition to provide guidance to the unet
for generation. If the type is
specified as torch.Tensor
, it is passed to ControlNet as is. PIL.Image.Image
can also be accepted
as an image. The dimensions of the output image defaults to image
’s dimensions. If height and/or
width are passed, image
is resized accordingly. If multiple ControlNets are specified in init
,
images must be passed as a list such that each element of the list can be correctly batched for input
to a single ControlNet. int
, optional, defaults to self.unet.config.sample_size * self.vae_scale_factor
) —
The height in pixels of the generated image. int
, optional, defaults to self.unet.config.sample_size * self.vae_scale_factor
) —
The width in pixels of the generated image. int
, optional, defaults to None
) —
The size of margin in the crop to be applied to the image and masking. If None
, no crop is applied to
image and mask_image. If padding_mask_crop
is not None
, it will first find a rectangular region
with the same aspect ration of the image and contains all masked area, and then expand that area based
on padding_mask_crop
. The image and mask_image will then be cropped based on the expanded area before
resizing to the original image size for inpainting. This is useful when the masked area is small while
the image is large and contain information irrelevant for inpainting, such as background. float
, optional, defaults to 1.0) —
Indicates extent to transform the reference image
. Must be between 0 and 1. image
is used as a
starting point and more noise is added the higher the strength
. The number of denoising steps depends
on the amount of noise initially added. When strength
is 1, added noise is maximum and the denoising
process runs for the full number of iterations specified in num_inference_steps
. A value of 1
essentially ignores image
. int
, optional, defaults to 50) —
The number of denoising steps. More denoising steps usually lead to a higher quality image at the
expense of slower inference. float
, optional, defaults to 7.5) —
A higher guidance scale value encourages the model to generate images closely linked to the text
prompt
at the expense of lower image quality. Guidance scale is enabled when guidance_scale > 1
. str
or List[str]
, optional) —
The prompt or prompts to guide what to not include in image generation. If not defined, you need to
pass negative_prompt_embeds
instead. Ignored when not using guidance (guidance_scale < 1
). int
, optional, defaults to 1) —
The number of images to generate per prompt. float
, optional, defaults to 0.0) —
Corresponds to parameter eta (η) from the DDIM paper. Only applies
to the DDIMScheduler, and is ignored in other schedulers. torch.Generator
or List[torch.Generator]
, optional) —
A torch.Generator
to make
generation deterministic. torch.Tensor
, optional) —
Pre-generated noisy latents sampled from a Gaussian distribution, to be used as inputs for image
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
tensor is generated by sampling using the supplied random generator
. torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not
provided, text embeddings are generated from the prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs (prompt weighting). If
not provided, negative_prompt_embeds
are generated from the negative_prompt
input argument.
ip_adapter_image — (PipelineImageInput
, optional): Optional image input to work with IP Adapters. List[torch.Tensor]
, optional) —
Pre-generated image embeddings for IP-Adapter. It should be a list of length same as number of
IP-adapters. Each element should be a tensor of shape (batch_size, num_images, emb_dim)
. It should
contain the negative image embedding if do_classifier_free_guidance
is set to True
. If not
provided, embeddings are computed from the ip_adapter_image
input argument. str
, optional, defaults to "pil"
) —
The output format of the generated image. Choose between PIL.Image
or np.array
. bool
, optional, defaults to True
) —
Whether or not to return a StableDiffusionPipelineOutput instead of a
plain tuple. dict
, optional) —
A kwargs dictionary that if specified is passed along to the AttentionProcessor
as defined in
self.processor
. float
or List[float]
, optional, defaults to 0.5) —
The outputs of the ControlNet are multiplied by controlnet_conditioning_scale
before they are added
to the residual in the original unet
. If multiple ControlNets are specified in init
, you can set
the corresponding scale as a list. float
or List[float]
, optional, defaults to 0.0) —
The percentage of total steps at which the ControlNet starts applying. float
or List[float]
, optional, defaults to 1.0) —
The percentage of total steps at which the ControlNet stops applying. int
, optional) —
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
the output of the pre-final layer will be used for computing the prompt embeddings. Callable
, PipelineCallback
, MultiPipelineCallbacks
, optional) —
A function or a subclass of PipelineCallback
or MultiPipelineCallbacks
that is called at the end of
each denoising step during the inference. with the following arguments: callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
. callback_kwargs
will include a
list of all tensors as specified by callback_on_step_end_tensor_inputs
. List
, optional) —
The list of tensor inputs for the callback_on_step_end
function. The tensors specified in the list
will be passed as callback_kwargs
argument. You will only be able to include variables listed in the
._callback_tensor_inputs
attribute of your pipeline class. float
, optional, defaults to 3.0) —
The scale factor for the perturbed attention guidance. If it is set to 0.0, the perturbed attention
guidance will not be used. float
, optional, defaults to 0.0) —
The adaptive scale factor for the perturbed attention guidance. If it is set to 0.0, pag_scale
is
used. Returns
StableDiffusionPipelineOutput or tuple
If return_dict
is True
, StableDiffusionPipelineOutput is returned,
otherwise a tuple
is returned where the first element is a list with the generated images and the
second element is a list of bool
s indicating whether the corresponding generated image contains
“not-safe-for-work” (nsfw) content.
The call function to the pipeline for generation.
Examples:
>>> # !pip install transformers accelerate
>>> import cv2
>>> from diffusers import AutoPipelineForInpainting, ControlNetModel, DDIMScheduler
>>> from diffusers.utils import load_image
>>> import numpy as np
>>> from PIL import Image
>>> import torch
>>> init_image = load_image(
... "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_inpaint/boy.png"
... )
>>> init_image = init_image.resize((512, 512))
>>> generator = torch.Generator(device="cpu").manual_seed(1)
>>> mask_image = load_image(
... "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_inpaint/boy_mask.png"
... )
>>> mask_image = mask_image.resize((512, 512))
>>> def make_canny_condition(image):
... image = np.array(image)
... image = cv2.Canny(image, 100, 200)
... image = image[:, :, None]
... image = np.concatenate([image, image, image], axis=2)
... image = Image.fromarray(image)
... return image
>>> control_image = make_canny_condition(init_image)
>>> controlnet = ControlNetModel.from_pretrained(
... "lllyasviel/control_v11p_sd15_inpaint", torch_dtype=torch.float16
... )
>>> pipe = AutoPipelineForInpainting.from_pretrained(
... "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16, enable_pag=True
... )
>>> pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
>>> pipe.enable_model_cpu_offload()
>>> # generate image
>>> image = pipe(
... "a handsome man with ray-ban sunglasses",
... num_inference_steps=20,
... generator=generator,
... eta=1.0,
... image=init_image,
... mask_image=mask_image,
... control_image=control_image,
... pag_scale=0.3,
... ).images[0]
( prompt device num_images_per_prompt do_classifier_free_guidance negative_prompt = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None lora_scale: Optional = None clip_skip: Optional = None )
Parameters
str
or List[str]
, optional) —
prompt to be encoded
device — (torch.device
):
torch device int
) —
number of images that should be generated per prompt bool
) —
whether to use classifier free guidance or not str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. float
, optional) —
A LoRA scale that will be applied to all LoRA layers of the text encoder if LoRA layers are loaded. int
, optional) —
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
the output of the pre-final layer will be used for computing the prompt embeddings. Encodes the prompt into text encoder hidden states.
( w: Tensor embedding_dim: int = 512 dtype: dtype = torch.float32 ) → torch.Tensor
Parameters
torch.Tensor
) —
Generate embedding vectors with a specified guidance scale to subsequently enrich timestep embeddings. int
, optional, defaults to 512) —
Dimension of the embeddings to generate. torch.dtype
, optional, defaults to torch.float32
) —
Data type of the generated embeddings. Returns
torch.Tensor
Embedding vectors with shape (len(w), embedding_dim)
.
( vae: AutoencoderKL text_encoder: CLIPTextModel text_encoder_2: CLIPTextModelWithProjection tokenizer: CLIPTokenizer tokenizer_2: CLIPTokenizer unet: UNet2DConditionModel scheduler: KarrasDiffusionSchedulers image_encoder: CLIPVisionModelWithProjection = None feature_extractor: CLIPImageProcessor = None force_zeros_for_empty_prompt: bool = True add_watermarker: Optional = None pag_applied_layers: Union = 'mid' )
Parameters
CLIPTextModel
) —
Frozen text-encoder. Stable Diffusion XL uses the text portion of
CLIP, specifically
the clip-vit-large-patch14 variant. CLIPTextModelWithProjection
) —
Second frozen text-encoder. Stable Diffusion XL uses the text and pool portion of
CLIP,
specifically the
laion/CLIP-ViT-bigG-14-laion2B-39B-b160k
variant. CLIPTokenizer
) —
Tokenizer of class
CLIPTokenizer. CLIPTokenizer
) —
Second Tokenizer of class
CLIPTokenizer. unet
to denoise the encoded image latents. Can be one of
DDIMScheduler, LMSDiscreteScheduler, or PNDMScheduler. bool
, optional, defaults to "True"
) —
Whether the negative prompt embeddings shall be forced to always be set to 0. Also see the config of
stabilityai/stable-diffusion-xl-base-1-0
. bool
, optional) —
Whether to use the invisible_watermark library to
watermark output images. If not defined, it will default to True if the package is installed, otherwise no
watermarker will be used. Pipeline for text-to-image generation using Stable Diffusion XL.
This model inherits from DiffusionPipeline. Check the superclass documentation for the generic methods the library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)
The pipeline also inherits the following loading methods:
.ckpt
files( prompt: Union = None prompt_2: Union = None height: Optional = None width: Optional = None num_inference_steps: int = 50 timesteps: List = None sigmas: List = None denoising_end: Optional = None guidance_scale: float = 5.0 negative_prompt: Union = None negative_prompt_2: Union = None num_images_per_prompt: Optional = 1 eta: float = 0.0 generator: Union = None latents: Optional = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None pooled_prompt_embeds: Optional = None negative_pooled_prompt_embeds: Optional = None ip_adapter_image: Union = None ip_adapter_image_embeds: Optional = None output_type: Optional = 'pil' return_dict: bool = True cross_attention_kwargs: Optional = None guidance_rescale: float = 0.0 original_size: Optional = None crops_coords_top_left: Tuple = (0, 0) target_size: Optional = None negative_original_size: Optional = None negative_crops_coords_top_left: Tuple = (0, 0) negative_target_size: Optional = None clip_skip: Optional = None callback_on_step_end: Optional = None callback_on_step_end_tensor_inputs: List = ['latents'] pag_scale: float = 3.0 pag_adaptive_scale: float = 0.0 ) → ~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput
or tuple
Parameters
str
or List[str]
, optional) —
The prompt or prompts to guide the image generation. If not defined, one has to pass prompt_embeds
.
instead. str
or List[str]
, optional) —
The prompt or prompts to be sent to the tokenizer_2
and text_encoder_2
. If not defined, prompt
is
used in both text-encoders int
, optional, defaults to self.unet.config.sample_size * self.vae_scale_factor) —
The height in pixels of the generated image. This is set to 1024 by default for the best results.
Anything below 512 pixels won’t work well for
stabilityai/stable-diffusion-xl-base-1.0
and checkpoints that are not specifically fine-tuned on low resolutions. int
, optional, defaults to self.unet.config.sample_size * self.vae_scale_factor) —
The width in pixels of the generated image. This is set to 1024 by default for the best results.
Anything below 512 pixels won’t work well for
stabilityai/stable-diffusion-xl-base-1.0
and checkpoints that are not specifically fine-tuned on low resolutions. int
, optional, defaults to 50) —
The number of denoising steps. More denoising steps usually lead to a higher quality image at the
expense of slower inference. List[int]
, optional) —
Custom timesteps to use for the denoising process with schedulers which support a timesteps
argument
in their set_timesteps
method. If not defined, the default behavior when num_inference_steps
is
passed will be used. Must be in descending order. List[float]
, optional) —
Custom sigmas to use for the denoising process with schedulers which support a sigmas
argument in
their set_timesteps
method. If not defined, the default behavior when num_inference_steps
is passed
will be used. float
, optional) —
When specified, determines the fraction (between 0.0 and 1.0) of the total denoising process to be
completed before it is intentionally prematurely terminated. As a result, the returned sample will
still retain a substantial amount of noise as determined by the discrete timesteps selected by the
scheduler. The denoising_end parameter should ideally be utilized when this pipeline forms a part of a
“Mixture of Denoisers” multi-pipeline setup, as elaborated in Refining the Image
Output float
, optional, defaults to 5.0) —
Guidance scale as defined in Classifier-Free Diffusion Guidance.
guidance_scale
is defined as w
of equation 2. of Imagen
Paper. Guidance scale is enabled by setting guidance_scale > 1
. Higher guidance scale encourages to generate images that are closely linked to the text prompt
,
usually at the expense of lower image quality. str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation to be sent to tokenizer_2
and
text_encoder_2
. If not defined, negative_prompt
is used in both text-encoders int
, optional, defaults to 1) —
The number of images to generate per prompt. float
, optional, defaults to 0.0) —
Corresponds to parameter eta (η) in the DDIM paper: https://arxiv.org/abs/2010.02502. Only applies to
schedulers.DDIMScheduler, will be ignored for others. torch.Generator
or List[torch.Generator]
, optional) —
One or a list of torch generator(s)
to make generation deterministic. torch.Tensor
, optional) —
Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
tensor will ge generated by sampling using the supplied random generator
. torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. torch.Tensor
, optional) —
Pre-generated pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting.
If not provided, pooled text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, pooled negative_prompt_embeds will be generated from negative_prompt
input argument.
ip_adapter_image — (PipelineImageInput
, optional): Optional image input to work with IP Adapters. List[torch.Tensor]
, optional) —
Pre-generated image embeddings for IP-Adapter. It should be a list of length same as number of
IP-adapters. Each element should be a tensor of shape (batch_size, num_images, emb_dim)
. It should
contain the negative image embedding if do_classifier_free_guidance
is set to True
. If not
provided, embeddings are computed from the ip_adapter_image
input argument. str
, optional, defaults to "pil"
) —
The output format of the generate image. Choose between
PIL: PIL.Image.Image
or np.array
. bool
, optional, defaults to True
) —
Whether or not to return a ~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput
instead
of a plain tuple. dict
, optional) —
A kwargs dictionary that if specified is passed along to the AttentionProcessor
as defined under
self.processor
in
diffusers.models.attention_processor. float
, optional, defaults to 0.0) —
Guidance rescale factor proposed by Common Diffusion Noise Schedules and Sample Steps are
Flawed guidance_scale
is defined as φ
in equation 16. of
Common Diffusion Noise Schedules and Sample Steps are Flawed.
Guidance rescale factor should fix overexposure when using zero terminal SNR. Tuple[int]
, optional, defaults to (1024, 1024)) —
If original_size
is not the same as target_size
the image will appear to be down- or upsampled.
original_size
defaults to (height, width)
if not specified. Part of SDXL’s micro-conditioning as
explained in section 2.2 of
https://huggingface.co/papers/2307.01952. Tuple[int]
, optional, defaults to (0, 0)) —
crops_coords_top_left
can be used to generate an image that appears to be “cropped” from the position
crops_coords_top_left
downwards. Favorable, well-centered images are usually achieved by setting
crops_coords_top_left
to (0, 0). Part of SDXL’s micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. Tuple[int]
, optional, defaults to (1024, 1024)) —
For most cases, target_size
should be set to the desired height and width of the generated image. If
not specified it will default to (height, width)
. Part of SDXL’s micro-conditioning as explained in
section 2.2 of https://huggingface.co/papers/2307.01952. Tuple[int]
, optional, defaults to (1024, 1024)) —
To negatively condition the generation process based on a specific image resolution. Part of SDXL’s
micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. For more
information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208. Tuple[int]
, optional, defaults to (0, 0)) —
To negatively condition the generation process based on a specific crop coordinates. Part of SDXL’s
micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. For more
information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208. Tuple[int]
, optional, defaults to (1024, 1024)) —
To negatively condition the generation process based on a target image resolution. It should be as same
as the target_size
for most cases. Part of SDXL’s micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. For more
information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208. Callable
, optional) —
A function that calls at the end of each denoising steps during the inference. The function is called
with the following arguments: callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
. callback_kwargs
will include a list of all tensors as specified by
callback_on_step_end_tensor_inputs
. List
, optional) —
The list of tensor inputs for the callback_on_step_end
function. The tensors specified in the list
will be passed as callback_kwargs
argument. You will only be able to include variables listed in the
._callback_tensor_inputs
attribute of your pipeline class. float
, optional, defaults to 3.0) —
The scale factor for the perturbed attention guidance. If it is set to 0.0, the perturbed attention
guidance will not be used. float
, optional, defaults to 0.0) —
The adaptive scale factor for the perturbed attention guidance. If it is set to 0.0, pag_scale
is
used. Returns
~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput
or tuple
~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput
if return_dict
is True, otherwise a
tuple
. When returning a tuple, the first element is a list with the generated images.
Function invoked when calling the pipeline for generation.
Examples:
>>> import torch
>>> from diffusers import AutoPipelineForText2Image
>>> pipe = AutoPipelineForText2Image.from_pretrained(
... "stabilityai/stable-diffusion-xl-base-1.0",
... torch_dtype=torch.float16,
... enable_pag=True,
... )
>>> pipe = pipe.to("cuda")
>>> prompt = "a photo of an astronaut riding a horse on mars"
>>> image = pipe(prompt, pag_scale=0.3).images[0]
( prompt: str prompt_2: Optional = None device: Optional = None num_images_per_prompt: int = 1 do_classifier_free_guidance: bool = True negative_prompt: Optional = None negative_prompt_2: Optional = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None pooled_prompt_embeds: Optional = None negative_pooled_prompt_embeds: Optional = None lora_scale: Optional = None clip_skip: Optional = None )
Parameters
str
or List[str]
, optional) —
prompt to be encoded str
or List[str]
, optional) —
The prompt or prompts to be sent to the tokenizer_2
and text_encoder_2
. If not defined, prompt
is
used in both text-encoders
device — (torch.device
):
torch device int
) —
number of images that should be generated per prompt bool
) —
whether to use classifier free guidance or not str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation to be sent to tokenizer_2
and
text_encoder_2
. If not defined, negative_prompt
is used in both text-encoders torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. torch.Tensor
, optional) —
Pre-generated pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting.
If not provided, pooled text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, pooled negative_prompt_embeds will be generated from negative_prompt
input argument. float
, optional) —
A lora scale that will be applied to all LoRA layers of the text encoder if LoRA layers are loaded. int
, optional) —
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
the output of the pre-final layer will be used for computing the prompt embeddings. Encodes the prompt into text encoder hidden states.
( w: Tensor embedding_dim: int = 512 dtype: dtype = torch.float32 ) → torch.Tensor
Parameters
torch.Tensor
) —
Generate embedding vectors with a specified guidance scale to subsequently enrich timestep embeddings. int
, optional, defaults to 512) —
Dimension of the embeddings to generate. torch.dtype
, optional, defaults to torch.float32
) —
Data type of the generated embeddings. Returns
torch.Tensor
Embedding vectors with shape (len(w), embedding_dim)
.
( vae: AutoencoderKL text_encoder: CLIPTextModel text_encoder_2: CLIPTextModelWithProjection tokenizer: CLIPTokenizer tokenizer_2: CLIPTokenizer unet: UNet2DConditionModel scheduler: KarrasDiffusionSchedulers image_encoder: CLIPVisionModelWithProjection = None feature_extractor: CLIPImageProcessor = None requires_aesthetics_score: bool = False force_zeros_for_empty_prompt: bool = True add_watermarker: Optional = None pag_applied_layers: Union = 'mid' )
Parameters
CLIPTextModel
) —
Frozen text-encoder. Stable Diffusion XL uses the text portion of
CLIP, specifically
the clip-vit-large-patch14 variant. CLIPTextModelWithProjection
) —
Second frozen text-encoder. Stable Diffusion XL uses the text and pool portion of
CLIP,
specifically the
laion/CLIP-ViT-bigG-14-laion2B-39B-b160k
variant. CLIPTokenizer
) —
Tokenizer of class
CLIPTokenizer. CLIPTokenizer
) —
Second Tokenizer of class
CLIPTokenizer. unet
to denoise the encoded image latents. Can be one of
DDIMScheduler, LMSDiscreteScheduler, or PNDMScheduler. bool
, optional, defaults to "False"
) —
Whether the unet
requires an aesthetic_score
condition to be passed during inference. Also see the
config of stabilityai/stable-diffusion-xl-refiner-1-0
. bool
, optional, defaults to "True"
) —
Whether the negative prompt embeddings shall be forced to always be set to 0. Also see the config of
stabilityai/stable-diffusion-xl-base-1-0
. bool
, optional) —
Whether to use the invisible_watermark library to
watermark output images. If not defined, it will default to True if the package is installed, otherwise no
watermarker will be used. Pipeline for text-to-image generation using Stable Diffusion XL.
This model inherits from DiffusionPipeline. Check the superclass documentation for the generic methods the library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)
The pipeline also inherits the following loading methods:
.ckpt
files( prompt: Union = None prompt_2: Union = None image: Union = None strength: float = 0.3 num_inference_steps: int = 50 timesteps: List = None sigmas: List = None denoising_start: Optional = None denoising_end: Optional = None guidance_scale: float = 5.0 negative_prompt: Union = None negative_prompt_2: Union = None num_images_per_prompt: Optional = 1 eta: float = 0.0 generator: Union = None latents: Optional = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None pooled_prompt_embeds: Optional = None negative_pooled_prompt_embeds: Optional = None ip_adapter_image: Union = None ip_adapter_image_embeds: Optional = None output_type: Optional = 'pil' return_dict: bool = True cross_attention_kwargs: Optional = None guidance_rescale: float = 0.0 original_size: Tuple = None crops_coords_top_left: Tuple = (0, 0) target_size: Tuple = None negative_original_size: Optional = None negative_crops_coords_top_left: Tuple = (0, 0) negative_target_size: Optional = None aesthetic_score: float = 6.0 negative_aesthetic_score: float = 2.5 clip_skip: Optional = None callback_on_step_end: Union = None callback_on_step_end_tensor_inputs: List = ['latents'] pag_scale: float = 3.0 pag_adaptive_scale: float = 0.0 ) → ~pipelines.stable_diffusion.StableDiffusionXLPipelineOutput
or tuple
Parameters
str
or List[str]
, optional) —
The prompt or prompts to guide the image generation. If not defined, one has to pass prompt_embeds
.
instead. str
or List[str]
, optional) —
The prompt or prompts to be sent to the tokenizer_2
and text_encoder_2
. If not defined, prompt
is
used in both text-encoders torch.Tensor
or PIL.Image.Image
or np.ndarray
or List[torch.Tensor]
or List[PIL.Image.Image]
or List[np.ndarray]
) —
The image(s) to modify with the pipeline. float
, optional, defaults to 0.3) —
Conceptually, indicates how much to transform the reference image
. Must be between 0 and 1. image
will be used as a starting point, adding more noise to it the larger the strength
. The number of
denoising steps depends on the amount of noise initially added. When strength
is 1, added noise will
be maximum and the denoising process will run for the full number of iterations specified in
num_inference_steps
. A value of 1, therefore, essentially ignores image
. Note that in the case of
denoising_start
being declared as an integer, the value of strength
will be ignored. int
, optional, defaults to 50) —
The number of denoising steps. More denoising steps usually lead to a higher quality image at the
expense of slower inference. List[int]
, optional) —
Custom timesteps to use for the denoising process with schedulers which support a timesteps
argument
in their set_timesteps
method. If not defined, the default behavior when num_inference_steps
is
passed will be used. Must be in descending order. List[float]
, optional) —
Custom sigmas to use for the denoising process with schedulers which support a sigmas
argument in
their set_timesteps
method. If not defined, the default behavior when num_inference_steps
is passed
will be used. float
, optional) —
When specified, indicates the fraction (between 0.0 and 1.0) of the total denoising process to be
bypassed before it is initiated. Consequently, the initial part of the denoising process is skipped and
it is assumed that the passed image
is a partly denoised image. Note that when this is specified,
strength will be ignored. The denoising_start
parameter is particularly beneficial when this pipeline
is integrated into a “Mixture of Denoisers” multi-pipeline setup, as detailed in Refine Image
Quality. float
, optional) —
When specified, determines the fraction (between 0.0 and 1.0) of the total denoising process to be
completed before it is intentionally prematurely terminated. As a result, the returned sample will
still retain a substantial amount of noise (ca. final 20% of timesteps still needed) and should be
denoised by a successor pipeline that has denoising_start
set to 0.8 so that it only denoises the
final 20% of the scheduler. The denoising_end parameter should ideally be utilized when this pipeline
forms a part of a “Mixture of Denoisers” multi-pipeline setup, as elaborated in Refine Image
Quality. float
, optional, defaults to 7.5) —
Guidance scale as defined in Classifier-Free Diffusion Guidance.
guidance_scale
is defined as w
of equation 2. of Imagen
Paper. Guidance scale is enabled by setting guidance_scale > 1
. Higher guidance scale encourages to generate images that are closely linked to the text prompt
,
usually at the expense of lower image quality. str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation to be sent to tokenizer_2
and
text_encoder_2
. If not defined, negative_prompt
is used in both text-encoders int
, optional, defaults to 1) —
The number of images to generate per prompt. float
, optional, defaults to 0.0) —
Corresponds to parameter eta (η) in the DDIM paper: https://arxiv.org/abs/2010.02502. Only applies to
schedulers.DDIMScheduler, will be ignored for others. torch.Generator
or List[torch.Generator]
, optional) —
One or a list of torch generator(s)
to make generation deterministic. torch.Tensor
, optional) —
Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
tensor will ge generated by sampling using the supplied random generator
. torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. torch.Tensor
, optional) —
Pre-generated pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting.
If not provided, pooled text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, pooled negative_prompt_embeds will be generated from negative_prompt
input argument.
ip_adapter_image — (PipelineImageInput
, optional): Optional image input to work with IP Adapters. List[torch.Tensor]
, optional) —
Pre-generated image embeddings for IP-Adapter. It should be a list of length same as number of
IP-adapters. Each element should be a tensor of shape (batch_size, num_images, emb_dim)
. It should
contain the negative image embedding if do_classifier_free_guidance
is set to True
. If not
provided, embeddings are computed from the ip_adapter_image
input argument. str
, optional, defaults to "pil"
) —
The output format of the generate image. Choose between
PIL: PIL.Image.Image
or np.array
. bool
, optional, defaults to True
) —
Whether or not to return a ~pipelines.stable_diffusion.StableDiffusionXLPipelineOutput
instead of a
plain tuple. dict
, optional) —
A kwargs dictionary that if specified is passed along to the AttentionProcessor
as defined under
self.processor
in
diffusers.models.attention_processor. float
, optional, defaults to 0.0) —
Guidance rescale factor proposed by Common Diffusion Noise Schedules and Sample Steps are
Flawed guidance_scale
is defined as φ
in equation 16. of
Common Diffusion Noise Schedules and Sample Steps are Flawed.
Guidance rescale factor should fix overexposure when using zero terminal SNR. Tuple[int]
, optional, defaults to (1024, 1024)) —
If original_size
is not the same as target_size
the image will appear to be down- or upsampled.
original_size
defaults to (height, width)
if not specified. Part of SDXL’s micro-conditioning as
explained in section 2.2 of
https://huggingface.co/papers/2307.01952. Tuple[int]
, optional, defaults to (0, 0)) —
crops_coords_top_left
can be used to generate an image that appears to be “cropped” from the position
crops_coords_top_left
downwards. Favorable, well-centered images are usually achieved by setting
crops_coords_top_left
to (0, 0). Part of SDXL’s micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. Tuple[int]
, optional, defaults to (1024, 1024)) —
For most cases, target_size
should be set to the desired height and width of the generated image. If
not specified it will default to (height, width)
. Part of SDXL’s micro-conditioning as explained in
section 2.2 of https://huggingface.co/papers/2307.01952. Tuple[int]
, optional, defaults to (1024, 1024)) —
To negatively condition the generation process based on a specific image resolution. Part of SDXL’s
micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. For more
information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208. Tuple[int]
, optional, defaults to (0, 0)) —
To negatively condition the generation process based on a specific crop coordinates. Part of SDXL’s
micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. For more
information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208. Tuple[int]
, optional, defaults to (1024, 1024)) —
To negatively condition the generation process based on a target image resolution. It should be as same
as the target_size
for most cases. Part of SDXL’s micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. For more
information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208. float
, optional, defaults to 6.0) —
Used to simulate an aesthetic score of the generated image by influencing the positive text condition.
Part of SDXL’s micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. float
, optional, defaults to 2.5) —
Part of SDXL’s micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. Can be used to
simulate an aesthetic score of the generated image by influencing the negative text condition. int
, optional) —
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
the output of the pre-final layer will be used for computing the prompt embeddings. Callable
, PipelineCallback
, MultiPipelineCallbacks
, optional) —
A function or a subclass of PipelineCallback
or MultiPipelineCallbacks
that is called at the end of
each denoising step during the inference. with the following arguments: callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
. callback_kwargs
will include a
list of all tensors as specified by callback_on_step_end_tensor_inputs
. List
, optional) —
The list of tensor inputs for the callback_on_step_end
function. The tensors specified in the list
will be passed as callback_kwargs
argument. You will only be able to include variables listed in the
._callback_tensor_inputs
attribute of your pipeline class. float
, optional, defaults to 3.0) —
The scale factor for the perturbed attention guidance. If it is set to 0.0, the perturbed attention
guidance will not be used. float
, optional, defaults to 0.0) —
The adaptive scale factor for the perturbed attention guidance. If it is set to 0.0, pag_scale
is
used. Returns
~pipelines.stable_diffusion.StableDiffusionXLPipelineOutput
or tuple
~pipelines.stable_diffusion.StableDiffusionXLPipelineOutput
if return_dict
is True, otherwise a
`tuple. When returning a tuple, the first element is a list with the generated images.
Function invoked when calling the pipeline for generation.
Examples:
>>> import torch
>>> from diffusers import AutoPipelineForImage2Image
>>> from diffusers.utils import load_image
>>> pipe = AutoPipelineForImage2Image.from_pretrained(
... "stabilityai/stable-diffusion-xl-refiner-1.0",
... torch_dtype=torch.float16,
... enable_pag=True,
... )
>>> pipe = pipe.to("cuda")
>>> url = "https://huggingface.co/datasets/patrickvonplaten/images/resolve/main/aa_xl/000000009.png"
>>> init_image = load_image(url).convert("RGB")
>>> prompt = "a photo of an astronaut riding a horse on mars"
>>> image = pipe(prompt, image=init_image, pag_scale=0.3).images[0]
( prompt: str prompt_2: Optional = None device: Optional = None num_images_per_prompt: int = 1 do_classifier_free_guidance: bool = True negative_prompt: Optional = None negative_prompt_2: Optional = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None pooled_prompt_embeds: Optional = None negative_pooled_prompt_embeds: Optional = None lora_scale: Optional = None clip_skip: Optional = None )
Parameters
str
or List[str]
, optional) —
prompt to be encoded str
or List[str]
, optional) —
The prompt or prompts to be sent to the tokenizer_2
and text_encoder_2
. If not defined, prompt
is
used in both text-encoders
device — (torch.device
):
torch device int
) —
number of images that should be generated per prompt bool
) —
whether to use classifier free guidance or not str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation to be sent to tokenizer_2
and
text_encoder_2
. If not defined, negative_prompt
is used in both text-encoders torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. torch.Tensor
, optional) —
Pre-generated pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting.
If not provided, pooled text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, pooled negative_prompt_embeds will be generated from negative_prompt
input argument. float
, optional) —
A lora scale that will be applied to all LoRA layers of the text encoder if LoRA layers are loaded. int
, optional) —
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
the output of the pre-final layer will be used for computing the prompt embeddings. Encodes the prompt into text encoder hidden states.
( w: Tensor embedding_dim: int = 512 dtype: dtype = torch.float32 ) → torch.Tensor
Parameters
torch.Tensor
) —
Generate embedding vectors with a specified guidance scale to subsequently enrich timestep embeddings. int
, optional, defaults to 512) —
Dimension of the embeddings to generate. torch.dtype
, optional, defaults to torch.float32
) —
Data type of the generated embeddings. Returns
torch.Tensor
Embedding vectors with shape (len(w), embedding_dim)
.
( vae: AutoencoderKL text_encoder: CLIPTextModel text_encoder_2: CLIPTextModelWithProjection tokenizer: CLIPTokenizer tokenizer_2: CLIPTokenizer unet: UNet2DConditionModel scheduler: KarrasDiffusionSchedulers image_encoder: CLIPVisionModelWithProjection = None feature_extractor: CLIPImageProcessor = None requires_aesthetics_score: bool = False force_zeros_for_empty_prompt: bool = True add_watermarker: Optional = None pag_applied_layers: Union = 'mid' )
Parameters
CLIPTextModel
) —
Frozen text-encoder. Stable Diffusion XL uses the text portion of
CLIP, specifically
the clip-vit-large-patch14 variant. CLIPTextModelWithProjection
) —
Second frozen text-encoder. Stable Diffusion XL uses the text and pool portion of
CLIP,
specifically the
laion/CLIP-ViT-bigG-14-laion2B-39B-b160k
variant. CLIPTokenizer
) —
Tokenizer of class
CLIPTokenizer. CLIPTokenizer
) —
Second Tokenizer of class
CLIPTokenizer. unet
to denoise the encoded image latents. Can be one of
DDIMScheduler, LMSDiscreteScheduler, or PNDMScheduler. bool
, optional, defaults to "False"
) —
Whether the unet
requires a aesthetic_score condition to be passed during inference. Also see the config
of stabilityai/stable-diffusion-xl-refiner-1-0
. bool
, optional, defaults to "True"
) —
Whether the negative prompt embeddings shall be forced to always be set to 0. Also see the config of
stabilityai/stable-diffusion-xl-base-1-0
. bool
, optional) —
Whether to use the invisible_watermark library to
watermark output images. If not defined, it will default to True if the package is installed, otherwise no
watermarker will be used. Pipeline for text-to-image generation using Stable Diffusion XL.
This model inherits from DiffusionPipeline. Check the superclass documentation for the generic methods the library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)
The pipeline also inherits the following loading methods:
.ckpt
files( prompt: Union = None prompt_2: Union = None image: Union = None mask_image: Union = None masked_image_latents: Tensor = None height: Optional = None width: Optional = None padding_mask_crop: Optional = None strength: float = 0.9999 num_inference_steps: int = 50 timesteps: List = None sigmas: List = None denoising_start: Optional = None denoising_end: Optional = None guidance_scale: float = 7.5 negative_prompt: Union = None negative_prompt_2: Union = None num_images_per_prompt: Optional = 1 eta: float = 0.0 generator: Union = None latents: Optional = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None pooled_prompt_embeds: Optional = None negative_pooled_prompt_embeds: Optional = None ip_adapter_image: Union = None ip_adapter_image_embeds: Optional = None output_type: Optional = 'pil' return_dict: bool = True cross_attention_kwargs: Optional = None guidance_rescale: float = 0.0 original_size: Tuple = None crops_coords_top_left: Tuple = (0, 0) target_size: Tuple = None negative_original_size: Optional = None negative_crops_coords_top_left: Tuple = (0, 0) negative_target_size: Optional = None aesthetic_score: float = 6.0 negative_aesthetic_score: float = 2.5 clip_skip: Optional = None callback_on_step_end: Union = None callback_on_step_end_tensor_inputs: List = ['latents'] pag_scale: float = 3.0 pag_adaptive_scale: float = 0.0 ) → ~pipelines.stable_diffusion.StableDiffusionXLPipelineOutput
or tuple
Parameters
str
or List[str]
, optional) —
The prompt or prompts to guide the image generation. If not defined, one has to pass prompt_embeds
.
instead. str
or List[str]
, optional) —
The prompt or prompts to be sent to the tokenizer_2
and text_encoder_2
. If not defined, prompt
is
used in both text-encoders PIL.Image.Image
) —
Image
, or tensor representing an image batch which will be inpainted, i.e. parts of the image will
be masked out with mask_image
and repainted according to prompt
. PIL.Image.Image
) —
Image
, or tensor representing an image batch, to mask image
. White pixels in the mask will be
repainted, while black pixels will be preserved. If mask_image
is a PIL image, it will be converted
to a single channel (luminance) before use. If it’s a tensor, it should contain one color channel (L)
instead of 3, so the expected shape would be (B, H, W, 1)
. int
, optional, defaults to self.unet.config.sample_size * self.vae_scale_factor) —
The height in pixels of the generated image. This is set to 1024 by default for the best results.
Anything below 512 pixels won’t work well for
stabilityai/stable-diffusion-xl-base-1.0
and checkpoints that are not specifically fine-tuned on low resolutions. int
, optional, defaults to self.unet.config.sample_size * self.vae_scale_factor) —
The width in pixels of the generated image. This is set to 1024 by default for the best results.
Anything below 512 pixels won’t work well for
stabilityai/stable-diffusion-xl-base-1.0
and checkpoints that are not specifically fine-tuned on low resolutions. int
, optional, defaults to None
) —
The size of margin in the crop to be applied to the image and masking. If None
, no crop is applied to
image and mask_image. If padding_mask_crop
is not None
, it will first find a rectangular region
with the same aspect ration of the image and contains all masked area, and then expand that area based
on padding_mask_crop
. The image and mask_image will then be cropped based on the expanded area before
resizing to the original image size for inpainting. This is useful when the masked area is small while
the image is large and contain information irrelevant for inpainting, such as background. float
, optional, defaults to 0.9999) —
Conceptually, indicates how much to transform the masked portion of the reference image
. Must be
between 0 and 1. image
will be used as a starting point, adding more noise to it the larger the
strength
. The number of denoising steps depends on the amount of noise initially added. When
strength
is 1, added noise will be maximum and the denoising process will run for the full number of
iterations specified in num_inference_steps
. A value of 1, therefore, essentially ignores the masked
portion of the reference image
. Note that in the case of denoising_start
being declared as an
integer, the value of strength
will be ignored. int
, optional, defaults to 50) —
The number of denoising steps. More denoising steps usually lead to a higher quality image at the
expense of slower inference. List[int]
, optional) —
Custom timesteps to use for the denoising process with schedulers which support a timesteps
argument
in their set_timesteps
method. If not defined, the default behavior when num_inference_steps
is
passed will be used. Must be in descending order. List[float]
, optional) —
Custom sigmas to use for the denoising process with schedulers which support a sigmas
argument in
their set_timesteps
method. If not defined, the default behavior when num_inference_steps
is passed
will be used. float
, optional) —
When specified, indicates the fraction (between 0.0 and 1.0) of the total denoising process to be
bypassed before it is initiated. Consequently, the initial part of the denoising process is skipped and
it is assumed that the passed image
is a partly denoised image. Note that when this is specified,
strength will be ignored. The denoising_start
parameter is particularly beneficial when this pipeline
is integrated into a “Mixture of Denoisers” multi-pipeline setup, as detailed in Refining the Image
Output. float
, optional) —
When specified, determines the fraction (between 0.0 and 1.0) of the total denoising process to be
completed before it is intentionally prematurely terminated. As a result, the returned sample will
still retain a substantial amount of noise (ca. final 20% of timesteps still needed) and should be
denoised by a successor pipeline that has denoising_start
set to 0.8 so that it only denoises the
final 20% of the scheduler. The denoising_end parameter should ideally be utilized when this pipeline
forms a part of a “Mixture of Denoisers” multi-pipeline setup, as elaborated in Refining the Image
Output. float
, optional, defaults to 7.5) —
Guidance scale as defined in Classifier-Free Diffusion Guidance.
guidance_scale
is defined as w
of equation 2. of Imagen
Paper. Guidance scale is enabled by setting guidance_scale > 1
. Higher guidance scale encourages to generate images that are closely linked to the text prompt
,
usually at the expense of lower image quality. str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation to be sent to tokenizer_2
and
text_encoder_2
. If not defined, negative_prompt
is used in both text-encoders torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. torch.Tensor
, optional) —
Pre-generated pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting.
If not provided, pooled text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, pooled negative_prompt_embeds will be generated from negative_prompt
input argument.
ip_adapter_image — (PipelineImageInput
, optional): Optional image input to work with IP Adapters. List[torch.Tensor]
, optional) —
Pre-generated image embeddings for IP-Adapter. It should be a list of length same as number of
IP-adapters. Each element should be a tensor of shape (batch_size, num_images, emb_dim)
. It should
contain the negative image embedding if do_classifier_free_guidance
is set to True
. If not
provided, embeddings are computed from the ip_adapter_image
input argument. int
, optional, defaults to 1) —
The number of images to generate per prompt. float
, optional, defaults to 0.0) —
Corresponds to parameter eta (η) in the DDIM paper: https://arxiv.org/abs/2010.02502. Only applies to
schedulers.DDIMScheduler, will be ignored for others. torch.Generator
, optional) —
One or a list of torch generator(s)
to make generation deterministic. torch.Tensor
, optional) —
Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
tensor will ge generated by sampling using the supplied random generator
. str
, optional, defaults to "pil"
) —
The output format of the generate image. Choose between
PIL: PIL.Image.Image
or np.array
. bool
, optional, defaults to True
) —
Whether or not to return a StableDiffusionPipelineOutput instead of a
plain tuple. dict
, optional) —
A kwargs dictionary that if specified is passed along to the AttentionProcessor
as defined under
self.processor
in
diffusers.models.attention_processor. Tuple[int]
, optional, defaults to (1024, 1024)) —
If original_size
is not the same as target_size
the image will appear to be down- or upsampled.
original_size
defaults to (height, width)
if not specified. Part of SDXL’s micro-conditioning as
explained in section 2.2 of
https://huggingface.co/papers/2307.01952. Tuple[int]
, optional, defaults to (0, 0)) —
crops_coords_top_left
can be used to generate an image that appears to be “cropped” from the position
crops_coords_top_left
downwards. Favorable, well-centered images are usually achieved by setting
crops_coords_top_left
to (0, 0). Part of SDXL’s micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. Tuple[int]
, optional, defaults to (1024, 1024)) —
For most cases, target_size
should be set to the desired height and width of the generated image. If
not specified it will default to (height, width)
. Part of SDXL’s micro-conditioning as explained in
section 2.2 of https://huggingface.co/papers/2307.01952. Tuple[int]
, optional, defaults to (1024, 1024)) —
To negatively condition the generation process based on a specific image resolution. Part of SDXL’s
micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. For more
information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208. Tuple[int]
, optional, defaults to (0, 0)) —
To negatively condition the generation process based on a specific crop coordinates. Part of SDXL’s
micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. For more
information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208. Tuple[int]
, optional, defaults to (1024, 1024)) —
To negatively condition the generation process based on a target image resolution. It should be as same
as the target_size
for most cases. Part of SDXL’s micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. For more
information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208. float
, optional, defaults to 6.0) —
Used to simulate an aesthetic score of the generated image by influencing the positive text condition.
Part of SDXL’s micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. float
, optional, defaults to 2.5) —
Part of SDXL’s micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. Can be used to
simulate an aesthetic score of the generated image by influencing the negative text condition. int
, optional) —
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
the output of the pre-final layer will be used for computing the prompt embeddings. Callable
, PipelineCallback
, MultiPipelineCallbacks
, optional) —
A function or a subclass of PipelineCallback
or MultiPipelineCallbacks
that is called at the end of
each denoising step during the inference. with the following arguments: callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
. callback_kwargs
will include a
list of all tensors as specified by callback_on_step_end_tensor_inputs
. List
, optional) —
The list of tensor inputs for the callback_on_step_end
function. The tensors specified in the list
will be passed as callback_kwargs
argument. You will only be able to include variables listed in the
._callback_tensor_inputs
attribute of your pipeline class. float
, optional, defaults to 3.0) —
The scale factor for the perturbed attention guidance. If it is set to 0.0, the perturbed attention
guidance will not be used. float
, optional, defaults to 0.0) —
The adaptive scale factor for the perturbed attention guidance. If it is set to 0.0, pag_scale
is
used. Returns
~pipelines.stable_diffusion.StableDiffusionXLPipelineOutput
or tuple
~pipelines.stable_diffusion.StableDiffusionXLPipelineOutput
if return_dict
is True, otherwise a
tuple.
tuple. When returning a tuple, the first element is a list with the generated images.
Function invoked when calling the pipeline for generation.
Examples:
>>> import torch
>>> from diffusers import AutoPipelineForInpainting
>>> from diffusers.utils import load_image
>>> pipe = AutoPipelineForInpainting.from_pretrained(
... "stabilityai/stable-diffusion-xl-base-1.0",
... torch_dtype=torch.float16,
... variant="fp16",
... enable_pag=True,
... )
>>> pipe.to("cuda")
>>> img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
>>> mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
>>> init_image = load_image(img_url).convert("RGB")
>>> mask_image = load_image(mask_url).convert("RGB")
>>> prompt = "A majestic tiger sitting on a bench"
>>> image = pipe(
... prompt=prompt,
... image=init_image,
... mask_image=mask_image,
... num_inference_steps=50,
... strength=0.80,
... pag_scale=0.3,
... ).images[0]
( prompt: str prompt_2: Optional = None device: Optional = None num_images_per_prompt: int = 1 do_classifier_free_guidance: bool = True negative_prompt: Optional = None negative_prompt_2: Optional = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None pooled_prompt_embeds: Optional = None negative_pooled_prompt_embeds: Optional = None lora_scale: Optional = None clip_skip: Optional = None )
Parameters
str
or List[str]
, optional) —
prompt to be encoded str
or List[str]
, optional) —
The prompt or prompts to be sent to the tokenizer_2
and text_encoder_2
. If not defined, prompt
is
used in both text-encoders
device — (torch.device
):
torch device int
) —
number of images that should be generated per prompt bool
) —
whether to use classifier free guidance or not str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation to be sent to tokenizer_2
and
text_encoder_2
. If not defined, negative_prompt
is used in both text-encoders torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. torch.Tensor
, optional) —
Pre-generated pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting.
If not provided, pooled text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, pooled negative_prompt_embeds will be generated from negative_prompt
input argument. float
, optional) —
A lora scale that will be applied to all LoRA layers of the text encoder if LoRA layers are loaded. int
, optional) —
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
the output of the pre-final layer will be used for computing the prompt embeddings. Encodes the prompt into text encoder hidden states.
( w: Tensor embedding_dim: int = 512 dtype: dtype = torch.float32 ) → torch.Tensor
Parameters
torch.Tensor
) —
Generate embedding vectors with a specified guidance scale to subsequently enrich timestep embeddings. int
, optional, defaults to 512) —
Dimension of the embeddings to generate. torch.dtype
, optional, defaults to torch.float32
) —
Data type of the generated embeddings. Returns
torch.Tensor
Embedding vectors with shape (len(w), embedding_dim)
.
( vae: AutoencoderKL text_encoder: CLIPTextModel text_encoder_2: CLIPTextModelWithProjection tokenizer: CLIPTokenizer tokenizer_2: CLIPTokenizer unet: UNet2DConditionModel controlnet: Union scheduler: KarrasDiffusionSchedulers force_zeros_for_empty_prompt: bool = True add_watermarker: Optional = None feature_extractor: CLIPImageProcessor = None image_encoder: CLIPVisionModelWithProjection = None pag_applied_layers: Union = 'mid' )
Parameters
CLIPTokenizer
to tokenize text. CLIPTokenizer
to tokenize text. UNet2DConditionModel
to denoise the encoded image latents. List[ControlNetModel]
) —
Provides additional conditioning to the unet
during the denoising process. If you set multiple
ControlNets as a list, the outputs from each ControlNet are added together to create one combined
additional conditioning. unet
to denoise the encoded image latents. Can be one of
DDIMScheduler, LMSDiscreteScheduler, or PNDMScheduler. bool
, optional, defaults to "True"
) —
Whether the negative prompt embeddings should always be set to 0. Also see the config of
stabilityai/stable-diffusion-xl-base-1-0
. bool
, optional) —
Whether to use the invisible_watermark library to
watermark output images. If not defined, it defaults to True
if the package is installed; otherwise no
watermarker is used. Pipeline for text-to-image generation using Stable Diffusion XL with ControlNet guidance.
This model inherits from DiffusionPipeline. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a particular device, etc.).
The pipeline also inherits the following loading methods:
.ckpt
files( prompt: Union = None prompt_2: Union = None image: Union = None height: Optional = None width: Optional = None num_inference_steps: int = 50 timesteps: List = None sigmas: List = None denoising_end: Optional = None guidance_scale: float = 5.0 negative_prompt: Union = None negative_prompt_2: Union = None num_images_per_prompt: Optional = 1 eta: float = 0.0 generator: Union = None latents: Optional = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None pooled_prompt_embeds: Optional = None negative_pooled_prompt_embeds: Optional = None ip_adapter_image: Union = None ip_adapter_image_embeds: Optional = None output_type: Optional = 'pil' return_dict: bool = True cross_attention_kwargs: Optional = None controlnet_conditioning_scale: Union = 1.0 control_guidance_start: Union = 0.0 control_guidance_end: Union = 1.0 original_size: Tuple = None crops_coords_top_left: Tuple = (0, 0) target_size: Tuple = None negative_original_size: Optional = None negative_crops_coords_top_left: Tuple = (0, 0) negative_target_size: Optional = None clip_skip: Optional = None callback_on_step_end: Union = None callback_on_step_end_tensor_inputs: List = ['latents'] pag_scale: float = 3.0 pag_adaptive_scale: float = 0.0 ) → StableDiffusionPipelineOutput or tuple
Parameters
str
or List[str]
, optional) —
The prompt or prompts to guide image generation. If not defined, you need to pass prompt_embeds
. str
or List[str]
, optional) —
The prompt or prompts to be sent to tokenizer_2
and text_encoder_2
. If not defined, prompt
is
used in both text-encoders. 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]]
or List[List[PIL.Image.Image]]
):
The ControlNet input condition to provide guidance to the unet
for generation. If the type is
specified as torch.Tensor
, it is passed to ControlNet as is. PIL.Image.Image
can also be accepted
as an image. The dimensions of the output image defaults to image
’s dimensions. If height and/or
width are passed, image
is resized accordingly. If multiple ControlNets are specified in init
,
images must be passed as a list such that each element of the list can be correctly batched for input
to a single ControlNet. int
, optional, defaults to self.unet.config.sample_size * self.vae_scale_factor
) —
The height in pixels of the generated image. Anything below 512 pixels won’t work well for
stabilityai/stable-diffusion-xl-base-1.0
and checkpoints that are not specifically fine-tuned on low resolutions. int
, optional, defaults to self.unet.config.sample_size * self.vae_scale_factor
) —
The width in pixels of the generated image. Anything below 512 pixels won’t work well for
stabilityai/stable-diffusion-xl-base-1.0
and checkpoints that are not specifically fine-tuned on low resolutions. int
, optional, defaults to 50) —
The number of denoising steps. More denoising steps usually lead to a higher quality image at the
expense of slower inference. List[int]
, optional) —
Custom timesteps to use for the denoising process with schedulers which support a timesteps
argument
in their set_timesteps
method. If not defined, the default behavior when num_inference_steps
is
passed will be used. Must be in descending order. List[float]
, optional) —
Custom sigmas to use for the denoising process with schedulers which support a sigmas
argument in
their set_timesteps
method. If not defined, the default behavior when num_inference_steps
is passed
will be used. float
, optional) —
When specified, determines the fraction (between 0.0 and 1.0) of the total denoising process to be
completed before it is intentionally prematurely terminated. As a result, the returned sample will
still retain a substantial amount of noise as determined by the discrete timesteps selected by the
scheduler. The denoising_end parameter should ideally be utilized when this pipeline forms a part of a
“Mixture of Denoisers” multi-pipeline setup, as elaborated in Refining the Image
Output float
, optional, defaults to 5.0) —
A higher guidance scale value encourages the model to generate images closely linked to the text
prompt
at the expense of lower image quality. Guidance scale is enabled when guidance_scale > 1
. str
or List[str]
, optional) —
The prompt or prompts to guide what to not include in image generation. If not defined, you need to
pass negative_prompt_embeds
instead. Ignored when not using guidance (guidance_scale < 1
). str
or List[str]
, optional) —
The prompt or prompts to guide what to not include in image generation. This is sent to tokenizer_2
and text_encoder_2
. If not defined, negative_prompt
is used in both text-encoders. int
, optional, defaults to 1) —
The number of images to generate per prompt. float
, optional, defaults to 0.0) —
Corresponds to parameter eta (η) from the DDIM paper. Only applies
to the DDIMScheduler, and is ignored in other schedulers. torch.Generator
or List[torch.Generator]
, optional) —
A torch.Generator
to make
generation deterministic. torch.Tensor
, optional) —
Pre-generated noisy latents sampled from a Gaussian distribution, to be used as inputs for image
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
tensor is generated by sampling using the supplied random generator
. torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not
provided, text embeddings are generated from the prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs (prompt weighting). If
not provided, negative_prompt_embeds
are generated from the negative_prompt
input argument. torch.Tensor
, optional) —
Pre-generated pooled text embeddings. Can be used to easily tweak text inputs (prompt weighting). If
not provided, pooled text embeddings are generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative pooled text embeddings. Can be used to easily tweak text inputs (prompt
weighting). If not provided, pooled negative_prompt_embeds
are generated from negative_prompt
input
argument.
ip_adapter_image — (PipelineImageInput
, optional): Optional image input to work with IP Adapters. List[torch.Tensor]
, optional) —
Pre-generated image embeddings for IP-Adapter. It should be a list of length same as number of
IP-adapters. Each element should be a tensor of shape (batch_size, num_images, emb_dim)
. It should
contain the negative image embedding if do_classifier_free_guidance
is set to True
. If not
provided, embeddings are computed from the ip_adapter_image
input argument. str
, optional, defaults to "pil"
) —
The output format of the generated image. Choose between PIL.Image
or np.array
. bool
, optional, defaults to True
) —
Whether or not to return a StableDiffusionPipelineOutput instead of a
plain tuple. dict
, optional) —
A kwargs dictionary that if specified is passed along to the AttentionProcessor
as defined in
self.processor
. float
or List[float]
, optional, defaults to 1.0) —
The outputs of the ControlNet are multiplied by controlnet_conditioning_scale
before they are added
to the residual in the original unet
. If multiple ControlNets are specified in init
, you can set
the corresponding scale as a list. float
or List[float]
, optional, defaults to 0.0) —
The percentage of total steps at which the ControlNet starts applying. float
or List[float]
, optional, defaults to 1.0) —
The percentage of total steps at which the ControlNet stops applying. Tuple[int]
, optional, defaults to (1024, 1024)) —
If original_size
is not the same as target_size
the image will appear to be down- or upsampled.
original_size
defaults to (height, width)
if not specified. Part of SDXL’s micro-conditioning as
explained in section 2.2 of
https://huggingface.co/papers/2307.01952. Tuple[int]
, optional, defaults to (0, 0)) —
crops_coords_top_left
can be used to generate an image that appears to be “cropped” from the position
crops_coords_top_left
downwards. Favorable, well-centered images are usually achieved by setting
crops_coords_top_left
to (0, 0). Part of SDXL’s micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. Tuple[int]
, optional, defaults to (1024, 1024)) —
For most cases, target_size
should be set to the desired height and width of the generated image. If
not specified it will default to (height, width)
. Part of SDXL’s micro-conditioning as explained in
section 2.2 of https://huggingface.co/papers/2307.01952. Tuple[int]
, optional, defaults to (1024, 1024)) —
To negatively condition the generation process based on a specific image resolution. Part of SDXL’s
micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. For more
information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208. Tuple[int]
, optional, defaults to (0, 0)) —
To negatively condition the generation process based on a specific crop coordinates. Part of SDXL’s
micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. For more
information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208. Tuple[int]
, optional, defaults to (1024, 1024)) —
To negatively condition the generation process based on a target image resolution. It should be as same
as the target_size
for most cases. Part of SDXL’s micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. For more
information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208. int
, optional) —
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
the output of the pre-final layer will be used for computing the prompt embeddings. Callable
, PipelineCallback
, MultiPipelineCallbacks
, optional) —
A function or a subclass of PipelineCallback
or MultiPipelineCallbacks
that is called at the end of
each denoising step during the inference. with the following arguments: callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
. callback_kwargs
will include a
list of all tensors as specified by callback_on_step_end_tensor_inputs
. List
, optional) —
The list of tensor inputs for the callback_on_step_end
function. The tensors specified in the list
will be passed as callback_kwargs
argument. You will only be able to include variables listed in the
._callback_tensor_inputs
attribute of your pipeline class. float
, optional, defaults to 3.0) —
The scale factor for the perturbed attention guidance. If it is set to 0.0, the perturbed attention
guidance will not be used. float
, optional, defaults to 0.0) —
The adaptive scale factor for the perturbed attention guidance. If it is set to 0.0, pag_scale
is
used. Returns
StableDiffusionPipelineOutput or tuple
If return_dict
is True
, StableDiffusionPipelineOutput is returned,
otherwise a tuple
is returned containing the output images.
The call function to the pipeline for generation.
Examples:
>>> # !pip install opencv-python transformers accelerate
>>> from diffusers import AutoPipelineForText2Image, ControlNetModel, AutoencoderKL
>>> from diffusers.utils import load_image
>>> import numpy as np
>>> import torch
>>> import cv2
>>> from PIL import Image
>>> prompt = "aerial view, a futuristic research complex in a bright foggy jungle, hard lighting"
>>> negative_prompt = "low quality, bad quality, sketches"
>>> # download an image
>>> image = load_image(
... "https://hf.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/hf-logo.png"
... )
>>> # initialize the models and pipeline
>>> controlnet_conditioning_scale = 0.5 # recommended for good generalization
>>> controlnet = ControlNetModel.from_pretrained(
... "diffusers/controlnet-canny-sdxl-1.0", torch_dtype=torch.float16
... )
>>> vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
>>> pipe = AutoPipelineForText2Image.from_pretrained(
... "stabilityai/stable-diffusion-xl-base-1.0",
... controlnet=controlnet,
... vae=vae,
... torch_dtype=torch.float16,
... enable_pag=True,
... )
>>> pipe.enable_model_cpu_offload()
>>> # get canny image
>>> image = np.array(image)
>>> image = cv2.Canny(image, 100, 200)
>>> image = image[:, :, None]
>>> image = np.concatenate([image, image, image], axis=2)
>>> canny_image = Image.fromarray(image)
>>> # generate image
>>> image = pipe(
... prompt, controlnet_conditioning_scale=controlnet_conditioning_scale, image=canny_image, pag_scale=0.3
... ).images[0]
( prompt: str prompt_2: Optional = None device: Optional = None num_images_per_prompt: int = 1 do_classifier_free_guidance: bool = True negative_prompt: Optional = None negative_prompt_2: Optional = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None pooled_prompt_embeds: Optional = None negative_pooled_prompt_embeds: Optional = None lora_scale: Optional = None clip_skip: Optional = None )
Parameters
str
or List[str]
, optional) —
prompt to be encoded str
or List[str]
, optional) —
The prompt or prompts to be sent to the tokenizer_2
and text_encoder_2
. If not defined, prompt
is
used in both text-encoders
device — (torch.device
):
torch device int
) —
number of images that should be generated per prompt bool
) —
whether to use classifier free guidance or not str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation to be sent to tokenizer_2
and
text_encoder_2
. If not defined, negative_prompt
is used in both text-encoders torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. torch.Tensor
, optional) —
Pre-generated pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting.
If not provided, pooled text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, pooled negative_prompt_embeds will be generated from negative_prompt
input argument. float
, optional) —
A lora scale that will be applied to all LoRA layers of the text encoder if LoRA layers are loaded. int
, optional) —
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
the output of the pre-final layer will be used for computing the prompt embeddings. Encodes the prompt into text encoder hidden states.
( w: Tensor embedding_dim: int = 512 dtype: dtype = torch.float32 ) → torch.Tensor
Parameters
torch.Tensor
) —
Generate embedding vectors with a specified guidance scale to subsequently enrich timestep embeddings. int
, optional, defaults to 512) —
Dimension of the embeddings to generate. torch.dtype
, optional, defaults to torch.float32
) —
Data type of the generated embeddings. Returns
torch.Tensor
Embedding vectors with shape (len(w), embedding_dim)
.
( vae: AutoencoderKL text_encoder: CLIPTextModel text_encoder_2: CLIPTextModelWithProjection tokenizer: CLIPTokenizer tokenizer_2: CLIPTokenizer unet: UNet2DConditionModel controlnet: Union scheduler: KarrasDiffusionSchedulers requires_aesthetics_score: bool = False force_zeros_for_empty_prompt: bool = True add_watermarker: Optional = None feature_extractor: CLIPImageProcessor = None image_encoder: CLIPVisionModelWithProjection = None pag_applied_layers: Union = 'mid' )
Parameters
CLIPTextModel
) —
Frozen text-encoder. Stable Diffusion uses the text portion of
CLIP, specifically
the clip-vit-large-patch14 variant. CLIPTextModelWithProjection
) —
Second frozen text-encoder. Stable Diffusion XL uses the text and pool portion of
CLIP,
specifically the
laion/CLIP-ViT-bigG-14-laion2B-39B-b160k
variant. CLIPTokenizer
) —
Tokenizer of class
CLIPTokenizer. CLIPTokenizer
) —
Second Tokenizer of class
CLIPTokenizer. List[ControlNetModel]
) —
Provides additional conditioning to the unet during the denoising process. If you set multiple ControlNets
as a list, the outputs from each ControlNet are added together to create one combined additional
conditioning. unet
to denoise the encoded image latents. Can be one of
DDIMScheduler, LMSDiscreteScheduler, or PNDMScheduler. bool
, optional, defaults to "False"
) —
Whether the unet
requires an aesthetic_score
condition to be passed during inference. Also see the
config of stabilityai/stable-diffusion-xl-refiner-1-0
. bool
, optional, defaults to "True"
) —
Whether the negative prompt embeddings shall be forced to always be set to 0. Also see the config of
stabilityai/stable-diffusion-xl-base-1-0
. bool
, optional) —
Whether to use the invisible_watermark library to
watermark output images. If not defined, it will default to True if the package is installed, otherwise no
watermarker will be used. CLIPImageProcessor
to extract features from generated images; used as inputs to the safety_checker
. Pipeline for image-to-image generation using Stable Diffusion XL with ControlNet guidance.
This model inherits from DiffusionPipeline. Check the superclass documentation for the generic methods the library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)
The pipeline also inherits the following loading methods:
( prompt: Union = None prompt_2: Union = None image: Union = None control_image: Union = None height: Optional = None width: Optional = None strength: float = 0.8 num_inference_steps: int = 50 guidance_scale: float = 5.0 negative_prompt: Union = None negative_prompt_2: Union = None num_images_per_prompt: Optional = 1 eta: float = 0.0 generator: Union = None latents: Optional = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None pooled_prompt_embeds: Optional = None negative_pooled_prompt_embeds: Optional = None ip_adapter_image: Union = None ip_adapter_image_embeds: Optional = None output_type: Optional = 'pil' return_dict: bool = True cross_attention_kwargs: Optional = None controlnet_conditioning_scale: Union = 0.8 guess_mode: bool = False control_guidance_start: Union = 0.0 control_guidance_end: Union = 1.0 original_size: Tuple = None crops_coords_top_left: Tuple = (0, 0) target_size: Tuple = None negative_original_size: Optional = None negative_crops_coords_top_left: Tuple = (0, 0) negative_target_size: Optional = None aesthetic_score: float = 6.0 negative_aesthetic_score: float = 2.5 clip_skip: Optional = None callback_on_step_end: Union = None callback_on_step_end_tensor_inputs: List = ['latents'] pag_scale: float = 3.0 pag_adaptive_scale: float = 0.0 ) → ~pipelines.stable_diffusion.StableDiffusionXLPipelineOutput
or tuple
Parameters
str
or List[str]
, optional) —
The prompt or prompts to guide the image generation. If not defined, one has to pass prompt_embeds
.
instead. str
or List[str]
, optional) —
The prompt or prompts to be sent to the tokenizer_2
and text_encoder_2
. If not defined, prompt
is
used in both text-encoders 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]]
or List[List[PIL.Image.Image]]
):
The initial image will be used as the starting point for the image generation process. Can also accept
image latents as image
, if passing latents directly, it will not be encoded again. 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]]
or List[List[PIL.Image.Image]]
):
The ControlNet input condition. ControlNet uses this input condition to generate guidance to Unet. If
the type is specified as torch.Tensor
, it is passed to ControlNet as is. PIL.Image.Image
can also
be accepted as an image. The dimensions of the output image defaults to image
’s dimensions. If height
and/or width are passed, image
is resized according to them. If multiple ControlNets are specified in
init, images must be passed as a list such that each element of the list can be correctly batched for
input to a single controlnet. int
, optional, defaults to the size of control_image) —
The height in pixels of the generated image. Anything below 512 pixels won’t work well for
stabilityai/stable-diffusion-xl-base-1.0
and checkpoints that are not specifically fine-tuned on low resolutions. int
, optional, defaults to the size of control_image) —
The width in pixels of the generated image. Anything below 512 pixels won’t work well for
stabilityai/stable-diffusion-xl-base-1.0
and checkpoints that are not specifically fine-tuned on low resolutions. float
, optional, defaults to 0.8) —
Indicates extent to transform the reference image
. Must be between 0 and 1. image
is used as a
starting point and more noise is added the higher the strength
. The number of denoising steps depends
on the amount of noise initially added. When strength
is 1, added noise is maximum and the denoising
process runs for the full number of iterations specified in num_inference_steps
. A value of 1
essentially ignores image
. int
, optional, defaults to 50) —
The number of denoising steps. More denoising steps usually lead to a higher quality image at the
expense of slower inference. float
, optional, defaults to 7.5) —
Guidance scale as defined in Classifier-Free Diffusion Guidance.
guidance_scale
is defined as w
of equation 2. of Imagen
Paper. Guidance scale is enabled by setting guidance_scale > 1
. Higher guidance scale encourages to generate images that are closely linked to the text prompt
,
usually at the expense of lower image quality. str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation to be sent to tokenizer_2
and
text_encoder_2
. If not defined, negative_prompt
is used in both text-encoders int
, optional, defaults to 1) —
The number of images to generate per prompt. float
, optional, defaults to 0.0) —
Corresponds to parameter eta (η) in the DDIM paper: https://arxiv.org/abs/2010.02502. Only applies to
schedulers.DDIMScheduler, will be ignored for others. torch.Generator
or List[torch.Generator]
, optional) —
One or a list of torch generator(s)
to make generation deterministic. torch.Tensor
, optional) —
Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
tensor will ge generated by sampling using the supplied random generator
. torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. torch.Tensor
, optional) —
Pre-generated pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting.
If not provided, pooled text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, pooled negative_prompt_embeds will be generated from negative_prompt
input argument.
ip_adapter_image — (PipelineImageInput
, optional): Optional image input to work with IP Adapters. List[torch.Tensor]
, optional) —
Pre-generated image embeddings for IP-Adapter. It should be a list of length same as number of
IP-adapters. Each element should be a tensor of shape (batch_size, num_images, emb_dim)
. It should
contain the negative image embedding if do_classifier_free_guidance
is set to True
. If not
provided, embeddings are computed from the ip_adapter_image
input argument. str
, optional, defaults to "pil"
) —
The output format of the generate image. Choose between
PIL: PIL.Image.Image
or np.array
. bool
, optional, defaults to True
) —
Whether or not to return a StableDiffusionPipelineOutput instead of a
plain tuple. dict
, optional) —
A kwargs dictionary that if specified is passed along to the AttentionProcessor
as defined under
self.processor
in
diffusers.models.attention_processor. float
or List[float]
, optional, defaults to 1.0) —
The outputs of the controlnet are multiplied by controlnet_conditioning_scale
before they are added
to the residual in the original unet. If multiple ControlNets are specified in init, you can set the
corresponding scale as a list. bool
, optional, defaults to False
) —
In this mode, the ControlNet encoder will try best to recognize the content of the input image even if
you remove all prompts. The guidance_scale
between 3.0 and 5.0 is recommended. float
or List[float]
, optional, defaults to 0.0) —
The percentage of total steps at which the controlnet starts applying. float
or List[float]
, optional, defaults to 1.0) —
The percentage of total steps at which the controlnet stops applying. Tuple[int]
, optional, defaults to (1024, 1024)) —
If original_size
is not the same as target_size
the image will appear to be down- or upsampled.
original_size
defaults to (height, width)
if not specified. Part of SDXL’s micro-conditioning as
explained in section 2.2 of
https://huggingface.co/papers/2307.01952. Tuple[int]
, optional, defaults to (0, 0)) —
crops_coords_top_left
can be used to generate an image that appears to be “cropped” from the position
crops_coords_top_left
downwards. Favorable, well-centered images are usually achieved by setting
crops_coords_top_left
to (0, 0). Part of SDXL’s micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. Tuple[int]
, optional, defaults to (1024, 1024)) —
For most cases, target_size
should be set to the desired height and width of the generated image. If
not specified it will default to (height, width)
. Part of SDXL’s micro-conditioning as explained in
section 2.2 of https://huggingface.co/papers/2307.01952. Tuple[int]
, optional, defaults to (1024, 1024)) —
To negatively condition the generation process based on a specific image resolution. Part of SDXL’s
micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. For more
information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208. Tuple[int]
, optional, defaults to (0, 0)) —
To negatively condition the generation process based on a specific crop coordinates. Part of SDXL’s
micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. For more
information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208. Tuple[int]
, optional, defaults to (1024, 1024)) —
To negatively condition the generation process based on a target image resolution. It should be as same
as the target_size
for most cases. Part of SDXL’s micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. For more
information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208. float
, optional, defaults to 6.0) —
Used to simulate an aesthetic score of the generated image by influencing the positive text condition.
Part of SDXL’s micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. float
, optional, defaults to 2.5) —
Part of SDXL’s micro-conditioning as explained in section 2.2 of
https://huggingface.co/papers/2307.01952. Can be used to
simulate an aesthetic score of the generated image by influencing the negative text condition. int
, optional) —
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
the output of the pre-final layer will be used for computing the prompt embeddings. Callable
, PipelineCallback
, MultiPipelineCallbacks
, optional) —
A function or a subclass of PipelineCallback
or MultiPipelineCallbacks
that is called at the end of
each denoising step during the inference. with the following arguments: callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
. callback_kwargs
will include a
list of all tensors as specified by callback_on_step_end_tensor_inputs
. List
, optional) —
The list of tensor inputs for the callback_on_step_end
function. The tensors specified in the list
will be passed as callback_kwargs
argument. You will only be able to include variables listed in the
._callback_tensor_inputs
attribute of your pipeline class. float
, optional, defaults to 3.0) —
The scale factor for the perturbed attention guidance. If it is set to 0.0, the perturbed attention
guidance will not be used. float
, optional, defaults to 0.0) —
The adaptive scale factor for the perturbed attention guidance. If it is set to 0.0, pag_scale
is
used. Returns
~pipelines.stable_diffusion.StableDiffusionXLPipelineOutput
or tuple
~pipelines.stable_diffusion.StableDiffusionXLPipelineOutput
if return_dict
is True, otherwise a
tuple
containing the output images.
Function invoked when calling the pipeline for generation.
Examples:
>>> # pip install accelerate transformers safetensors diffusers
>>> import torch
>>> import numpy as np
>>> from PIL import Image
>>> from transformers import DPTFeatureExtractor, DPTForDepthEstimation
>>> from diffusers import ControlNetModel, StableDiffusionXLControlNetPAGImg2ImgPipeline, AutoencoderKL
>>> from diffusers.utils import load_image
>>> depth_estimator = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas").to("cuda")
>>> feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-hybrid-midas")
>>> controlnet = ControlNetModel.from_pretrained(
... "diffusers/controlnet-depth-sdxl-1.0-small",
... variant="fp16",
... use_safetensors="True",
... torch_dtype=torch.float16,
... )
>>> vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
>>> pipe = StableDiffusionXLControlNetPAGImg2ImgPipeline.from_pretrained(
... "stabilityai/stable-diffusion-xl-base-1.0",
... controlnet=controlnet,
... vae=vae,
... variant="fp16",
... use_safetensors=True,
... torch_dtype=torch.float16,
... enable_pag=True,
... )
>>> pipe.enable_model_cpu_offload()
>>> def get_depth_map(image):
... image = feature_extractor(images=image, return_tensors="pt").pixel_values.to("cuda")
... with torch.no_grad(), torch.autocast("cuda"):
... depth_map = depth_estimator(image).predicted_depth
... depth_map = torch.nn.fuctional.interpolate(
... depth_map.unsqueeze(1),
... size=(1024, 1024),
... mode="bicubic",
... align_corners=False,
... )
... depth_min = torch.amin(depth_map, dim=[1, 2, 3], keepdim=True)
... depth_max = torch.amax(depth_map, dim=[1, 2, 3], keepdim=True)
... depth_map = (depth_map - depth_min) / (depth_max - depth_min)
... image = torch.cat([depth_map] * 3, dim=1)
... image = image.permute(0, 2, 3, 1).cpu().numpy()[0]
... image = Image.fromarray((image * 255.0).clip(0, 255).astype(np.uint8))
... return image
>>> prompt = "A robot, 4k photo"
>>> image = load_image(
... "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
... "/kandinsky/cat.png"
... ).resize((1024, 1024))
>>> controlnet_conditioning_scale = 0.5 # recommended for good generalization
>>> depth_image = get_depth_map(image)
>>> images = pipe(
... prompt,
... image=image,
... control_image=depth_image,
... strength=0.99,
... num_inference_steps=50,
... controlnet_conditioning_scale=controlnet_conditioning_scale,
... ).images
>>> images[0].save(f"robot_cat.png")
( prompt: str prompt_2: Optional = None device: Optional = None num_images_per_prompt: int = 1 do_classifier_free_guidance: bool = True negative_prompt: Optional = None negative_prompt_2: Optional = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None pooled_prompt_embeds: Optional = None negative_pooled_prompt_embeds: Optional = None lora_scale: Optional = None clip_skip: Optional = None )
Parameters
str
or List[str]
, optional) —
prompt to be encoded str
or List[str]
, optional) —
The prompt or prompts to be sent to the tokenizer_2
and text_encoder_2
. If not defined, prompt
is
used in both text-encoders
device — (torch.device
):
torch device int
) —
number of images that should be generated per prompt bool
) —
whether to use classifier free guidance or not str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation to be sent to tokenizer_2
and
text_encoder_2
. If not defined, negative_prompt
is used in both text-encoders torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. torch.Tensor
, optional) —
Pre-generated pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting.
If not provided, pooled text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, pooled negative_prompt_embeds will be generated from negative_prompt
input argument. float
, optional) —
A lora scale that will be applied to all LoRA layers of the text encoder if LoRA layers are loaded. int
, optional) —
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
the output of the pre-final layer will be used for computing the prompt embeddings. Encodes the prompt into text encoder hidden states.
( transformer: SD3Transformer2DModel scheduler: FlowMatchEulerDiscreteScheduler vae: AutoencoderKL text_encoder: CLIPTextModelWithProjection tokenizer: CLIPTokenizer text_encoder_2: CLIPTextModelWithProjection tokenizer_2: CLIPTokenizer text_encoder_3: T5EncoderModel tokenizer_3: T5TokenizerFast pag_applied_layers: Union = 'blocks.1' )
Parameters
transformer
to denoise the encoded image latents. CLIPTextModelWithProjection
) —
CLIP,
specifically the clip-vit-large-patch14 variant,
with an additional added projection layer that is initialized with a diagonal matrix with the hidden_size
as its dimension. CLIPTextModelWithProjection
) —
CLIP,
specifically the
laion/CLIP-ViT-bigG-14-laion2B-39B-b160k
variant. T5EncoderModel
) —
Frozen text-encoder. Stable Diffusion 3 uses
T5, specifically the
t5-v1_1-xxl variant. CLIPTokenizer
) —
Tokenizer of class
CLIPTokenizer. CLIPTokenizer
) —
Second Tokenizer of class
CLIPTokenizer. T5TokenizerFast
) —
Tokenizer of class
T5Tokenizer. PAG pipeline for text-to-image generation using Stable Diffusion 3.
( prompt: Union = None prompt_2: Union = None prompt_3: Union = None height: Optional = None width: Optional = None num_inference_steps: int = 28 timesteps: List = None guidance_scale: float = 7.0 negative_prompt: Union = None negative_prompt_2: Union = None negative_prompt_3: Union = None num_images_per_prompt: Optional = 1 generator: Union = None latents: Optional = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None pooled_prompt_embeds: Optional = None negative_pooled_prompt_embeds: Optional = None output_type: Optional = 'pil' return_dict: bool = True joint_attention_kwargs: Optional = None clip_skip: Optional = None callback_on_step_end: Optional = None callback_on_step_end_tensor_inputs: List = ['latents'] max_sequence_length: int = 256 pag_scale: float = 3.0 pag_adaptive_scale: float = 0.0 ) → ~pipelines.stable_diffusion_3.StableDiffusion3PipelineOutput
or tuple
Parameters
str
or List[str]
, optional) —
The prompt or prompts to guide the image generation. If not defined, one has to pass prompt_embeds
.
instead. str
or List[str]
, optional) —
The prompt or prompts to be sent to tokenizer_2
and text_encoder_2
. If not defined, prompt
is
will be used instead str
or List[str]
, optional) —
The prompt or prompts to be sent to tokenizer_3
and text_encoder_3
. If not defined, prompt
is
will be used instead int
, optional, defaults to self.unet.config.sample_size * self.vae_scale_factor) —
The height in pixels of the generated image. This is set to 1024 by default for the best results. int
, optional, defaults to self.unet.config.sample_size * self.vae_scale_factor) —
The width in pixels of the generated image. This is set to 1024 by default for the best results. int
, optional, defaults to 50) —
The number of denoising steps. More denoising steps usually lead to a higher quality image at the
expense of slower inference. List[int]
, optional) —
Custom timesteps to use for the denoising process with schedulers which support a timesteps
argument
in their set_timesteps
method. If not defined, the default behavior when num_inference_steps
is
passed will be used. Must be in descending order. float
, optional, defaults to 7.0) —
Guidance scale as defined in Classifier-Free Diffusion Guidance.
guidance_scale
is defined as w
of equation 2. of Imagen
Paper. Guidance scale is enabled by setting guidance_scale > 1
. Higher guidance scale encourages to generate images that are closely linked to the text prompt
,
usually at the expense of lower image quality. str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation to be sent to tokenizer_2
and
text_encoder_2
. If not defined, negative_prompt
is used instead str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation to be sent to tokenizer_3
and
text_encoder_3
. If not defined, negative_prompt
is used instead int
, optional, defaults to 1) —
The number of images to generate per prompt. torch.Generator
or List[torch.Generator]
, optional) —
One or a list of torch generator(s)
to make generation deterministic. torch.FloatTensor
, optional) —
Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
tensor will ge generated by sampling using the supplied random generator
. torch.FloatTensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.FloatTensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. torch.FloatTensor
, optional) —
Pre-generated pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting.
If not provided, pooled text embeddings will be generated from prompt
input argument. torch.FloatTensor
, optional) —
Pre-generated negative pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, pooled negative_prompt_embeds will be generated from negative_prompt
input argument. str
, optional, defaults to "pil"
) —
The output format of the generate image. Choose between
PIL: PIL.Image.Image
or np.array
. bool
, optional, defaults to True
) —
Whether or not to return a ~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput
instead
of a plain tuple. dict
, optional) —
A kwargs dictionary that if specified is passed along to the AttentionProcessor
as defined under
self.processor
in
diffusers.models.attention_processor. Callable
, optional) —
A function that calls at the end of each denoising steps during the inference. The function is called
with the following arguments: callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
. callback_kwargs
will include a list of all tensors as specified by
callback_on_step_end_tensor_inputs
. List
, optional) —
The list of tensor inputs for the callback_on_step_end
function. The tensors specified in the list
will be passed as callback_kwargs
argument. You will only be able to include variables listed in the
._callback_tensor_inputs
attribute of your pipeline class. int
defaults to 256) — Maximum sequence length to use with the prompt
. float
, optional, defaults to 3.0) —
The scale factor for the perturbed attention guidance. If it is set to 0.0, the perturbed attention
guidance will not be used. float
, optional, defaults to 0.0) —
The adaptive scale factor for the perturbed attention guidance. If it is set to 0.0, pag_scale
is
used. Returns
~pipelines.stable_diffusion_3.StableDiffusion3PipelineOutput
or tuple
~pipelines.stable_diffusion_3.StableDiffusion3PipelineOutput
if return_dict
is True, otherwise a
tuple
. When returning a tuple, the first element is a list with the generated images.
Function invoked when calling the pipeline for generation.
Examples:
>>> import torch
>>> from diffusers import AutoPipelineForText2Image
>>> pipe = AutoPipelineForText2Image.from_pretrained(
... "stabilityai/stable-diffusion-3-medium-diffusers",
... torch_dtype=torch.float16,
... enable_pag=True,
... pag_applied_layers=["blocks.13"],
... )
>>> pipe.to("cuda")
>>> prompt = "A cat holding a sign that says hello world"
>>> image = pipe(prompt, guidance_scale=5.0, pag_scale=0.7).images[0]
>>> image.save("sd3_pag.png")
( prompt: Union prompt_2: Union prompt_3: Union device: Optional = None num_images_per_prompt: int = 1 do_classifier_free_guidance: bool = True negative_prompt: Union = None negative_prompt_2: Union = None negative_prompt_3: Union = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None pooled_prompt_embeds: Optional = None negative_pooled_prompt_embeds: Optional = None clip_skip: Optional = None max_sequence_length: int = 256 lora_scale: Optional = None )
Parameters
str
or List[str]
, optional) —
prompt to be encoded str
or List[str]
, optional) —
The prompt or prompts to be sent to the tokenizer_2
and text_encoder_2
. If not defined, prompt
is
used in all text-encoders str
or List[str]
, optional) —
The prompt or prompts to be sent to the tokenizer_3
and text_encoder_3
. If not defined, prompt
is
used in all text-encoders
device — (torch.device
):
torch device int
) —
number of images that should be generated per prompt bool
) —
whether to use classifier free guidance or not str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation to be sent to tokenizer_2
and
text_encoder_2
. If not defined, negative_prompt
is used in all the text-encoders. str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation to be sent to tokenizer_3
and
text_encoder_3
. If not defined, negative_prompt
is used in both text-encoders torch.FloatTensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.FloatTensor
, optional) —
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, negative_prompt_embeds will be generated from negative_prompt
input
argument. torch.FloatTensor
, optional) —
Pre-generated pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting.
If not provided, pooled text embeddings will be generated from prompt
input argument. torch.FloatTensor
, optional) —
Pre-generated negative pooled text embeddings. Can be used to easily tweak text inputs, e.g. prompt
weighting. If not provided, pooled negative_prompt_embeds will be generated from negative_prompt
input argument. int
, optional) —
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
the output of the pre-final layer will be used for computing the prompt embeddings. float
, optional) —
A lora scale that will be applied to all LoRA layers of the text encoder if LoRA layers are loaded. ( tokenizer: T5Tokenizer text_encoder: T5EncoderModel vae: AutoencoderKL transformer: PixArtTransformer2DModel scheduler: KarrasDiffusionSchedulers pag_applied_layers: Union = 'blocks.1' )
PAG pipeline for text-to-image generation using PixArt-Sigma.
( prompt: Union = None negative_prompt: str = '' num_inference_steps: int = 20 timesteps: List = None sigmas: List = None guidance_scale: float = 4.5 num_images_per_prompt: Optional = 1 height: Optional = None width: Optional = None eta: float = 0.0 generator: Union = None latents: Optional = None prompt_embeds: Optional = None prompt_attention_mask: Optional = None negative_prompt_embeds: Optional = None negative_prompt_attention_mask: Optional = None output_type: Optional = 'pil' return_dict: bool = True callback: Optional = None callback_steps: int = 1 clean_caption: bool = True use_resolution_binning: bool = True max_sequence_length: int = 300 pag_scale: float = 3.0 pag_adaptive_scale: float = 0.0 ) → ImagePipelineOutput or tuple
Parameters
str
or List[str]
, optional) —
The prompt or prompts to guide the image generation. If not defined, one has to pass prompt_embeds
.
instead. str
or List[str]
, optional) —
The prompt or prompts not to guide the image generation. If not defined, one has to pass
negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is
less than 1
). int
, optional, defaults to 100) —
The number of denoising steps. More denoising steps usually lead to a higher quality image at the
expense of slower inference. List[int]
, optional) —
Custom timesteps to use for the denoising process with schedulers which support a timesteps
argument
in their set_timesteps
method. If not defined, the default behavior when num_inference_steps
is
passed will be used. Must be in descending order. List[float]
, optional) —
Custom sigmas to use for the denoising process with schedulers which support a sigmas
argument in
their set_timesteps
method. If not defined, the default behavior when num_inference_steps
is passed
will be used. float
, optional, defaults to 4.5) —
Guidance scale as defined in Classifier-Free Diffusion Guidance.
guidance_scale
is defined as w
of equation 2. of Imagen
Paper. Guidance scale is enabled by setting guidance_scale > 1
. Higher guidance scale encourages to generate images that are closely linked to the text prompt
,
usually at the expense of lower image quality. int
, optional, defaults to 1) —
The number of images to generate per prompt. int
, optional, defaults to self.unet.config.sample_size) —
The height in pixels of the generated image. int
, optional, defaults to self.unet.config.sample_size) —
The width in pixels of the generated image. float
, optional, defaults to 0.0) —
Corresponds to parameter eta (η) in the DDIM paper: https://arxiv.org/abs/2010.02502. Only applies to
schedulers.DDIMScheduler, will be ignored for others. torch.Generator
or List[torch.Generator]
, optional) —
One or a list of torch generator(s)
to make generation deterministic. torch.Tensor
, optional) —
Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
tensor will ge generated by sampling using the supplied random generator
. torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) — Pre-generated attention mask for text embeddings. torch.Tensor
, optional) —
Pre-generated negative text embeddings. For PixArt-Sigma this negative prompt should be "". If not
provided, negative_prompt_embeds will be generated from negative_prompt
input argument. torch.Tensor
, optional) —
Pre-generated attention mask for negative text embeddings. str
, optional, defaults to "pil"
) —
The output format of the generate image. Choose between
PIL: PIL.Image.Image
or np.array
. bool
, optional, defaults to True
) —
Whether or not to return a ~pipelines.stable_diffusion.IFPipelineOutput
instead of a plain tuple. Callable
, optional) —
A function that will be called every callback_steps
steps during inference. The function will be
called with the following arguments: callback(step: int, timestep: int, latents: torch.Tensor)
. int
, optional, defaults to 1) —
The frequency at which the callback
function will be called. If not specified, the callback will be
called at every step. bool
, optional, defaults to True
) —
Whether or not to clean the caption before creating embeddings. Requires beautifulsoup4
and ftfy
to
be installed. If the dependencies are not installed, the embeddings will be created from the raw
prompt. bool
defaults to True
) —
If set to True
, the requested height and width are first mapped to the closest resolutions using
ASPECT_RATIO_1024_BIN
. After the produced latents are decoded into images, they are resized back to
the requested resolution. Useful for generating non-square images. int
defaults to 300) — Maximum sequence length to use with the prompt
. float
, optional, defaults to 3.0) —
The scale factor for the perturbed attention guidance. If it is set to 0.0, the perturbed attention
guidance will not be used. float
, optional, defaults to 0.0) —
The adaptive scale factor for the perturbed attention guidance. If it is set to 0.0, pag_scale
is
used. Returns
ImagePipelineOutput or tuple
If return_dict
is True
, ImagePipelineOutput is returned, otherwise a tuple
is
returned where the first element is a list with the generated images
Function invoked when calling the pipeline for generation.
Examples:
>>> import torch
>>> from diffusers import AutoPipelineForText2Image
>>> pipe = AutoPipelineForText2Image.from_pretrained(
... "PixArt-alpha/PixArt-Sigma-XL-2-1024-MS",
... torch_dtype=torch.float16,
... pag_applied_layers=["blocks.14"],
... enable_pag=True,
... )
>>> pipe = pipe.to("cuda")
>>> prompt = "A small cactus with a happy face in the Sahara desert"
>>> image = pipe(prompt, pag_scale=4.0, guidance_scale=1.0).images[0]
( prompt: Union do_classifier_free_guidance: bool = True negative_prompt: str = '' num_images_per_prompt: int = 1 device: Optional = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None prompt_attention_mask: Optional = None negative_prompt_attention_mask: Optional = None clean_caption: bool = False max_sequence_length: int = 300 **kwargs )
Parameters
str
or List[str]
, optional) —
prompt to be encoded str
or List[str]
, optional) —
The prompt not to guide the image generation. If not defined, one has to pass negative_prompt_embeds
instead. Ignored when not using guidance (i.e., ignored if guidance_scale
is less than 1
). For
PixArt-Alpha, this should be "". bool
, optional, defaults to True
) —
whether to use classifier free guidance or not int
, optional, defaults to 1) —
number of images that should be generated per prompt
device — (torch.device
, optional):
torch device to place the resulting embeddings on torch.Tensor
, optional) —
Pre-generated text embeddings. Can be used to easily tweak text inputs, e.g. prompt weighting. If not
provided, text embeddings will be generated from prompt
input argument. torch.Tensor
, optional) —
Pre-generated negative text embeddings. For PixArt-Alpha, it’s should be the embeddings of the ""
string. bool
, defaults to False
) —
If True
, the function will preprocess and clean the provided caption before encoding. int
, defaults to 300) — Maximum sequence length to use for the prompt. Encodes the prompt into text encoder hidden states.