Skip to content

Commit

Permalink
remove unused functions from paint from example
Browse files Browse the repository at this point in the history
  • Loading branch information
eaidova committed Jan 24, 2024
1 parent 3047747 commit 292c2c1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@
" latent_timestep = timesteps[:1]\n",
"\n",
" # get the initial random noise unless the user supplied it\n",
" latents, meta = self.prepare_latents(None, latent_timestep)\n",
" latents, meta = self.prepare_latents(latent_timestep)\n",
" mask, masked_image_latents = self.prepare_mask_latents(\n",
" mask,\n",
" masked_image,\n",
Expand Down Expand Up @@ -801,13 +801,11 @@
"\n",
" return text_embeddings\n",
"\n",
" def prepare_latents(self, image:PIL.Image.Image = None, latent_timestep:torch.Tensor = None):\n",
" def prepare_latents(self, latent_timestep:torch.Tensor = None):\n",
" \"\"\"\n",
" Function for getting initial latents for starting generation\n",
" \n",
" Parameters:\n",
" image (PIL.Image.Image, *optional*, None):\n",
" Input image for generation, if not provided randon noise will be used as starting point\n",
" latent_timestep (torch.Tensor, *optional*, None):\n",
" Predicted by scheduler initial step for image generation, required for latent image mixing with nosie\n",
" Returns:\n",
Expand All @@ -816,16 +814,10 @@
" \"\"\"\n",
" latents_shape = (1, 4, self.height // 8, self.width // 8)\n",
" noise = np.random.randn(*latents_shape).astype(np.float32)\n",
" if image is None:\n",
" # if we use LMSDiscreteScheduler, let's make sure latents are mulitplied by sigmas\n",
" if isinstance(self.scheduler, LMSDiscreteScheduler):\n",
" noise = noise * self.scheduler.sigmas[0].numpy()\n",
" return noise, {}\n",
" input_image, meta = preprocess(image)\n",
" latents = self.vae_encoder(input_image)[self._vae_e_output]\n",
" latents = latents * 0.18215\n",
" latents = self.scheduler.add_noise(torch.from_numpy(latents), torch.from_numpy(noise), latent_timestep).numpy()\n",
" return latents, meta\n",
" # if we use LMSDiscreteScheduler, let's make sure latents are mulitplied by sigmas\n",
" if isinstance(self.scheduler, LMSDiscreteScheduler):\n",
" noise = noise * self.scheduler.sigmas[0].numpy()\n",
" return noise, {}\n",
"\n",
" def postprocess_image(self, image:np.ndarray, meta:Dict, output_type:str = \"pil\"):\n",
" \"\"\"\n",
Expand Down
44 changes: 20 additions & 24 deletions notebooks/272-paint-by-example/272-paint-by-example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Note: you may need to restart the kernel to use updated packages.\n"
]
"name": "stdout",
"output_type": "stream",
"text": [
"Note: you may need to restart the kernel to use updated packages.\n"
]
},
{
"name": "stderr",
Expand Down Expand Up @@ -873,13 +873,11 @@
"\n",
" return image_embeddings\n",
"\n",
" def prepare_latents(self, image:PIL.Image.Image = None, latent_timestep:torch.Tensor = None):\n",
" def prepare_latents(self, latent_timestep:torch.Tensor = None):\n",
" \"\"\"\n",
" Function for getting initial latents for starting generation\n",
" \n",
" Parameters:\n",
" image (PIL.Image.Image, *optional*, None):\n",
" Input image for generation, if not provided randon noise will be used as starting point\n",
" latent_timestep (torch.Tensor, *optional*, None):\n",
" Predicted by scheduler initial step for image generation, required for latent image mixing with nosie\n",
" Returns:\n",
Expand All @@ -888,18 +886,10 @@
" \"\"\"\n",
" latents_shape = (1, 4, self.height // 8, self.width // 8)\n",
" noise = np.random.randn(*latents_shape).astype(np.float32)\n",
" if image is None:\n",
" # if we use LMSDiscreteScheduler, let's make sure latents are mulitplied by sigmas\n",
" if isinstance(self.scheduler, LMSDiscreteScheduler):\n",
" noise = noise * self.scheduler.sigmas[0].numpy()\n",
" return noise, {}\n",
" input_image, meta = preprocess(image)\n",
" moments = self.vae_encoder(input_image)[self._vae_e_output]\n",
" mean, logvar = np.split(moments, 2, axis=1) \n",
" std = np.exp(logvar * 0.5)\n",
" latents = (mean + std * np.random.randn(*mean.shape)) * 0.18215\n",
" latents = self.scheduler.add_noise(torch.from_numpy(latents), torch.from_numpy(noise), latent_timestep).numpy()\n",
" return latents, meta\n",
" # if we use LMSDiscreteScheduler, let's make sure latents are mulitplied by sigmas\n",
" if isinstance(self.scheduler, LMSDiscreteScheduler):\n",
" noise = noise * self.scheduler.sigmas[0].numpy()\n",
" return noise, {}\n",
"\n",
" def postprocess_image(self, image:np.ndarray, meta:Dict, output_type:str = \"pil\"):\n",
" \"\"\"\n",
Expand Down Expand Up @@ -1196,7 +1186,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "openvino_env",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -1210,10 +1200,16 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
"version": "3.8.10"
},
"orig_nbformat": 4
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}

0 comments on commit 292c2c1

Please sign in to comment.