Versatile Diffusion

Versatile Diffusion

Versatile Diffusion was proposed in Versatile Diffusion: Text, Images and Variations All in One Diffusion Modelarrow-up-right by Xingqian Xu, Zhangyang Wang, Eric Zhang, Kai Wang, Humphrey Shi .

The abstract from the paper is:

The recent advances in diffusion models have set an impressive milestone in many generation tasks. Trending works such as DALL-E2, Imagen, and Stable Diffusion have attracted great interest in academia and industry. Despite the rapid landscape changes, recent new approaches focus on extensions and performance rather than capacity, thus requiring separate models for separate tasks. In this work, we expand the existing single-flow diffusion pipeline into a multi-flow network, dubbed Versatile Diffusion (VD), that handles text-to-image, image-to-text, image-variation, and text-variation in one unified model. Moreover, we generalize VD to a unified multi-flow multimodal diffusion framework with grouped layers, swappable streams, and other propositions that can process modalities beyond images and text. Through our experiments, we demonstrate that VD and its underlying framework have the following merits: a) VD handles all subtasks with competitive quality; b) VD initiates novel extensions and applications such as disentanglement of style and semantic, image-text dual-guided generation, etc.; c) Through these experiments and applications, VD provides more semantic insights of the generated outputs.

Tips

You can load the more memory intensive β€œall-in-one” VersatileDiffusionPipelinearrow-up-right that supports all the tasks or use the individual pipelines which are more memory efficient.

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

VersatileDiffusionPipeline

class diffusers.VersatileDiffusionPipeline

<source>arrow-up-right

( tokenizer: CLIPTokenizerimage_feature_extractor: CLIPImageProcessortext_encoder: CLIPTextModelimage_encoder: CLIPVisionModelimage_unet: UNet2DConditionModeltext_unet: UNet2DConditionModelvae: AutoencoderKLscheduler: KarrasDiffusionSchedulers )

Parameters

Pipeline for text-to-image generation using Stable Diffusion.

This model inherits from DiffusionPipelinearrow-up-right. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a particular device, etc.).

dual_guided

<source>arrow-up-right

( prompt: typing.Union[PIL.Image.Image, typing.List[PIL.Image.Image]]image: typing.Union[str, typing.List[str]]text_to_image_strength: float = 0.5height: typing.Optional[int] = Nonewidth: typing.Optional[int] = Nonenum_inference_steps: int = 50guidance_scale: float = 7.5num_images_per_prompt: typing.Optional[int] = 1eta: float = 0.0generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = Nonelatents: typing.Optional[torch.FloatTensor] = Noneoutput_type: typing.Optional[str] = 'pil'return_dict: bool = Truecallback: typing.Union[typing.Callable[[int, int, torch.FloatTensor], NoneType], NoneType] = Nonecallback_steps: int = 1 ) β†’ ImagePipelineOutputarrow-up-right or tuple

