IMvision12 commited on
Commit
2546b6e
·
verified ·
1 Parent(s): 0015356

Add stable-diffusion-2: zeromodels Keras 3 conversion of sd2-community/stable-diffusion-2

Browse files
Files changed (4) hide show
  1. README.md +109 -0
  2. model.weights.h5 +3 -0
  3. tokenizer.json +0 -0
  4. zm_config.json +99 -0
README.md ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ pipeline_tag: text-to-image
3
+ license: openrail++
4
+ base_model: sd2-community/stable-diffusion-2
5
+ library_name: zeromodels
6
+ language:
7
+ - en
8
+ tags:
9
+ - keras
10
+ - zeromodels
11
+ - stable-diffusion
12
+ - stable-diffusion-diffusers
13
+ - text-to-image
14
+ - diffusion
15
+ - latent-diffusion
16
+ - arxiv:2112.10752
17
+ - pytorch
18
+ - jax
19
+ - tf
20
+ ---
21
+ *See [our collection](https://huggingface.co/collections/zeromodels/stable-diffusion-v2-6aa7906bed026a6b11f4e7be) for all Stable Diffusion 2.x checkpoints.*
22
+
23
+ # Run Stable Diffusion 2.x with Keras 3: JAX, PyTorch, or TensorFlow
24
+
25
+ [![GitHub](https://img.shields.io/badge/GitHub-ZeroModels-181717?logo=github)](https://github.com/IMvision12/ZeroModels) [![Docs](https://img.shields.io/badge/Docs-Stable_Diffusion_2.x-1f6feb)](https://imvision12.github.io/ZeroModels/stable_diffusion_2/) [![HuggingFace](https://img.shields.io/badge/HuggingFace-Stable_Diffusion_2.x-ffd21e?logo=huggingface&logoColor=black)](https://huggingface.co/collections/zeromodels/stable-diffusion-v2-6aa7906bed026a6b11f4e7be)
26
+
27
+ # zeromodels/stable-diffusion-2
28
+
29
+ Paper: [High-Resolution Image Synthesis with Latent Diffusion Models (arXiv:2112.10752)](https://arxiv.org/abs/2112.10752) | [HF Papers](https://huggingface.co/papers/2112.10752)
30
+
31
+ Pure-**Keras 3** conversion of [`sd2-community/stable-diffusion-2`](https://huggingface.co/sd2-community/stable-diffusion-2) for
32
+ [zeromodels](https://github.com/IMvision12/ZeroModels). One implementation runs unmodified on
33
+ **TensorFlow / Torch / JAX**. The whole text-to-image model ships as **one container**:
34
+ the UNet denoiser, the VAE and the OpenCLIP ViT-H/14 text encoder (penultimate layer) in `model.weights.h5`
35
+ (1.29B parameters, 4.81 GB), plus `zm_config.json` (the three
36
+ component configs, the checkpoint's `DDIMScheduler` schedule with its `v_prediction` objective
37
+ and the default generation settings) and the tokenizer as `tokenizer.json`. Weights are stored in **float32**, exactly as released.
38
+ This checkpoint generates **768x768** images (a 96x96 latent).
39
+
40
+ For model details, intended use and limitations, see the upstream
41
+ [model card](https://huggingface.co/sd2-community/stable-diffusion-2).
42
+
43
+ ## Architecture
44
+
45
+ | Component | zeromodels class | Details |
46
+ | --- | --- | --- |
47
+ | Denoiser | `UNet2DConditionModel` | (320, 640, 1280, 1280) channels, 2 ResNet blocks per level, (5, 10, 20, 20) attention heads on the 1024-d text context, linear token projection, 96x96x4 latent |
48
+ | Autoencoder | `AutoencoderKL` | (128, 256, 512, 512) channels, x8 spatial compression to 4 latent channels, `scaling_factor` 0.18215 |
49
+ | Text encoder | `CLIPTextModel` | OpenCLIP ViT-H/14 text encoder (penultimate layer): 1024-d, 23 layers, 16 heads, 77 tokens, `gelu` |
50
+ | Scheduler | `DDIMScheduler` | scaled_linear betas 0.00085 to 0.012 over 1000 steps, `v_prediction`; DDIM / PNDM / Euler / Euler-ancestral are drop-in |
51
+
52
+ ## Quick start
53
+
54
+ ```python
55
+ import os
56
+ os.environ["KERAS_BACKEND"] = "torch" # or "jax" / "tensorflow"
57
+
58
+ from PIL import Image
59
+ from zeromodels.models.stable_diffusion_2 import StableDiffusion2TextToImage, StableDiffusion2Tokenizer
60
+
61
+ model = StableDiffusion2TextToImage.from_weights("zeromodels/stable-diffusion-2")
62
+ tokenizer = StableDiffusion2Tokenizer.from_weights("zeromodels/stable-diffusion-2")
63
+
64
+ inputs = tokenizer("a photograph of an astronaut riding a horse")
65
+ images = model.generate(**inputs, num_inference_steps=50, guidance_scale=7.5, seed=0)
66
+ Image.fromarray(images[0]).save("astronaut.png") # (768, 768, 3) uint8
67
+ ```
68
+
69
+ `generate` takes the tokenizer's `input_ids` (batch them for several prompts), an optional
70
+ `negative_input_ids` (tokenize the negative prompt), `num_inference_steps`, `guidance_scale`,
71
+ a `seed`, or explicit `latents` of shape `(batch, 96, 96, 4)` for results that are
72
+ identical across backends.
73
+
74
+ Load any Stable Diffusion 2.x checkpoint the same way with `from_weights("zeromodels/<variant>")`:
75
+
76
+ | Variant | Hub | Training |
77
+ | --- | --- | --- |
78
+ | `stable-diffusion-2-base` | [zeromodels/stable-diffusion-2-base](https://huggingface.co/zeromodels/stable-diffusion-2-base) | 512px, epsilon: from scratch, 550k steps at 256px on LAION-5B (aesthetics >= 4.5), then 850k steps at 512px |
79
+ | `stable-diffusion-2` | [zeromodels/stable-diffusion-2](https://huggingface.co/zeromodels/stable-diffusion-2) | 768px, v-prediction: 2-base + 150k steps at 768px |
80
+ | `stable-diffusion-2-1-base` | [zeromodels/stable-diffusion-2-1-base](https://huggingface.co/zeromodels/stable-diffusion-2-1-base) | 512px, epsilon: 2-base + 220k steps at 512px (punsafe 0.98) |
81
+ | `stable-diffusion-2-1` | [zeromodels/stable-diffusion-2-1](https://huggingface.co/zeromodels/stable-diffusion-2-1) | 768px, v-prediction: 2 + 55k steps (punsafe 0.1) + 155k steps (punsafe 0.98) at 768px |
82
+ | `sd-turbo` | [zeromodels/sd-turbo](https://huggingface.co/zeromodels/sd-turbo) | 512px, epsilon, Euler (trailing spacing), 1 to 4 steps, no guidance: SD 2.1 distilled with Adversarial Diffusion Distillation (non-commercial research license) |
83
+
84
+ ## Tips
85
+
86
+ - Set `KERAS_BACKEND` **before** importing Keras / zeromodels.
87
+ - The graphs are built for 768px. Pass `unet_sample_size=<px / 8>, vae_sample_size=<px>` to
88
+ `from_weights` to build for another multiple of 64px (the weights are resolution-independent).
89
+ - Swap the sampler any time: `model.scheduler = EulerDiscreteScheduler.from_config(model.config.scheduler_config)`
90
+ (`zeromodels.base.base_scheduler`).
91
+ - `StableDiffusion2Model.from_weights(...)` loads the same repo as the bare container
92
+ (UNet / VAE / text encoder as `.unet` / `.vae` / `.text_encoder`) without the generation loop.
93
+
94
+ - Both `channels_last` and `channels_first` are supported (`keras.config.set_image_data_format`
95
+ before loading); `generate` always returns `(batch, H, W, 3)` uint8.
96
+ - On-the-fly `hf:` conversion is not supported for diffusion models; the checkpoints are
97
+ hosted here, converted once.
98
+ - See the [Stable Diffusion 2.x docs](https://imvision12.github.io/ZeroModels/stable_diffusion_2/).
99
+
100
+ ## License
101
+
102
+ The weights are redistributed under the
103
+ [CreativeML Open RAIL++-M](https://huggingface.co/sd2-community/stable-diffusion-2-1/blob/main/LICENSE-MODEL) of the upstream checkpoint,
104
+ including its use-based restrictions. By using them you agree to those terms.
105
+
106
+ ## Special Thanks
107
+
108
+ Thank you to Stability AI and the LAION / OpenCLIP teams for training and releasing Stable Diffusion, and to the
109
+ Hugging Face diffusers team, whose implementation this port was verified against.
model.weights.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7269f7c0c0b857f9bfafac7547b22a9fe73ed26131c802808a56b1fe618c9050
3
+ size 5162332160
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
zm_config.json ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "library_name": "zeromodels",
3
+ "zeromodels_version": "1.3.3",
4
+ "model_module": "zeromodels.models.stable_diffusion_2",
5
+ "model_class": "StableDiffusion2Model",
6
+ "variant": "stable-diffusion-2",
7
+ "weights": "model.weights.h5",
8
+ "schema_version": 2,
9
+ "weight_dtype": "float32",
10
+ "model_type": "stable_diffusion_2",
11
+ "unet_config": {
12
+ "sample_size": 96,
13
+ "in_channels": 4,
14
+ "out_channels": 4,
15
+ "down_block_types": [
16
+ "CrossAttnDownBlock2D",
17
+ "CrossAttnDownBlock2D",
18
+ "CrossAttnDownBlock2D",
19
+ "DownBlock2D"
20
+ ],
21
+ "up_block_types": [
22
+ "UpBlock2D",
23
+ "CrossAttnUpBlock2D",
24
+ "CrossAttnUpBlock2D",
25
+ "CrossAttnUpBlock2D"
26
+ ],
27
+ "block_out_channels": [
28
+ 320,
29
+ 640,
30
+ 1280,
31
+ 1280
32
+ ],
33
+ "layers_per_block": 2,
34
+ "cross_attention_dim": 1024,
35
+ "num_attention_heads": [
36
+ 5,
37
+ 10,
38
+ 20,
39
+ 20
40
+ ],
41
+ "norm_num_groups": 32,
42
+ "use_linear_projection": true,
43
+ "transformer_layers_per_block": 1,
44
+ "addition_embed_type": null,
45
+ "addition_time_embed_dim": 256,
46
+ "projection_class_embeddings_input_dim": null,
47
+ "num_time_ids": 6,
48
+ "text_seq_len": 77
49
+ },
50
+ "vae_config": {
51
+ "in_channels": 3,
52
+ "out_channels": 3,
53
+ "latent_channels": 4,
54
+ "block_out_channels": [
55
+ 128,
56
+ 256,
57
+ 512,
58
+ 512
59
+ ],
60
+ "layers_per_block": 2,
61
+ "norm_num_groups": 32,
62
+ "sample_size": 768,
63
+ "scaling_factor": 0.18215,
64
+ "force_upcast": false,
65
+ "shift_factor": 0.0,
66
+ "use_quant_conv": true,
67
+ "use_post_quant_conv": true
68
+ },
69
+ "text_config": {
70
+ "hidden_dim": 1024,
71
+ "num_heads": 16,
72
+ "num_layers": 23,
73
+ "mlp_ratio": 4.0,
74
+ "vocab_size": 49408,
75
+ "max_seq_len": 77
76
+ },
77
+ "hidden_act": "gelu",
78
+ "layer_norm_eps": 1e-05,
79
+ "bos_token_id": 49406,
80
+ "eos_token_id": 49407,
81
+ "pad_token_id": 0,
82
+ "scheduler_config": {
83
+ "_class_name": "DDIMScheduler",
84
+ "beta_end": 0.012,
85
+ "beta_schedule": "scaled_linear",
86
+ "beta_start": 0.00085,
87
+ "clip_sample": false,
88
+ "num_train_timesteps": 1000,
89
+ "prediction_type": "v_prediction",
90
+ "set_alpha_to_one": false,
91
+ "skip_prk_steps": true,
92
+ "steps_offset": 1,
93
+ "trained_betas": null
94
+ },
95
+ "generate_args": {
96
+ "num_inference_steps": 50,
97
+ "guidance_scale": 7.5
98
+ }
99
+ }