| 553 | |
| 554 | |
| 555 | class Decoder(nn.Module): |
| 556 | def __init__(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks, |
| 557 | attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels, |
| 558 | resolution, z_channels, give_pre_end=False, tanh_out=False, use_linear_attn=False, |
| 559 | attn_type="vanilla", **ignorekwargs): |
| 560 | super().__init__() |
| 561 | if use_linear_attn: attn_type = "linear" |
| 562 | self.ch = ch |
| 563 | self.temb_ch = 0 |
| 564 | self.num_resolutions = len(ch_mult) |
| 565 | self.num_res_blocks = num_res_blocks |
| 566 | self.resolution = resolution |
| 567 | self.in_channels = in_channels |
| 568 | self.give_pre_end = give_pre_end |
| 569 | self.tanh_out = tanh_out |
| 570 | |
| 571 | # compute in_ch_mult, block_in and curr_res at lowest res |
| 572 | in_ch_mult = (1,)+tuple(ch_mult) |
| 573 | block_in = ch*ch_mult[self.num_resolutions-1] |
| 574 | curr_res = resolution // 2**(self.num_resolutions-1) |
| 575 | self.z_shape = (1,z_channels,curr_res,curr_res) |
| 576 | print("Working with z of shape {} = {} dimensions.".format( |
| 577 | self.z_shape, np.prod(self.z_shape))) |
| 578 | |
| 579 | # z to block_in |
| 580 | self.conv_in = torch.nn.Conv2d(z_channels, |
| 581 | block_in, |
| 582 | kernel_size=3, |
| 583 | stride=1, |
| 584 | padding=1) |
| 585 | |
| 586 | # middle |
| 587 | self.mid = nn.Module() |
| 588 | self.mid.block_1 = ResnetBlock(in_channels=block_in, |
| 589 | out_channels=block_in, |
| 590 | temb_channels=self.temb_ch, |
| 591 | dropout=dropout) |
| 592 | self.mid.attn_1 = make_attn(block_in, attn_type=attn_type) |
| 593 | self.mid.block_2 = ResnetBlock(in_channels=block_in, |
| 594 | out_channels=block_in, |
| 595 | temb_channels=self.temb_ch, |
| 596 | dropout=dropout) |
| 597 | |
| 598 | # upsampling |
| 599 | self.up = nn.ModuleList() |
| 600 | for i_level in reversed(range(self.num_resolutions)): |
| 601 | block = nn.ModuleList() |
| 602 | attn = nn.ModuleList() |
| 603 | block_out = ch*ch_mult[i_level] |
| 604 | for i_block in range(self.num_res_blocks+1): |
| 605 | block.append(ResnetBlock(in_channels=block_in, |
| 606 | out_channels=block_out, |
| 607 | temb_channels=self.temb_ch, |
| 608 | dropout=dropout)) |
| 609 | block_in = block_out |
| 610 | if curr_res in attn_resolutions: |
| 611 | attn.append(make_attn(block_in, attn_type=attn_type)) |
| 612 | up = nn.Module() |