Parameters

  • prompt (str or List[str]) β€” The prompt or prompts to guide image generation.

  • height (int, optional, defaults to self.image_unet.config.sample_size * self.vae_scale_factor) β€” The height in pixels of the generated image.

  • width (int, optional, defaults to self.image_unet.config.sample_size * self.vae_scale_factor) β€” The width in pixels of the generated image.

  • num_inference_steps (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.

  • guidance_scale (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.

  • negative_prompt (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).

  • num_images_per_prompt (int, optional, defaults to 1) β€” The number of images to generate per prompt.

  • eta (float, optional, defaults to 0.0) β€” Corresponds to parameter eta (Ξ·) from the DDIMarrow-up-right paper. Only applies to the DDIMSchedulerarrow-up-right, and is ignored in other schedulers.

  • generator (torch.Generator or List[torch.Generator], optional) β€” A torch.Generatorarrow-up-right to make generation deterministic.

  • latents (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 is generated by sampling using the supplied random generator.

  • 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 StableDiffusionPipelineOutputarrow-up-right instead of a plain tuple.

  • callback (Callable, optional) β€” A function that calls every callback_steps steps during inference. The function is called with the following arguments: callback(step: int, timestep: int, latents: torch.FloatTensor).

  • callback_steps (int, optional, defaults to 1) β€” The frequency at which the callback function is called. If not specified, the callback is called at every step.

Returns

ImagePipelineOutputarrow-up-right or tuple

If return_dict is True, ImagePipelineOutputarrow-up-right 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.

Examples:

Copied

image_variation

<source>arrow-up-right

( image: typing.Union[torch.FloatTensor, PIL.Image.Image]height: typing.Optional[int] = Nonewidth: typing.Optional[int] = Nonenum_inference_steps: int = 50guidance_scale: float = 7.5negative_prompt: typing.Union[str, typing.List[str], NoneType] = Nonenum_images_per_prompt: typing.Optional[int] = 1eta: float = 0.0generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = Nonelatents: typing.Optional[torch.FloatTensor] = Noneoutput_type: typing.Optional[str] = 'pil'return_dict: bool = Truecallback: typing.Union[typing.Callable[[int, int, torch.FloatTensor], NoneType], NoneType] = Nonecallback_steps: int = 1 ) β†’ StableDiffusionPipelineOutputarrow-up-right or tuple

Parameters

  • image (PIL.Image.Image, List[PIL.Image.Image] or torch.Tensor) β€” The image prompt or prompts to guide the image generation.

  • height (int, optional, defaults to self.image_unet.config.sample_size * self.vae_scale_factor) β€” The height in pixels of the generated image.

  • width (int, optional, defaults to self.image_unet.config.sample_size * self.vae_scale_factor) β€” The width in pixels of the generated image.

  • num_inference_steps (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.

  • guidance_scale (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.

  • negative_prompt (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).

  • num_images_per_prompt (int, optional, defaults to 1) β€” The number of images to generate per prompt.

  • eta (float, optional, defaults to 0.0) β€” Corresponds to parameter eta (Ξ·) from the DDIMarrow-up-right paper. Only applies to the DDIMSchedulerarrow-up-right, and is ignored in other schedulers.

  • generator (torch.Generator, optional) β€” A torch.Generatorarrow-up-right to make generation deterministic.

  • latents (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 is generated by sampling using the supplied random generator.

  • 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 StableDiffusionPipelineOutputarrow-up-right instead of a plain tuple.

  • callback (Callable, optional) β€” A function that calls every callback_steps steps during inference. The function is called with the following arguments: callback(step: int, timestep: int, latents: torch.FloatTensor).

  • callback_steps (int, optional, defaults to 1) β€” The frequency at which the callback function is called. If not specified, the callback is called at every step.

Returns

StableDiffusionPipelineOutputarrow-up-right or tuple

If return_dict is True, StableDiffusionPipelineOutputarrow-up-right 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 bools indicating whether the corresponding generated image contains β€œnot-safe-for-work” (nsfw) content.

The call function to the pipeline for generation.

Examples:

Copied

text_to_image

<source>arrow-up-right

( prompt: typing.Union[str, typing.List[str]]height: typing.Optional[int] = Nonewidth: typing.Optional[int] = Nonenum_inference_steps: int = 50guidance_scale: float = 7.5negative_prompt: typing.Union[str, typing.List[str], NoneType] = Nonenum_images_per_prompt: typing.Optional[int] = 1eta: float = 0.0generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = Nonelatents: typing.Optional[torch.FloatTensor] = Noneoutput_type: typing.Optional[str] = 'pil'return_dict: bool = Truecallback: typing.Union[typing.Callable[[int, int, torch.FloatTensor], NoneType], NoneType] = Nonecallback_steps: int = 1 ) β†’ StableDiffusionPipelineOutputarrow-up-right or tuple

Parameters

  • prompt (str or List[str]) β€” The prompt or prompts to guide image generation.

  • height (int, optional, defaults to self.image_unet.config.sample_size * self.vae_scale_factor) β€” The height in pixels of the generated image.

  • width (int, optional, defaults to self.image_unet.config.sample_size * self.vae_scale_factor) β€” The width in pixels of the generated image.

  • num_inference_steps (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.

  • guidance_scale (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.

  • negative_prompt (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).

  • num_images_per_prompt (int, optional, defaults to 1) β€” The number of images to generate per prompt.

  • eta (float, optional, defaults to 0.0) β€” Corresponds to parameter eta (Ξ·) from the DDIMarrow-up-right paper. Only applies to the DDIMSchedulerarrow-up-right, and is ignored in other schedulers.

  • generator (torch.Generator, optional) β€” A torch.Generatorarrow-up-right to make generation deterministic.

  • latents (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 is generated by sampling using the supplied random generator.

  • 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 StableDiffusionPipelineOutputarrow-up-right instead of a plain tuple.

  • callback (Callable, optional) β€” A function that calls every callback_steps steps during inference. The function is called with the following arguments: callback(step: int, timestep: int, latents: torch.FloatTensor).

  • callback_steps (int, optional, defaults to 1) β€” The frequency at which the callback function is called. If not specified, the callback is called at every step.

Returns

StableDiffusionPipelineOutputarrow-up-right or tuple

If return_dict is True, StableDiffusionPipelineOutputarrow-up-right 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 bools indicating whether the corresponding generated image contains β€œnot-safe-for-work” (nsfw) content.

The call function to the pipeline for generation.

Examples:

Copied

VersatileDiffusionTextToImagePipeline

class diffusers.VersatileDiffusionTextToImagePipeline

<source>arrow-up-right

( tokenizer: CLIPTokenizertext_encoder: CLIPTextModelWithProjectionimage_unet: UNet2DConditionModeltext_unet: UNetFlatConditionModelvae: AutoencoderKLscheduler: KarrasDiffusionSchedulers )

Parameters

Pipeline for text-to-image generation using Versatile Diffusion.

This model inherits from DiffusionPipelinearrow-up-right. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a particular device, etc.).

__call__

<source>arrow-up-right

( prompt: typing.Union[str, typing.List[str]]height: typing.Optional[int] = Nonewidth: typing.Optional[int] = Nonenum_inference_steps: int = 50guidance_scale: float = 7.5negative_prompt: typing.Union[str, typing.List[str], NoneType] = Nonenum_images_per_prompt: typing.Optional[int] = 1eta: float = 0.0generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = Nonelatents: typing.Optional[torch.FloatTensor] = Noneoutput_type: typing.Optional[str] = 'pil'return_dict: bool = Truecallback: typing.Union[typing.Callable[[int, int, torch.FloatTensor], NoneType], NoneType] = Nonecallback_steps: int = 1**kwargs ) β†’ StableDiffusionPipelineOutputarrow-up-right or tuple

Parameters

  • prompt (str or List[str]) β€” The prompt or prompts to guide image generation.

  • height (int, optional, defaults to self.image_unet.config.sample_size * self.vae_scale_factor) β€” The height in pixels of the generated image.

  • width (int, optional, defaults to self.image_unet.config.sample_size * self.vae_scale_factor) β€” The width in pixels of the generated image.

  • num_inference_steps (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.

  • guidance_scale (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.

  • negative_prompt (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).

  • num_images_per_prompt (int, optional, defaults to 1) β€” The number of images to generate per prompt.

  • eta (float, optional, defaults to 0.0) β€” Corresponds to parameter eta (Ξ·) from the DDIMarrow-up-right paper. Only applies to the DDIMSchedulerarrow-up-right, and is ignored in other schedulers.

  • generator (torch.Generator, optional) β€” A torch.Generatorarrow-up-right to make generation deterministic.

  • latents (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 is generated by sampling using the supplied random generator.

  • 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 StableDiffusionPipelineOutputarrow-up-right instead of a plain tuple.

  • callback (Callable, optional) β€” A function that calls every callback_steps steps during inference. The function is called with the following arguments: callback(step: int, timestep: int, latents: torch.FloatTensor).

  • callback_steps (int, optional, defaults to 1) β€” The frequency at which the callback function is called. If not specified, the callback is called at every step.

Returns

StableDiffusionPipelineOutputarrow-up-right or tuple

If return_dict is True, StableDiffusionPipelineOutputarrow-up-right 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.

Examples:

Copied

VersatileDiffusionImageVariationPipeline

class diffusers.VersatileDiffusionImageVariationPipeline

<source>arrow-up-right

( image_feature_extractor: CLIPImageProcessorimage_encoder: CLIPVisionModelWithProjectionimage_unet: UNet2DConditionModelvae: AutoencoderKLscheduler: KarrasDiffusionSchedulers )

Parameters

Pipeline for image variation using Versatile Diffusion.

This model inherits from DiffusionPipelinearrow-up-right. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a particular device, etc.).

__call__

<source>arrow-up-right

( image: typing.Union[PIL.Image.Image, typing.List[PIL.Image.Image], torch.Tensor]height: typing.Optional[int] = Nonewidth: typing.Optional[int] = Nonenum_inference_steps: int = 50guidance_scale: float = 7.5negative_prompt: typing.Union[str, typing.List[str], NoneType] = Nonenum_images_per_prompt: typing.Optional[int] = 1eta: float = 0.0generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = Nonelatents: typing.Optional[torch.FloatTensor] = Noneoutput_type: typing.Optional[str] = 'pil'return_dict: bool = Truecallback: typing.Union[typing.Callable[[int, int, torch.FloatTensor], NoneType], NoneType] = Nonecallback_steps: int = 1**kwargs ) β†’ StableDiffusionPipelineOutputarrow-up-right or tuple

Parameters

  • image (PIL.Image.Image, List[PIL.Image.Image] or torch.Tensor) β€” The image prompt or prompts to guide the image generation.

  • height (int, optional, defaults to self.image_unet.config.sample_size * self.vae_scale_factor) β€” The height in pixels of the generated image.

  • width (int, optional, defaults to self.image_unet.config.sample_size * self.vae_scale_factor) β€” The width in pixels of the generated image.

  • num_inference_steps (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.

  • guidance_scale (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.

  • negative_prompt (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).

  • num_images_per_prompt (int, optional, defaults to 1) β€” The number of images to generate per prompt.

  • eta (float, optional, defaults to 0.0) β€” Corresponds to parameter eta (Ξ·) from the DDIMarrow-up-right paper. Only applies to the DDIMSchedulerarrow-up-right, and is ignored in other schedulers.

  • generator (torch.Generator, optional) β€” A torch.Generatorarrow-up-right to make generation deterministic.

  • latents (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 is generated by sampling using the supplied random generator.

  • 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 StableDiffusionPipelineOutputarrow-up-right instead of a plain tuple.

  • callback (Callable, optional) β€” A function that calls every callback_steps steps during inference. The function is called with the following arguments: callback(step: int, timestep: int, latents: torch.FloatTensor).

  • callback_steps (int, optional, defaults to 1) β€” The frequency at which the callback function is called. If not specified, the callback is called at every step.

Returns

StableDiffusionPipelineOutputarrow-up-right or tuple

If return_dict is True, StableDiffusionPipelineOutputarrow-up-right 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.

Examples:

Copied

VersatileDiffusionDualGuidedPipeline

class diffusers.VersatileDiffusionDualGuidedPipeline

<source>arrow-up-right

( tokenizer: CLIPTokenizerimage_feature_extractor: CLIPImageProcessortext_encoder: CLIPTextModelWithProjectionimage_encoder: CLIPVisionModelWithProjectionimage_unet: UNet2DConditionModeltext_unet: UNetFlatConditionModelvae: AutoencoderKLscheduler: KarrasDiffusionSchedulers )

Parameters

Pipeline for image-text dual-guided generation using Versatile Diffusion.

This model inherits from DiffusionPipelinearrow-up-right. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a particular device, etc.).

__call__

<source>arrow-up-right

( prompt: typing.Union[PIL.Image.Image, typing.List[PIL.Image.Image]]image: typing.Union[str, typing.List[str]]text_to_image_strength: float = 0.5height: typing.Optional[int] = Nonewidth: typing.Optional[int] = Nonenum_inference_steps: int = 50guidance_scale: float = 7.5num_images_per_prompt: typing.Optional[int] = 1eta: float = 0.0generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = Nonelatents: typing.Optional[torch.FloatTensor] = Noneoutput_type: typing.Optional[str] = 'pil'return_dict: bool = Truecallback: typing.Union[typing.Callable[[int, int, torch.FloatTensor], NoneType], NoneType] = Nonecallback_steps: int = 1**kwargs ) β†’ ImagePipelineOutputarrow-up-right or tuple

Parameters

  • prompt (str or List[str]) β€” The prompt or prompts to guide image generation.

  • height (int, optional, defaults to self.image_unet.config.sample_size * self.vae_scale_factor) β€” The height in pixels of the generated image.

  • width (int, optional, defaults to self.image_unet.config.sample_size * self.vae_scale_factor) β€” The width in pixels of the generated image.

  • num_inference_steps (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.

  • guidance_scale (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.

  • negative_prompt (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).

  • num_images_per_prompt (int, optional, defaults to 1) β€” The number of images to generate per prompt.

  • eta (float, optional, defaults to 0.0) β€” Corresponds to parameter eta (Ξ·) from the DDIMarrow-up-right paper. Only applies to the DDIMSchedulerarrow-up-right, and is ignored in other schedulers.

  • generator (torch.Generator or List[torch.Generator], optional) β€” A torch.Generatorarrow-up-right to make generation deterministic.

  • latents (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 is generated by sampling using the supplied random generator.

  • 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 ImagePipelineOutputarrow-up-right instead of a plain tuple.

  • callback (Callable, optional) β€” A function that calls every callback_steps steps during inference. The function is called with the following arguments: callback(step: int, timestep: int, latents: torch.FloatTensor).

  • callback_steps (int, optional, defaults to 1) β€” The frequency at which the callback function is called. If not specified, the callback is called at every step.

Returns

ImagePipelineOutputarrow-up-right or tuple

If return_dict is True, ImagePipelineOutputarrow-up-right 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.

Examples:

Copied

Last updated