(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks,
attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels,
resolution, z_channels, double_z=True, use_linear_attn=False, attn_type="vanilla",
**ignore_kwargs)
| 367 | |
| 368 | class Encoder(nn.Module): |
| 369 | def __init__(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks, |
| 370 | attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels, |
| 371 | resolution, z_channels, double_z=True, use_linear_attn=False, attn_type="vanilla", |
| 372 | **ignore_kwargs): |
| 373 | super().__init__() |
| 374 | if use_linear_attn: attn_type = "linear" |
| 375 | self.ch = ch |
| 376 | self.temb_ch = 0 |
| 377 | self.num_resolutions = len(ch_mult) |
| 378 | self.num_res_blocks = num_res_blocks |
| 379 | self.resolution = resolution |
| 380 | self.in_channels = in_channels |
| 381 | |
| 382 | # downsampling |
| 383 | self.conv_in = torch.nn.Conv2d(in_channels, |
| 384 | self.ch, |
| 385 | kernel_size=3, |
| 386 | stride=1, |
| 387 | padding=1) |
| 388 | |
| 389 | curr_res = resolution |
| 390 | in_ch_mult = (1,)+tuple(ch_mult) |
| 391 | self.in_ch_mult = in_ch_mult |
| 392 | self.down = nn.ModuleList() |
| 393 | for i_level in range(self.num_resolutions): |
| 394 | block = nn.ModuleList() |
| 395 | attn = nn.ModuleList() |
| 396 | block_in = ch*in_ch_mult[i_level] |
| 397 | block_out = ch*ch_mult[i_level] |
| 398 | for i_block in range(self.num_res_blocks): |
| 399 | block.append(ResnetBlock(in_channels=block_in, |
| 400 | out_channels=block_out, |
| 401 | temb_channels=self.temb_ch, |
| 402 | dropout=dropout)) |
| 403 | block_in = block_out |
| 404 | if curr_res in attn_resolutions: |
| 405 | attn.append(make_attn(block_in, attn_type=attn_type)) |
| 406 | down = nn.Module() |
| 407 | down.block = block |
| 408 | down.attn = attn |
| 409 | if i_level != self.num_resolutions-1: |
| 410 | down.downsample = Downsample(block_in, resamp_with_conv) |
| 411 | curr_res = curr_res // 2 |
| 412 | self.down.append(down) |
| 413 | |
| 414 | # middle |
| 415 | self.mid = nn.Module() |
| 416 | self.mid.block_1 = ResnetBlock(in_channels=block_in, |
| 417 | out_channels=block_in, |
| 418 | temb_channels=self.temb_ch, |
| 419 | dropout=dropout) |
| 420 | self.mid.attn_1 = make_attn(block_in, attn_type=attn_type) |
| 421 | self.mid.block_2 = ResnetBlock(in_channels=block_in, |
| 422 | out_channels=block_in, |
| 423 | temb_channels=self.temb_ch, |
| 424 | dropout=dropout) |
| 425 | |
| 426 | # end |
nothing calls this directly
no test coverage detected