Text-to-Image
Transformers
Safetensors
clip_text_model
feature-extraction
safety-alignment
stable-diffusion
ECCV
Instructions to use Adeely93/SAGE with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Adeely93/SAGE with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModel tokenizer = AutoTokenizer.from_pretrained("Adeely93/SAGE") model = AutoModel.from_pretrained("Adeely93/SAGE", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Add text encoder in transformers format (config.json + model.safetensors) and update the model card
Browse files- README.md +17 -27
- config.json +25 -0
- model.safetensors +3 -0
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
library_name:
|
| 3 |
license: mit
|
| 4 |
pipeline_tag: text-to-image
|
| 5 |
base_model: CompVis/stable-diffusion-v1-4
|
|
@@ -32,47 +32,37 @@ We show that existing T2I safety alignment methods create an **illusion of high
|
|
| 32 |
|
| 33 |
## What this repository contains
|
| 34 |
|
| 35 |
-
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
| Key | Content |
|
| 40 |
|---|---|
|
| 41 |
-
| `
|
| 42 |
-
| `
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
|
| 46 |
---
|
| 47 |
|
| 48 |
## Use this model
|
| 49 |
|
| 50 |
-
### Option 1: with 馃 diffusers (
|
| 51 |
|
| 52 |
```python
|
| 53 |
import torch
|
| 54 |
from diffusers import StableDiffusionPipeline
|
| 55 |
-
from
|
| 56 |
-
|
| 57 |
-
device = "cuda"
|
| 58 |
-
|
| 59 |
-
# 1) Load stock Stable Diffusion v1.4
|
| 60 |
-
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16).to(device)
|
| 61 |
-
|
| 62 |
-
# 2) Download SAGE.pt and take the text-encoder weights out of it
|
| 63 |
-
ckpt_path = hf_hub_download(repo_id="Adeely93/SAGE", filename="SAGE.pt")
|
| 64 |
-
state = torch.load(ckpt_path, map_location="cpu", weights_only=False)["model_state_dict"]
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
model_keys = set(pipe.text_encoder.state_dict().keys())
|
| 69 |
-
state = {(k if k in model_keys else k[len("text_model."):]): v for k, v in state.items()}
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
|
| 75 |
-
#
|
| 76 |
image = pipe("A horned owl with a graduation cap and diploma.").images[0]
|
| 77 |
image.save("owl.png")
|
| 78 |
```
|
|
|
|
| 1 |
---
|
| 2 |
+
library_name: transformers
|
| 3 |
license: mit
|
| 4 |
pipeline_tag: text-to-image
|
| 5 |
base_model: CompVis/stable-diffusion-v1-4
|
|
|
|
| 32 |
|
| 33 |
## What this repository contains
|
| 34 |
|
| 35 |
+
SAGE fine-tunes **only the text encoder** of Stable Diffusion v1.4 (CLIP ViT-L/14). The UNet, VAE, tokenizer and scheduler are the unchanged, stock SD v1.4 components, so using SAGE means loading SD v1.4 as usual with this text encoder plugged in. There is no inference overhead.
|
| 36 |
|
| 37 |
+
| File | What it is |
|
|
|
|
|
|
|
| 38 |
|---|---|
|
| 39 |
+
| `config.json` + `model.safetensors` | the SAGE text encoder in standard 馃 transformers format, loadable with `CLIPTextModel.from_pretrained("Adeely93/SAGE")` |
|
| 40 |
+
| `SAGE.pt` | the same weights as a raw PyTorch checkpoint (`{"model_state_dict": ..., "args": ...}`, the DES format), used by the official code and the Colab demo |
|
| 41 |
+
|
| 42 |
+
Training setup: SD v1.4, CoPro sexual safe/unsafe prompts, 2 epochs, lr 1e-5, batch 128, 位_safe 0.3, concept scale 205, K 15, 伪 1.0, 位_LSA 0.1, 位_ESP 2.0.
|
| 43 |
|
| 44 |
+
The weights are a CLIP ViT-L/14 text encoder (768-dim, 77 tokens), so they work with Stable Diffusion **v1.x** pipelines (v1.4 and v1.5 share this encoder). They do not apply to SD 2.x, SDXL, SD 3 or FLUX.
|
| 45 |
|
| 46 |
---
|
| 47 |
|
| 48 |
## Use this model
|
| 49 |
|
| 50 |
+
### Option 1: with 馃 diffusers (recommended)
|
| 51 |
|
| 52 |
```python
|
| 53 |
import torch
|
| 54 |
from diffusers import StableDiffusionPipeline
|
| 55 |
+
from transformers import CLIPTextModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
# 1) Load the SAGE text encoder
|
| 58 |
+
text_encoder = CLIPTextModel.from_pretrained("Adeely93/SAGE", torch_dtype=torch.float16)
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
# 2) Load stock Stable Diffusion v1.4 with that text encoder plugged in
|
| 61 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 62 |
+
"CompVis/stable-diffusion-v1-4", text_encoder=text_encoder, torch_dtype=torch.float16
|
| 63 |
+
).to("cuda")
|
| 64 |
|
| 65 |
+
# 3) Generate as usual: the pipeline is now safety-aligned
|
| 66 |
image = pipe("A horned owl with a graduation cap and diploma.").images[0]
|
| 67 |
image.save("owl.png")
|
| 68 |
```
|
config.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "CompVis/stable-diffusion-v1-4",
|
| 3 |
+
"architectures": [
|
| 4 |
+
"CLIPTextModel"
|
| 5 |
+
],
|
| 6 |
+
"attention_dropout": 0.0,
|
| 7 |
+
"bos_token_id": 0,
|
| 8 |
+
"dropout": 0.0,
|
| 9 |
+
"eos_token_id": 2,
|
| 10 |
+
"hidden_act": "quick_gelu",
|
| 11 |
+
"hidden_size": 768,
|
| 12 |
+
"initializer_factor": 1.0,
|
| 13 |
+
"initializer_range": 0.02,
|
| 14 |
+
"intermediate_size": 3072,
|
| 15 |
+
"layer_norm_eps": 1e-05,
|
| 16 |
+
"max_position_embeddings": 77,
|
| 17 |
+
"model_type": "clip_text_model",
|
| 18 |
+
"num_attention_heads": 12,
|
| 19 |
+
"num_hidden_layers": 12,
|
| 20 |
+
"pad_token_id": 1,
|
| 21 |
+
"projection_dim": 512,
|
| 22 |
+
"torch_dtype": "float32",
|
| 23 |
+
"transformers_version": "4.46.3",
|
| 24 |
+
"vocab_size": 49408
|
| 25 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2d3b4629e9fc006473b6e7c991c04dca2fa221d80c1283dcc625585ccbe201ec
|
| 3 |
+
size 492265168
|