-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathencoder.py
33 lines (30 loc) · 1.38 KB
/
encoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import torch.nn as nn
from helper import ResidualBlock, NonLocalBlock, DownSampleBlock, UpSampleBlock, GroupNorm, Swish
class Encoder(nn.Module):
def __init__(self, args):
super(Encoder, self).__init__()
channels = [128, 128, 128, 256, 256, 512]
attn_resolutions = [16]
num_res_blocks = 2
resolution = 256
layers = [nn.Conv2d(args.image_channels, channels[0], 3, 1, 1)]
for i in range(len(channels)-1):
in_channels = channels[i]
out_channels = channels[i + 1]
for j in range(num_res_blocks):
layers.append(ResidualBlock(in_channels, out_channels))
in_channels = out_channels
if resolution in attn_resolutions:
layers.append(NonLocalBlock(in_channels))
if i != len(channels)-2:
layers.append(DownSampleBlock(channels[i+1]))
resolution //= 2
layers.append(ResidualBlock(channels[-1], channels[-1]))
layers.append(NonLocalBlock(channels[-1]))
layers.append(ResidualBlock(channels[-1], channels[-1]))
layers.append(GroupNorm(channels[-1]))
layers.append(Swish())
layers.append(nn.Conv2d(channels[-1], args.latent_dim, 3, 1, 1))
self.model = nn.Sequential(*layers)
def forward(self, x):
return self.model(x)