Text-to-Image
ZeroModels
Keras
PyTorch
JAX
TensorFlow
English
stable-diffusion
stable-diffusion-diffusers
diffusion
latent-diffusion
Instructions to use zeromodels/stable-diffusion-2-base with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- ZeroModels
How to use zeromodels/stable-diffusion-2-base with ZeroModels:
# pip install -U zeromodels # ZeroModels is pure Keras 3, so pick a backend: "jax", "torch" or "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" from zeromodels import AutoZModel # AutoZModel reads the repo's model_type and loads the matching class. # For a task head use the matching loader, e.g. AutoZMImageClassify / AutoZMDetect / # AutoZMSemanticSegment / AutoZMTextGenerate (see zeromodels.auto). model = AutoZModel.from_weights("zeromodels/stable-diffusion-2-base") - Keras
How to use zeromodels/stable-diffusion-2-base with Keras:
# !pip install -U keras tensorflow huggingface_hub # Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here; # "jax" and "torch" also work for computation once TensorFlow is installed. import os os.environ["KERAS_BACKEND"] = "tensorflow" import keras model = keras.saving.load_model("hf://zeromodels/stable-diffusion-2-base") - Notebooks
- Google Colab
- Kaggle
Load with ZMDiffusion / ZMDiffusionTokenizer (zeromodels.diffusers)
Browse files- README.md +9 -7
- zm_config.json +3 -3
- zm_preprocessor.json +10 -0
README.md
CHANGED
|
@@ -22,7 +22,7 @@ tags:
|
|
| 22 |
|
| 23 |
# Run Stable Diffusion 2.x with Keras 3: JAX, PyTorch, or TensorFlow
|
| 24 |
|
| 25 |
-
[](https://github.com/IMvision12/ZeroModels) [](https://imvision12.github.io/ZeroModels/
|
| 26 |
|
| 27 |
# zeromodels/stable-diffusion-2-base
|
| 28 |
|
|
@@ -56,10 +56,10 @@ import os
|
|
| 56 |
os.environ["KERAS_BACKEND"] = "torch" # or "jax" / "tensorflow"
|
| 57 |
|
| 58 |
from PIL import Image
|
| 59 |
-
from zeromodels.
|
| 60 |
|
| 61 |
-
model =
|
| 62 |
-
tokenizer =
|
| 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)
|
|
@@ -84,18 +84,20 @@ Load any Stable Diffusion 2.x checkpoint the same way with `from_weights("zeromo
|
|
| 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 |
-
-
|
| 92 |
-
(UNet / VAE / text encoder as `.unet` / `.vae` / `.text_encoder`)
|
| 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/
|
| 99 |
|
| 100 |
## License
|
| 101 |
|
|
|
|
| 22 |
|
| 23 |
# Run Stable Diffusion 2.x with Keras 3: JAX, PyTorch, or TensorFlow
|
| 24 |
|
| 25 |
+
[](https://github.com/IMvision12/ZeroModels) [](https://imvision12.github.io/ZeroModels/diffusers/#stable-diffusion-2) [](https://huggingface.co/collections/zeromodels/stable-diffusion-v2-6aa7906bed026a6b11f4e7be)
|
| 26 |
|
| 27 |
# zeromodels/stable-diffusion-2-base
|
| 28 |
|
|
|
|
| 56 |
os.environ["KERAS_BACKEND"] = "torch" # or "jax" / "tensorflow"
|
| 57 |
|
| 58 |
from PIL import Image
|
| 59 |
+
from zeromodels.diffusers import ZMDiffusion, ZMDiffusionTokenizer
|
| 60 |
|
| 61 |
+
model = ZMDiffusion.from_weights("zeromodels/stable-diffusion-2-base")
|
| 62 |
+
tokenizer = ZMDiffusionTokenizer.from_weights("zeromodels/stable-diffusion-2-base")
|
| 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)
|
|
|
|
| 84 |
## Tips
|
| 85 |
|
| 86 |
- Set `KERAS_BACKEND` **before** importing Keras / zeromodels.
|
| 87 |
+
- `AutoZModel` / `AutoZMTokenizer` (`from zeromodels import ...`) load this repo too;
|
| 88 |
+
they resolve to `ZMDiffusion` / `ZMDiffusionTokenizer` from its `model_type`.
|
| 89 |
- The graphs are built for 512px. Pass `unet_sample_size=<px / 8>, vae_sample_size=<px>` to
|
| 90 |
`from_weights` to build for another multiple of 64px (the weights are resolution-independent).
|
| 91 |
- Swap the sampler any time: `model.scheduler = EulerDiscreteScheduler.from_config(model.config.scheduler_config)`
|
| 92 |
(`zeromodels.base.base_scheduler`).
|
| 93 |
+
- The trained components are attributes of the loaded `ZMDiffusion`
|
| 94 |
+
(UNet / VAE / text encoder as `.unet` / `.vae` / `.text_encoder`), each callable on its own.
|
| 95 |
|
| 96 |
- Both `channels_last` and `channels_first` are supported (`keras.config.set_image_data_format`
|
| 97 |
before loading); `generate` always returns `(batch, H, W, 3)` uint8.
|
| 98 |
- On-the-fly `hf:` conversion is not supported for diffusion models; the checkpoints are
|
| 99 |
hosted here, converted once.
|
| 100 |
+
- See the [Stable Diffusion 2.x docs](https://imvision12.github.io/ZeroModels/diffusers/#stable-diffusion-2).
|
| 101 |
|
| 102 |
## License
|
| 103 |
|
zm_config.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
{
|
| 2 |
"library_name": "zeromodels",
|
| 3 |
"zeromodels_version": "1.3.3",
|
| 4 |
-
"model_module": "zeromodels.
|
| 5 |
-
"model_class": "
|
| 6 |
"variant": "stable-diffusion-2-base",
|
| 7 |
"weights": "model.weights.h5",
|
| 8 |
"schema_version": 2,
|
|
@@ -95,4 +95,4 @@
|
|
| 95 |
"num_inference_steps": 50,
|
| 96 |
"guidance_scale": 7.5
|
| 97 |
}
|
| 98 |
-
}
|
|
|
|
| 1 |
{
|
| 2 |
"library_name": "zeromodels",
|
| 3 |
"zeromodels_version": "1.3.3",
|
| 4 |
+
"model_module": "zeromodels.diffusers",
|
| 5 |
+
"model_class": "ZMDiffusion",
|
| 6 |
"variant": "stable-diffusion-2-base",
|
| 7 |
"weights": "model.weights.h5",
|
| 8 |
"schema_version": 2,
|
|
|
|
| 95 |
"num_inference_steps": 50,
|
| 96 |
"guidance_scale": 7.5
|
| 97 |
}
|
| 98 |
+
}
|
zm_preprocessor.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"library_name": "zeromodels",
|
| 3 |
+
"zeromodels_version": "1.3.3",
|
| 4 |
+
"preprocessor_module": "zeromodels.diffusers",
|
| 5 |
+
"preprocessor_class": "ZMDiffusionTokenizer",
|
| 6 |
+
"variant": "stable-diffusion-2-base",
|
| 7 |
+
"model_type": "stable_diffusion_2",
|
| 8 |
+
"max_seq_len": 77,
|
| 9 |
+
"pad_token": "!"
|
| 10 |
+
}
|