# AIML-TUDA /stable-diffusion-safe

## You need to agree to share your contact informations to access this model

This repository is publicly accessible, but you have to accept the conditions to access its files and content.

This model is open access and available to all, with a CreativeML OpenRAIL-M license further specifying rights and usage.

1. You can't use the model to deliberately produce nor share illegal or harmful outputs or content
2. Neither AIML, nor CompVis claims rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in the license
3. You may re-distribute the weights and use the model commercially and/or as a service. If you do, please be aware you have to include the same use restrictions as the ones in the license and share a copy of the CreativeML OpenRAIL-M to all your users (please read the license entirely and carefully)

By clicking on "Access repository" below, you accept that your contact information (email address and username) can be shared with the model authors as well.

or to review the conditions and access this model content.

# Safe Stable Diffusion Model Card

Safe Stable Diffusion is a latent text-to-image diffusion model capable of generating photo-realistic images given any text input. Safe Stable Diffusion is driven by the goal of suppressing inappropriate images other large Diffusion models generate, often unexpectedly.

Safe Stable Diffusion shares weights with the Stable Diffusion v1.5. For more information please have a look at Stable Diffusion v1.5 and 🤗's Stable Diffusion blog.

Currently, you can use this with the ml-research GitHub repository and soon it will be available with the 🧨Diffusers library. Check this Pull Request for updates.

### Original GitHub Repository

2. Install the Safe Latent Diffusion Libray.

\$ pip install git+https://github.com/ml-research/safe-latent-diffusion.git

3. And load the SLD pipline as follows.

from sld import SLDPipeline
device='cuda'
#####################
# Path to your local clone of the weights
ckpt_path = ''
####################
pipe = SLDPipeline.from_pretrained(
ckpt_path,
).to(device)


### Definition of inappropriate content

What is considered inappropriate imagery may differ based on context, setting, cultural and social predisposition as well as individual factors and is overall highly subjective. In this work we base our definition of inappropriate content on the work of Gebru et al.:

[data that] if viewed directly, might be offensive, insulting, threatening, or might otherwise cause anxiety.

--Gebru, Timnit, et al. "Datasheets for datasets," (2021)

Specifically, we consider images from the following categories: hate, harassment, violence, self-harm, sexual content, shocking images, illegal activity. Note that inappropriateness is not limited to these concepts, varies between cultures, and constantly evolves.

## Model Details

• Developed by: Patrick Schramowski, Manuel Brack

• Model type: Diffusion-based text-to-image generation model with suppression of inappropriate content

• Language(s): English

• Model Description: This is a model that can be used to generate and modify images based on text prompts. It is a Latent Diffusion Model that uses a fixed, pretrained text encoder (CLIP ViT-L/14) as suggested in the Imagen paper. This model actively suppresses the generation of inappropriate content.

• Cite as:

@article{schramowski2022safe,
title={Safe Latent Diffusion: Mitigating Inappropriate Degeneration in Diffusion Models},
author={Patrick Schramowski and Manuel Brack and Björn Deiseroth and Kristian Kersting},
year={2022},
journal={arXiv preprint arXiv:2211.05105}
}


# Uses

We distinguish between the following.

## Direct Use

The model is intended for research purposes only. Possible research areas and tasks include

• Safe deployment of models which have the potential to generate harmful content.
• Probing and understanding the limitations and biases of generative models.
• Generation of artworks and use in design and other artistic processes.
• Applications in educational or creative tools.
• Research on generative models.

Excluded uses are described below.

### Misuse, Malicious Use, and Out-of-Scope Use

_Note: This section is taken from the DALLE-MINI model card, but applies in the same way to Safe Stable Diffusion.

The model should not be used to intentionally create or disseminate images that create hostile or alienating environments for people. This includes generating images that people would foreseeably find disturbing, distressing, or offensive; or content that propagates historical or current stereotypes.

#### Out-of-Scope Use

The model was not trained to be factual or true representations of people or events, and therefore using the model to generate such content is out-of-scope for the abilities of this model. Furthermore, the default safety concept only reduces the amount of content related to the aforementioned categories: hate, harassment, violence, self-harm, sexual content, shocking images, illegal activity. Mitigating otherwise inappropriate material is out of scope for this model and requires changes to the safety concept. Furthermore, the approach relies on the representations of those concepts learned by the model to identify and correct corresponding content. Cases were the model itself fails to correlate an image with our textual definition of inappropriateness are out of scope.

#### Misuse and Malicious Use

Using the model to generate content that is cruel to individuals is a misuse of this model. This includes, but is not limited to:

• Generating demeaning, dehumanizing, or otherwise harmful representations of people or their environments, cultures, religions, etc.
• Intentionally promoting or propagating discriminatory content or harmful stereotypes.
• Impersonating individuals without their consent.
• Sexual content without consent of the people who might see it.
• Mis- and disinformation
• Representations of egregious violence and gore

## Limitations and Bias

### Limitations

• The model does not achieve perfect photorealism
• The model cannot render legible text
• The model does not perform well on more difficult tasks which involve compositionality, such as rendering an image corresponding to “A red cube on top of a blue sphere”
• Faces and people in general may not be generated properly.
• The model was trained mainly with English captions and will not work as well in other languages.
• The autoencoding part of the model is lossy
• The model was trained on a large-scale dataset LAION-5B which contains adult material and is not fit for product use without additional safety mechanisms and considerations.
• No additional measures were used to deduplicate the dataset. As a result, we observe some degree of memorization for images that are duplicated in the training data. The training data can be searched at https://rom1504.github.io/clip-retrieval/ to possibly assist in the detection of memorized images.

### Bias

While the capabilities of image generation models are impressive, they can also reinforce or exacerbate social biases. Stable Diffusion v1 was trained on subsets of LAION-2B(en), which consists of images that are primarily limited to English descriptions. Texts and images from communities and cultures that use other languages are likely to be insufficiently accounted for. This affects the overall output of the model, as white and western cultures are often set as the default. Further, the ability of the model to generate content with non-English prompts is significantly worse than with English-language prompts. Additionally, the models' notion of inappropriateness and definitions of the included concepts are equally biased towards their perception in western, english

### Safety Module

The intended use of this model is with the Safety Checker in Diffusers. This checker works by checking model outputs against known hard-coded NSFW concepts. The concepts are intentionally hidden to reduce the likelihood of reverse-engineering this filter. Specifically, the checker compares the class probability of harmful concepts in the embedding space of the CLIPTextModel after generation of the images. The concepts are passed into the model with the generated image and compared to a hand-engineered weight for each NSFW concept.

## Training

Training Data The model developers used the following dataset for training the model:

• LAION-2B (en) and subsets thereof (see next section)

Training Procedure For the training procedure of the underlying Stable Diffusion v1.5 check its model card.

Safe Stable diffusion does not perform any additional training and only performs extensive evaluation of the Stable Diffusion v1.4 model.

• Hardware: A100 GPUs

## Evaluation Results

Evaluation of inappropriateness mitigation using the I2P Benchmark and the Stable Diffusion v1.4 checkpoint.

Category Stable Diffusion SLD-Weak SLD-Medium SLD-Strong SLD-Max
Hate 0.40 0.27 0.20 0.15 0.09
Harassment 0.34 0.24 0.17 0.13 0.09
Violence 0.43 0.36 0.23 0.17 0.14
Self-harm 0.40 0.27 0.16 0.10 0.07
Sexual 0.35 0.23 0.14 0.09 0.06
Shocking 0.52 0.41 0.30 0.20 0.13
Illegal activity 0.34 0.23 0.14 0.09 0.06
Overall 0.39 0.29 0.19 0.13 0.09

Table shows probabilities of generating inappropriate content on I2P prompts. SLD-Configurations refer to the following hyper-parameters:

Config sld_warmup_steps sld_guidance_scale sld_threshold sld_momentum_scale sld_mom_beta
Hyp-Weak 15 200 0.0 0.0 -
Hyp-Medium 10 1000 0.01 0.3 0.4
Hyp-Strong 7 2000 0.025 0.5 0.7
Hyp-Max 0 5000 1.0 0.5 0.7

Evaluated using 10 images for each I2G prompt.

## Environmental Impact

Safe Stable Diffusion Estimated Emissions For evaluation and development of our approach we estimate the following CO2 emissions using the Machine Learning Impact calculator presented in Lacoste et al. (2019). The hardware, runtime, and compute region were utilized to estimate the carbon impact.

• Hardware Type: A100 PCIe 40GB & A100 SXM4 80GB
• Hours used: 130
• Cloud Provider: Private Infrastructure
• Compute Region: Germany
• Carbon Emitted (Power consumption x Time x Carbon produced based on location of power grid): 20.62 kg CO2 eq.

## Citation

@article{schramowski2022safe,
title={Safe Latent Diffusion: Mitigating Inappropriate Degeneration in Diffusion Models},
author={Patrick Schramowski and Manuel Brack and Björn Deiseroth and Kristian Kersting},
year={2022},
journal={arXiv preprint arXiv:2211.05105}
}


This model card was written by: Manuel Brack and is based on the Stable Diffusion v1.5 model card.