File size: 10,420 Bytes
923499f
 
 
 
 
264905e
 
 
923499f
b8ea55d
923499f
264905e
 
 
 
1fb6129
264905e
 
 
 
deb4641
 
264905e
 
 
 
 
 
 
 
 
 
 
 
 
cd47cca
f99f763
 
97fb4ba
 
f99f763
 
 
 
 
 
 
ce21df3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adbea7e
ce21df3
 
 
 
f99f763
cd47cca
 
 
 
 
 
 
 
 
 
 
06e9a10
cd47cca
 
 
 
 
 
 
 
 
 
 
 
 
06e9a10
cd47cca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06e9a10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd47cca
06e9a10
cd47cca
 
 
06e9a10
cd47cca
 
 
 
 
06e9a10
cd47cca
 
 
 
 
 
 
 
 
06e9a10
cd47cca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
721f70c
 
5cafa9f
721f70c
5cafa9f
f598b29
721f70c
 
 
 
 
 
 
 
 
 
 
 
 
5cafa9f
721f70c
5cafa9f
f598b29
721f70c
 
 
 
 
 
 
 
 
 
 
 
264905e
f598b29
264905e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9c463c
 
264905e
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
---
pipeline_tag: text-to-image
license: other
license_name: faipl-1.0-sd
license_link: LICENSE
tags:
- text-to-image
- diffusers
prior:
- Disty0/sotediffusion-v2-prior
---


# SoteDiffusion V2

An anime finetune of Würstchen V3 / Stable Cascade. 

# Release Notes

- This release is sponsored by <a href="https://fal.ai/grants?rel=sote-diffusion" target="_blank">fal.ai/grants</a>  
- Trained on 12M text & image paris including WD tags and natural language captions for a single epoch on 8xH100 80GB SXM5 GPUs.  
- Trained with Full FP32 and MAE Loss.  

<style>
.image {
    float: left;
    margin-left: 10px;
}
</style>

<table>
<img class="image" src="https://cdn-uploads.huggingface.co/production/uploads/6456af6195082f722d178522/KJTHqR3otoKoiXxvbudp8.png" width="320">
<img class="image" src="https://cdn-uploads.huggingface.co/production/uploads/6456af6195082f722d178522/uua4L9aaqJ0LI8gYv4xmC.png" width="320">
</table>


# ComfyUI

Use these arguments when starting ComfyUI: `--fp16-vae --fp16-unet`  

Download the Stage C to unet folder: https://huggingface.co/Disty0/sotediffusion-v2/resolve/main/sotediffusion-v2-stage_c.safetensors  
Download the Stage C Text Encoder to clip folder: https://huggingface.co/Disty0/sotediffusion-v2/resolve/main/sotediffusion-v2-stage_c_text_encoder.safetensors  
Download the Stage B to unet folder: https://huggingface.co/Disty0/sotediffusion-v2/resolve/main/sotediffusion-v2-stage_b.safetensors  
Download the Stage A to vae folder: https://huggingface.co/Disty0/sotediffusion-v2/resolve/main/stage_a_ft_hq.safetensors  

Download the workflow and load it: https://huggingface.co/Disty0/sotediffusion-v2/resolve/main/comfyui_workflow.json  

## SD.Next
URL: https://github.com/vladmandic/automatic/

Go to Models -> Huggingface and type `Disty0/sotediffusion-v2` into the model name and press download.  
Load `Disty0/sotediffusion-v2` after the download process is complete.  

Prompt:  
```
your prompt goes here
very aesthetic, best quality, newest,
```
(New lines act the same way as BREAK in SD.Next)  

Negative Prompt:  
```
very displeasing, displeasing, worst quality, bad quality, low quality, realistic, monochrome, comic, sketch, oldest, early, artist name, signature, blurry, simple background, upside down,
```

Parameters:  
Sampler: Default  

Steps: 30 or 40  
Refiner Steps: 10  

CFG: 7  
Secondary CFG: 1.5 or 1  

Resolution: 1024x1536, 2048x1152  
Anything works as long as it's a multiply of 128.  

# Diffusers

```shell
pip install diffusers
```

