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`. Can accept `height` and `width` arguments from [`image_processor.VaeImageProces
| 58 | |
| 59 | |
| 60 | class VaeImageProcessor(ConfigMixin): |
| 61 | """ |
| 62 | Image processor for VAE. |
| 63 | |
| 64 | Args: |
| 65 | do_resize (`bool`, *optional*, defaults to `True`): |
| 66 | Whether to downscale the image's (height, width) dimensions to multiples of `vae_scale_factor`. Can accept |
| 67 | `height` and `width` arguments from [`image_processor.VaeImageProcessor.preprocess`] method. |
| 68 | vae_scale_factor (`int`, *optional*, defaults to `8`): |
| 69 | VAE scale factor. If `do_resize` is `True`, the image is automatically resized to multiples of this factor. |
| 70 | resample (`str`, *optional*, defaults to `lanczos`): |
| 71 | Resampling filter to use when resizing the image. |
| 72 | do_normalize (`bool`, *optional*, defaults to `True`): |
| 73 | Whether to normalize the image to [-1,1]. |
| 74 | do_binarize (`bool`, *optional*, defaults to `False`): |
| 75 | Whether to binarize the image to 0/1. |
| 76 | do_convert_rgb (`bool`, *optional*, defaults to be `False`): |
| 77 | Whether to convert the images to RGB format. |
| 78 | do_convert_grayscale (`bool`, *optional*, defaults to be `False`): |
| 79 | Whether to convert the images to grayscale format. |
| 80 | """ |
| 81 | |
| 82 | config_name = CONFIG_NAME |
| 83 | |
| 84 | @register_to_config |
| 85 | def __init__( |
| 86 | self, |
| 87 | do_resize: bool = True, |
| 88 | vae_scale_factor: int = 8, |
| 89 | vae_latent_channels: int = 4, |
| 90 | resample: str = "lanczos", |
| 91 | do_normalize: bool = True, |
| 92 | do_binarize: bool = False, |
| 93 | do_convert_rgb: bool = False, |
| 94 | do_convert_grayscale: bool = False, |
| 95 | ): |
| 96 | super().__init__() |
| 97 | if do_convert_rgb and do_convert_grayscale: |
| 98 | raise ValueError( |
| 99 | "`do_convert_rgb` and `do_convert_grayscale` can not both be set to `True`," |
| 100 | " if you intended to convert the image into RGB format, please set `do_convert_grayscale = False`.", |
| 101 | " if you intended to convert the image into grayscale format, please set `do_convert_rgb = False`", |
| 102 | ) |
| 103 | |
| 104 | @staticmethod |
| 105 | def numpy_to_pil(images: np.ndarray) -> List[PIL.Image.Image]: |
| 106 | """ |
| 107 | Convert a numpy image or a batch of images to a PIL image. |
| 108 | """ |
| 109 | if images.ndim == 3: |
| 110 | images = images[None, ...] |
| 111 | images = (images * 255).round().astype("uint8") |
| 112 | if images.shape[-1] == 1: |
| 113 | # special case for grayscale (single channel) images |
| 114 | pil_images = [Image.fromarray(image.squeeze(), mode="L") for image in images] |
| 115 | else: |
| 116 | pil_images = [Image.fromarray(image) for image in images] |
| 117 |
no outgoing calls