Skip to content

Instantly share code, notes, and snippets.

@nbonamy
Last active December 4, 2023 21:27
Show Gist options
  • Save nbonamy/c59771b394d5acc614a92a51c3adf8f4 to your computer and use it in GitHub Desktop.
Save nbonamy/c59771b394d5acc614a92a51c3adf8f4 to your computer and use it in GitHub Desktop.
StableDiffusion with intermediate save
from diffusers import StableDiffusionPipeline
import torch
def progress(step, timestep, latents):
print(step, timestep, latents[0][0][0][0])
with torch.no_grad():
latents = 1 / 0.18215 * latents
image = pipe.vae.decode(latents).sample
image = (image / 2 + 0.5).clamp(0, 1)
image = image.cpu().permute(0, 2, 3, 1).float().numpy()
image = pipe.numpy_to_pil(image)
image[0].save(f"step_{step}.png")
# main image
prompt = "A fantasy landscape with mountains and a river"
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
images = pipe(prompt, callback=progress, callback_steps=10).images
images[0].save(f"image.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment