#
unCLIP

Hierarchical Text-Conditional Image Generation with CLIP Latents is by Aditya Ramesh, Prafulla Dhariwal, Alex Nichol, Casey Chu, Mark Chen. The unCLIP model in 🤗 Diffusers comes from kakaobrain’s karlo.

The abstract from the paper is following:

Contrastive models like CLIP have been shown to learn robust representations of images that capture both semantics and style. To leverage these representations for image generation, we propose a two-stage model: a prior that generates a CLIP image embedding given a text caption, and a decoder that generates an image conditioned on the image embedding. We show that explicitly generating image representations improves image diversity with minimal loss in photorealism and caption similarity. Our decoders conditioned on image representations can also produce variations of an image that preserve both its semantics and style, while varying the non-essential details absent from the image representation. Moreover, the joint embedding space of CLIP enables language-guided image manipulations in a zero-shot fashion. We use diffusion models for the decoder and experiment with both autoregressive and diffusion models for the prior, finding that the latter are computationally more efficient and produce higher-quality samples.

You can find lucidrains’ DALL-E 2 recreation at lucidrains/DALLE2-pytorch.

Make sure to check out the Schedulers guide to learn how to explore the tradeoff between scheduler speed and quality, and see the reuse components across pipelines section to learn how to efficiently load the same components into multiple pipelines.

##
UnCLIPPipeline

###
class diffusers.UnCLIPPipeline

< source >

( prior: PriorTransformer decoder: UNet2DConditionModel text_encoder: CLIPTextModelWithProjection tokenizer: CLIPTokenizer text_proj: UnCLIPTextProjModel super_res_first: UNet2DModel super_res_last: UNet2DModel prior_scheduler: UnCLIPScheduler decoder_scheduler: UnCLIPScheduler super_res_scheduler: UnCLIPScheduler )

Parameters

- text_encoder (CLIPTextModelWithProjection) — Frozen text-encoder.
- tokenizer (CLIPTokenizer) — A `CLIPTokenizer` to tokenize text.
- prior (PriorTransformer) — The canonical unCLIP prior to approximate the image embedding from the text embedding.
- text_proj (`UnCLIPTextProjModel`) — Utility class to prepare and combine the embeddings before they are passed to the decoder.
- decoder (UNet2DConditionModel) — The decoder to invert the image embedding into an image.
- super_res_first (UNet2DModel) — Super resolution UNet. Used in all but the last step of the super resolution diffusion process.
- super_res_last (UNet2DModel) — Super resolution UNet. Used in the last step of the super resolution diffusion process.
- prior_scheduler (`UnCLIPScheduler`) — Scheduler used in the prior denoising process (a modified DDPMScheduler).
- decoder_scheduler (`UnCLIPScheduler`) — Scheduler used in the decoder denoising process (a modified DDPMScheduler).
- super_res_scheduler (`UnCLIPScheduler`) — Scheduler used in the super resolution denoising process (a modified DDPMScheduler).

Pipeline for text-to-image generation using unCLIP.

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.).

####
__call__

< source >

( prompt: Union = None num_images_per_prompt: int = 1 prior_num_inference_steps: int = 25 decoder_num_inference_steps: int = 25 super_res_num_inference_steps: int = 7 generator: Union = None prior_latents: Optional = None decoder_latents: Optional = None super_res_latents: Optional = None text_model_output: Union = None text_attention_mask: Optional = None prior_guidance_scale: float = 4.0 decoder_guidance_scale: float = 8.0 output_type: Optional = 'pil' return_dict: bool = True ) →
ImagePipelineOutput or `tuple`

Parameters

