-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Add Safe Stable Diffusion Pipeline #1244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
patrickvonplaten
merged 25 commits into
huggingface:main
from
ml-research:safe-latent-diffusion
Nov 22, 2022
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
c83ef32
Add pipeline_stable_diffusion_safe.py to pipelines
manuelbrack ebea7ca
Fix repository consistency
manuelbrack 3b50c6f
Merge branch 'main' into safe-latent-diffusion
manuelbrack 4bb11f8
Add Paper/Equation reference for parameters to doc string
manuelbrack 110e647
Merge branch 'safe-latent-diffusion' of https://github.com/ml-researc…
manuelbrack 8c10f57
Ensure code style and quality
manuelbrack 5097c5c
Merge branch 'huggingface:main' into safe-latent-diffusion
manuelbrack b6f3249
Perform code refactoring
manuelbrack e34e68b
Fix copies inherited from merge with huggingface/main
manuelbrack 44a1134
Add docs
manuelbrack 3ae2ce5
Fix code style
manuelbrack f51a5a3
Fix errors in documentation
manuelbrack 60766f5
Fix refactoring error
manuelbrack ef701e6
remove debugging print statement
manuelbrack a418a95
added Safe Latent Diffusion tests
PatrickSchrML 79243b5
Fix style
manuelbrack 0d08060
Fix style
manuelbrack 66f8943
Add pre-defined safety configurations
manuelbrack 97ab342
Fix line-break
manuelbrack 8d92e75
fix some tests
patrickvonplaten 17da913
Merge branch 'main' of https://github.com/huggingface/diffusers into …
patrickvonplaten 7c7c4f1
finish
patrickvonplaten 8d3b98d
Change safety checker
manuelbrack a8fa897
Add missing safety_checker.py file
manuelbrack adda9b2
Remove unused imports
manuelbrack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| <!--Copyright 2022 The HuggingFace Team. All rights reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations under the License. | ||
| --> | ||
|
|
||
| # Safe Stable Diffusion | ||
|
|
||
| Safe Stable Diffusion was proposed in [Safe Latent Diffusion: Mitigating Inappropriate Degeneration in Diffusion Models](https://arxiv.org/abs/2211.05105) and mitigates the well known issue that models like Stable Diffusion that are trained on unfiltered, web-crawled datasets tend to suffer from inappropriate degeneration. For instance Stable Diffusion may unexpectedly generate nudity, violence, images depicting self-harm, or otherwise offensive content. | ||
| Safe Stable Diffusion is an extension to the Stable Diffusion that drastically reduces content like this. | ||
|
|
||
| The abstract of the paper is the following: | ||
|
|
||
| *Text-conditioned image generation models have recently achieved astonishing results in image quality and text alignment and are consequently employed in a fast-growing number of applications. Since they are highly data-driven, relying on billion-sized datasets randomly scraped from the internet, they also suffer, as we demonstrate, from degenerated and biased human behavior. In turn, they may even reinforce such biases. To help combat these undesired side effects, we present safe latent diffusion (SLD). Specifically, to measure the inappropriate degeneration due to unfiltered and imbalanced training sets, we establish a novel image generation test bed-inappropriate image prompts (I2P)-containing dedicated, real-world image-to-text prompts covering concepts such as nudity and violence. As our exhaustive empirical evaluation demonstrates, the introduced SLD removes and suppresses inappropriate image parts during the diffusion process, with no additional training required and no adverse effect on overall image quality or text alignment.* | ||
|
|
||
|
|
||
| *Overview*: | ||
|
|
||
| | Pipeline | Tasks | Colab | Demo | ||
| |---|---|:---:|:---:| | ||
| | [pipeline_stable_diffusion_safe.py](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion_safe/pipeline_stable_diffusion_safe.py) | *Text-to-Image Generation* | [](https://colab.research.google.com/github/ml-research/safe-latent-diffusion/blob/main/examples/Safe%20Latent%20Diffusion.ipynb) | - | ||
|
|
||
| ## Tips | ||
|
|
||
| - Safe Stable Diffusion may also be used with weights of [Stable Diffusion](./api/pipelines/stable_diffusion). | ||
|
|
||
| ### Run Safe Stable Diffusion | ||
|
|
||
| Safe Stable Diffusion can be tested very easily with the [`StableDiffusionPipelineSafe`], and the `"AIML-TUDA/stable-diffusion-safe"` checkpoint exactly in the same way it is shown in the [Conditional Image Generation Guide](./using-diffusers/conditional_image_generation). | ||
|
|
||
| ### Interacting with the Safety Concept | ||
|
|
||
| To check and edit the currently used safety concept, use the `safety_concept` property of [`StableDiffusionPipelineSafe`] | ||
| ```python | ||
| >>> from diffusers import StableDiffusionPipelineSafe | ||
|
|
||
| >>> pipeline = StableDiffusionPipelineSafe.from_pretrained("AIML-TUDA/stable-diffusion-safe") | ||
| >>> pipeline.safety_concept | ||
| ``` | ||
| For each image generation the active concept is also contained in [`StableDiffusionSafePipelineOutput`]. | ||
|
|
||
| ### Using pre-defined safety configurations | ||
|
|
||
| You may use the 4 configurations defined in the [Safe Latent Diffusion paper](https://arxiv.org/abs/2211.05105) as follows: | ||
|
|
||
| ```python | ||
| >>> 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) | ||
| ``` | ||
|
|
||
| The following configurations are available: `SafetyConfig.WEAK`, `SafetyConfig.MEDIUM`, `SafetyConfig.STRONg`, and `SafetyConfig.MAX`. | ||
|
|
||
| ### How to load and use different schedulers. | ||
|
|
||
| The safe stable diffusion pipeline uses [`PNDMScheduler`] scheduler by default. But `diffusers` provides many other schedulers that can be used with the stable diffusion pipeline such as [`DDIMScheduler`], [`LMSDiscreteScheduler`], [`EulerDiscreteScheduler`], [`EulerAncestralDiscreteScheduler`] etc. | ||
| To use a different scheduler, you can either change it via the [`ConfigMixin.from_config`] method or pass the `scheduler` argument to the `from_pretrained` method of the pipeline. For example, to use the [`EulerDiscreteScheduler`], you can do the following: | ||
|
|
||
| ```python | ||
| >>> from diffusers import StableDiffusionPipelineSafe, EulerDiscreteScheduler | ||
|
|
||
| >>> pipeline = StableDiffusionPipelineSafe.from_pretrained("AIML-TUDA/stable-diffusion-safe") | ||
| >>> pipeline.scheduler = EulerDiscreteScheduler.from_config(pipeline.scheduler.config) | ||
|
|
||
| >>> # or | ||
| >>> euler_scheduler = EulerDiscreteScheduler.from_pretrained("AIML-TUDA/stable-diffusion-safe", subfolder="scheduler") | ||
| >>> pipeline = StableDiffusionPipelineSafe.from_pretrained( | ||
| ... "AIML-TUDA/stable-diffusion-safe", scheduler=euler_scheduler | ||
| ... ) | ||
| ``` | ||
|
|
||
|
|
||
| ## StableDiffusionSafePipelineOutput | ||
| [[autodoc]] pipelines.stable_diffusion_safe.StableDiffusionSafePipelineOutput | ||
|
|
||
| ## StableDiffusionPipelineSafe | ||
| [[autodoc]] StableDiffusionPipelineSafe | ||
| - __call__ | ||
| - enable_attention_slicing | ||
| - disable_attention_slicing | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| from dataclasses import dataclass | ||
| from enum import Enum | ||
| from typing import List, Optional, Union | ||
|
|
||
| import numpy as np | ||
|
|
||
| import PIL | ||
| from PIL import Image | ||
|
|
||
| from ...utils import BaseOutput, is_torch_available, is_transformers_available | ||
|
|
||
|
|
||
| @dataclass | ||
| class SafetyConfig(object): | ||
| WEAK = { | ||
| "sld_warmup_steps": 15, | ||
| "sld_guidance_scale": 20, | ||
| "sld_threshold": 0.0, | ||
| "sld_momentum_scale": 0.0, | ||
| "sld_mom_beta": 0.0, | ||
| } | ||
| MEDIUM = { | ||
| "sld_warmup_steps": 10, | ||
| "sld_guidance_scale": 1000, | ||
| "sld_threshold": 0.01, | ||
| "sld_momentum_scale": 0.3, | ||
| "sld_mom_beta": 0.4, | ||
| } | ||
| STRONG = { | ||
| "sld_warmup_steps": 7, | ||
| "sld_guidance_scale": 2000, | ||
| "sld_threshold": 0.025, | ||
| "sld_momentum_scale": 0.5, | ||
| "sld_mom_beta": 0.7, | ||
| } | ||
| MAX = { | ||
| "sld_warmup_steps": 0, | ||
| "sld_guidance_scale": 5000, | ||
| "sld_threshold": 1.0, | ||
| "sld_momentum_scale": 0.5, | ||
| "sld_mom_beta": 0.7, | ||
| } | ||
|
|
||
|
|
||
| @dataclass | ||
| class StableDiffusionSafePipelineOutput(BaseOutput): | ||
| """ | ||
| Output class for Safe Stable Diffusion pipelines. | ||
|
|
||
| Args: | ||
| 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)`. PIL images or numpy array present the denoised images of the diffusion pipeline. | ||
| nsfw_content_detected (`List[bool]`) | ||
| List of flags denoting whether the corresponding generated image likely represents "not-safe-for-work" | ||
| (nsfw) content, or `None` if safety checking could not be performed. | ||
| images (`List[PIL.Image.Image]` or `np.ndarray`) | ||
| List of denoised PIL images that were flagged by the safety checker any may contain "not-safe-for-work" | ||
| (nsfw) content, or `None` if no safety check was performed or no images were flagged. | ||
| applied_safety_concept (`str`) | ||
| The safety concept that was applied for safety guidance, or `None` if safety guidance was disabled | ||
| """ | ||
|
|
||
| images: Union[List[PIL.Image.Image], np.ndarray] | ||
| nsfw_content_detected: Optional[List[bool]] | ||
| unsafe_images: Optional[Union[List[PIL.Image.Image], np.ndarray]] | ||
| applied_safety_concept: Optional[str] | ||
|
|
||
|
|
||
| if is_transformers_available() and is_torch_available(): | ||
| from .pipeline_stable_diffusion_safe import StableDiffusionPipelineSafe | ||
| from .safety_checker import SafeStableDiffusionSafetyChecker |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool examples!