whooray commited on
Commit
6103fa5
1 Parent(s): 6eea5d6

Upload pipeline.py

Browse files
Files changed (1) hide show
  1. pipeline.py +403 -0
pipeline.py ADDED
@@ -0,0 +1,403 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import inspect
2
+ import warnings
3
+ from typing import Any, Callable, Dict, List, Optional, Union
4
+
5
+ import numpy as np
6
+ import PIL
7
+ from PIL import Image, ImageFilter, ImageOps
8
+ import torch
9
+
10
+ from diffusers.models import AutoencoderKL, UNet2DConditionModel
11
+ from diffusers.pipelines.stable_diffusion import (
12
+ StableDiffusionInpaintPipeline,
13
+ StableDiffusionPipelineOutput,
14
+ StableDiffusionSafetyChecker,
15
+ )
16
+ from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_inpaint import prepare_mask_and_masked_image
17
+
18
+
19
+ def fill_images_masks(images: Union[list, PIL.Image.Image], masks: Union[list, PIL.Image.Image]):
20
+
21
+ new_images = []
22
+
23
+ if isinstance(images, PIL.Image.Image) is True:
24
+ if isinstance(masks, PIL.Image.Image) is False:
25
+ raise TypeError(f"`image` is a PIL.Image.Image but `mask` (type: {type(masks)} is not")
26
+ images = [images]
27
+ masks = [masks]
28
+
29
+ if isinstance(images, list) is True:
30
+ if isinstance(masks, list) is False:
31
+ raise TypeError(f"`image` is a list but `mask` (type: {type(masks)} is not")
32
+
33
+ for image, mask in zip(images, masks):
34
+ filled_image = fill(image, mask)
35
+ new_images.append(filled_image)
36
+ else:
37
+ raise ValueError(f"image is not a list but {type(images)}")
38
+
39
+ return new_images, masks
40
+
41
+
42
+ def fill(image: PIL.Image.Image, mask: PIL.Image.Image):
43
+ """fills masked regions with colors from image using blur. Not extremely effective."""
44
+
45
+ image_mod = Image.new('RGBA', (image.width, image.height))
46
+
47
+ image_masked = Image.new('RGBa', (image.width, image.height))
48
+ image_masked.paste(image.convert("RGBA").convert("RGBa"), mask=ImageOps.invert(mask.convert('L')))
49
+
50
+ image_masked = image_masked.convert('RGBa')
51
+
52
+ for radius, repeats in [(256, 1), (64, 1), (16, 2), (4, 4), (2, 2), (0, 1)]:
53
+ blurred = image_masked.filter(ImageFilter.GaussianBlur(radius)).convert('RGBA')
54
+ for _ in range(repeats):
55
+ image_mod.alpha_composite(blurred)
56
+
57
+ return image_mod.convert("RGB")
58
+
59
+ class StableDiffusionFillInpaintPipeline(StableDiffusionInpaintPipeline):
60
+
61
+ @torch.no_grad()
62
+ def __call__(
63
+ self,
64
+ prompt: Union[str, List[str]] = None,
65
+ image: Union[torch.FloatTensor, PIL.Image.Image] = None,
66
+ mask_image: Union[torch.FloatTensor, PIL.Image.Image] = None,
67
+ height: Optional[int] = None,
68
+ width: Optional[int] = None,
69
+ strength: float = 1.0,
70
+ num_inference_steps: int = 50,
71
+ guidance_scale: float = 7.5,
72
+ negative_prompt: Optional[Union[str, List[str]]] = None,
73
+ num_images_per_prompt: Optional[int] = 1,
74
+ eta: float = 0.0,
75
+ generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
76
+ latents: Optional[torch.FloatTensor] = None,
77
+ prompt_embeds: Optional[torch.FloatTensor] = None,
78
+ negative_prompt_embeds: Optional[torch.FloatTensor] = None,
79
+ output_type: Optional[str] = "pil",
80
+ return_dict: bool = True,
81
+ callback: Optional[Callable[[int, int, torch.FloatTensor], None]] = None,
82
+ callback_steps: int = 1,
83
+ cross_attention_kwargs: Optional[Dict[str, Any]] = None,
84
+ ):
85
+ r"""
86
+ Function invoked when calling the pipeline for generation.
87
+
88
+ Args:
89
+ prompt (`str` or `List[str]`, *optional*):
90
+ The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`.
91
+ instead.
92
+ image (`PIL.Image.Image`):
93
+ `Image`, or tensor representing an image batch which will be inpainted, *i.e.* parts of the image will
94
+ be masked out with `mask_image` and repainted according to `prompt`.
95
+ mask_image (`PIL.Image.Image`):
96
+ `Image`, or tensor representing an image batch, to mask `image`. White pixels in the mask will be
97
+ repainted, while black pixels will be preserved. If `mask_image` is a PIL image, it will be converted
98
+ to a single channel (luminance) before use. If it's a tensor, it should contain one color channel (L)
99
+ instead of 3, so the expected shape would be `(B, H, W, 1)`.
100
+ height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor):
101
+ The height in pixels of the generated image.
102
+ width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor):
103
+ The width in pixels of the generated image.
104
+ strength (`float`, *optional*, defaults to 1.):
105
+ Conceptually, indicates how much to transform the masked portion of the reference `image`. Must be
106
+ between 0 and 1. `image` will be used as a starting point, adding more noise to it the larger the
107
+ `strength`. The number of denoising steps depends on the amount of noise initially added. When
108
+ `strength` is 1, added noise will be maximum and the denoising process will run for the full number of
109
+ iterations specified in `num_inference_steps`. A value of 1, therefore, essentially ignores the masked
110
+ portion of the reference `image`.
111
+ num_inference_steps (`int`, *optional*, defaults to 50):
112
+ The number of denoising steps. More denoising steps usually lead to a higher quality image at the
113
+ expense of slower inference.
114
+ guidance_scale (`float`, *optional*, defaults to 7.5):
115
+ Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598).
116
+ `guidance_scale` is defined as `w` of equation 2. of [Imagen
117
+ Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale >
118
+ 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`,
119
+ usually at the expense of lower image quality.
120
+ negative_prompt (`str` or `List[str]`, *optional*):
121
+ The prompt or prompts not to guide the image generation. If not defined, one has to pass
122
+ `negative_prompt_embeds`. instead. Ignored when not using guidance (i.e., ignored if `guidance_scale`
123
+ is less than `1`).
124
+ num_images_per_prompt (`int`, *optional*, defaults to 1):
125
+ The number of images to generate per prompt.
126
+ eta (`float`, *optional*, defaults to 0.0):
127
+ Corresponds to parameter eta (η) in the DDIM paper: https://arxiv.org/abs/2010.02502. Only applies to
128
+ [`schedulers.DDIMScheduler`], will be ignored for others.
129
+ generator (`torch.Generator`, *optional*):
130
+ One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html)
131
+ to make generation deterministic.
132
+ latents (`torch.FloatTensor`, *optional*):
133
+ Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
134
+ generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
135
+ tensor will ge generated by sampling using the supplied random `generator`.
136
+ prompt_embeds (`torch.FloatTensor`, *optional*):
137
+ Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
138
+ provided, text embeddings will be generated from `prompt` input argument.
139
+ negative_prompt_embeds (`torch.FloatTensor`, *optional*):
140
+ Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt
141
+ weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input
142
+ argument.
143
+ output_type (`str`, *optional*, defaults to `"pil"`):
144
+ The output format of the generate image. Choose between
145
+ [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`.
146
+ return_dict (`bool`, *optional*, defaults to `True`):
147
+ Whether or not to return a [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] instead of a
148
+ plain tuple.
149
+ callback (`Callable`, *optional*):
150
+ A function that will be called every `callback_steps` steps during inference. The function will be
151
+ called with the following arguments: `callback(step: int, timestep: int, latents: torch.FloatTensor)`.
152
+ callback_steps (`int`, *optional*, defaults to 1):
153
+ The frequency at which the `callback` function will be called. If not specified, the callback will be
154
+ called at every step.
155
+ cross_attention_kwargs (`dict`, *optional*):
156
+ A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under
157
+ `self.processor` in
158
+ [diffusers.cross_attention](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/cross_attention.py).
159
+ Examples:
160
+
161
+ ```py
162
+ >>> import PIL
163
+ >>> import requests
164
+ >>> import torch
165
+ >>> from io import BytesIO
166
+
167
+ >>> from diffusers import StableDiffusionInpaintPipeline
168
+
169
+
170
+ >>> def download_image(url):
171
+ ... response = requests.get(url)
172
+ ... return PIL.Image.open(BytesIO(response.content)).convert("RGB")
173
+
174
+
175
+ >>> img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
176
+ >>> mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
177
+
178
+ >>> init_image = download_image(img_url).resize((512, 512))
179
+ >>> mask_image = download_image(mask_url).resize((512, 512))
180
+
181
+ >>> pipe = StableDiffusionInpaintPipeline.from_pretrained(
182
+ ... "runwayml/stable-diffusion-inpainting", torch_dtype=torch.float16
183
+ ... )
184
+ >>> pipe = pipe.to("cuda")
185
+
186
+ >>> prompt = "Face of a yellow cat, high resolution, sitting on a park bench"
187
+ >>> image = pipe(prompt=prompt, image=init_image, mask_image=mask_image).images[0]
188
+ ```
189
+
190
+ Returns:
191
+ [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] or `tuple`:
192
+ [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] if `return_dict` is True, otherwise a `tuple.
193
+ When returning a tuple, the first element is a list with the generated images, and the second element is a
194
+ list of `bool`s denoting whether the corresponding generated image likely represents "not-safe-for-work"
195
+ (nsfw) content, according to the `safety_checker`.
196
+ """
197
+ # 0. Default height and width to unet
198
+ height = height or self.unet.config.sample_size * self.vae_scale_factor
199
+ width = width or self.unet.config.sample_size * self.vae_scale_factor
200
+
201
+ # 1. Check inputs
202
+ self.check_inputs(
203
+ prompt,
204
+ height,
205
+ width,
206
+ strength,
207
+ callback_steps,
208
+ negative_prompt,
209
+ prompt_embeds,
210
+ negative_prompt_embeds,
211
+ )
212
+
213
+ # 2. Define call parameters
214
+ if prompt is not None and isinstance(prompt, str):
215
+ batch_size = 1
216
+ elif prompt is not None and isinstance(prompt, list):
217
+ batch_size = len(prompt)
218
+ else:
219
+ batch_size = prompt_embeds.shape[0]
220
+
221
+ device = self._execution_device
222
+ # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
223
+ # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
224
+ # corresponds to doing no classifier free guidance.
225
+ do_classifier_free_guidance = guidance_scale > 1.0
226
+
227
+ # 3. Encode input prompt
228
+ text_encoder_lora_scale = (
229
+ cross_attention_kwargs.get("scale", None) if cross_attention_kwargs is not None else None
230
+ )
231
+ prompt_embeds = self._encode_prompt(
232
+ prompt,
233
+ device,
234
+ num_images_per_prompt,
235
+ do_classifier_free_guidance,
236
+ negative_prompt,
237
+ prompt_embeds=prompt_embeds,
238
+ negative_prompt_embeds=negative_prompt_embeds,
239
+ lora_scale=text_encoder_lora_scale,
240
+ )
241
+
242
+ # 4. set timesteps
243
+ self.scheduler.set_timesteps(num_inference_steps, device=device)
244
+ timesteps, num_inference_steps = self.get_timesteps(
245
+ num_inference_steps=num_inference_steps, strength=strength, device=device
246
+ )
247
+ # check that number of inference steps is not < 1 - as this doesn't make sense
248
+ if num_inference_steps < 1:
249
+ raise ValueError(
250
+ f"After adjusting the num_inference_steps by strength parameter: {strength}, the number of pipeline"
251
+ f"steps is {num_inference_steps} which is < 1 and not appropriate for this pipeline."
252
+ )
253
+ # at which timestep to set the initial noise (n.b. 50% if strength is 0.5)
254
+ latent_timestep = timesteps[:1].repeat(batch_size * num_images_per_prompt)
255
+ # create a boolean to check if the strength is set to 1. if so then initialise the latents with pure noise
256
+ is_strength_max = strength == 1.0
257
+
258
+ #Fill mask area
259
+ if batch_size == 1:
260
+ original_image, original_mask = [image], [mask_image]
261
+
262
+ image, mask_image = fill_images_masks(image, mask_image)
263
+
264
+
265
+ # 5. Preprocess mask and image
266
+ mask, masked_image, init_image = prepare_mask_and_masked_image(
267
+ image, mask_image, height, width, return_image=True
268
+ )
269
+
270
+ # 6. Prepare latent variables
271
+ num_channels_latents = self.vae.config.latent_channels
272
+ num_channels_unet = self.unet.config.in_channels
273
+ return_image_latents = num_channels_unet == 4
274
+
275
+ latents_outputs = self.prepare_latents(
276
+ batch_size * num_images_per_prompt,
277
+ num_channels_latents,
278
+ height,
279
+ width,
280
+ prompt_embeds.dtype,
281
+ device,
282
+ generator,
283
+ latents,
284
+ image=init_image,
285
+ timestep=latent_timestep,
286
+ is_strength_max=is_strength_max,
287
+ return_noise=True,
288
+ return_image_latents=return_image_latents,
289
+ )
290
+
291
+ if return_image_latents:
292
+ latents, noise, image_latents = latents_outputs
293
+ else:
294
+ latents, noise = latents_outputs
295
+
296
+ # 7. Prepare mask latent variables
297
+ mask, masked_image_latents = self.prepare_mask_latents(
298
+ mask,
299
+ masked_image,
300
+ batch_size * num_images_per_prompt,
301
+ height,
302
+ width,
303
+ prompt_embeds.dtype,
304
+ device,
305
+ generator,
306
+ do_classifier_free_guidance,
307
+ )
308
+ init_image = init_image.to(device=device, dtype=masked_image_latents.dtype)
309
+ init_image = self._encode_vae_image(init_image, generator=generator)
310
+
311
+ # 8. Check that sizes of mask, masked image and latents match
312
+ if num_channels_unet == 9:
313
+ # default case for runwayml/stable-diffusion-inpainting
314
+ num_channels_mask = mask.shape[1]
315
+ num_channels_masked_image = masked_image_latents.shape[1]
316
+ if num_channels_latents + num_channels_mask + num_channels_masked_image != self.unet.config.in_channels:
317
+ raise ValueError(
318
+ f"Incorrect configuration settings! The config of `pipeline.unet`: {self.unet.config} expects"
319
+ f" {self.unet.config.in_channels} but received `num_channels_latents`: {num_channels_latents} +"
320
+ f" `num_channels_mask`: {num_channels_mask} + `num_channels_masked_image`: {num_channels_masked_image}"
321
+ f" = {num_channels_latents+num_channels_masked_image+num_channels_mask}. Please verify the config of"
322
+ " `pipeline.unet` or your `mask_image` or `image` input."
323
+ )
324
+ elif num_channels_unet != 4:
325
+ raise ValueError(
326
+ f"The unet {self.unet.__class__} should have either 4 or 9 input channels, not {self.unet.config.in_channels}."
327
+ )
328
+
329
+ # 9. Prepare extra step kwargs. TODO: Logic should ideally just be moved out of the pipeline
330
+ extra_step_kwargs = self.prepare_extra_step_kwargs(generator, eta)
331
+
332
+ # 10. Denoising loop
333
+ num_warmup_steps = len(timesteps) - num_inference_steps * self.scheduler.order
334
+ with self.progress_bar(total=num_inference_steps) as progress_bar:
335
+ for i, t in enumerate(timesteps):
336
+ # expand the latents if we are doing classifier free guidance
337
+ latent_model_input = torch.cat([latents] * 2) if do_classifier_free_guidance else latents
338
+
339
+ # concat latents, mask, masked_image_latents in the channel dimension
340
+ latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)
341
+
342
+ if num_channels_unet == 9:
343
+ latent_model_input = torch.cat([latent_model_input, mask, masked_image_latents], dim=1)
344
+
345
+ # predict the noise residual
346
+ noise_pred = self.unet(
347
+ latent_model_input,
348
+ t,
349
+ encoder_hidden_states=prompt_embeds,
350
+ cross_attention_kwargs=cross_attention_kwargs,
351
+ return_dict=False,
352
+ )[0]
353
+
354
+ # perform guidance
355
+ if do_classifier_free_guidance:
356
+ noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
357
+ noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
358
+
359
+ # compute the previous noisy sample x_t -> x_t-1
360
+ latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs, return_dict=False)[0]
361
+
362
+ if num_channels_unet == 4:
363
+ init_latents_proper = image_latents[:1]
364
+ init_mask = mask[:1]
365
+
366
+ if i < len(timesteps) - 1:
367
+ noise_timestep = timesteps[i + 1]
368
+ init_latents_proper = self.scheduler.add_noise(
369
+ init_latents_proper, noise, torch.tensor([noise_timestep])
370
+ )
371
+
372
+ latents = (1 - init_mask) * init_latents_proper + init_mask * latents
373
+
374
+ # call the callback, if provided
375
+ if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
376
+ progress_bar.update()
377
+ if callback is not None and i % callback_steps == 0:
378
+ callback(i, t, latents)
379
+
380
+ if not output_type == "latent":
381
+ image = self.vae.decode(latents / self.vae.config.scaling_factor, return_dict=False)[0]
382
+ image, has_nsfw_concept = self.run_safety_checker(image, device, prompt_embeds.dtype)
383
+ else:
384
+ image = latents
385
+ has_nsfw_concept = None
386
+
387
+ if has_nsfw_concept is None:
388
+ do_denormalize = [True] * image.shape[0]
389
+ else:
390
+ do_denormalize = [not has_nsfw for has_nsfw in has_nsfw_concept]
391
+
392
+ image = self.image_processor.postprocess(image, output_type=output_type, do_denormalize=do_denormalize)
393
+
394
+ image = [Image.composite(generate_img, original_img, mask_img) for generate_img, original_img, mask_img in zip(image, original_image, original_mask)]
395
+
396
+ # Offload last model to CPU
397
+ if hasattr(self, "final_offload_hook") and self.final_offload_hook is not None:
398
+ self.final_offload_hook.offload()
399
+
400
+ if not return_dict:
401
+ return (image, has_nsfw_concept)
402
+
403
+ return StableDiffusionPipelineOutput(images=image, nsfw_content_detected=has_nsfw_concept)