Diffusers 文件
影像變體
並獲得增強的文件體驗
開始使用
影像變體
Stable Diffusion 模型還可以從輸入影像生成變體。它使用了 Justin Pinkney(來自 Lambda)的 Stable Diffusion 模型的一個微調版本。
原始程式碼庫可在 LambdaLabsML/lambda-diffusers 找到,影像變體的其他官方檢查點可在 lambdalabs/sd-image-variations-diffusers 找到。
請務必檢視 Stable Diffusion 的提示部分,瞭解如何探索排程器速度和質量之間的權衡,以及如何高效地重用管道元件!
StableDiffusionImageVariationPipeline
class diffusers.StableDiffusionImageVariationPipeline
< 來源 >( vae: AutoencoderKL image_encoder: CLIPVisionModelWithProjection unet: UNet2DConditionModel scheduler: KarrasDiffusionSchedulers safety_checker: StableDiffusionSafetyChecker feature_extractor: CLIPImageProcessor requires_safety_checker: bool = True )
引數
- vae (AutoencoderKL) — 變分自編碼器 (VAE) 模型,用於將影像編碼和解碼為潛在表示。
- image_encoder (CLIPVisionModelWithProjection) — 凍結的 CLIP 影像編碼器 (clip-vit-large-patch14)。
- text_encoder (CLIPTextModel) — 凍結的文字編碼器 (clip-vit-large-patch14)。
- tokenizer (CLIPTokenizer) — 一個
CLIPTokenizer
,用於文字分詞。 - unet (UNet2DConditionModel) — 一個
UNet2DConditionModel
,用於對編碼的影像潛在表示進行去噪。 - scheduler (SchedulerMixin) — 一個與
unet
結合使用的排程器,用於對編碼的影像潛在表示進行去噪。可以是 DDIMScheduler、LMSDiscreteScheduler 或 PNDMScheduler 之一。 - safety_checker (
StableDiffusionSafetyChecker
) — 評估生成影像是否可能被視為冒犯性或有害的分類模組。有關模型潛在危害的更多詳細資訊,請參閱模型卡。 - feature_extractor (CLIPImageProcessor) — 一個
CLIPImageProcessor
,用於從生成的影像中提取特徵;用作safety_checker
的輸入。
用於從輸入影像生成影像變體的 Stable Diffusion 管道。
此模型繼承自 DiffusionPipeline。有關所有管道實現的通用方法(下載、儲存、在特定裝置上執行等),請檢視超類文件。
__call__
< 來源 >( image: typing.Union[PIL.Image.Image, typing.List[PIL.Image.Image], torch.Tensor] height: typing.Optional[int] = None width: typing.Optional[int] = None num_inference_steps: int = 50 guidance_scale: float = 7.5 num_images_per_prompt: typing.Optional[int] = 1 eta: float = 0.0 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = None callback_steps: int = 1 ) → StableDiffusionPipelineOutput 或 tuple
引數
- image (
PIL.Image.Image
或List[PIL.Image.Image]
或torch.Tensor
) — 用於引導影像生成的影像。如果提供張量,則需要與CLIPImageProcessor
相容。 - height (
int
, 可選, 預設為self.unet.config.sample_size * self.vae_scale_factor
) — 生成影像的高度(畫素)。 - width (
int
, 可選, 預設為self.unet.config.sample_size * self.vae_scale_factor
) — 生成影像的寬度(畫素)。 - num_inference_steps (
int
, 可選, 預設為 50) — 去噪步驟的數量。更多的去噪步驟通常會帶來更高質量的影像,但推理速度會變慢。此引數受strength
調製。 - guidance_scale (
float
, 可選, 預設為 7.5) — 較高的引導比例值鼓勵模型生成與文字prompt
緊密相關的影像,但影像質量會降低。當guidance_scale > 1
時,啟用引導比例。 - num_images_per_prompt (
int
, 可選, 預設為 1) — 每個提示詞生成的影像數量。 - eta (
float
, 可選, 預設為 0.0) — 對應於 DDIM 論文中的引數 eta (η)。僅適用於 DDIMScheduler,在其他排程器中被忽略。 - generator (
torch.Generator
或List[torch.Generator]
, 可選) — 一個torch.Generator
,用於使生成具有確定性。 - latents (
torch.Tensor
, 可選) — 預生成的從高斯分佈中取樣的帶噪潛在變數,用作影像生成的輸入。可用於使用不同的提示調整相同的生成。如果未提供,則使用提供的隨機generator
透過取樣生成一個潛在變數張量。 - output_type (
str
, 可選, 預設為"pil"
) — 生成影像的輸出格式。選擇PIL.Image
或np.array
。 - return_dict (
bool
, 可選, 預設為True
) — 是否返回 StableDiffusionPipelineOutput 而不是普通元組。 - callback (
Callable
, 可選) — 在推理過程中每隔callback_steps
步呼叫的函式。該函式透過以下引數呼叫:callback(step: int, timestep: int, latents: torch.Tensor)
。 - callback_steps (
int
, 可選, 預設為 1) — 呼叫callback
函式的頻率。如果未指定,則在每個步驟中呼叫回撥。
返回
StableDiffusionPipelineOutput 或 tuple
如果 return_dict
為 True
,則返回 StableDiffusionPipelineOutput,否則返回一個 tuple
,其中第一個元素是生成的影像列表,第二個元素是布林值列表,指示相應的生成的影像是否包含“不適合工作”(nsfw) 內容。
用於生成的管道的呼叫函式。
示例
from diffusers import StableDiffusionImageVariationPipeline
from PIL import Image
from io import BytesIO
import requests
pipe = StableDiffusionImageVariationPipeline.from_pretrained(
"lambdalabs/sd-image-variations-diffusers", revision="v2.0"
)
pipe = pipe.to("cuda")
url = "https://lh3.googleusercontent.com/y-iFOHfLTwkuQSUegpwDdgKmOjRSTvPxat63dQLB25xkTs4lhIbRUFeNBWZzYf370g=s1200"
response = requests.get(url)
image = Image.open(BytesIO(response.content)).convert("RGB")
out = pipe(image, num_images_per_prompt=3, guidance_scale=15)
out["images"][0].save("result.jpg")
enable_attention_slicing
< 來源 >( slice_size: typing.Union[int, str, NoneType] = 'auto' )
啟用分片注意力計算。啟用此選項後,注意力模組將輸入張量分割成切片,分多步計算注意力。對於多個注意力頭,計算按每個頭順序執行。這有助於節省一些記憶體,但會略微降低速度。
⚠️ 如果您已經在使用 PyTorch 2.0 或 xFormers 中的 scaled_dot_product_attention
(SDPA),請不要啟用注意力分片。這些注意力計算已經非常節省記憶體,因此您無需啟用此功能。如果您將注意力分片與 SDPA 或 xFormers 一起啟用,可能會導致嚴重的速度下降!
示例
>>> import torch
>>> from diffusers import StableDiffusionPipeline
>>> pipe = StableDiffusionPipeline.from_pretrained(
... "stable-diffusion-v1-5/stable-diffusion-v1-5",
... torch_dtype=torch.float16,
... use_safetensors=True,
... )
>>> prompt = "a photo of an astronaut riding a horse on mars"
>>> pipe.enable_attention_slicing()
>>> image = pipe(prompt).images[0]
停用切片注意力計算。如果之前呼叫過 enable_attention_slicing
,則注意力將一步計算完成。
enable_xformers_memory_efficient_attention
< 來源 >( attention_op: typing.Optional[typing.Callable] = None )
引數
- attention_op (
Callable
, 可選) — 覆蓋預設的None
運算子,用作 xFormers 的memory_efficient_attention()
函式的op
引數。
啟用 xFormers 的記憶體高效注意力。啟用此選項後,您將觀察到 GPU 記憶體使用量降低,推理速度可能會加快。訓練期間的速度提升不保證。
⚠️ 當記憶體高效注意力和切片注意力同時啟用時,記憶體高效注意力優先。
示例
>>> import torch
>>> from diffusers import DiffusionPipeline
>>> from xformers.ops import MemoryEfficientAttentionFlashAttentionOp
>>> pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16)
>>> pipe = pipe.to("cuda")
>>> pipe.enable_xformers_memory_efficient_attention(attention_op=MemoryEfficientAttentionFlashAttentionOp)
>>> # Workaround for not accepting attention shape using VAE for Flash Attention
>>> pipe.vae.enable_xformers_memory_efficient_attention(attention_op=None)
StableDiffusionPipelineOutput
class diffusers.pipelines.stable_diffusion.StableDiffusionPipelineOutput
< 來源 >( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] nsfw_content_detected: typing.Optional[typing.List[bool]] )
Stable Diffusion 管道的輸出類。