| 441 | return z |
| 442 | |
| 443 | class Decoder(nn.Module): |
| 444 | def __init__(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks, |
| 445 | attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels, |
| 446 | resolution, z_channels, give_pre_end=False, **ignorekwargs): |
| 447 | super().__init__() |
| 448 | self.ch = ch |
| 449 | self.temb_ch = 0 |
| 450 | self.num_resolutions = len(ch_mult) |
| 451 | self.num_res_blocks = num_res_blocks |
| 452 | self.resolution = resolution |
| 453 | self.in_channels = in_channels |
| 454 | self.give_pre_end = give_pre_end |
| 455 | |
| 456 | # compute in_ch_mult, block_in and curr_res at lowest res |
| 457 | in_ch_mult = (1,)+tuple(ch_mult) |
| 458 | block_in = ch*ch_mult[self.num_resolutions-1] |
| 459 | curr_res = resolution // 2**(self.num_resolutions-1) |
| 460 | self.z_shape = (1,z_channels,curr_res,curr_res) |
| 461 | print("Working with z of shape {} = {} dimensions.".format( |
| 462 | self.z_shape, np.prod(self.z_shape))) |
| 463 | |
| 464 | # z to block_in |
| 465 | self.conv_in = torch.nn.Conv2d(z_channels, |
| 466 | block_in, |
| 467 | kernel_size=3, |
| 468 | stride=1, |
| 469 | padding=1) |
| 470 | |
| 471 | # middle |
| 472 | self.mid = nn.Module() |
| 473 | self.mid.block_1 = ResnetBlock(in_channels=block_in, |
| 474 | out_channels=block_in, |
| 475 | temb_channels=self.temb_ch, |
| 476 | dropout=dropout) |
| 477 | self.mid.attn_1 = AttnBlock(block_in) |
| 478 | self.mid.block_2 = ResnetBlock(in_channels=block_in, |
| 479 | out_channels=block_in, |
| 480 | temb_channels=self.temb_ch, |
| 481 | dropout=dropout) |
| 482 | |
| 483 | # upsampling |
| 484 | self.up = nn.ModuleList() |
| 485 | for i_level in reversed(range(self.num_resolutions)): |
| 486 | block = nn.ModuleList() |
| 487 | attn = nn.ModuleList() |
| 488 | block_out = ch*ch_mult[i_level] |
| 489 | for i_block in range(self.num_res_blocks+1): |
| 490 | block.append(ResnetBlock(in_channels=block_in, |
| 491 | out_channels=block_out, |
| 492 | temb_channels=self.temb_ch, |
| 493 | dropout=dropout)) |
| 494 | block_in = block_out |
| 495 | if curr_res in attn_resolutions: |
| 496 | attn.append(AttnBlock(block_in)) |
| 497 | up = nn.Module() |
| 498 | up.block = block |
| 499 | up.attn = attn |
| 500 | if i_level != 0: |
nothing calls this directly
no outgoing calls
no test coverage detected