Diffusers 文件

安全穩定的擴散

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

安全穩定擴散

安全穩定擴散 (Safe Stable Diffusion) 是在 安全潛在擴散:緩解擴散模型中不當退化 中提出的,它旨在緩解 Stable Diffusion 模型中不當退化的問題,因為這些模型是在未過濾的網路抓取資料集上訓練的。例如,Stable Diffusion 可能會意外生成裸露、暴力、自殘影像以及其他冒犯性內容。安全穩定擴散是 Stable Diffusion 的一個擴充套件,它大大減少了此類內容的生成。

論文摘要如下:

文字條件影像生成模型最近在影像質量和文字對齊方面取得了驚人的成果,並因此被應用於快速增長的應用程式中。由於它們是高度資料驅動的,依賴於從網際網路上隨機抓取數十億規模的資料集,因此正如我們所證明的,它們也受到了退化和偏見的人類行為的影響。反過來,它們甚至可能強化這些偏見。為了幫助對抗這些不良副作用,我們提出了安全潛在擴散 (SLD)。具體來說,為了衡量由於未過濾和不平衡的訓練集導致的不當退化,我們建立了一個新穎的影像生成測試平臺——不當影像提示 (I2P)——其中包含專門的、涵蓋裸露和暴力等概念的真實世界影像到文字提示。正如我們詳盡的實證評估所證明的,引入的 SLD 在擴散過程中移除並抑制了不當影像部分,無需額外訓練,並且對整體影像質量或文字對齊沒有不利影響。

提示

使用 StableDiffusionPipelineSafesafety_concept 屬性來檢查和編輯當前的安全概念。

>>> from diffusers import StableDiffusionPipelineSafe

>>> pipeline = StableDiffusionPipelineSafe.from_pretrained("AIML-TUDA/stable-diffusion-safe")
>>> pipeline.safety_concept
'an image showing hate, harassment, violence, suffering, humiliation, harm, suicide, sexual, nudity, bodily fluids, blood, obscene gestures, illegal activity, drug use, theft, vandalism, weapons, child abuse, brutality, cruelty'

對於每個影像生成,活動概念也包含在 StableDiffusionSafePipelineOutput 中。

有 4 種配置(SafetyConfig.WEAKSafetyConfig.MEDIUMSafetyConfig.STRONGSafetyConfig.MAX)可供應用。

>>> from diffusers import StableDiffusionPipelineSafe
>>> from diffusers.pipelines.stable_diffusion_safe import SafetyConfig

>>> pipeline = StableDiffusionPipelineSafe.from_pretrained("AIML-TUDA/stable-diffusion-safe")
>>> prompt = "the four horsewomen of the apocalypse, painting by tom of finland, gaston bussiere, craig mullins, j. c. leyendecker"
>>> out = pipeline(prompt=prompt, **SafetyConfig.MAX)

務必檢視 Stable Diffusion 的 提示 部分,瞭解如何探索排程器速度和質量之間的權衡,以及如何高效地重用管道元件!

StableDiffusionPipelineSafe

diffusers.StableDiffusionPipelineSafe

< >

( vae: AutoencoderKL text_encoder: CLIPTextModel tokenizer: CLIPTokenizer unet: UNet2DConditionModel scheduler: KarrasDiffusionSchedulers safety_checker: SafeStableDiffusionSafetyChecker feature_extractor: CLIPImageProcessor image_encoder: typing.Optional[transformers.models.clip.modeling_clip.CLIPVisionModelWithProjection] = None requires_safety_checker: bool = True )

__call__

< >

( prompt: typing.Union[str, typing.List[str]] height: typing.Optional[int] = None width: typing.Optional[int] = None num_inference_steps: int = 50 guidance_scale: float = 7.5 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None 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 ip_adapter_image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor], NoneType] = 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 sld_guidance_scale: typing.Optional[float] = 1000 sld_warmup_steps: typing.Optional[int] = 10 sld_threshold: typing.Optional[float] = 0.01 sld_momentum_scale: typing.Optional[float] = 0.3 sld_mom_beta: typing.Optional[float] = 0.4 ) StableDiffusionPipelineOutputtuple

