| 450 | |
| 451 | |
| 452 | class Encoder(nn.Module): |
| 453 | def __init__(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks, |
| 454 | attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels, |
| 455 | resolution, z_channels, double_z=True, use_linear_attn=False, attn_type="vanilla", |
| 456 | **ignore_kwargs): |
| 457 | super().__init__() |
| 458 | if use_linear_attn: attn_type = "linear" |
| 459 | self.ch = ch |
| 460 | self.temb_ch = 0 |
| 461 | self.num_resolutions = len(ch_mult) |
| 462 | self.num_res_blocks = num_res_blocks |
| 463 | self.resolution = resolution |
| 464 | self.in_channels = in_channels |
| 465 | |
| 466 | # downsampling |
| 467 | self.conv_in = torch.nn.Conv2d(in_channels, |
| 468 | self.ch, |
| 469 | kernel_size=3, |
| 470 | stride=1, |
| 471 | padding=1) |
| 472 | |
| 473 | curr_res = resolution |
| 474 | in_ch_mult = (1,)+tuple(ch_mult) |
| 475 | self.in_ch_mult = in_ch_mult |
| 476 | self.down = nn.ModuleList() |
| 477 | for i_level in range(self.num_resolutions): |
| 478 | block = nn.ModuleList() |
| 479 | attn = nn.ModuleList() |
| 480 | block_in = ch*in_ch_mult[i_level] |
| 481 | block_out = ch*ch_mult[i_level] |
| 482 | for i_block in range(self.num_res_blocks): |
| 483 | block.append(ResnetBlock(in_channels=block_in, |
| 484 | out_channels=block_out, |
| 485 | temb_channels=self.temb_ch, |
| 486 | dropout=dropout)) |
| 487 | block_in = block_out |
| 488 | if curr_res in attn_resolutions: |
| 489 | attn.append(make_attn(block_in, attn_type=attn_type)) |
| 490 | down = nn.Module() |
| 491 | down.block = block |
| 492 | down.attn = attn |
| 493 | if i_level != self.num_resolutions-1: |
| 494 | down.downsample = Downsample(block_in, resamp_with_conv) |
| 495 | curr_res = curr_res // 2 |
| 496 | self.down.append(down) |
| 497 | |
| 498 | # middle |
| 499 | self.mid = nn.Module() |
| 500 | self.mid.block_1 = ResnetBlock(in_channels=block_in, |
| 501 | out_channels=block_in, |
| 502 | temb_channels=self.temb_ch, |
| 503 | dropout=dropout) |
| 504 | self.mid.attn_1 = make_attn(block_in, attn_type=attn_type) |
| 505 | self.mid.block_2 = ResnetBlock(in_channels=block_in, |
| 506 | out_channels=block_in, |
| 507 | temb_channels=self.temb_ch, |
| 508 | dropout=dropout) |
| 509 | |