| 427 | |
| 428 | |
| 429 | class Encoder(nn.Module): |
| 430 | def __init__(self, *, ch, out_ch, ch_mult=(1, 2, 4, 8), num_res_blocks, |
| 431 | attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels, |
| 432 | resolution, z_channels, double_z=True, **ignore_kwargs): |
| 433 | super().__init__() |
| 434 | self.ch = ch |
| 435 | self.temb_ch = 0 |
| 436 | self.num_resolutions = len(ch_mult) |
| 437 | self.num_res_blocks = num_res_blocks |
| 438 | self.resolution = resolution |
| 439 | self.in_channels = in_channels |
| 440 | |
| 441 | # downsampling |
| 442 | self.conv_in = torch.nn.Conv2d(in_channels, |
| 443 | self.ch, |
| 444 | kernel_size=3, |
| 445 | stride=1, |
| 446 | padding=1) |
| 447 | |
| 448 | curr_res = resolution |
| 449 | in_ch_mult = (1,)+tuple(ch_mult) |
| 450 | self.down = nn.ModuleList() |
| 451 | for i_level in range(self.num_resolutions): |
| 452 | block = nn.ModuleList() |
| 453 | attn = nn.ModuleList() |
| 454 | block_in = ch*in_ch_mult[i_level] |
| 455 | block_out = ch*ch_mult[i_level] |
| 456 | for i_block in range(self.num_res_blocks): |
| 457 | block.append(ResnetBlock(in_channels=block_in, |
| 458 | out_channels=block_out, |
| 459 | temb_channels=self.temb_ch, |
| 460 | dropout=dropout)) |
| 461 | block_in = block_out |
| 462 | if curr_res in attn_resolutions: |
| 463 | attn.append(AttnBlock(block_in)) |
| 464 | down = nn.Module() |
| 465 | down.block = block |
| 466 | down.attn = attn |
| 467 | if i_level != self.num_resolutions-1: |
| 468 | down.downsample = Downsample(block_in, resamp_with_conv) |
| 469 | curr_res = curr_res // 2 |
| 470 | self.down.append(down) |
| 471 | |
| 472 | # middle |
| 473 | self.mid = nn.Module() |
| 474 | self.mid.block_1 = ResnetBlock(in_channels=block_in, |
| 475 | out_channels=block_in, |
| 476 | temb_channels=self.temb_ch, |
| 477 | dropout=dropout) |
| 478 | self.mid.attn_1 = AttnBlock(block_in) |
| 479 | self.mid.block_2 = ResnetBlock(in_channels=block_in, |
| 480 | out_channels=block_in, |
| 481 | temb_channels=self.temb_ch, |
| 482 | dropout=dropout) |
| 483 | |
| 484 | # end |
| 485 | self.norm_out = Normalize(block_in) |
| 486 | self.conv_out = torch.nn.Conv2d(block_in, |
nothing calls this directly
no outgoing calls
no test coverage detected