| 340 | |
| 341 | |
| 342 | class Encoder(nn.Module): |
| 343 | def __init__(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks, |
| 344 | attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels, |
| 345 | resolution, z_channels, double_z=True, **ignore_kwargs): |
| 346 | super().__init__() |
| 347 | self.ch = ch |
| 348 | self.temb_ch = 0 |
| 349 | self.num_resolutions = len(ch_mult) |
| 350 | self.num_res_blocks = num_res_blocks |
| 351 | self.resolution = resolution |
| 352 | self.in_channels = in_channels |
| 353 | |
| 354 | # downsampling |
| 355 | self.conv_in = torch.nn.Conv2d(in_channels, |
| 356 | self.ch, |
| 357 | kernel_size=3, |
| 358 | stride=1, |
| 359 | padding=1) |
| 360 | |
| 361 | curr_res = resolution |
| 362 | in_ch_mult = (1,)+tuple(ch_mult) |
| 363 | self.down = nn.ModuleList() |
| 364 | for i_level in range(self.num_resolutions): |
| 365 | block = nn.ModuleList() |
| 366 | attn = nn.ModuleList() |
| 367 | block_in = ch*in_ch_mult[i_level] |
| 368 | block_out = ch*ch_mult[i_level] |
| 369 | for i_block in range(self.num_res_blocks): |
| 370 | block.append(ResnetBlock(in_channels=block_in, |
| 371 | out_channels=block_out, |
| 372 | temb_channels=self.temb_ch, |
| 373 | dropout=dropout)) |
| 374 | block_in = block_out |
| 375 | if curr_res in attn_resolutions: |
| 376 | attn.append(AttnBlock(block_in)) |
| 377 | down = nn.Module() |
| 378 | down.block = block |
| 379 | down.attn = attn |
| 380 | if i_level != self.num_resolutions-1: |
| 381 | down.downsample = Downsample(block_in, resamp_with_conv) |
| 382 | curr_res = curr_res // 2 |
| 383 | self.down.append(down) |
| 384 | |
| 385 | # middle |
| 386 | self.mid = nn.Module() |
| 387 | self.mid.block_1 = ResnetBlock(in_channels=block_in, |
| 388 | out_channels=block_in, |
| 389 | temb_channels=self.temb_ch, |
| 390 | dropout=dropout) |
| 391 | self.mid.attn_1 = AttnBlock(block_in) |
| 392 | self.mid.block_2 = ResnetBlock(in_channels=block_in, |
| 393 | out_channels=block_in, |
| 394 | temb_channels=self.temb_ch, |
| 395 | dropout=dropout) |
| 396 | |
| 397 | # end |
| 398 | self.norm_out = Normalize(block_in) |
| 399 | self.conv_out = torch.nn.Conv2d(block_in, |
nothing calls this directly
no outgoing calls
no test coverage detected