Callbacks for 🧨 Diffusers pipelines for Stable Diffusion
Callbacks for logging experiment details, configs and generated images for the Stable Diffusion family of pipelines from Diffusers 🧨 pipelines to your Weights & Biases workspace.
Callback | Run in Colab | WandB Run |
---|---|---|
Stable Diffusion | ||
Stable Diffusion XL | ||
Stable Diffusion Image2Image |
StableDiffusionCallback
Bases: BaseDiffusersCallback
Callback for 🧨 Diffusers logging
the results of a
StableDiffusionPipeline
generation to Weights & Biases.
Features:
- The callback automatically logs basic configs like prompt, negative prompt,
etc. along with the generated image in a
wandb.Table
. - The callback also logs configs for both the experiment as well as pipelines with the wandb run.
- No need to initialize a run, the callback automatically initialized and ends runs gracefully.
Example usage:
You can fine an example notebook here.
import torch
from diffusers import StableDiffusionPipeline
from wandb_addons.diffusers import StableDiffusionCallback
pipeline = StableDiffusionPipeline.from_pretrained(
"CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16
)
pipeline = pipeline.to("cuda")
prompt = [
"a photograph of an astronaut riding a horse",
"a photograph of a dragon"
]
negative_prompt = ["ugly, deformed", "ugly, deformed"]
num_images_per_prompt = 2
configs = {
"eta": 0.0,
"guidance_rescale": 0.0,
}
# Create the WandB callback for StableDiffusionPipeline
callback = StableDiffusionCallback(
pipe,
prompt=prompt,
negative_prompt=negative_prompt,
wandb_project="diffusers",
num_images_per_prompt=num_images_per_prompt,
configs=configs,
)
# Add the callback to the pipeline
image = pipe(
prompt,
negative_prompt=negative_prompt,
callback=callback,
num_images_per_prompt=num_images_per_prompt,
**configs,
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pipeline |
StableDiffusionPipeline
|
The |
required |
prompt |
Union[str, List[str]]
|
The prompt or prompts to guide the image generation. |
required |
wandb_project |
Optional[str]
|
The name of the project where you're sending the new run. The project is not necessary to be specified unless the run has automatically been initiatlized before the callback is defined. |
required |
wandb_entity |
Optional[str]
|
An entity is a username or team name where you're sending runs. This entity must exist before you can send runs there, so make sure to create your account or team in the UI before starting to log runs. If you don't specify an entity, the run will be sent to your default entity, which is usually your username. Change your default entity in your settings under "default location to create new projects". |
None
|
weave_mode |
bool
|
Whether to use log to a
weave board instead of W&B dashboard
or not. The weave mode logs the configs, generated images and timestamp in a
|
False
|
num_inference_steps |
int
|
The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference. |
50
|
num_images_per_prompt |
Optional[int]
|
The number of images to generate per prompt. |
1
|
guidance_scale |
float
|
Guidance scale as defined in
Classifier-Free Diffusion Guidance.
|
7.5
|
negative_prompt |
Optional[Union[str, List[str]]]
|
The prompt or prompts not
to guide the image generation. Ignored when not using guidance
(i.e., ignored if |
None
|
configs |
Optional[Dict]
|
Additional configs for the experiment you want to
sync, for example, for example, |
None
|
Source code in wandb_addons/diffusers/callbacks/stable_diffusion/text_to_image.py
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 |
|
StableDiffusionImg2ImgCallback
Bases: BaseImage2ImageCallback
Callback for 🧨 Diffusers logging
the results of a
StableDiffusionImg2ImgPipeline
generation to Weights & Biases.
Features:
- The callback automatically logs basic configs like input image, prompt,
negative prompt, etc. along with the generated image in a
wandb.Table
. - The callback also logs configs for both the experiment as well as pipelines with the wandb run.
- No need to initialize a run, the callback automatically initialized and ends runs gracefully.
Example usage:
You can fine an example notebook here.
import requests
import torch
from PIL import Image
from io import BytesIO
from diffusers import StableDiffusionImg2ImgPipeline
from wandb_addons.diffusers import StableDiffusionImg2ImgCallback
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
)
pipe = pipe.to("cuda")
url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
response = requests.get(url)
init_image = Image.open(BytesIO(response.content)).convert("RGB")
init_image = init_image.resize((768, 512))
prompt = "A fantasy landscape, trending on artstation"
num_images_per_prompt = 1
strength = 0.75
callback = StableDiffusionImg2ImgCallback(
pipeline=pipe, prompt=prompt,
input_images=init_image,
wandb_project="diffusers",
num_images_per_prompt=num_images_per_prompt,
strength=strength,
)
results = pipe(
prompt=prompt,
image=init_image,
strength=strength,
num_images_per_prompt=num_images_per_prompt,
callback=callback
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pipeline |
StableDiffusionPipeline
|
The |
required |
prompt |
Union[str, List[str]]
|
The prompt or prompts to guide the image generation. |
required |
input_images |
PipelineImageInput
|
The input image, numpy array or tensor
representing an image batch to be used as the starting point. For both numpy
array and pytorch tensor, the expected value range is between [0, 1] If it's
a tensor or a list or tensors, the expected shape should be |
required |
wandb_project |
Optional[str]
|
The name of the project where you're sending the new run. The project is not necessary to be specified unless the run has automatically been initiatlized before the callback is defined. |
required |
wandb_entity |
Optional[str]
|
An entity is a username or team name where you're sending runs. This entity must exist before you can send runs there, so make sure to create your account or team in the UI before starting to log runs. If you don't specify an entity, the run will be sent to your default entity, which is usually your username. Change your default entity in your settings under "default location to create new projects". |
None
|
weave_mode |
bool
|
Whether to use log to a
weave board instead of W&B dashboard
or not. The weave mode logs the configs, generated images and timestamp in a
|
False
|
num_inference_steps |
int
|
The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference. |
50
|
strength |
Optional[float]
|
Indicates extent to transform the reference image.
Must be between 0 and 1. image is used as a starting point and more noise
is added the higher the strength. The number of denoising steps depends on
the amount of noise initially added. When strength is 1, added noise is
maximum and the denoising process runs for the full number of iterations
specified in |
0.8
|
guidance_scale |
float
|
Guidance scale as defined in
Classifier-Free Diffusion Guidance.
|
7.5
|
num_images_per_prompt |
Optional[int]
|
The number of images to generate per prompt. |
1
|
negative_prompt |
Optional[Union[str, List[str]]]
|
The prompt or prompts not
to guide the image generation. Ignored when not using guidance
(i.e., ignored if |
None
|
configs |
Optional[Dict]
|
Additional configs for the experiment you want to
sync, for example, for example, |
None
|
Source code in wandb_addons/diffusers/callbacks/stable_diffusion/image_to_image.py
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 |
|
StableDiffusionXLCallback
Bases: BaseMultiPipelineCallback
Callback for logging the resulst of a text-to-image workflow with Stable Diffusion XL to Weights & Biases.
Features:
- The callback automatically logs basic configs like prompt, negative prompt,
etc. along with the generated image in a
wandb.Table
. - The callback also logs configs for both the experiment as well as pipelines with the wandb run.
- No need to initialize a run, the callback automatically initialized and ends runs gracefully.
- Supports logging base and refinement stages of a workflow using a single callback.
Example usage:
You can fine an example notebook here.
from functools import partial
import torch
from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline
from wandb_addons.diffusers import StableDiffusionXLCallback
base_pipeline = StableDiffusionXLPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
torch_dtype=torch.float16,
variant="fp16",
use_safetensors=True,
)
base_pipeline.enable_model_cpu_offload()
prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
num_inference_steps = 50
callback = StableDiffusionXLCallback(
pipeline=base_pipeline,
prompt=prompt,
wandb_project="diffusers-sdxl",
wandb_entity="geekyrakshit",
weave_mode=True,
num_inference_steps=num_inference_steps,
initial_stage_name="base",
)
image = base_pipeline(
prompt=prompt,
output_type="latent",
num_inference_steps=num_inference_steps,
callback=partial(callback, end_experiment=False)
).images[0]
refiner_pipeline = StableDiffusionXLImg2ImgPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-refiner-1.0",
text_encoder_2=base_pipeline.text_encoder_2,
vae=base_pipeline.vae,
torch_dtype=torch.float16,
use_safetensors=True,
variant="fp16",
)
refiner_pipeline.enable_model_cpu_offload()
num_inference_steps = 50
strength = 0.3
callback.add_refiner_stage(
refiner_pipeline, num_inference_steps=num_inference_steps, strength=strength
)
image = refiner_pipeline(
prompt=prompt, image=image[None, :], callback=callback
).images[0]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pipeline |
DiffusionPipeline
|
The |
required |
prompt |
Union[str, List[str]]
|
The prompt or prompts to guide the image generation. |
required |
wandb_project |
Optional[str]
|
The name of the project where you're sending the new run. The project is not necessary to be specified unless the run has automatically been initiatlized before the callback is defined. |
required |
wandb_entity |
Optional[str]
|
An entity is a username or team name where you're sending runs. This entity must exist before you can send runs there, so make sure to create your account or team in the UI before starting to log runs. If you don't specify an entity, the run will be sent to your default entity, which is usually your username. Change your default entity in your settings under "default location to create new projects". |
None
|
weave_mode |
bool
|
Whether to use log to a
weave board instead of W&B dashboard
or not. The weave mode logs the configs, generated images and timestamp in a
|
False
|
num_inference_steps |
int
|
The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference. |
50
|
num_images_per_prompt |
Optional[int]
|
The number of images to generate per prompt. |
1
|
negative_prompt |
Optional[Union[str, List[str]]]
|
The prompt or prompts not
to guide the image generation. Ignored when not using guidance
(i.e., ignored if |
None
|
initial_stage_name |
Optional[str]
|
The name of the initial stage. If not
specified, it would be set to |
'Base-Pipeline'
|
configs |
Optional[Dict]
|
Additional configs for the experiment you want to
sync, for example, for example, |
None
|
Source code in wandb_addons/diffusers/callbacks/stable_diffusion/sdxl_t2i.py
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 |
|
add_refiner_stage(pipeline, num_inference_steps=None, strength=0.3, configs=None)
Add the refinement stage to the callback to log the results of the refiner pipeline.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pipeline |
DiffusionPipeline
|
The |
required |
num_inference_steps |
Optional[int]
|
The number of denoising steps for the new stage. More denoising steps usually lead to a higher quality image at the expense of slower inference. |
None
|
strength |
Optional[float]
|
Conceptually, indicates how much to transform
the reference image. Must be between 0 and 1. image will be used as a
starting point, adding more noise to it the larger the strength. The
number of denoising steps depends on the amount of noise initially
added. When strength is 1, added noise will be maximum and the
denoising process will run for the full number of iterations specified
in num_inference_steps. A value of 1, therefore, essentially ignores
image. Note that in the case of |
0.3
|
configs |
Optional[Dict]
|
Additional configs for the new stage you want to
sync, for example, for example, |
None
|