Postprocess the image output from tensor to `output_type`. Args: image (`torch.Tensor`): 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 output ty
(
self,
image: torch.Tensor,
output_type: str = "pil",
do_denormalize: Optional[List[bool]] = None,
)
| 594 | return image |
| 595 | |
| 596 | def postprocess( |
| 597 | self, |
| 598 | image: torch.Tensor, |
| 599 | output_type: str = "pil", |
| 600 | do_denormalize: Optional[List[bool]] = None, |
| 601 | ) -> Union[PIL.Image.Image, np.ndarray, torch.Tensor]: |
| 602 | """ |
| 603 | Postprocess the image output from tensor to `output_type`. |
| 604 | |
| 605 | Args: |
| 606 | image (`torch.Tensor`): |
| 607 | The image input, should be a pytorch tensor with shape `B x C x H x W`. |
| 608 | output_type (`str`, *optional*, defaults to `pil`): |
| 609 | The output type of the image, can be one of `pil`, `np`, `pt`, `latent`. |
| 610 | do_denormalize (`List[bool]`, *optional*, defaults to `None`): |
| 611 | Whether to denormalize the image to [0,1]. If `None`, will use the value of `do_normalize` in the |
| 612 | `VaeImageProcessor` config. |
| 613 | |
| 614 | Returns: |
| 615 | `PIL.Image.Image`, `np.ndarray` or `torch.Tensor`: |
| 616 | The postprocessed image. |
| 617 | """ |
| 618 | if not isinstance(image, torch.Tensor): |
| 619 | raise ValueError( |
| 620 | f"Input for postprocessing is in incorrect format: {type(image)}. We only support pytorch tensor" |
| 621 | ) |
| 622 | if output_type not in ["latent", "pt", "np", "pil"]: |
| 623 | deprecation_message = ( |
| 624 | 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: " |
| 625 | "`pil`, `np`, `pt`, `latent`" |
| 626 | ) |
| 627 | deprecate("Unsupported output_type", "1.0.0", deprecation_message, standard_warn=False) |
| 628 | output_type = "np" |
| 629 | |
| 630 | if output_type == "latent": |
| 631 | return image |
| 632 | |
| 633 | if do_denormalize is None: |
| 634 | do_denormalize = [self.config.do_normalize] * image.shape[0] |
| 635 | |
| 636 | image = torch.stack( |
| 637 | [self.denormalize(image[i]) if do_denormalize[i] else image[i] for i in range(image.shape[0])] |
| 638 | ) |
| 639 | |
| 640 | if output_type == "pt": |
| 641 | return image |
| 642 | |
| 643 | image = self.pt_to_numpy(image) |
| 644 | |
| 645 | if output_type == "np": |
| 646 | return image |
| 647 | |
| 648 | if output_type == "pil": |
| 649 | return self.numpy_to_pil(image) |
| 650 | |
| 651 | def apply_overlay( |
| 652 | self, |