Image Processor for VAE Args: do_resize (`bool`, *optional*, defaults to `True`): Whether to downscale the image's (height, width) dimensions to multiples of `vae_scale_factor`. vae_scale_factor (`int`, *optional*, defaults to `8`): VAE scale
| 25 | |
| 26 | |
| 27 | class VaeImageProcessor(ConfigMixin): |
| 28 | """ |
| 29 | Image Processor for VAE |
| 30 | |
| 31 | Args: |
| 32 | do_resize (`bool`, *optional*, defaults to `True`): |
| 33 | Whether to downscale the image's (height, width) dimensions to multiples of `vae_scale_factor`. |
| 34 | vae_scale_factor (`int`, *optional*, defaults to `8`): |
| 35 | VAE scale factor. If `do_resize` is True, the image will be automatically resized to multiples of this |
| 36 | factor. |
| 37 | resample (`str`, *optional*, defaults to `lanczos`): |
| 38 | Resampling filter to use when resizing the image. |
| 39 | do_normalize (`bool`, *optional*, defaults to `True`): |
| 40 | Whether to normalize the image to [-1,1] |
| 41 | """ |
| 42 | |
| 43 | config_name = CONFIG_NAME |
| 44 | |
| 45 | @register_to_config |
| 46 | def __init__( |
| 47 | self, |
| 48 | do_resize: bool = True, |
| 49 | vae_scale_factor: int = 8, |
| 50 | resample: str = "lanczos", |
| 51 | do_normalize: bool = True, |
| 52 | ): |
| 53 | super().__init__() |
| 54 | |
| 55 | @staticmethod |
| 56 | def numpy_to_pil(images): |
| 57 | """ |
| 58 | Convert a numpy image or a batch of images to a PIL image. |
| 59 | """ |
| 60 | if images.ndim == 3: |
| 61 | images = images[None, ...] |
| 62 | images = (images * 255).round().astype("uint8") |
| 63 | if images.shape[-1] == 1: |
| 64 | # special case for grayscale (single channel) images |
| 65 | pil_images = [Image.fromarray(image.squeeze(), mode="L") for image in images] |
| 66 | else: |
| 67 | pil_images = [Image.fromarray(image) for image in images] |
| 68 | |
| 69 | return pil_images |
| 70 | |
| 71 | @staticmethod |
| 72 | def numpy_to_pt(images): |
| 73 | """ |
| 74 | Convert a numpy image to a pytorch tensor |
| 75 | """ |
| 76 | if images.ndim == 3: |
| 77 | images = images[..., None] |
| 78 | |
| 79 | images = torch.from_numpy(images.transpose(0, 3, 1, 2)) |
| 80 | return images |
| 81 | |
| 82 | @staticmethod |
| 83 | def pt_to_numpy(images): |
| 84 | """ |
no outgoing calls
no test coverage detected