IMvision12 commited on
Commit
cd3787d
·
0 Parent(s):

stable-diffusion-v1-1: zeromodels Keras 3 conversion of CompVis/stable-diffusion-v1-1

Browse files
Files changed (5) hide show
  1. .gitattributes +35 -0
  2. README.md +109 -0
  3. model.weights.h5 +3 -0
  4. tokenizer.json +0 -0
  5. zm_config.json +92 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ pipeline_tag: text-to-image
3
+ license: creativeml-openrail-m
4
+ base_model: CompVis/stable-diffusion-v1-1
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/zeromodels) for all Stable Diffusion 1.x checkpoints.*
22
+
23
+ # Run Stable Diffusion 1.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_1.x-1f6feb)](https://imvision12.github.io/ZeroModels/stable_diffusion/) [![HuggingFace](https://img.shields.io/badge/HuggingFace-Stable_Diffusion_1.x-ffd21e?logo=huggingface&logoColor=black)](https://huggingface.co/zeromodels)
26
+
27
+ # zeromodels/stable-diffusion-v1-1
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 [`CompVis/stable-diffusion-v1-1`](https://huggingface.co/CompVis/stable-diffusion-v1-1) 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 CLIP ViT-L/14 text encoder in `model.weights.h5`
35
+ (1.07B parameters, 3.97 GB), plus `zm_config.json` (the three
36
+ component configs, the checkpoint's `PNDMScheduler` schedule with its `epsilon` 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 **512x512** images (a 64x64 latent).
39
+
40
+ For model details, intended use and limitations, see the upstream
41
+ [model card](https://huggingface.co/CompVis/stable-diffusion-v1-1).
42
+
43
+ ## Architecture
44
+
45
+ | Component | zeromodels class | Details |
46
+ | --- | --- | --- |
47
+ | Denoiser | `UNet2DConditionModel` | (320, 640, 1280, 1280) channels, 2 ResNet blocks per level, 8 attention heads on the 768-d text context, 1x1 conv token projection, 64x64x4 latent |
48
+ | Autoencoder | `AutoencoderKL` | (128, 256, 512, 512) channels, x8 spatial compression to 4 latent channels, `scaling_factor` 0.18215 |
49
+ | Text encoder | `CLIPTextModel` | CLIP ViT-L/14 text encoder: 768-d, 12 layers, 12 heads, 77 tokens, `quick_gelu` |
50
+ | Scheduler | `PNDMScheduler` | scaled_linear betas 0.00085 to 0.012 over 1000 steps, `epsilon`; 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 import StableDiffusionTextToImage, StableDiffusionTokenizer
60
+
61
+ model = StableDiffusionTextToImage.from_weights("zeromodels/stable-diffusion-v1-1")
62
+ tokenizer = StableDiffusionTokenizer.from_weights("zeromodels/stable-diffusion-v1-1")
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") # (512, 512, 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, 64, 64, 4)` for results that are
72
+ identical across backends.
73
+
74
+ Load any Stable Diffusion 1.x checkpoint the same way with `from_weights("zeromodels/<variant>")`:
75
+
76
+ | Variant | Hub | Training |
77
+ | --- | --- | --- |
78
+ | `stable-diffusion-v1-1` | [zeromodels/stable-diffusion-v1-1](https://huggingface.co/zeromodels/stable-diffusion-v1-1) | 237k steps at 256px on laion2B-en, then 194k steps at 512px on laion-high-resolution |
79
+ | `stable-diffusion-v1-2` | [zeromodels/stable-diffusion-v1-2](https://huggingface.co/zeromodels/stable-diffusion-v1-2) | v1-1 + 515k steps at 512px on laion-aesthetics v2 5+ |
80
+ | `stable-diffusion-v1-3` | [zeromodels/stable-diffusion-v1-3](https://huggingface.co/zeromodels/stable-diffusion-v1-3) | v1-2 + 195k steps at 512px, 10% text-conditioning dropout (classifier-free guidance) |
81
+ | `stable-diffusion-v1-4` | [zeromodels/stable-diffusion-v1-4](https://huggingface.co/zeromodels/stable-diffusion-v1-4) | v1-2 + 225k steps at 512px, 10% text-conditioning dropout (classifier-free guidance) |
82
+ | `stable-diffusion-v1-5` | [zeromodels/stable-diffusion-v1-5](https://huggingface.co/zeromodels/stable-diffusion-v1-5) | v1-2 + 595k steps at 512px, 10% text-conditioning dropout (classifier-free guidance) |
83
+
84
+ ## Tips
85
+
86
+ - Set `KERAS_BACKEND` **before** importing Keras / zeromodels.
87
+ - The graphs are built for 512px. 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
+ - `StableDiffusionModel.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 1.x docs](https://imvision12.github.io/ZeroModels/stable_diffusion/).
99
+
100
+ ## License
101
+
102
+ The weights are redistributed under the
103
+ [CreativeML OpenRAIL-M](https://huggingface.co/spaces/CompVis/stable-diffusion-license) 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 the CompVis group at LMU Munich, Runway and Stability AI 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:d98c9d89856688f47abf88c7ce92214d37f2fe5464def3cbde6eb7e19e8b99df
3
+ size 4267129096
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
zm_config.json ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "library_name": "zeromodels",
3
+ "zeromodels_version": "1.3.3",
4
+ "model_module": "zeromodels.models.stable_diffusion",
5
+ "model_class": "StableDiffusionModel",
6
+ "variant": "stable-diffusion-v1-1",
7
+ "weights": "model.weights.h5",
8
+ "schema_version": 2,
9
+ "weight_dtype": "float32",
10
+ "model_type": "stable_diffusion",
11
+ "unet_config": {
12
+ "sample_size": 64,
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": 768,
35
+ "num_attention_heads": 8,
36
+ "norm_num_groups": 32,
37
+ "use_linear_projection": false,
38
+ "transformer_layers_per_block": 1,
39
+ "addition_embed_type": null,
40
+ "addition_time_embed_dim": 256,
41
+ "projection_class_embeddings_input_dim": null,
42
+ "num_time_ids": 6,
43
+ "text_seq_len": 77
44
+ },
45
+ "vae_config": {
46
+ "in_channels": 3,
47
+ "out_channels": 3,
48
+ "latent_channels": 4,
49
+ "block_out_channels": [
50
+ 128,
51
+ 256,
52
+ 512,
53
+ 512
54
+ ],
55
+ "layers_per_block": 2,
56
+ "norm_num_groups": 32,
57
+ "sample_size": 512,
58
+ "scaling_factor": 0.18215,
59
+ "force_upcast": false,
60
+ "shift_factor": 0.0,
61
+ "use_quant_conv": true,
62
+ "use_post_quant_conv": true
63
+ },
64
+ "text_config": {
65
+ "hidden_dim": 768,
66
+ "num_heads": 12,
67
+ "num_layers": 12,
68
+ "mlp_ratio": 4.0,
69
+ "vocab_size": 49408,
70
+ "max_seq_len": 77
71
+ },
72
+ "hidden_act": "quick_gelu",
73
+ "layer_norm_eps": 1e-05,
74
+ "bos_token_id": 49406,
75
+ "eos_token_id": 49407,
76
+ "pad_token_id": 49407,
77
+ "scheduler_config": {
78
+ "_class_name": "PNDMScheduler",
79
+ "beta_end": 0.012,
80
+ "beta_schedule": "scaled_linear",
81
+ "beta_start": 0.00085,
82
+ "num_train_timesteps": 1000,
83
+ "set_alpha_to_one": false,
84
+ "skip_prk_steps": true,
85
+ "steps_offset": 1,
86
+ "trained_betas": null
87
+ },
88
+ "generate_args": {
89
+ "num_inference_steps": 50,
90
+ "guidance_scale": 7.5
91
+ }
92
+ }