引數

  • prompt (strList[str]) — 用於引導影像生成的提示詞或提示詞列表。如果未定義,您需要傳入 prompt_embeds
  • 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) — 去噪步數。更多的去噪步數通常會產生更高質量的影像,但推理速度會變慢。
  • guidance_scale (float, 可選, 預設為 7.5) — 較高的引導尺度值鼓勵模型生成與文字 prompt 緊密相關的影像,但會犧牲影像質量。當 guidance_scale > 1 時啟用引導尺度。
  • negative_prompt (strList[str], 可選) — 用於引導影像生成中不包含的內容的提示詞或提示詞列表。如果未定義,您需要傳入 negative_prompt_embeds。當不使用引導 (guidance_scale < 1) 時,此引數將被忽略。
  • num_images_per_prompt (int, 可選, 預設為 1) — 每個提示詞要生成的影像數量。
  • eta (float, 可選, 預設為 0.0) — 對應於 DDIM 論文中的引數 eta (η)。僅適用於 DDIMScheduler,在其他排程器中被忽略。
  • generator (torch.GeneratorList[torch.Generator], 可選) — 一個 torch.Generator 用於生成確定性結果。
  • latents (torch.Tensor, 可選) — 從高斯分佈中取樣的預生成噪聲潛在變數,用作影像生成的輸入。可用於使用不同提示詞調整相同的生成。如果未提供,則使用提供的隨機 generator 進行取樣生成潛在張量。
  • ip_adapter_image — (PipelineImageInput, 可選):用於 IP 介面卡的可選影像輸入。
  • output_type (str, 可選, 預設為 "pil") — 生成影像的輸出格式。選擇 PIL.Imagenp.array
  • return_dict (bool, 可選, 預設為 True) — 是否返回 StableDiffusionPipelineOutput 而不是普通元組。
  • callback (Callable, 可選) — 在推理過程中每隔 callback_steps 步呼叫的函式。該函式以以下引數呼叫:callback(step: int, timestep: int, latents: torch.Tensor)
  • callback_steps (int, 可選, 預設為 1) — callback 函式被呼叫的頻率。如果未指定,回撥將在每一步都被呼叫。
  • sld_guidance_scale (float, 可選, 預設為 1000) — 如果 sld_guidance_scale < 1,則停用安全引導。
  • sld_warmup_steps (int, 可選, 預設為 10) — 安全指導的預熱步數。SLD 僅適用於擴散步數大於 sld_warmup_steps 的情況。
  • sld_threshold (float, 可選, 預設為 0.01) — 將合適影像和不合適影像分開的超平面閾值。
  • sld_momentum_scale (float, 可選, 預設為 0.3) — SLD 動量新增到每個擴散步驟中的安全指導的比例。如果設定為 0.0,則停用動量。在擴散步數小於 sld_warmup_steps 的預熱期間,動量會累積。
  • sld_mom_beta (float, 可選, 預設為 0.4) — 定義安全指導動量如何累積。sld_mom_beta 表示保留了多少之前的動量。在擴散步數小於 sld_warmup_steps 的預熱期間,動量會累積。

返回

StableDiffusionPipelineOutputtuple

如果 return_dictTrue,則返回 StableDiffusionPipelineOutput,否則返回一個 tuple,其中第一個元素是生成的影像列表,第二個元素是一個 bool 列表,指示相應的生成的影像是否包含“不適合工作”(nsfw)內容。

用於生成的管道的呼叫函式。

示例

import torch
from diffusers import StableDiffusionPipelineSafe
from diffusers.pipelines.stable_diffusion_safe import SafetyConfig

pipeline = StableDiffusionPipelineSafe.from_pretrained(
    "AIML-TUDA/stable-diffusion-safe", torch_dtype=torch.float16
).to("cuda")
prompt = "the four horsewomen of the apocalypse, painting by tom of finland, gaston bussiere, craig mullins, j. c. leyendecker"
image = pipeline(prompt=prompt, **SafetyConfig.MEDIUM).images[0]

StableDiffusionSafePipelineOutput

class diffusers.pipelines.stable_diffusion_safe.StableDiffusionSafePipelineOutput

< >

( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] nsfw_content_detected: typing.Optional[typing.List[bool]] unsafe_images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray, NoneType] applied_safety_concept: typing.Optional[str] )

引數

  • images (List[PIL.Image.Image]np.ndarray) — 長度為 batch_size 的去噪 PIL 影像列表或形狀為 (batch_size, height, width, num_channels) 的 numpy 陣列。PIL 影像或 numpy 陣列表示擴散管道的去噪影像。
  • nsfw_content_detected (List[bool]) — 標誌列表,表示相應的生成的影像是否可能包含“不適合工作”(nsfw)內容,如果無法執行安全檢查,則為 None
  • images (List[PIL.Image.Image]np.ndarray) — 被安全檢查器標記且可能包含“不適合工作”(nsfw)內容的去噪 PIL 影像列表,如果未執行安全檢查或未標記任何影像,則為 None
  • applied_safety_concept (str) — 用於安全指導的安全概念,如果停用安全指導,則為 None

Safe Stable Diffusion 管道的輸出類。

__call__

( *args **kwargs )

將自身作為函式呼叫。

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.