Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it easier to change the batch size in the stable diffusion loop examples #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions Stable Diffusion Deep Dive.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@
"text_input = tokenizer(prompt, padding=\"max_length\", max_length=tokenizer.model_max_length, truncation=True, return_tensors=\"pt\")\n",
"with torch.no_grad():\n",
" text_embeddings = text_encoder(text_input.input_ids.to(torch_device))[0]\n",
"text_embeddings = text_embeddings.repeat(batch_size, 1, 1)\n",
"\n",
"max_length = text_input.input_ids.shape[-1]\n",
"uncond_input = tokenizer(\n",
" [\"\"] * batch_size, padding=\"max_length\", max_length=max_length, return_tensors=\"pt\"\n",
Expand Down Expand Up @@ -304,7 +306,9 @@
"image = image.detach().cpu().permute(0, 2, 3, 1).numpy()\n",
"images = (image * 255).round().astype(\"uint8\")\n",
"pil_images = [Image.fromarray(image) for image in images]\n",
"pil_images[0]"
"for img in pil_images:\n",
" plt.figure()\n",
" plt.imshow(img)\n"
]
},
{
Expand Down Expand Up @@ -867,12 +871,13 @@
"num_inference_steps = 50 # Number of denoising steps\n",
"guidance_scale = 8 # Scale for classifier-free guidance\n",
"generator = torch.manual_seed(32) # Seed generator to create the inital latent noise\n",
"batch_size = 1\n",
"batch_size = encoded.size()[0]\n",
"\n",
"# Prep text (same as before)\n",
"text_input = tokenizer(prompt, padding=\"max_length\", max_length=tokenizer.model_max_length, truncation=True, return_tensors=\"pt\")\n",
"with torch.no_grad():\n",
" text_embeddings = text_encoder(text_input.input_ids.to(torch_device))[0]\n",
"text_embeddings = text_embeddings.repeat(batch_size, 1, 1)\n",
"max_length = text_input.input_ids.shape[-1]\n",
"uncond_input = tokenizer(\n",
" [\"\"] * batch_size, padding=\"max_length\", max_length=max_length, return_tensors=\"pt\"\n",
Expand Down Expand Up @@ -911,7 +916,9 @@
" # compute the previous noisy sample x_t -> x_t-1\n",
" latents = scheduler.step(noise_pred, t, latents).prev_sample\n",
"\n",
"latents_to_pil(latents)[0]"
"for img in latents_to_pil(latents):\n",
" plt.figure()\n",
" plt.imshow(img)\n"
]
},
{
Expand Down