Postprocess the image output from tensor to `output_type`. Args: image (`torch.FloatTensor`): The image input, should be a pytorch tensor with shape `B x C x H x W`. output_type (`str`, *optional*, defaults to `pil`): The outp
(
self,
image: torch.FloatTensor,
output_type: str = "pil",
do_denormalize: Optional[List[bool]] = None,
)
| 352 | return image |
| 353 | |
| 354 | def postprocess( |
| 355 | self, |
| 356 | image: torch.FloatTensor, |
| 357 | output_type: str = "pil", |
| 358 | do_denormalize: Optional[List[bool]] = None, |
| 359 | ) -> Union[PIL.Image.Image, np.ndarray, torch.FloatTensor]: |
| 360 | """ |
| 361 | Postprocess the image output from tensor to `output_type`. |
| 362 | |
| 363 | Args: |
| 364 | image (`torch.FloatTensor`): |
| 365 | The image input, should be a pytorch tensor with shape `B x C x H x W`. |
| 366 | output_type (`str`, *optional*, defaults to `pil`): |
| 367 | The output type of the image, can be one of `pil`, `np`, `pt`, `latent`. |
| 368 | do_denormalize (`List[bool]`, *optional*, defaults to `None`): |
| 369 | Whether to denormalize the image to [0,1]. If `None`, will use the value of `do_normalize` in the |
| 370 | `VaeImageProcessor` config. |
| 371 | |
| 372 | Returns: |
| 373 | `PIL.Image.Image`, `np.ndarray` or `torch.FloatTensor`: |
| 374 | The postprocessed image. |
| 375 | """ |
| 376 | if not isinstance(image, torch.Tensor): |
| 377 | raise ValueError( |
| 378 | f"Input for postprocessing is in incorrect format: {type(image)}. We only support pytorch tensor" |
| 379 | ) |
| 380 | if output_type not in ["latent", "pt", "np", "pil"]: |
| 381 | deprecation_message = ( |
| 382 | f"the output_type {output_type} is outdated and has been set to `np`. Please make sure to set it to one of these instead: " |
| 383 | "`pil`, `np`, `pt`, `latent`" |
| 384 | ) |
| 385 | deprecate("Unsupported output_type", "1.0.0", deprecation_message, standard_warn=False) |
| 386 | output_type = "np" |
| 387 | |
| 388 | if output_type == "latent": |
| 389 | return image |
| 390 | |
| 391 | if do_denormalize is None: |
| 392 | do_denormalize = [self.config.do_normalize] * image.shape[0] |
| 393 | |
| 394 | image = torch.stack( |
| 395 | [self.denormalize(image[i]) if do_denormalize[i] else image[i] for i in range(image.shape[0])] |
| 396 | ) |
| 397 | |
| 398 | if output_type == "pt": |
| 399 | return image |
| 400 | |
| 401 | image = self.pt_to_numpy(image) |
| 402 | |
| 403 | if output_type == "np": |
| 404 | return image |
| 405 | |
| 406 | if output_type == "pil": |
| 407 | return self.numpy_to_pil(image) |
| 408 | |
| 409 | |
| 410 | class VaeImageProcessorLDM3D(VaeImageProcessor): |