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,
)
| 555 | return image |
| 556 | |
| 557 | def postprocess( |
| 558 | self, |
| 559 | image: torch.FloatTensor, |
| 560 | output_type: str = "pil", |
| 561 | do_denormalize: Optional[List[bool]] = None, |
| 562 | ) -> Union[PIL.Image.Image, np.ndarray, torch.FloatTensor]: |
| 563 | """ |
| 564 | Postprocess the image output from tensor to `output_type`. |
| 565 | |
| 566 | Args: |
| 567 | image (`torch.FloatTensor`): |
| 568 | The image input, should be a pytorch tensor with shape `B x C x H x W`. |
| 569 | output_type (`str`, *optional*, defaults to `pil`): |
| 570 | The output type of the image, can be one of `pil`, `np`, `pt`, `latent`. |
| 571 | do_denormalize (`List[bool]`, *optional*, defaults to `None`): |
| 572 | Whether to denormalize the image to [0,1]. If `None`, will use the value of `do_normalize` in the |
| 573 | `VaeImageProcessor` config. |
| 574 | |
| 575 | Returns: |
| 576 | `PIL.Image.Image`, `np.ndarray` or `torch.FloatTensor`: |
| 577 | The postprocessed image. |
| 578 | """ |
| 579 | if not isinstance(image, torch.Tensor): |
| 580 | raise ValueError( |
| 581 | f"Input for postprocessing is in incorrect format: {type(image)}. We only support pytorch tensor" |
| 582 | ) |
| 583 | if output_type not in ["latent", "pt", "np", "pil"]: |
| 584 | deprecation_message = ( |
| 585 | 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: " |
| 586 | "`pil`, `np`, `pt`, `latent`" |
| 587 | ) |
| 588 | deprecate("Unsupported output_type", "1.0.0", deprecation_message, standard_warn=False) |
| 589 | output_type = "np" |
| 590 | |
| 591 | if output_type == "latent": |
| 592 | return image |
| 593 | |
| 594 | if do_denormalize is None: |
| 595 | do_denormalize = [self.config.do_normalize] * image.shape[0] |
| 596 | |
| 597 | image = torch.stack( |
| 598 | [self.denormalize(image[i]) if do_denormalize[i] else image[i] for i in range(image.shape[0])] |
| 599 | ) |
| 600 | |
| 601 | if output_type == "pt": |
| 602 | return image |
| 603 | |
| 604 | image = self.pt_to_numpy(image) |
| 605 | |
| 606 | if output_type == "np": |
| 607 | return image |
| 608 | |
| 609 | if output_type == "pil": |
| 610 | return self.numpy_to_pil(image) |
| 611 | |
| 612 | def apply_overlay( |
| 613 | self, |