This repository has been archived by the owner on Nov 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
aae.py
117 lines (85 loc) · 3.49 KB
/
aae.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import torch
import torch.nn as nn
_device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
class Generator(nn.Module):
def __init__(self, config):
super().__init__()
self.z_size = config['z_size']
self.use_bias = config['model']['G']['use_bias']
self.relu_slope = config['model']['G']['relu_slope']
self.model = nn.Sequential(
nn.Linear(in_features=self.z_size, out_features=64, bias=self.use_bias),
nn.ReLU(inplace=True),
nn.Linear(in_features=64, out_features=128, bias=self.use_bias),
nn.ReLU(inplace=True),
nn.Linear(in_features=128, out_features=512, bias=self.use_bias),
nn.ReLU(inplace=True),
nn.Linear(in_features=512, out_features=1024, bias=self.use_bias),
nn.ReLU(inplace=True),
nn.Linear(in_features=1024, out_features=2048 * 3, bias=self.use_bias),
)
def forward(self, input):
output = self.model(input.squeeze())
output = output.view(-1, 3, 2048)
return output
class Discriminator(nn.Module):
def __init__(self, config):
super().__init__()
self.z_size = config['z_size']
self.use_bias = config['model']['D']['use_bias']
self.relu_slope = config['model']['D']['relu_slope']
self.dropout = config['model']['D']['dropout']
self.model = nn.Sequential(
nn.Linear(self.z_size, 512, bias=True),
nn.ReLU(inplace=True),
nn.Linear(512, 512, bias=True),
nn.ReLU(inplace=True),
nn.Linear(512, 128, bias=True),
nn.ReLU(inplace=True),
nn.Linear(128, 64, bias=True),
nn.ReLU(inplace=True),
nn.Linear(64, 1, bias=True)
)
def forward(self, x):
logit = self.model(x)
return logit
class Encoder(nn.Module):
def __init__(self, config):
super().__init__()
self.z_size = config['z_size']
self.use_bias = config['model']['E']['use_bias']
self.relu_slope = config['model']['E']['relu_slope']
self.conv = nn.Sequential(
nn.Conv1d(in_channels=3, out_channels=64, kernel_size=1,
bias=self.use_bias),
nn.ReLU(inplace=True),
nn.Conv1d(in_channels=64, out_channels=128, kernel_size=1,
bias=self.use_bias),
nn.ReLU(inplace=True),
nn.Conv1d(in_channels=128, out_channels=256, kernel_size=1,
bias=self.use_bias),
nn.ReLU(inplace=True),
nn.Conv1d(in_channels=256, out_channels=256, kernel_size=1,
bias=self.use_bias),
nn.ReLU(inplace=True),
nn.Conv1d(in_channels=256, out_channels=512, kernel_size=1,
bias=self.use_bias),
)
self.fc = nn.Sequential(
nn.Linear(512, 256, bias=True),
nn.ReLU(inplace=True)
)
self.mu_layer = nn.Linear(256, self.z_size, bias=True)
self.std_layer = nn.Linear(256, self.z_size, bias=True)
def reparameterize(self, mu, logvar):
std = torch.exp(0.5*logvar)
eps = torch.randn_like(std)
return eps.mul(std).add_(mu)
def forward(self, x):
output = self.conv(x)
output2 = output.max(dim=2)[0]
logit = self.fc(output2)
mu = self.mu_layer(logit)
logvar = self.std_layer(logit)
z = self.reparameterize(mu, logvar)
return z, mu, logvar