Preprocess the image input, accepted formats are PIL images, numpy arrays or pytorch tensors"
(
self,
image: Union[torch.FloatTensor, PIL.Image.Image, np.ndarray],
)
| 111 | return images |
| 112 | |
| 113 | def preprocess( |
| 114 | self, |
| 115 | image: Union[torch.FloatTensor, PIL.Image.Image, np.ndarray], |
| 116 | ) -> torch.Tensor: |
| 117 | """ |
| 118 | Preprocess the image input, accepted formats are PIL images, numpy arrays or pytorch tensors" |
| 119 | """ |
| 120 | supported_formats = (PIL.Image.Image, np.ndarray, torch.Tensor) |
| 121 | if isinstance(image, supported_formats): |
| 122 | image = [image] |
| 123 | elif not (isinstance(image, list) and all(isinstance(i, supported_formats) for i in image)): |
| 124 | raise ValueError( |
| 125 | f"Input is in incorrect format: {[type(i) for i in image]}. Currently, we only support {', '.join(supported_formats)}" |
| 126 | ) |
| 127 | |
| 128 | if isinstance(image[0], PIL.Image.Image): |
| 129 | if self.config.do_resize: |
| 130 | image = [self.resize(i) for i in image] |
| 131 | image = [np.array(i).astype(np.float32) / 255.0 for i in image] |
| 132 | image = np.stack(image, axis=0) # to np |
| 133 | image = self.numpy_to_pt(image) # to pt |
| 134 | |
| 135 | elif isinstance(image[0], np.ndarray): |
| 136 | image = np.concatenate(image, axis=0) if image[0].ndim == 4 else np.stack(image, axis=0) |
| 137 | image = self.numpy_to_pt(image) |
| 138 | _, _, height, width = image.shape |
| 139 | if self.config.do_resize and ( |
| 140 | height % self.config.vae_scale_factor != 0 or width % self.config.vae_scale_factor != 0 |
| 141 | ): |
| 142 | raise ValueError( |
| 143 | f"Currently we only support resizing for PIL image - please resize your numpy array to be divisible by {self.config.vae_scale_factor}" |
| 144 | f"currently the sizes are {height} and {width}. You can also pass a PIL image instead to use resize option in VAEImageProcessor" |
| 145 | ) |
| 146 | |
| 147 | elif isinstance(image[0], torch.Tensor): |
| 148 | image = torch.cat(image, axis=0) if image[0].ndim == 4 else torch.stack(image, axis=0) |
| 149 | _, _, height, width = image.shape |
| 150 | if self.config.do_resize and ( |
| 151 | height % self.config.vae_scale_factor != 0 or width % self.config.vae_scale_factor != 0 |
| 152 | ): |
| 153 | raise ValueError( |
| 154 | f"Currently we only support resizing for PIL image - please resize your pytorch tensor to be divisible by {self.config.vae_scale_factor}" |
| 155 | f"currently the sizes are {height} and {width}. You can also pass a PIL image instead to use resize option in VAEImageProcessor" |
| 156 | ) |
| 157 | |
| 158 | # expected range [0,1], normalize to [-1,1] |
| 159 | do_normalize = self.config.do_normalize |
| 160 | if image.min() < 0: |
| 161 | warnings.warn( |
| 162 | "Passing `image` as torch tensor with value range in [-1,1] is deprecated. The expected value range for image tensor is [0,1] " |
| 163 | f"when passing as pytorch tensor or numpy Array. You passed `image` with value range [{image.min()},{image.max()}]", |
| 164 | FutureWarning, |
| 165 | ) |
| 166 | do_normalize = False |
| 167 | |
| 168 | if do_normalize: |
| 169 | image = self.normalize(image) |
| 170 |
no test coverage detected