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