| 332 | |
| 333 | |
| 334 | class TransparentVAEDecoder(torch.nn.Module): |
| 335 | def __init__(self, filename, dtype=torch.float16, *args, **kwargs): |
| 336 | super().__init__(*args, **kwargs) |
| 337 | sd = sf.load_file(filename) |
| 338 | model = UNet1024(in_channels=3, out_channels=4) |
| 339 | model.load_state_dict(sd, strict=True) |
| 340 | model.to(dtype=dtype) |
| 341 | model.eval() |
| 342 | self.model = model |
| 343 | self.dtype = dtype |
| 344 | return |
| 345 | |
| 346 | @torch.no_grad() |
| 347 | def estimate_single_pass(self, pixel, latent): |
| 348 | y = self.model(pixel, latent) |
| 349 | return y |
| 350 | |
| 351 | @torch.no_grad() |
| 352 | def estimate_augmented(self, pixel, latent): |
| 353 | args = [ |
| 354 | [False, 0], [False, 1], [False, 2], [False, 3], [True, 0], [True, 1], [True, 2], [True, 3], |
| 355 | ] |
| 356 | |
| 357 | result = [] |
| 358 | |
| 359 | for flip, rok in tqdm(args): |
| 360 | feed_pixel = pixel.clone() |
| 361 | feed_latent = latent.clone() |
| 362 | |
| 363 | if flip: |
| 364 | feed_pixel = torch.flip(feed_pixel, dims=(3,)) |
| 365 | feed_latent = torch.flip(feed_latent, dims=(3,)) |
| 366 | |
| 367 | feed_pixel = torch.rot90(feed_pixel, k=rok, dims=(2, 3)) |
| 368 | feed_latent = torch.rot90(feed_latent, k=rok, dims=(2, 3)) |
| 369 | |
| 370 | eps = self.estimate_single_pass(feed_pixel, feed_latent).clip(0, 1) |
| 371 | eps = torch.rot90(eps, k=-rok, dims=(2, 3)) |
| 372 | |
| 373 | if flip: |
| 374 | eps = torch.flip(eps, dims=(3,)) |
| 375 | |
| 376 | result += [eps] |
| 377 | |
| 378 | result = torch.stack(result, dim=0) |
| 379 | median = torch.median(result, dim=0).values |
| 380 | return median |
| 381 | |
| 382 | @torch.no_grad() |
| 383 | def forward(self, sd_vae, latent): |
| 384 | pixel = sd_vae.decode(latent).sample |
| 385 | pixel = (pixel * 0.5 + 0.5).clip(0, 1).to(self.dtype) |
| 386 | latent = latent.to(self.dtype) |
| 387 | result_list = [] |
| 388 | vis_list = [] |
| 389 | |
| 390 | for i in range(int(latent.shape[0])): |
| 391 | y = self.estimate_augmented(pixel[i:i + 1], latent[i:i + 1]) |
nothing calls this directly
no outgoing calls
no test coverage detected