```python
import torch
import diffusers

device = "cuda"
dtype = torch.float16
model_path = "/mnt/DataSSD/AI/SoteDiffusion/Wuerstchen3/diffusers/sotediffusion-v2"


def get_timestep_ratio_conditioning(t, alphas_cumprod):
    s = torch.tensor([0.008]) # diffusers uses 0.003 while the original is 0.008
    clamp_range = [0, 1]
    min_var = torch.cos(s / (1 + s) * torch.pi * 0.5) ** 2
    var = alphas_cumprod[t]
    var = var.clamp(*clamp_range)
    s, min_var = s.to(var.device), min_var.to(var.device)
    ratio = (((var * min_var) ** 0.5).acos() / (torch.pi * 0.5)) * (1 + s) - s
    return ratio


pipe = diffusers.AutoPipelineForText2Image.from_pretrained(model_path, text_encoder=None, torch_dtype=dtype)
pipe.prior_pipe.get_timestep_ratio_conditioning = get_timestep_ratio_conditioning
pipe.prior_pipe.scheduler.config.clip_sample = False

# de-dupe
pipe.decoder_pipe.text_encoder = pipe.text_encoder = None # nothing uses this
del pipe.decoder_pipe.text_encoder
del pipe.prior_prior
del pipe.prior_text_encoder
del pipe.prior_tokenizer
del pipe.prior_scheduler
del pipe.prior_feature_extractor
del pipe.prior_image_encoder

pipe = pipe.to(device, dtype=dtype)
pipe.prior_pipe = pipe.prior_pipe.to(device, dtype=dtype)


def encode_empty_prompt(
    prior_pipe,
    device,
    batch_size,
    num_images_per_prompt,
    ):

    text_inputs = prior_pipe.tokenizer(
        "",
        padding="max_length",
        max_length=prior_pipe.tokenizer.model_max_length,
        truncation=True,
        return_tensors="pt",
    )

    # Don't use attention mask for empty prompt
    text_encoder_output = prior_pipe.text_encoder(
        text_inputs.input_ids.to(device), attention_mask=None, output_hidden_states=True
    )
    prompt_embeds = text_encoder_output.hidden_states[-1]
    prompt_embeds = prompt_embeds.to(dtype=prior_pipe.text_encoder.dtype, device=device)
    prompt_embeds = prompt_embeds.repeat_interleave(num_images_per_prompt, dim=0)
    return prompt_embeds


prompt = "1girl, solo, looking at viewer, open mouth, blue eyes, medium breasts, blonde hair, gloves, dress, bow, hair between eyes, bare shoulders, upper body, hair bow, indoors, elbow gloves, hand on own chest, bridal gauntlets, candlestand, smile, rim lighting, from side, castle interior, looking side,"
quality_prompt = "extremely aesthetic, best quality, newest"
negative_prompt = "very displeasing, displeasing, worst quality, bad quality, low quality, realistic, monochrome, comic, sketch, oldest, early, artist name, signature, blurry, simple background, upside down,"
num_images_per_prompt=1

# Encode prompts and quality prompts eperately:
# device, batch_size, num_images_per_prompt, cfg, prompt
prompt_embeds, prompt_embeds_pooled, _, _ = pipe.prior_pipe.encode_prompt(device, 1, num_images_per_prompt, False, prompt=prompt)
quality_prompt_embeds, _, _, _ = pipe.prior_pipe.encode_prompt(device, 1, num_images_per_prompt, False, prompt=quality_prompt)

negative_prompt_embeds, negative_prompt_embeds_pooled, _, _ = pipe.prior_pipe.encode_prompt(device, 1, num_images_per_prompt, False, prompt=negative_prompt)
empty_prompt_embeds = encode_empty_prompt(pipe.prior_pipe, device, 1, num_images_per_prompt)

prompt_embeds = torch.cat([prompt_embeds, quality_prompt_embeds], dim=1)
negative_prompt_embeds = torch.cat([negative_prompt_embeds, empty_prompt_embeds], dim=1)

pipe.prior_pipe.maybe_free_model_hooks()

output = pipe(
    width=1024,
    height=1536,
    decoder_guidance_scale=1.2,
    prior_guidance_scale=7.0,
    prior_num_inference_steps=30,
    num_inference_steps=10,
    output_type="pil",
    prompt=prompt + " " + quality_prompt,
    negative_prompt=negative_prompt,
    prompt_embeds=prompt_embeds,
    prompt_embeds_pooled=prompt_embeds_pooled,
    negative_prompt_embeds=negative_prompt_embeds,
    negative_prompt_embeds_pooled=negative_prompt_embeds_pooled,
    num_images_per_prompt=num_images_per_prompt,
).images[0]

display(output)
```

