Daankular commited on
Commit
bc24ce2
·
1 Parent(s): a83768e

Fix PSHuman CLIPFeatureExtractor removed in transformers>=5.0

Browse files

CLIPFeatureExtractor was removed in transformers 5.x, replaced by CLIPImageProcessor.
Committed pre-patched pipeline_mvdiffusion_unclip.py to patches/pshuman/ and updated
pshuman_local.py to copy patches after cloning (same pattern as TripoSG).

patches/pshuman/mvdiffusion/pipelines/pipeline_mvdiffusion_unclip.py ADDED
@@ -0,0 +1,651 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import inspect
2
+ import warnings
3
+ from typing import Callable, List, Optional, Union, Dict, Any
4
+ import PIL
5
+ import torch
6
+ from packaging import version
7
+ from transformers import CLIPImageProcessor, CLIPVisionModelWithProjection, CLIPTokenizer, CLIPTextModel
8
+ from diffusers.utils.import_utils import is_accelerate_available
9
+ from diffusers.configuration_utils import FrozenDict
10
+ from diffusers.image_processor import VaeImageProcessor
11
+ from diffusers.models import AutoencoderKL, UNet2DConditionModel
12
+ from diffusers.models.embeddings import get_timestep_embedding
13
+ from diffusers.schedulers import KarrasDiffusionSchedulers
14
+ from diffusers.utils import deprecate, logging
15
+ from diffusers.utils.torch_utils import randn_tensor
16
+ from diffusers.pipelines.pipeline_utils import DiffusionPipeline, ImagePipelineOutput
17
+ from diffusers.pipelines.stable_diffusion.stable_unclip_image_normalizer import StableUnCLIPImageNormalizer
18
+ import os
19
+ import torchvision.transforms.functional as TF
20
+ from einops import rearrange
21
+ logger = logging.get_logger(__name__)
22
+
23
+ class StableUnCLIPImg2ImgPipeline(DiffusionPipeline):
24
+ """
25
+ Pipeline for text-guided image to image generation using stable unCLIP.
26
+
27
+ This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods the
28
+ library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)
29
+
30
+ Args:
31
+ feature_extractor ([`CLIPImageProcessor`]):
32
+ Feature extractor for image pre-processing before being encoded.
33
+ image_encoder ([`CLIPVisionModelWithProjection`]):
34
+ CLIP vision model for encoding images.
35
+ image_normalizer ([`StableUnCLIPImageNormalizer`]):
36
+ Used to normalize the predicted image embeddings before the noise is applied and un-normalize the image
37
+ embeddings after the noise has been applied.
38
+ image_noising_scheduler ([`KarrasDiffusionSchedulers`]):
39
+ Noise schedule for adding noise to the predicted image embeddings. The amount of noise to add is determined
40
+ by `noise_level` in `StableUnCLIPPipeline.__call__`.
41
+ tokenizer (`CLIPTokenizer`):
42
+ Tokenizer of class
43
+ [CLIPTokenizer](https://huggingface.co/docs/transformers/v4.21.0/en/model_doc/clip#transformers.CLIPTokenizer).
44
+ text_encoder ([`CLIPTextModel`]):
45
+ Frozen text-encoder.
46
+ unet ([`UNet2DConditionModel`]): Conditional U-Net architecture to denoise the encoded image latents.
47
+ scheduler ([`KarrasDiffusionSchedulers`]):
48
+ A scheduler to be used in combination with `unet` to denoise the encoded image latents.
49
+ vae ([`AutoencoderKL`]):
50
+ Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations.
51
+ """
52
+ # image encoding components
53
+ feature_extractor: CLIPImageProcessor
54
+ image_encoder: CLIPVisionModelWithProjection
55
+ # image noising components
56
+ image_normalizer: StableUnCLIPImageNormalizer
57
+ image_noising_scheduler: KarrasDiffusionSchedulers
58
+ # regular denoising components
59
+ tokenizer: CLIPTokenizer
60
+ text_encoder: CLIPTextModel
61
+ unet: UNet2DConditionModel
62
+ scheduler: KarrasDiffusionSchedulers
63
+ vae: AutoencoderKL
64
+
65
+ def __init__(
66
+ self,
67
+ # image encoding components
68
+ feature_extractor: CLIPImageProcessor,
69
+ image_encoder: CLIPVisionModelWithProjection,
70
+ # image noising components
71
+ image_normalizer: StableUnCLIPImageNormalizer,
72
+ image_noising_scheduler: KarrasDiffusionSchedulers,
73
+ # regular denoising components
74
+ tokenizer: CLIPTokenizer,
75
+ text_encoder: CLIPTextModel,
76
+ unet: UNet2DConditionModel,
77
+ scheduler: KarrasDiffusionSchedulers,
78
+ # vae
79
+ vae: AutoencoderKL,
80
+ num_views: int = 7,
81
+ ):
82
+ super().__init__()
83
+
84
+ self.register_modules(
85
+ feature_extractor=feature_extractor,
86
+ image_encoder=image_encoder,
87
+ image_normalizer=image_normalizer,
88
+ image_noising_scheduler=image_noising_scheduler,
89
+ tokenizer=tokenizer,
90
+ text_encoder=text_encoder,
91
+ unet=unet,
92
+ scheduler=scheduler,
93
+ vae=vae,
94
+ )
95
+ self.vae_scale_factor = 2 ** (len(self.vae.config.block_out_channels) - 1)
96
+ self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor)
97
+ self.num_views: int = num_views
98
+ # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.enable_vae_slicing
99
+ def enable_vae_slicing(self):
100
+ r"""
101
+ Enable sliced VAE decoding.
102
+
103
+ When this option is enabled, the VAE will split the input tensor in slices to compute decoding in several
104
+ steps. This is useful to save some memory and allow larger batch sizes.
105
+ """
106
+ self.vae.enable_slicing()
107
+
108
+ # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.disable_vae_slicing
109
+ def disable_vae_slicing(self):
110
+ r"""
111
+ Disable sliced VAE decoding. If `enable_vae_slicing` was previously invoked, this method will go back to
112
+ computing decoding in one step.
113
+ """
114
+ self.vae.disable_slicing()
115
+
116
+ def enable_sequential_cpu_offload(self, gpu_id=0):
117
+ r"""
118
+ Offloads all models to CPU using accelerate, significantly reducing memory usage. When called, the pipeline's
119
+ models have their state dicts saved to CPU and then are moved to a `torch.device('meta') and loaded to GPU only
120
+ when their specific submodule has its `forward` method called.
121
+ """
122
+ if is_accelerate_available():
123
+ from accelerate import cpu_offload
124
+ else:
125
+ raise ImportError("Please install accelerate via `pip install accelerate`")
126
+
127
+ device = torch.device(f"cuda:{gpu_id}")
128
+
129
+ # TODO: self.image_normalizer.{scale,unscale} are not covered by the offload hooks, so they fails if added to the list
130
+ models = [
131
+ self.image_encoder,
132
+ self.text_encoder,
133
+ self.unet,
134
+ self.vae,
135
+ ]
136
+ for cpu_offloaded_model in models:
137
+ if cpu_offloaded_model is not None:
138
+ cpu_offload(cpu_offloaded_model, device)
139
+
140
+ @property
141
+ # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
142
+ def _execution_device(self):
143
+ r"""
144
+ Returns the device on which the pipeline's models will be executed. After calling
145
+ `pipeline.enable_sequential_cpu_offload()` the execution device can only be inferred from Accelerate's module
146
+ hooks.
147
+ """
148
+ if not hasattr(self.unet, "_hf_hook"):
149
+ return self.device
150
+ for module in self.unet.modules():
151
+ if (
152
+ hasattr(module, "_hf_hook")
153
+ and hasattr(module._hf_hook, "execution_device")
154
+ and module._hf_hook.execution_device is not None
155
+ ):
156
+ return torch.device(module._hf_hook.execution_device)
157
+ return self.device
158
+
159
+ # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._encode_prompt
160
+ def _encode_prompt(
161
+ self,
162
+ prompt,
163
+ device,
164
+ num_images_per_prompt,
165
+ do_classifier_free_guidance,
166
+ negative_prompt=None,
167
+ prompt_embeds: Optional[torch.FloatTensor] = None,
168
+ negative_prompt_embeds: Optional[torch.FloatTensor] = None,
169
+ lora_scale: Optional[float] = None,
170
+ ):
171
+ r"""
172
+ Encodes the prompt into text encoder hidden states.
173
+
174
+ Args:
175
+ prompt (`str` or `List[str]`, *optional*):
176
+ prompt to be encoded
177
+ device: (`torch.device`):
178
+ torch device
179
+ num_images_per_prompt (`int`):
180
+ number of images that should be generated per prompt
181
+ do_classifier_free_guidance (`bool`):
182
+ whether to use classifier free guidance or not
183
+ negative_prompt (`str` or `List[str]`, *optional*):
184
+ The prompt or prompts not to guide the image generation. If not defined, one has to pass
185
+ `negative_prompt_embeds`. instead. If not defined, one has to pass `negative_prompt_embeds`. instead.
186
+ Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`).
187
+ prompt_embeds (`torch.FloatTensor`, *optional*):
188
+ Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
189
+ provided, text embeddings will be generated from `prompt` input argument.
190
+ negative_prompt_embeds (`torch.FloatTensor`, *optional*):
191
+ Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt
192
+ weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input
193
+ argument.
194
+ """
195
+ prompt_embeds = prompt_embeds.to(dtype=self.text_encoder.dtype, device=device)
196
+
197
+
198
+ if do_classifier_free_guidance:
199
+ normal_prompt_embeds, color_prompt_embeds = torch.chunk(prompt_embeds, 2, dim=0)
200
+ prompt_embeds = torch.cat([normal_prompt_embeds, normal_prompt_embeds, color_prompt_embeds, color_prompt_embeds], 0)
201
+
202
+ return prompt_embeds
203
+
204
+ def _encode_image(
205
+ self,
206
+ image_pil,
207
+ smpl_pil,
208
+ device,
209
+ num_images_per_prompt,
210
+ do_classifier_free_guidance,
211
+ noise_level: int=0,
212
+ generator: Optional[torch.Generator] = None
213
+ ):
214
+ dtype = next(self.image_encoder.parameters()).dtype
215
+ # ______________________________clip image embedding______________________________
216
+ image = self.feature_extractor(images=image_pil, return_tensors="pt").pixel_values
217
+ image = image.to(device=device, dtype=dtype)
218
+ image_embeds = self.image_encoder(image).image_embeds
219
+
220
+ image_embeds = self.noise_image_embeddings(
221
+ image_embeds=image_embeds,
222
+ noise_level=noise_level,
223
+ generator=generator,
224
+ )
225
+ # duplicate image embeddings for each generation per prompt, using mps friendly method
226
+ # image_embeds = image_embeds.unsqueeze(1)
227
+ # note: the condition input is same
228
+ image_embeds = image_embeds.repeat(num_images_per_prompt, 1)
229
+
230
+ if do_classifier_free_guidance:
231
+ normal_image_embeds, color_image_embeds = torch.chunk(image_embeds, 2, dim=0)
232
+ negative_prompt_embeds = torch.zeros_like(normal_image_embeds)
233
+
234
+ # For classifier free guidance, we need to do two forward passes.
235
+ # Here we concatenate the unconditional and text embeddings into a single batch
236
+ # to avoid doing two forward passes
237
+ image_embeds = torch.cat([negative_prompt_embeds, normal_image_embeds, negative_prompt_embeds, color_image_embeds], 0)
238
+
239
+ # _____________________________vae input latents__________________________________________________
240
+ def vae_encode(tensor):
241
+ image_pt = torch.stack([TF.to_tensor(img) for img in tensor], dim=0).to(device)
242
+ image_pt = image_pt * 2.0 - 1.0
243
+ image_latents = self.vae.encode(image_pt).latent_dist.mode() * self.vae.config.scaling_factor
244
+ # Note: repeat differently from official pipelines
245
+ image_latents = image_latents.repeat(num_images_per_prompt, 1, 1, 1)
246
+ return image_latents
247
+
248
+ image_latents = vae_encode(image_pil)
249
+ if smpl_pil is not None:
250
+ smpl_latents = vae_encode(smpl_pil)
251
+ image_latents = torch.cat([image_latents, smpl_latents], 1)
252
+
253
+ if do_classifier_free_guidance:
254
+ normal_image_latents, color_image_latents = torch.chunk(image_latents, 2, dim=0)
255
+ image_latents = torch.cat([torch.zeros_like(normal_image_latents), normal_image_latents,
256
+ torch.zeros_like(color_image_latents), color_image_latents], 0)
257
+
258
+ return image_embeds, image_latents
259
+
260
+ # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.decode_latents
261
+ def decode_latents(self, latents):
262
+ latents = 1 / self.vae.config.scaling_factor * latents
263
+ image = self.vae.decode(latents).sample
264
+ image = (image / 2 + 0.5).clamp(0, 1)
265
+ # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
266
+ image = image.cpu().permute(0, 2, 3, 1).float().numpy()
267
+ return image
268
+
269
+ # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.prepare_extra_step_kwargs
270
+ def prepare_extra_step_kwargs(self, generator, eta):
271
+ # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
272
+ # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
273
+ # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502
274
+ # and should be between [0, 1]
275
+
276
+ accepts_eta = "eta" in set(inspect.signature(self.scheduler.step).parameters.keys())
277
+ extra_step_kwargs = {}
278
+ if accepts_eta:
279
+ extra_step_kwargs["eta"] = eta
280
+
281
+ # check if the scheduler accepts generator
282
+ accepts_generator = "generator" in set(inspect.signature(self.scheduler.step).parameters.keys())
283
+ if accepts_generator:
284
+ extra_step_kwargs["generator"] = generator
285
+ return extra_step_kwargs
286
+
287
+ def check_inputs(
288
+ self,
289
+ prompt,
290
+ image,
291
+ height,
292
+ width,
293
+ callback_steps,
294
+ noise_level,
295
+ ):
296
+ if height % 8 != 0 or width % 8 != 0:
297
+ raise ValueError(f"`height` and `width` have to be divisible by 8 but are {height} and {width}.")
298
+
299
+ if (callback_steps is None) or (
300
+ callback_steps is not None and (not isinstance(callback_steps, int) or callback_steps <= 0)
301
+ ):
302
+ raise ValueError(
303
+ f"`callback_steps` has to be a positive integer but is {callback_steps} of type"
304
+ f" {type(callback_steps)}."
305
+ )
306
+
307
+ if prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)):
308
+ raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}")
309
+
310
+
311
+ if noise_level < 0 or noise_level >= self.image_noising_scheduler.config.num_train_timesteps:
312
+ raise ValueError(
313
+ f"`noise_level` must be between 0 and {self.image_noising_scheduler.config.num_train_timesteps - 1}, inclusive."
314
+ )
315
+
316
+ # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.prepare_latents
317
+ def prepare_latents(self, batch_size, num_channels_latents, height, width, dtype, device, generator, latents=None):
318
+ shape = (batch_size, num_channels_latents, height // self.vae_scale_factor, width // self.vae_scale_factor)
319
+ if isinstance(generator, list) and len(generator) != batch_size:
320
+ raise ValueError(
321
+ f"You have passed a list of generators of length {len(generator)}, but requested an effective batch"
322
+ f" size of {batch_size}. Make sure the batch size matches the length of the generators."
323
+ )
324
+
325
+ if latents is None:
326
+ latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype)
327
+ else:
328
+ latents = latents.to(device)
329
+
330
+ # scale the initial noise by the standard deviation required by the scheduler
331
+ latents = latents * self.scheduler.init_noise_sigma
332
+ return latents
333
+
334
+ # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_unclip.StableUnCLIPPipeline.noise_image_embeddings
335
+ def noise_image_embeddings(
336
+ self,
337
+ image_embeds: torch.Tensor,
338
+ noise_level: int,
339
+ noise: Optional[torch.FloatTensor] = None,
340
+ generator: Optional[torch.Generator] = None,
341
+ ):
342
+ """
343
+ Add noise to the image embeddings. The amount of noise is controlled by a `noise_level` input. A higher
344
+ `noise_level` increases the variance in the final un-noised images.
345
+
346
+ The noise is applied in two ways
347
+ 1. A noise schedule is applied directly to the embeddings
348
+ 2. A vector of sinusoidal time embeddings are appended to the output.
349
+
350
+ In both cases, the amount of noise is controlled by the same `noise_level`.
351
+
352
+ The embeddings are normalized before the noise is applied and un-normalized after the noise is applied.
353
+ """
354
+ if noise is None:
355
+ noise = randn_tensor(
356
+ image_embeds.shape, generator=generator, device=image_embeds.device, dtype=image_embeds.dtype
357
+ )
358
+
359
+ noise_level = torch.tensor([noise_level] * image_embeds.shape[0], device=image_embeds.device)
360
+
361
+ image_embeds = self.image_normalizer.scale(image_embeds)
362
+
363
+ image_embeds = self.image_noising_scheduler.add_noise(image_embeds, timesteps=noise_level, noise=noise)
364
+
365
+ image_embeds = self.image_normalizer.unscale(image_embeds)
366
+
367
+ noise_level = get_timestep_embedding(
368
+ timesteps=noise_level, embedding_dim=image_embeds.shape[-1], flip_sin_to_cos=True, downscale_freq_shift=0
369
+ )
370
+
371
+ # `get_timestep_embeddings` does not contain any weights and will always return f32 tensors,
372
+ # but we might actually be running in fp16. so we need to cast here.
373
+ # there might be better ways to encapsulate this.
374
+ noise_level = noise_level.to(image_embeds.dtype)
375
+
376
+ image_embeds = torch.cat((image_embeds, noise_level), 1)
377
+
378
+ return image_embeds
379
+ def process_dino_feature(self, feat, device, num_images_per_prompt, do_classifier_free_guidance):
380
+ feat = feat.to(dtype=self.text_encoder.dtype, device=device)
381
+ if do_classifier_free_guidance:
382
+ # # duplicate unconditional embeddings for each generation per prompt, using mps friendly method
383
+ # seq_len = negative_prompt_embeds.shape[1]
384
+
385
+ # negative_prompt_embeds = negative_prompt_embeds.to(dtype=self.text_encoder.dtype, device=device)
386
+
387
+ # negative_prompt_embeds = negative_prompt_embeds.repeat(1, num_images_per_prompt, 1)
388
+ # negative_prompt_embeds = negative_prompt_embeds.view(batch_size * num_images_per_prompt, seq_len, -1)
389
+
390
+ # For classifier free guidance, we need to do two forward passes.
391
+ # Here we concatenate the unconditional and text embeddings into a single batch
392
+ # to avoid doing two forward passes
393
+ feat = torch.cat([feat, feat], 0)
394
+ return feat
395
+ @torch.no_grad()
396
+ # @replace_example_docstring(EXAMPLE_DOC_STRING)
397
+ def __call__(
398
+ self,
399
+ image: Union[torch.FloatTensor, PIL.Image.Image],
400
+ prompt: Union[str, List[str]],
401
+ prompt_embeds: torch.FloatTensor = None,
402
+ dino_feature: torch.FloatTensor = None,
403
+ height: Optional[int] = None,
404
+ width: Optional[int] = None,
405
+ num_inference_steps: int = 20,
406
+ guidance_scale: float = 10,
407
+ negative_prompt: Optional[Union[str, List[str]]] = None,
408
+ num_images_per_prompt: Optional[int] = 1,
409
+ eta: float = 0.0,
410
+ generator: Optional[torch.Generator] = None,
411
+ latents: Optional[torch.FloatTensor] = None,
412
+ negative_prompt_embeds: Optional[torch.FloatTensor] = None,
413
+ output_type: Optional[str] = "pil",
414
+ return_dict: bool = True,
415
+ callback: Optional[Callable[[int, int, torch.FloatTensor], None]] = None,
416
+ callback_steps: int = 1,
417
+ cross_attention_kwargs: Optional[Dict[str, Any]] = None,
418
+ noise_level: int = 0,
419
+ image_embeds: Optional[torch.FloatTensor] = None,
420
+ gt_img_in: Optional[torch.FloatTensor] = None,
421
+ smpl_in: Optional[torch.FloatTensor] = None,
422
+ ):
423
+ r"""
424
+ Function invoked when calling the pipeline for generation.
425
+
426
+ Args:
427
+ prompt (`str` or `List[str]`, *optional*):
428
+ The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`.
429
+ instead.
430
+ image (`torch.FloatTensor` or `PIL.Image.Image`):
431
+ `Image`, or tensor representing an image batch. The image will be encoded to its CLIP embedding which
432
+ the unet will be conditioned on. Note that the image is _not_ encoded by the vae and then used as the
433
+ latents in the denoising process such as in the standard stable diffusion text guided image variation
434
+ process.
435
+ height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor):
436
+ The height in pixels of the generated image.
437
+ width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor):
438
+ The width in pixels of the generated image.
439
+ num_inference_steps (`int`, *optional*, defaults to 20):
440
+ The number of denoising steps. More denoising steps usually lead to a higher quality image at the
441
+ expense of slower inference.
442
+ guidance_scale (`float`, *optional*, defaults to 10.0):
443
+ Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598).
444
+ `guidance_scale` is defined as `w` of equation 2. of [Imagen
445
+ Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale >
446
+ 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`,
447
+ usually at the expense of lower image quality.
448
+ negative_prompt (`str` or `List[str]`, *optional*):
449
+ The prompt or prompts not to guide the image generation. If not defined, one has to pass
450
+ `negative_prompt_embeds`. instead. If not defined, one has to pass `negative_prompt_embeds`. instead.
451
+ Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`).
452
+ num_images_per_prompt (`int`, *optional*, defaults to 1):
453
+ The number of images to generate per prompt.
454
+ eta (`float`, *optional*, defaults to 0.0):
455
+ Corresponds to parameter eta (η) in the DDIM paper: https://arxiv.org/abs/2010.02502. Only applies to
456
+ [`schedulers.DDIMScheduler`], will be ignored for others.
457
+ generator (`torch.Generator` or `List[torch.Generator]`, *optional*):
458
+ One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html)
459
+ to make generation deterministic.
460
+ latents (`torch.FloatTensor`, *optional*):
461
+ Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
462
+ generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
463
+ tensor will ge generated by sampling using the supplied random `generator`.
464
+ prompt_embeds (`torch.FloatTensor`, *optional*):
465
+ Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
466
+ provided, text embeddings will be generated from `prompt` input argument.
467
+ negative_prompt_embeds (`torch.FloatTensor`, *optional*):
468
+ Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt
469
+ weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input
470
+ argument.
471
+ output_type (`str`, *optional*, defaults to `"pil"`):
472
+ The output format of the generate image. Choose between
473
+ [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`.
474
+ return_dict (`bool`, *optional*, defaults to `True`):
475
+ Whether or not to return a [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] instead of a
476
+ plain tuple.
477
+ callback (`Callable`, *optional*):
478
+ A function that will be called every `callback_steps` steps during inference. The function will be
479
+ called with the following arguments: `callback(step: int, timestep: int, latents: torch.FloatTensor)`.
480
+ callback_steps (`int`, *optional*, defaults to 1):
481
+ The frequency at which the `callback` function will be called. If not specified, the callback will be
482
+ called at every step.
483
+ cross_attention_kwargs (`dict`, *optional*):
484
+ A kwargs dictionary that if specified is passed along to the `AttnProcessor` as defined under
485
+ `self.processor` in
486
+ [diffusers.cross_attention](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/cross_attention.py).
487
+ noise_level (`int`, *optional*, defaults to `0`):
488
+ The amount of noise to add to the image embeddings. A higher `noise_level` increases the variance in
489
+ the final un-noised images. See `StableUnCLIPPipeline.noise_image_embeddings` for details.
490
+ image_embeds (`torch.FloatTensor`, *optional*):
491
+ Pre-generated CLIP embeddings to condition the unet on. Note that these are not latents to be used in
492
+ the denoising process. If you want to provide pre-generated latents, pass them to `__call__` as
493
+ `latents`.
494
+
495
+ Examples:
496
+
497
+ Returns:
498
+ [`~pipelines.ImagePipelineOutput`] or `tuple`: [`~ pipeline_utils.ImagePipelineOutput`] if `return_dict` is
499
+ True, otherwise a `tuple`. When returning a tuple, the first element is a list with the generated images.
500
+ """
501
+ # 0. Default height and width to unet
502
+ height = height or self.unet.config.sample_size * self.vae_scale_factor
503
+ width = width or self.unet.config.sample_size * self.vae_scale_factor
504
+
505
+ # 1. Check inputs. Raise error if not correct
506
+ self.check_inputs(
507
+ prompt=prompt,
508
+ image=image,
509
+ height=height,
510
+ width=width,
511
+ callback_steps=callback_steps,
512
+ noise_level=noise_level
513
+ )
514
+
515
+ # 2. Define call parameters
516
+ if isinstance(image, list):
517
+ batch_size = len(image)
518
+ elif isinstance(image, torch.Tensor):
519
+ batch_size = image.shape[0]
520
+ assert batch_size >= self.num_views and batch_size % self.num_views == 0
521
+ elif isinstance(image, PIL.Image.Image):
522
+ image = [image]*self.num_views*2
523
+ batch_size = self.num_views*2
524
+
525
+ if isinstance(prompt, str):
526
+ prompt = [prompt] * self.num_views * 2
527
+
528
+ device = self._execution_device
529
+
530
+ # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
531
+ # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
532
+ # corresponds to doing no classifier free guidance.
533
+ do_classifier_free_guidance = guidance_scale != 1.0
534
+
535
+ # 3. Encode input prompt
536
+ text_encoder_lora_scale = (
537
+ cross_attention_kwargs.get("scale", None) if cross_attention_kwargs is not None else None
538
+ )
539
+ prompt_embeds = self._encode_prompt(
540
+ prompt=prompt,
541
+ device=device,
542
+ num_images_per_prompt=num_images_per_prompt,
543
+ do_classifier_free_guidance=do_classifier_free_guidance,
544
+ negative_prompt=negative_prompt,
545
+ prompt_embeds=prompt_embeds,
546
+ negative_prompt_embeds=negative_prompt_embeds,
547
+ lora_scale=text_encoder_lora_scale,
548
+ )
549
+
550
+ if dino_feature is not None:
551
+ dino_feature = self.process_dino_feature(dino_feature, device=device,
552
+ do_classifier_free_guidance=do_classifier_free_guidance,
553
+ num_images_per_prompt=num_images_per_prompt)
554
+
555
+ # 4. Encoder input image
556
+ if isinstance(image, list):
557
+ image_pil = image
558
+ smpl_pil = smpl_in
559
+ elif isinstance(image, torch.Tensor):
560
+ image_pil = [TF.to_pil_image(image[i]) for i in range(image.shape[0])]
561
+ smpl_pil = [TF.to_pil_image(smpl_in[i]) for i in range(smpl_in.shape[0])] if smpl_in is not None else None
562
+ noise_level = torch.tensor([noise_level], device=device)
563
+ image_embeds, image_latents = self._encode_image(
564
+ image_pil=image_pil,
565
+ smpl_pil=smpl_pil,
566
+ device=device,
567
+ num_images_per_prompt=num_images_per_prompt,
568
+ do_classifier_free_guidance=do_classifier_free_guidance,
569
+ noise_level=noise_level,
570
+ generator=generator,
571
+ )
572
+
573
+ # 5. Prepare timesteps
574
+ self.scheduler.set_timesteps(num_inference_steps, device=device)
575
+ timesteps = self.scheduler.timesteps
576
+
577
+ # 6. Prepare latent variables
578
+ num_channels_latents = self.unet.config.out_channels
579
+ if gt_img_in is not None:
580
+ latents = gt_img_in * self.scheduler.init_noise_sigma
581
+ else:
582
+ latents = self.prepare_latents(
583
+ batch_size=batch_size,
584
+ num_channels_latents=num_channels_latents,
585
+ height=height,
586
+ width=width,
587
+ dtype=prompt_embeds.dtype,
588
+ device=device,
589
+ generator=generator,
590
+ latents=latents,
591
+ )
592
+
593
+ # 7. Prepare extra step kwargs. TODO: Logic should ideally just be moved out of the pipeline
594
+ extra_step_kwargs = self.prepare_extra_step_kwargs(generator, eta)
595
+
596
+ eles, focals = [], []
597
+ # 8. Denoising loop
598
+ for i, t in enumerate(self.progress_bar(timesteps)):
599
+ if do_classifier_free_guidance:
600
+ normal_latents, color_latents = torch.chunk(latents, 2, dim=0)
601
+ latent_model_input = torch.cat([normal_latents, normal_latents, color_latents, color_latents], 0)
602
+ else:
603
+ latent_model_input = latents
604
+ latent_model_input = torch.cat([
605
+ latent_model_input, image_latents
606
+ ], dim=1)
607
+ latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)
608
+
609
+ # predict the noise residual
610
+ unet_out = self.unet(
611
+ latent_model_input,
612
+ t,
613
+ encoder_hidden_states=prompt_embeds,
614
+ dino_feature=dino_feature,
615
+ class_labels=image_embeds,
616
+ cross_attention_kwargs=cross_attention_kwargs,
617
+ return_dict=False)
618
+
619
+ noise_pred = unet_out[0]
620
+
621
+
622
+ # perform guidance
623
+ if do_classifier_free_guidance:
624
+ normal_noise_pred_uncond, normal_noise_pred_text, color_noise_pred_uncond, color_noise_pred_text = torch.chunk(noise_pred, 4, dim=0)
625
+
626
+ noise_pred_uncond, noise_pred_text = torch.cat([normal_noise_pred_uncond, color_noise_pred_uncond], 0), torch.cat([normal_noise_pred_text, color_noise_pred_text], 0)
627
+ noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
628
+
629
+ # compute the previous noisy sample x_t -> x_t-1
630
+ latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs, return_dict=False)[0]
631
+
632
+ if callback is not None and i % callback_steps == 0:
633
+ callback(i, t, latents)
634
+
635
+ # 9. Post-processing
636
+ if not output_type == "latent":
637
+ if num_channels_latents == 8:
638
+ latents = torch.cat([latents[:, :4], latents[:, 4:]], dim=0)
639
+ with torch.no_grad():
640
+ image = self.vae.decode(latents / self.vae.config.scaling_factor, return_dict=False)[0]
641
+ else:
642
+ image = latents
643
+
644
+ image = self.image_processor.postprocess(image, output_type=output_type)
645
+
646
+ # Offload last model to CPU
647
+ # if hasattr(self, "final_offload_hook") and self.final_offload_hook is not None:
648
+ # self.final_offload_hook.offload()
649
+ if not return_dict:
650
+ return (image, )
651
+ return ImagePipelineOutput(images=image)
pipeline/pshuman_local.py CHANGED
@@ -51,7 +51,15 @@ def _ensure_repo() -> None:
51
  ["git", "clone", "--depth=1", _PSHUMAN_REPO, str(_PSHUMAN_SRC)],
52
  check=True,
53
  )
54
- print("[pshuman] Repo cloned.")
 
 
 
 
 
 
 
 
55
 
56
 
57
  def _ensure_sys_path() -> None:
 
51
  ["git", "clone", "--depth=1", _PSHUMAN_REPO, str(_PSHUMAN_SRC)],
52
  check=True,
53
  )
54
+ # Apply pre-patched files (e.g. transformers>=5.0 compatibility fixes)
55
+ import shutil as _shutil
56
+ _patches = Path(__file__).parent.parent / "patches" / "pshuman"
57
+ for _pf in _patches.rglob("*"):
58
+ if _pf.is_file():
59
+ _dest = _PSHUMAN_SRC / _pf.relative_to(_patches)
60
+ _dest.parent.mkdir(parents=True, exist_ok=True)
61
+ _shutil.copy2(str(_pf), str(_dest))
62
+ print("[pshuman] Repo cloned + patches applied.")
63
 
64
 
65
  def _ensure_sys_path() -> None: