(
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,
**ignore_kwargs,
)
| 157 | |
| 158 | class Encoder(nn.Module): |
| 159 | def __init__( |
| 160 | self, |
| 161 | *, |
| 162 | ch, |
| 163 | out_ch, |
| 164 | ch_mult=(1, 2, 4, 8), |
| 165 | num_res_blocks, |
| 166 | attn_resolutions, |
| 167 | dropout=0.0, |
| 168 | resamp_with_conv=True, |
| 169 | in_channels, |
| 170 | resolution, |
| 171 | z_channels, |
| 172 | double_z=True, |
| 173 | **ignore_kwargs, |
| 174 | ): |
| 175 | super().__init__() |
| 176 | self.ch = ch |
| 177 | self.temb_ch = 0 |
| 178 | self.num_resolutions = len(ch_mult) |
| 179 | self.num_res_blocks = num_res_blocks |
| 180 | self.resolution = resolution |
| 181 | self.in_channels = in_channels |
| 182 | |
| 183 | # downsampling |
| 184 | self.conv_in = torch.nn.Conv2d(in_channels, self.ch, kernel_size=3, stride=1, padding=1) |
| 185 | |
| 186 | curr_res = resolution |
| 187 | in_ch_mult = (1,) + tuple(ch_mult) |
| 188 | self.down = nn.ModuleList() |
| 189 | for i_level in range(self.num_resolutions): |
| 190 | block = nn.ModuleList() |
| 191 | attn = nn.ModuleList() |
| 192 | block_in = ch * in_ch_mult[i_level] |
| 193 | block_out = ch * ch_mult[i_level] |
| 194 | for i_block in range(self.num_res_blocks): |
| 195 | block.append( |
| 196 | ResnetBlock( |
| 197 | in_channels=block_in, out_channels=block_out, temb_channels=self.temb_ch, dropout=dropout |
| 198 | ) |
| 199 | ) |
| 200 | block_in = block_out |
| 201 | if curr_res in attn_resolutions: |
| 202 | attn.append(AttnBlock(block_in)) |
| 203 | down = nn.Module() |
| 204 | down.block = block |
| 205 | down.attn = attn |
| 206 | if i_level != self.num_resolutions - 1: |
| 207 | down.downsample = Downsample(block_in, resamp_with_conv) |
| 208 | curr_res = curr_res // 2 |
| 209 | self.down.append(down) |
| 210 | |
| 211 | # middle |
| 212 | self.mid = nn.Module() |
| 213 | self.mid.block_1 = ResnetBlock( |
| 214 | in_channels=block_in, out_channels=block_in, temb_channels=self.temb_ch, dropout=dropout |
| 215 | ) |
| 216 | self.mid.attn_1 = AttnBlock(block_in) |
no test coverage detected