## Training:


### Stage C
**Base model**: Disty0/sotediffusion-wuerstchen3
**GPU used**: 7x Nvidia H100 80GB SXM5   
| parameter | value |
|---|---|
| **amp** | no |
| **weights** | fp32 |
| **save weights** | fp32 |
| **resolution** | 1024x1024 |
| **effective batch size** | 84 |
| **unet learning rate** | 2e-6 |
| **te learning rate** | 1e-7 |
| **optimizer** | AdamW 8bit |
| **images** | 6M * 2 captions per image |
| **epochs** | 1 |


### Stage B
**Base model**: Disty0/sotediffusion-wuerstchen3-decoder
**GPU used**: 1x Nvidia H100 80GB SXM5 
| parameter | value |
|---|---|
| **amp** | no |
| **weights** | fp32 |
| **save weights** | fp32 |
| **resolution** | 1024x1024 |
| **effective batch size** | 8 |
| **unet learning rate** |  8e-6 |
| **te learning rate** | none |
| **optimizer** | AdamW |
| **images** | 120K |
| **epochs** | 6 |


## WD Tags:

Model is trained with this tag order:  
```
aesthetic tags, quality tags, date tags, custom tags, rating tags, character, series, rest of the tags
```

### Date:

| tag | date |
|---|---|
| **newest** | 2022 to 2024 |
| **recent** | 2019 to 2021 |
| **mid** | 2015 to 2018 |
| **early** | 2011 to 2014 |
| **oldest** | 2005 to 2010 |

### Aesthetic Tags:
**Model used**: shadowlilac/aesthetic-shadow-v2

| score greater than | tag | count |
|---|---|---|
| **0.90** | extremely aesthetic | 125.451 |
| **0.80** | very aesthetic | 887.382 |
| **0.70** | aesthetic | 1.049.857 |
| **0.50** | slightly aesthetic | 1.643.091 |
| **0.40** | not displeasing | 569.543 |
| **0.30** | not aesthetic | 445.188 |
| **0.20** | slightly displeasing | 341.424 |
| **0.10** | displeasing | 237.660 |
| **rest of them** | very displeasing | 328.712 |

### Quality Tags:
**Model used**: https://huggingface.co/hakurei/waifu-diffusion-v1-4/blob/main/models/aes-B32-v0.pth

| score greater than | tag | count |
|---|---|---|
| **0.980** | best quality | 1.270.447 |
| **0.900** | high quality | 498.244 |
| **0.750** | great quality | 351.006 |
| **0.500** | medium quality | 366.448 |
| **0.250** | normal quality | 368.380 |
| **0.125** | bad quality | 279.050 |
| **0.025** | low quality | 538.958 |
| **rest of them** | worst quality | 1.955.966 |

## Rating Tags:

| tag | count |
|---|---|
| **general** | 1.416.451 |
| **sensitive** | 3.447.664 |
| **nsfw** | 427.459 |
| **explicit nsfw** | 336.925 |

## Custom Tags:

| dataset name | custom tag |
|---|---|
| **image boards** | date, |
| **text** | The text says "text", |
| **characters** | character, series
| **pixiv** | art by Display_Name, |
| **visual novel cg** | Full_VN_Name (short_3_letter_name), visual novel cg, |
| **anime wallpaper** | date, anime wallpaper, |


## Limitations and Bias

### Bias

- This model is intended for anime illustrations.  
  Realistic capabilites are not tested at all.  

### Limitations

- Can fall back to realistic.  
  Add "realistic" tag to the negatives when this happens.  
- Far shot eyes and hands can be bad.  
- Still has a lot more room for more training.  


## License

SoteDiffusion models falls under [Fair AI Public License 1.0-SD](https://freedevproject.org/faipl-1.0-sd/) license, which is compatible with Stable Diffusion models’ license. Key points:

1. **Modification Sharing:** If you modify SoteDiffusion models, you must share both your changes and the original license.
2. **Source Code Accessibility:** If your modified version is network-accessible, provide a way (like a download link) for others to get the source code. This applies to derived models too.
3. **Distribution Terms:** Any distribution must be under this license or another with similar rules.
4. **Compliance:** Non-compliance must be fixed within 30 days to avoid license termination, emphasizing transparency and adherence to open-source values.

**Notes**: Anything not covered by Fair AI license is inherited from Stability AI Non-Commercial license which is named as LICENSE_INHERIT.