-
Notifications
You must be signed in to change notification settings - Fork 5
/
ccevae.py
executable file
·161 lines (140 loc) · 4.47 KB
/
ccevae.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import numpy as np
import torch
import torch.distributions as dist
from ae_bases import BasicEncoder, BasicGenerator
class VAE(torch.nn.Module):
def __init__(
self,
input_size,
z_dim=256,
fmap_sizes=(16, 64, 256, 1024),
to_1x1=True,
conv_op=torch.nn.Conv2d,
conv_params=None,
tconv_op=torch.nn.ConvTranspose2d,
tconv_params=None,
normalization_op=None,
normalization_params=None,
activation_op=torch.nn.LeakyReLU,
activation_params=None,
block_op=None,
block_params=None,
*args,
**kwargs
):
super(VAE, self).__init__()
input_size_enc = list(input_size)
input_size_dec = list(input_size)
self.enc = BasicEncoder(
input_size=input_size_enc,
fmap_sizes=fmap_sizes,
z_dim=z_dim * 2,
conv_op=conv_op,
conv_params=conv_params,
normalization_op=normalization_op,
normalization_params=normalization_params,
activation_op=activation_op,
activation_params=activation_params,
block_op=block_op,
block_params=block_params,
to_1x1=to_1x1,
)
self.dec = BasicGenerator(
input_size=input_size_dec,
fmap_sizes=fmap_sizes[::-1],
z_dim=z_dim,
upsample_op=tconv_op,
conv_params=tconv_params,
normalization_op=normalization_op,
normalization_params=normalization_params,
activation_op=activation_op,
activation_params=activation_params,
block_op=block_op,
block_params=block_params,
to_1x1=to_1x1,
)
self.hidden_size = self.enc.output_size
def forward(self, inpt, sample=True, no_dist=False, **kwargs):
y1 = self.enc(inpt, **kwargs)
mu, log_std = torch.chunk(y1, 2, dim=1)
std = torch.exp(log_std)
z_dist = dist.Normal(mu, std)
if sample:
z_sample = z_dist.rsample()
else:
z_sample = mu
x_rec = self.dec(z_sample)
if no_dist:
return x_rec
else:
return x_rec, z_dist
def encode(self, inpt, **kwargs):
enc = self.enc(inpt, **kwargs)
mu, log_std = torch.chunk(enc, 2, dim=1)
std = torch.exp(log_std)
return mu, std
def decode(self, inpt, **kwargs):
x_rec = self.dec(inpt, **kwargs)
return x_rec
class AE(torch.nn.Module):
def __init__(
self,
input_size,
z_dim=1024,
fmap_sizes=(16, 64, 256, 1024),
to_1x1=True,
conv_op=torch.nn.Conv2d,
conv_params=None,
tconv_op=torch.nn.ConvTranspose2d,
tconv_params=None,
normalization_op=None,
normalization_params=None,
activation_op=torch.nn.LeakyReLU,
activation_params=None,
block_op=None,
block_params=None,
*args,
**kwargs
):
super(AE, self).__init__()
input_size_enc = list(input_size)
input_size_dec = list(input_size)
self.enc = BasicEncoder(
input_size=input_size_enc,
fmap_sizes=fmap_sizes,
z_dim=z_dim,
conv_op=conv_op,
conv_params=conv_params,
normalization_op=normalization_op,
normalization_params=normalization_params,
activation_op=activation_op,
activation_params=activation_params,
block_op=block_op,
block_params=block_params,
to_1x1=to_1x1,
)
self.dec = BasicGenerator(
input_size=input_size_dec,
fmap_sizes=fmap_sizes[::-1],
z_dim=z_dim,
upsample_op=tconv_op,
conv_params=tconv_params,
normalization_op=normalization_op,
normalization_params=normalization_params,
activation_op=activation_op,
activation_params=activation_params,
block_op=block_op,
block_params=block_params,
to_1x1=to_1x1,
)
self.hidden_size = self.enc.output_size
def forward(self, inpt, **kwargs):
y1 = self.enc(inpt, **kwargs)
x_rec = self.dec(y1)
return x_rec
def encode(self, inpt, **kwargs):
enc = self.enc(inpt, **kwargs)
return enc
def decode(self, inpt, **kwargs):
rec = self.dec(inpt, **kwargs)
return rec