r"""Encode a batch of images using a tiled encoder. Args: When this option is enabled, the VAE will split the input tensor into tiles to compute encoding in several steps. This is useful to keep memory use constant regardless of image size. The end result of tiled encodi
(self, x: torch.FloatTensor, return_dict: bool = True)
| 208 | return b |
| 209 | |
| 210 | def tiled_encode(self, x: torch.FloatTensor, return_dict: bool = True) -> AutoencoderKLOutput: |
| 211 | r"""Encode a batch of images using a tiled encoder. |
| 212 | |
| 213 | Args: |
| 214 | When this option is enabled, the VAE will split the input tensor into tiles to compute encoding in several |
| 215 | steps. This is useful to keep memory use constant regardless of image size. The end result of tiled encoding is: |
| 216 | different from non-tiled encoding due to each tile using a different encoder. To avoid tiling artifacts, the |
| 217 | tiles overlap and are blended together to form a smooth output. You may still see tile-sized changes in the |
| 218 | look of the output, but they should be much less noticeable. |
| 219 | x (`torch.FloatTensor`): Input batch of images. return_dict (`bool`, *optional*, defaults to `True`): |
| 220 | Whether or not to return a [`AutoencoderKLOutput`] instead of a plain tuple. |
| 221 | """ |
| 222 | overlap_size = int(self.tile_sample_min_size * (1 - self.tile_overlap_factor)) |
| 223 | blend_extent = int(self.tile_latent_min_size * self.tile_overlap_factor) |
| 224 | row_limit = self.tile_latent_min_size - blend_extent |
| 225 | |
| 226 | # Split the image into 512x512 tiles and encode them separately. |
| 227 | rows = [] |
| 228 | for i in range(0, x.shape[2], overlap_size): |
| 229 | row = [] |
| 230 | for j in range(0, x.shape[3], overlap_size): |
| 231 | tile = x[:, :, i : i + self.tile_sample_min_size, j : j + self.tile_sample_min_size] |
| 232 | tile = self.encoder(tile) |
| 233 | tile = self.quant_conv(tile) |
| 234 | row.append(tile) |
| 235 | rows.append(row) |
| 236 | result_rows = [] |
| 237 | for i, row in enumerate(rows): |
| 238 | result_row = [] |
| 239 | for j, tile in enumerate(row): |
| 240 | # blend the above tile and the left tile |
| 241 | # to the current tile and add the current tile to the result row |
| 242 | if i > 0: |
| 243 | tile = self.blend_v(rows[i - 1][j], tile, blend_extent) |
| 244 | if j > 0: |
| 245 | tile = self.blend_h(row[j - 1], tile, blend_extent) |
| 246 | result_row.append(tile[:, :, :row_limit, :row_limit]) |
| 247 | result_rows.append(torch.cat(result_row, dim=3)) |
| 248 | |
| 249 | moments = torch.cat(result_rows, dim=2) |
| 250 | posterior = DiagonalGaussianDistribution(moments) |
| 251 | |
| 252 | if not return_dict: |
| 253 | return (posterior,) |
| 254 | |
| 255 | return AutoencoderKLOutput(latent_dist=posterior) |
| 256 | |
| 257 | def tiled_decode(self, z: torch.FloatTensor, return_dict: bool = True) -> Union[DecoderOutput, torch.FloatTensor]: |
| 258 | r"""Decode a batch of images using a tiled decoder. |
no test coverage detected