basimazam commited on
Commit
01836d7
·
verified ·
1 Parent(s): bad5b88

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +48 -54
README.md CHANGED
@@ -1,54 +1,48 @@
1
- ---
2
- library_name: diffusers
3
- pipeline_tag: text-to-image
4
- tags:
5
- - safety
6
- - classifier-guidance
7
- - stable-diffusion
8
- - plug-and-play
9
- license: apache-2.0
10
- ---
11
-
12
- # Safe Diffusion Guidance (SDG) — plug-and-play safety layer for Stable Diffusion
13
-
14
- **Safe Diffusion Guidance (SDG)** is a *classifier-guided denoising* layer that steers the sampling trajectory away from unsafe content **without retraining** the base model.
15
- It works **standalone** with SD 1.4 / 1.5 / 2.1 and **composes** cleanly with ESD/UCE/SLD.
16
-
17
- - **Safety signal:** a 5-class mid-UNet feature classifier (classes: `gore, hate, medical, safe, sexual`) trained on (1280×8×8) features.
18
- - **Controls:** `safety_scale` (strength), `mid_fraction` (fraction of steps guided).
19
- - **Plug-in:** drop into any SD pipeline, or stack on top of ESD/UCE/SLD.
20
- - **No retraining:** small gradient nudges to latents during denoising.
21
-
22
- > **Note on metrics** (matching our paper): FID/KID are computed vs. _baseline model outputs_ rather than real images; baseline FID/KID are ≈0 by construction.
23
-
24
- ## Quickstart (SD 1.5)
25
-
26
- ```python
27
- import torch
28
- from diffusers import StableDiffusionPipeline
29
-
30
- # 1) Load base SD pipeline (disable default safety checker)
31
- base = StableDiffusionPipeline.from_pretrained(
32
- "runwayml/stable-diffusion-v1-5",
33
- torch_dtype=torch.float16,
34
- safety_checker=None
35
- ).to("cuda")
36
-
37
- # 2) Load SDG custom pipeline from Hub (this repo)
38
- sdg = StableDiffusionPipeline.from_pretrained(
39
- "your-org/safe-diffusion-guidance",
40
- custom_pipeline="safe_diffusion_guidance",
41
- torch_dtype=torch.float16
42
- ).to("cuda")
43
-
44
- img = sdg(
45
- base_pipe=base,
46
- prompt="portrait photograph, studio light, 85mm, realistic",
47
- num_inference_steps=50,
48
- guidance_scale=7.5,
49
- safety_scale=5.0, # strength: ~2–8 (Light→Strong)
50
- mid_fraction=1.0, # guide fraction of steps: 0.5, 0.8, 1.0
51
- safe_class_index=3 # index of 'safe' in [gore,hate,medical,safe,sexual]
52
- ).images[0]
53
-
54
- img.save("sdg_safe_output.png")
 
1
+ ---
2
+ library_name: diffusers
3
+ pipeline_tag: text-to-image
4
+ tags:
5
+ - safety
6
+ - classifier-guidance
7
+ - stable-diffusion
8
+ - plug-and-play
9
+ license: apache-2.0
10
+ ---
11
+
12
+ # Safe Diffusion Guidance (SDG) —
13
+
14
+ **Safe Diffusion Guidance (SDG)** is a *classifier-guided denoising* layer that steers the sampling trajectory away from unsafe content **without retraining** the base model.
15
+ It works **standalone** with SD 1.4 / 1.5 / 2.1.
16
+
17
+
18
+ ## Quickstart (SD 1.5)
19
+
20
+ ```python
21
+ import torch
22
+ from diffusers import StableDiffusionPipeline
23
+
24
+ # 1) Load base SD pipeline (disable default safety checker)
25
+ base = StableDiffusionPipeline.from_pretrained(
26
+ "runwayml/stable-diffusion-v1-5",
27
+ torch_dtype=torch.float16,
28
+ safety_checker=None
29
+ ).to("cuda")
30
+
31
+ # 2) Load SDG custom pipeline from Hub (this repo)
32
+ sdg = StableDiffusionPipeline.from_pretrained(
33
+ "basimazam/safe-diffusion-guidance",
34
+ custom_pipeline="safe_diffusion_guidance",
35
+ torch_dtype=torch.float16
36
+ ).to("cuda")
37
+
38
+ img = sdg(
39
+ base_pipe=base,
40
+ prompt="portrait photograph, studio light, 85mm, realistic",
41
+ num_inference_steps=50,
42
+ guidance_scale=7.5,
43
+ safety_scale=5.0,
44
+ mid_fraction=1.0,
45
+ safe_class_index=3
46
+ ).images[0]
47
+
48
+ img.save("sdg_safe_output.png")