- prompt (`str` or `List[str]`) — The prompt or prompts to guide image generation. This can only be left undefined if `text_model_output` and `text_attention_mask` is passed.
- num_images_per_prompt (`int`, optional, defaults to 1) — The number of images to generate per prompt.
- prior_num_inference_steps (`int`, optional, defaults to 25) — The number of denoising steps for the prior. More denoising steps usually lead to a higher quality image at the expense of slower inference.
- decoder_num_inference_steps (`int`, optional, defaults to 25) — The number of denoising steps for the decoder. More denoising steps usually lead to a higher quality image at the expense of slower inference.
- super_res_num_inference_steps (`int`, optional, defaults to 7) — The number of denoising steps for super resolution. More denoising steps usually lead to a higher quality image at the expense of slower inference.
- generator (`torch.Generator` or `List[torch.Generator]`, optional) — A `torch.Generator` to make generation deterministic.
- prior_latents (`torch.Tensor` of shape (batch size, embeddings dimension), optional) — Pre-generated noisy latents to be used as inputs for the prior.
- decoder_latents (`torch.Tensor` of shape (batch size, channels, height, width), optional) — Pre-generated noisy latents to be used as inputs for the decoder.
- super_res_latents (`torch.Tensor` of shape (batch size, channels, super res height, super res width), optional) — Pre-generated noisy latents to be used as inputs for the decoder.
- prior_guidance_scale (`float`, optional, defaults to 4.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`.
- decoder_guidance_scale (`float`, optional, defaults to 4.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`.
- text_model_output (`CLIPTextModelOutput`, optional) — Pre-defined `CLIPTextModel` outputs that can be derived from the text encoder. Pre-defined text outputs can be passed for tasks like text embedding interpolations. Make sure to also pass `text_attention_mask` in this case. `prompt` can the be left `None`.
- text_attention_mask (`torch.Tensor`, optional) — Pre-defined CLIP text attention mask that can be derived from the tokenizer. Pre-defined text attention masks are necessary when passing `text_model_output`.
- output_type (`str`, optional, defaults to `"pil"`) — The output format of the generated image. Choose between `PIL.Image` or `np.array`.
- return_dict (`bool`, optional, defaults to `True`) — Whether or not to return a ImagePipelineOutput instead of a plain tuple.

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.

The call function to the pipeline for generation.

##
UnCLIPImageVariationPipeline

###
class diffusers.UnCLIPImageVariationPipeline

< source >

( decoder: UNet2DConditionModel text_encoder: CLIPTextModelWithProjection tokenizer: CLIPTokenizer text_proj: UnCLIPTextProjModel feature_extractor: CLIPImageProcessor image_encoder: CLIPVisionModelWithProjection super_res_first: UNet2DModel super_res_last: UNet2DModel decoder_scheduler: UnCLIPScheduler super_res_scheduler: UnCLIPScheduler )

Parameters

- text_encoder (CLIPTextModelWithProjection) — Frozen text-encoder.
- tokenizer (CLIPTokenizer) — A `CLIPTokenizer` to tokenize text.
- feature_extractor (CLIPImageProcessor) — Model that extracts features from generated images to be used as inputs for the `image_encoder`.
- image_encoder (CLIPVisionModelWithProjection) — Frozen CLIP image-encoder (clip-vit-large-patch14).
- text_proj (`UnCLIPTextProjModel`) — Utility class to prepare and combine the embeddings before they are passed to the decoder.
- decoder (UNet2DConditionModel) — The decoder to invert the image embedding into an image.
- super_res_first (UNet2DModel) — Super resolution UNet. Used in all but the last step of the super resolution diffusion process.
- super_res_last (UNet2DModel) — Super resolution UNet. Used in the last step of the super resolution diffusion process.
- decoder_scheduler (`UnCLIPScheduler`) — Scheduler used in the decoder denoising process (a modified DDPMScheduler).
- super_res_scheduler (`UnCLIPScheduler`) — Scheduler used in the super resolution denoising process (a modified DDPMScheduler).

Pipeline to generate image variations from an input image using UnCLIP.

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.).

####
__call__

< source >

( image: Union = None num_images_per_prompt: int = 1 decoder_num_inference_steps: int = 25 super_res_num_inference_steps: int = 7 generator: Optional = None decoder_latents: Optional = None super_res_latents: Optional = None image_embeddings: Optional = None decoder_guidance_scale: float = 8.0 output_type: Optional = 'pil' return_dict: bool = True ) →
ImagePipelineOutput or `tuple`

Parameters

- image (`PIL.Image.Image` or `List[PIL.Image.Image]` or `torch.Tensor`) — `Image` or tensor representing an image batch to be used as the starting point. If you provide a tensor, it needs to be compatible with the `CLIPImageProcessor` configuration. Can be left as `None` only when `image_embeddings` are passed.
- num_images_per_prompt (`int`, optional, defaults to 1) — The number of images to generate per prompt.
- decoder_num_inference_steps (`int`, optional, defaults to 25) — The number of denoising steps for the decoder. More denoising steps usually lead to a higher quality image at the expense of slower inference.
- super_res_num_inference_steps (`int`, optional, defaults to 7) — The number of denoising steps for super resolution. More denoising steps usually lead to a higher quality image at the expense of slower inference.
- generator (`torch.Generator`, optional) — A `torch.Generator` to make generation deterministic.
- decoder_latents (`torch.Tensor` of shape (batch size, channels, height, width), optional) — Pre-generated noisy latents to be used as inputs for the decoder.
- super_res_latents (`torch.Tensor` of shape (batch size, channels, super res height, super res width), optional) — Pre-generated noisy latents to be used as inputs for the decoder.
- decoder_guidance_scale (`float`, optional, defaults to 4.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`.
- image_embeddings (`torch.Tensor`, optional) — Pre-defined image embeddings that can be derived from the image encoder. Pre-defined image embeddings can be passed for tasks like image interpolations. `image` can be left as `None`.
- output_type (`str`, optional, defaults to `"pil"`) — The output format of the generated image. Choose between `PIL.Image` or `np.array`.
- return_dict (`bool`, optional, defaults to `True`) — Whether or not to return a ImagePipelineOutput instead of a plain tuple.

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.

The call function to the pipeline for generation.

##
ImagePipelineOutput

###
class diffusers.ImagePipelineOutput

< source >

( images: Union )

Parameters

- images (`List[PIL.Image.Image]` or `np.ndarray`) — List of denoised PIL images of length `batch_size` or NumPy array of shape `(batch_size, height, width, num_channels)`.

Output class for image pipelines.

< > Update on GitHub