text stringlengths 1 1.02k | class_index int64 0 1.38k | source stringclasses 431
values |
|---|---|---|
generation deterministic.
latents (`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
... | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
pooled_prompt_embeds (`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.
negative_pooled_prompt_embeds (`torch.Tenso... | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
cross_attention_kwargs (`dict`, *optional*):
A kwargs dictionary that if specified is passed along to the [`AttentionProcessor`] as defined in
[`self.processor`](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py).
controlnet_condit... | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
If `original_size` is not the same as `target_size` the image will appear to be down- or upsampled.
`original_size` defaults to `(width, height)` if not specified. Part of SDXL's micro-conditioning as
explained in section 2.2 of
[https://huggingface.co/papers/2307.01952](... | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
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 `(width, height)`. Part of SDXL's micro-conditioning as explained in
section 2.2 of [https://huggingface.co/papers/2307.01952](https://huggingface.co/pa... | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
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](https://huggingface.co/papers/2307.01952). For more
information, refer to this ... | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
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.
callback_on_step_end (`Callable`, `PipelineCallback`, `MultiPipelineCallbacks`, *optional*):
... | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
`._callback_tensor_inputs` attribute of your pipeine class. | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
Examples:
Returns:
[`~pipelines.stable_diffusion.StableDiffusionXLPipelineOutput`] or `tuple`:
If `return_dict` is `True`, [`~pipelines.stable_diffusion.StableDiffusionXLPipelineOutput`] is
returned, otherwise a `tuple` is returned containing the output images.
... | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
# 1. Check inputs. Raise error if not correct
self.check_inputs(
prompt,
prompt_2,
image,
negative_prompt,
negative_prompt_2,
prompt_embeds,
negative_prompt_embeds,
pooled_prompt_embeds,
negative_pooled_p... | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
device = self._execution_device
# here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
# of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
# corresponds to doing no classifier free guidance.
do_classifier_free_guidance = guidan... | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
# 3. Encode input prompt
text_encoder_lora_scale = (
cross_attention_kwargs.get("scale", None) if cross_attention_kwargs is not None else None
)
(
prompt_embeds,
negative_prompt_embeds,
pooled_prompt_embeds,
negative_pooled_prompt_embed... | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
# 4. Prepare image
if isinstance(unet, UNetControlNetXSModel):
image = self.prepare_image(
image=image,
width=width,
height=height,
batch_size=batch_size * num_images_per_prompt,
num_images_per_prompt=num_images_per_prom... | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
# 6. Prepare latent variables
num_channels_latents = self.unet.in_channels
latents = self.prepare_latents(
batch_size * num_images_per_prompt,
num_channels_latents,
height,
width,
prompt_embeds.dtype,
device,
generator,
... | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
add_text_embeds = pooled_prompt_embeds
if self.text_encoder_2 is None:
text_encoder_projection_dim = int(pooled_prompt_embeds.shape[-1])
else:
text_encoder_projection_dim = self.text_encoder_2.config.projection_dim
add_time_ids = self._get_add_time_ids(
origi... | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
if do_classifier_free_guidance:
prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds], dim=0)
add_text_embeds = torch.cat([negative_pooled_prompt_embeds, add_text_embeds], dim=0)
add_time_ids = torch.cat([negative_add_time_ids, add_time_ids], dim=0)
prompt_embeds... | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
# 8. Denoising loop
num_warmup_steps = len(timesteps) - num_inference_steps * self.scheduler.order
self._num_timesteps = len(timesteps)
is_controlnet_compiled = is_compiled_module(self.unet)
is_torch_higher_equal_2_1 = is_torch_version(">=", "2.1")
with self.progress_bar(total=nu... | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
added_cond_kwargs = {"text_embeds": add_text_embeds, "time_ids": add_time_ids}
# predict the noise residual
apply_control = (
i / len(timesteps) >= control_guidance_start and (i + 1) / len(timesteps) <= control_guidance_end
)
noise_pre... | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
# perform guidance
if do_classifier_free_guidance:
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
# compute the previous noisy sample x_t -> x_t-1
... | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
latents = callback_outputs.pop("latents", latents)
prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds)
negative_prompt_embeds = callback_outputs.pop("negative_prompt_embeds", negative_prompt_embeds)
# call the callback, if provided
... | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
if needs_upcasting:
self.upcast_vae()
latents = latents.to(next(iter(self.vae.post_quant_conv.parameters())).dtype)
image = self.vae.decode(latents / self.vae.config.scaling_factor, return_dict=False)[0]
# cast back to fp16 if needed
if needs_upcasti... | 359 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py |
class StableDiffusionControlNetXSPipeline(
DiffusionPipeline,
StableDiffusionMixin,
TextualInversionLoaderMixin,
StableDiffusionLoraLoaderMixin,
FromSingleFileMixin,
):
r"""
Pipeline for text-to-image generation using Stable Diffusion with ControlNet-XS guidance.
This model inherits fro... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
Args:
vae ([`AutoencoderKL`]):
Variational Auto-Encoder (VAE) model to encode and decode images to and from latent representations.
text_encoder ([`~transformers.CLIPTextModel`]):
Frozen text-encoder ([clip-vit-large-patch14](https://huggingface.co/openai/clip-vit-large-patch14))... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
safety_checker ([`StableDiffusionSafetyChecker`]):
Classification module that estimates whether generated images could be considered offensive or harmful.
Please refer to the [model card](https://huggingface.co/runwayml/stable-diffusion-v1-5) for more details
about a model's potentia... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
model_cpu_offload_seq = "text_encoder->unet->vae"
_optional_components = ["safety_checker", "feature_extractor"]
_exclude_from_cpu_offload = ["safety_checker"]
_callback_tensor_inputs = ["latents", "prompt_embeds", "negative_prompt_embeds"]
def __init__(
self,
vae: AutoencoderKL,
... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
if safety_checker is None and requires_safety_checker:
logger.warning(
f"You have disabled the safety checker for {self.__class__} by passing `safety_checker=None`. Ensure"
" that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered"
... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
if safety_checker is not None and feature_extractor is None:
raise ValueError(
"Make sure to define a feature extractor when loading {self.__class__} if you want to use the safety"
" checker. If you do not want to use the safety checker, you can pass `'safety_checker=None'` i... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
self.register_modules(
vae=vae,
text_encoder=text_encoder,
tokenizer=tokenizer,
unet=unet,
controlnet=controlnet,
scheduler=scheduler,
safety_checker=safety_checker,
feature_extractor=feature_extractor,
)
sel... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._encode_prompt
def _encode_prompt(
self,
prompt,
device,
num_images_per_prompt,
do_classifier_free_guidance,
negative_prompt=None,
prompt_embeds: Optional[torc... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
prompt_embeds_tuple = self.encode_prompt(
prompt=prompt,
device=device,
num_images_per_prompt=num_images_per_prompt,
do_classifier_free_guidance=do_classifier_free_guidance,
negative_prompt=negative_prompt,
prompt_embeds=prompt_embeds,
... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.encode_prompt
def encode_prompt(
self,
prompt,
device,
num_images_per_prompt,
do_classifier_free_guidance,
negative_prompt=None,
prompt_embeds: Optional[torch.... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
Args:
prompt (`str` or `List[str]`, *optional*):
prompt to be encoded
device: (`torch.device`):
torch device
num_images_per_prompt (`int`):
number of images that should be generated per prompt
do_classifier_free_guidance (`b... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
negative_prompt_embeds (`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.
lora... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
# dynamically adjust the LoRA scale
if not USE_PEFT_BACKEND:
adjust_lora_scale_text_encoder(self.text_encoder, lora_scale)
else:
scale_lora_layers(self.text_encoder, lora_scale)
if prompt is not None and isinstance(prompt, str):
batch_size = 1... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
text_inputs = self.tokenizer(
prompt,
padding="max_length",
max_length=self.tokenizer.model_max_length,
truncation=True,
return_tensors="pt",
)
text_input_ids = text_inputs.input_ids
untruncated_ids = sel... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
if hasattr(self.text_encoder.config, "use_attention_mask") and self.text_encoder.config.use_attention_mask:
attention_mask = text_inputs.attention_mask.to(device)
else:
attention_mask = None | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
if clip_skip is None:
prompt_embeds = self.text_encoder(text_input_ids.to(device), attention_mask=attention_mask)
prompt_embeds = prompt_embeds[0]
else:
prompt_embeds = self.text_encoder(
text_input_ids.to(device), attention_mask=attention_... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
prompt_embeds = self.text_encoder.text_model.final_layer_norm(prompt_embeds) | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
if self.text_encoder is not None:
prompt_embeds_dtype = self.text_encoder.dtype
elif self.unet is not None:
prompt_embeds_dtype = self.unet.dtype
else:
prompt_embeds_dtype = prompt_embeds.dtype
prompt_embeds = prompt_embeds.to(dtype=prompt_embeds_dtype, devic... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
# get unconditional embeddings for classifier free guidance
if do_classifier_free_guidance and negative_prompt_embeds is None:
uncond_tokens: List[str]
if negative_prompt is None:
uncond_tokens = [""] * batch_size
elif prompt is not None and type(prompt) is no... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
" the batch size of `prompt`."
)
else:
uncond_tokens = negative_prompt | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
# textual inversion: process multi-vector tokens if necessary
if isinstance(self, TextualInversionLoaderMixin):
uncond_tokens = self.maybe_convert_prompt(uncond_tokens, self.tokenizer)
max_length = prompt_embeds.shape[1]
uncond_input = self.tokenizer(
... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
if do_classifier_free_guidance:
# duplicate unconditional embeddings for each generation per prompt, using mps friendly method
seq_len = negative_prompt_embeds.shape[1]
negative_prompt_embeds = negative_prompt_embeds.to(dtype=prompt_embeds_dtype, device=device)
negative... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.run_safety_checker
def run_safety_checker(self, image, device, dtype):
if self.safety_checker is None:
has_nsfw_concept = None
else:
if torch.is_tensor(image):
... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.decode_latents
def decode_latents(self, latents):
deprecation_message = "The decode_latents method is deprecated and will be removed in 1.0.0. Please use VaeImageProcessor.postprocess(...) instead"
d... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.prepare_extra_step_kwargs
def prepare_extra_step_kwargs(self, generator, eta):
# prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
# eta (η) is only used w... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
def check_inputs(
self,
prompt,
image,
negative_prompt=None,
prompt_embeds=None,
negative_prompt_embeds=None,
controlnet_conditioning_scale=1.0,
control_guidance_start=0.0,
control_guidance_end=1.0,
callback_on_step_end_tensor_inputs=None,
... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
if prompt is not None and prompt_embeds is not None:
raise ValueError(
f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to"
" only forward one of the two."
)
elif prompt is None and prompt_embeds is None:
... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
if prompt_embeds is not None and negative_prompt_embeds is not None:
if prompt_embeds.shape != negative_prompt_embeds.shape:
raise ValueError(
"`prompt_embeds` and `negative_prompt_embeds` must have the same shape when passed directly, but"
f" got: `pr... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
# Check `image` and `controlnet_conditioning_scale`
is_compiled = hasattr(F, "scaled_dot_product_attention") and isinstance(
self.unet, torch._dynamo.eval_frame.OptimizedModule
)
if (
isinstance(self.unet, UNetControlNetXSModel)
or is_compiled
and ... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
start, end = control_guidance_start, control_guidance_end
if start >= end:
raise ValueError(
f"control guidance start: {start} cannot be larger or equal to control guidance end: {end}."
)
if start < 0.0:
raise ValueError(f"control guidance start: {star... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
if (
not image_is_pil
and not image_is_tensor
and not image_is_np
and not image_is_pil_list
and not image_is_tensor_list
and not image_is_np_list
):
raise TypeError(
f"image must be passed and be one of PIL image... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
if image_batch_size != 1 and image_batch_size != prompt_batch_size:
raise ValueError(
f"If image batch size is not 1, image batch size must be same as prompt batch size. image batch size: {image_batch_size}, prompt batch size: {prompt_batch_size}"
)
def prepare_image(
... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
if do_classifier_free_guidance:
image = torch.cat([image] * 2)
return image
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.prepare_latents
def prepare_latents(self, batch_size, num_channels_latents, height, width, dtype, device, generat... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
if latents is None:
latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype)
else:
latents = latents.to(device)
# scale the initial noise by the standard deviation required by the scheduler
latents = latents * self.scheduler.init_noise_sigma
... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
@property
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.cross_attention_kwargs
def cross_attention_kwargs(self):
return self._cross_attention_kwargs
@property
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.Stab... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
@torch.no_grad()
@replace_example_docstring(EXAMPLE_DOC_STRING)
def __call__(
self,
prompt: Union[str, List[str]] = None,
image: PipelineImageInput = None,
height: Optional[int] = None,
width: Optional[int] = None,
num_inference_steps: int = 50,
guidance_s... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
control_guidance_end: float = 1.0,
clip_skip: Optional[int] = None,
callback_on_step_end: Optional[
Union[Callable[[int, int, Dict], None], PipelineCallback, MultiPipelineCallbacks]
] = None,
callback_on_step_end_tensor_inputs: List[str] = ["latents"],
):
r"""
... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
Args:
prompt (`str` or `List[str]`, *optional*):
The prompt or prompts to guide image generation. If not defined, you need to pass `prompt_embeds`.
image (`torch.Tensor`, `PIL.Image.Image`, `np.ndarray`, `List[torch.Tensor]`, `List[PIL.Image.Image]`, `List[np.ndarray]`,:
... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
to a single ControlNet.
height (`int`, *optional*, defaults to `self.unet.config.sample_size * self.vae_scale_factor`):
The height in pixels of the generated image.
width (`int`, *optional*, defaults to `self.unet.config.sample_size * self.vae_scale_factor`):
The ... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
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`).
num_images_per_prompt (`int`, *optional*, defaults to 1):
The number of images to... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
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`.
prompt_embeds (`torch.Tensor`, *optional*):
Pre-generated text embeddings. Can be used to easily twea... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
Whether or not to return a [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] instead of a
plain tuple.
cross_attention_kwargs (`dict`, *optional*):
A kwargs dictionary that if specified is passed along to the [`AttentionProcessor`] as defined in
[`... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
control_guidance_end (`float` or `List[float]`, *optional*, defaults to 1.0):
The percentage of total steps at which the ControlNet stops applying.
clip_skip (`int`, *optional*):
Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 mean... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
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... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
Returns:
[`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] or `tuple`:
If `return_dict` is `True`, [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] is returned,
otherwise a `tuple` is returned where the first element is a list with the generated image... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
# 1. Check inputs. Raise error if not correct
self.check_inputs(
prompt,
image,
negative_prompt,
prompt_embeds,
negative_prompt_embeds,
controlnet_conditioning_scale,
control_guidance_start,
control_guidance_end,
... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
device = self._execution_device
# here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
# of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
# corresponds to doing no classifier free guidance.
do_classifier_free_guidance = guidan... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
# For classifier free guidance, we need to do two forward passes.
# Here we concatenate the unconditional and text embeddings into a single batch
# to avoid doing two forward passes
if do_classifier_free_guidance:
prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds])
... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
# 6. Prepare latent variables
num_channels_latents = self.unet.in_channels
latents = self.prepare_latents(
batch_size * num_images_per_prompt,
num_channels_latents,
height,
width,
prompt_embeds.dtype,
device,
generator,
... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
# 8. Denoising loop
num_warmup_steps = len(timesteps) - num_inference_steps * self.scheduler.order
self._num_timesteps = len(timesteps)
is_controlnet_compiled = is_compiled_module(self.unet)
is_torch_higher_equal_2_1 = is_torch_version(">=", "2.1")
with self.progress_bar(total=nu... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
# predict the noise residual
apply_control = (
i / len(timesteps) >= control_guidance_start and (i + 1) / len(timesteps) <= control_guidance_end
)
noise_pred = self.unet(
sample=latent_model_input,
timestep=t,
... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs, return_dict=False)[0]
if callback_on_step_end is not None:
callback_kwargs = {}
for k in callback_on_step_end_tensor_inputs:
callback_kwargs[k] = locals()[k]
... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
# If we do sequential model offloading, let's offload unet and controlnet
# manually for max memory savings
if hasattr(self, "final_offload_hook") and self.final_offload_hook is not None:
self.unet.to("cpu")
self.controlnet.to("cpu")
torch.cuda.empty_cache()
... | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
# Offload all models
self.maybe_free_model_hooks()
if not return_dict:
return (image, has_nsfw_concept)
return StableDiffusionPipelineOutput(images=image, nsfw_content_detected=has_nsfw_concept) | 360 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py |
class PaintByExamplePipeline(DiffusionPipeline, StableDiffusionMixin):
r"""
<Tip warning={true}>
🧪 This is an experimental feature!
</Tip>
Pipeline for image-guided image inpainting using Stable Diffusion.
This model inherits from [`DiffusionPipeline`]. Check the superclass documentation fo... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
Args:
vae ([`AutoencoderKL`]):
Variational Auto-Encoder (VAE) model to encode and decode images to and from latent representations.
image_encoder ([`PaintByExampleImageEncoder`]):
Encodes the example input image. The `unet` is conditioned on the example image instead of a text pr... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
Please refer to the [model card](https://huggingface.co/runwayml/stable-diffusion-v1-5) for more details
about a model's potential harms.
feature_extractor ([`~transformers.CLIPImageProcessor`]):
A `CLIPImageProcessor` to extract features from generated images; used as inputs to the `saf... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
"""
# TODO: feature_extractor is required to encode initial images (if they are in PIL format),
# we should give a descriptive message if the pipeline doesn't have one.
model_cpu_offload_seq = "unet->vae"
_exclude_from_cpu_offload = ["image_encoder"]
_optional_components = ["safety_checker"]
... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
self.register_modules(
vae=vae,
image_encoder=image_encoder,
unet=unet,
scheduler=scheduler,
safety_checker=safety_checker,
feature_extractor=feature_extractor,
)
self.vae_scale_factor = 2 ** (len(self.vae.config.block_out_channels)... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.run_safety_checker
def run_safety_checker(self, image, device, dtype):
if self.safety_checker is None:
has_nsfw_concept = None
else:
if torch.is_tensor(image):
... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.prepare_extra_step_kwargs
def prepare_extra_step_kwargs(self, generator, eta):
# prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
# eta (η) is only used w... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.decode_latents
def decode_latents(self, latents):
deprecation_message = "The decode_latents method is deprecated and will be removed in 1.0.0. Please use VaeImageProcessor.postprocess(...) instead"
d... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_image_variation.StableDiffusionImageVariationPipeline.check_inputs
def check_inputs(self, image, height, width, callback_steps):
if (
not isinstance(image, torch.Tensor)
and not isinstance(image, PIL.Image.I... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
if (callback_steps is None) or (
callback_steps is not None and (not isinstance(callback_steps, int) or callback_steps <= 0)
):
raise ValueError(
f"`callback_steps` has to be a positive integer but is {callback_steps} of type"
f" {type(callback_steps)}."
... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.prepare_latents
def prepare_latents(self, batch_size, num_channels_latents, height, width, dtype, device, generator, latents=None):
shape = (
batch_size,
num_channels_latents,
... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
# scale the initial noise by the standard deviation required by the scheduler
latents = latents * self.scheduler.init_noise_sigma
return latents
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_inpaint.StableDiffusionInpaintPipeline.prepare_mask_latents
def prepare_m... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
if masked_image.shape[1] == 4:
masked_image_latents = masked_image
else:
masked_image_latents = self._encode_vae_image(masked_image, generator=generator) | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
# duplicate mask and masked_image_latents for each generation per prompt, using mps friendly method
if mask.shape[0] < batch_size:
if not batch_size % mask.shape[0] == 0:
raise ValueError(
"The passed mask and the required batch size don't match. Masks are suppose... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
f" to a total batch size of {batch_size}, but {masked_image_latents.shape[0]} images were passed."
" Make sure the number of images that you pass is divisible by the total requested batch size."
)
masked_image_latents = masked_image_latents.repeat(batch_size // masked_ima... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
mask = torch.cat([mask] * 2) if do_classifier_free_guidance else mask
masked_image_latents = (
torch.cat([masked_image_latents] * 2) if do_classifier_free_guidance else masked_image_latents
)
# aligning device to prevent device errors when concating it with the latent model input
... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_inpaint.StableDiffusionInpaintPipeline._encode_vae_image
def _encode_vae_image(self, image: torch.Tensor, generator: torch.Generator):
if isinstance(generator, list):
image_latents = [
retrieve_latents(s... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
image = image.to(device=device, dtype=dtype)
image_embeddings, negative_prompt_embeds = self.image_encoder(image, return_uncond_vector=True)
# duplicate image embeddings for each generation per prompt, using mps friendly method
bs_embed, seq_len, _ = image_embeddings.shape
image_embeddi... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
# For classifier free guidance, we need to do two forward passes.
# Here we concatenate the unconditional and text embeddings into a single batch
# to avoid doing two forward passes
image_embeddings = torch.cat([negative_prompt_embeds, image_embeddings])
return image_embeddi... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
@torch.no_grad()
def __call__(
self,
example_image: Union[torch.Tensor, PIL.Image.Image],
image: Union[torch.Tensor, PIL.Image.Image],
mask_image: Union[torch.Tensor, PIL.Image.Image],
height: Optional[int] = None,
width: Optional[int] = None,
num_inference_st... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
Args:
example_image (`torch.Tensor` or `PIL.Image.Image` or `List[PIL.Image.Image]`):
An example image to guide image generation.
image (`torch.Tensor` or `PIL.Image.Image` or `List[PIL.Image.Image]`):
`Image` or tensor representing an image batch to be inpainted ... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
height (`int`, *optional*, defaults to `self.unet.config.sample_size * self.vae_scale_factor`):
The height in pixels of the generated image.
width (`int`, *optional*, defaults to `self.unet.config.sample_size * self.vae_scale_factor`):
The width in pixels of the generated ima... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
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`).
num_images_per_prompt (`int`, *optional*, defaults to 1):
The number of images to... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
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`.
output_type (`str`, *optional*, defaults to `"pil"`):
The output format of the generated image. Choos... | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
The frequency at which the `callback` function is called. If not specified, the callback is called at
every step. | 361 | /Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/pipelines/paint_by_example/pipeline_paint_by_example.py |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.