-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathperceptual_embedder.py
274 lines (230 loc) · 9.19 KB
/
perceptual_embedder.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# Library imports
import torch
import numpy as np
import torchvision.models as models
from torch.nn import functional as F
import torch.nn as nn
import matplotlib.pyplot as plt
# File imports
from utility import run_training, EarlyStopper
from VAE import _create_coder, TemplateVAE
class FeaturePredictorCVAE(TemplateVAE):
'''
A Convolutional Variational autoencoder trained with feature prediction
I-F-FP procedure in the paper
Args:
input_size (int,int): The height and width of the input image
acceptable sizes are 64+16*n
z_dimensions (int): The number of latent dimensions in the encoding
variational (bool): Whether the model is variational or not
gamma (float): The weight of the KLD loss
perceptual_net: Which perceptual network to use
'''
def __init__(self, input_size=(64,64), z_dimensions=32,
variational=True, gamma=20.0, perceptual_net=None
):
super().__init__()
#Parameter check
if (input_size[0] - 64) % 16 != 0 or (input_size[1] - 64) % 16 != 0:
raise ValueError(
f'Input_size is {input_size}, but must be 64+16*N'
)
assert perceptual_net != None, \
'For FeaturePredictorCVAE, perceptual_net cannot be None'
#Attributes
self.input_size = input_size
self.z_dimensions = z_dimensions
self.variational = variational
self.gamma = gamma
self.perceptual_net = perceptual_net
inp = torch.rand((1,3,input_size[0],input_size[1]))
out = self.perceptual_net(
inp.to(next(perceptual_net.parameters()).device)
)
self.perceptual_size = out.numel()
self.perceptual_loss = True
encoder_channels = [3,32,64,128,256]
self.encoder = _create_coder(
encoder_channels, [4,4,4,4], [2,2,2,2],
nn.Conv2d, nn.ReLU,
batch_norms=[True,True,True,True]
)
f = lambda x: np.floor((x - (2,2))/2)
conv_sizes = f(f(f(f(np.array(input_size)))))
conv_flat_size = int(encoder_channels[-1]*conv_sizes[0]*conv_sizes[1])
self.mu = nn.Linear(conv_flat_size, self.z_dimensions)
self.logvar = nn.Linear(conv_flat_size, self.z_dimensions)
g = lambda x: int((x-64)/16)+1
deconv_flat_size = g(input_size[0]) * g(input_size[1]) * 1024
hidden_layer_size = int(min(self.perceptual_size/2, 2048))
self.decoder = nn.Sequential(
nn.Linear(self.z_dimensions, hidden_layer_size),
nn.ReLU(),
nn.Linear(hidden_layer_size, self.perceptual_size)
)
def loss(self, output, x):
rec_y, z, mu, logvar = output
y = self.perceptual_net(x)
REC = F.mse_loss(rec_y, y, reduction='mean')
if self.variational:
KLD = -1 * torch.mean(1 + logvar - mu.pow(2) - logvar.exp())
return REC + self.gamma*KLD, REC, KLD
else:
return [REC]
class FeatureAutoencoder(TemplateVAE):
'''
An fc autoencoder that autoencodes the features of a perceptual network
F-F-FP procedure in the paper
Args:
input_size (int,int): The height and width of the input image
acceptable sizes are 64+16*n
z_dimensions (int): The number of latent dimensions in the encoding
variational (bool): Whether the model is variational or not
gamma (float): The weight of the KLD loss
perceptual_net: Which perceptual network to use
'''
def __init__(self, input_size=(64,64), z_dimensions=32,
variational=True, gamma=20.0, perceptual_net=None
):
super().__init__()
#Parameter check
if (input_size[0] - 64) % 16 != 0 or (input_size[1] - 64) % 16 != 0:
raise ValueError(
f'Input_size is {input_size}, but must be 64+16*N'
)
assert perceptual_net != None, \
'For FeatureAutoencoder, perceptual_net cannot be None'
#Attributes
self.input_size = input_size
self.z_dimensions = z_dimensions
self.variational = variational
self.gamma = gamma
self.perceptual_net = perceptual_net
inp = torch.rand((1,3,input_size[0],input_size[1]))
out = self.perceptual_net(
inp.to(next(perceptual_net.parameters()).device)
)
self.perceptual_size = out.numel()
self.perceptual_loss = True
hidden_layer_size = int(min(self.perceptual_size/2, 2048))
self.encoder = nn.Sequential(
nn.Linear(self.perceptual_size, hidden_layer_size),
nn.ReLU(),
)
self.mu = nn.Linear(hidden_layer_size, self.z_dimensions)
self.logvar = nn.Linear(hidden_layer_size, self.z_dimensions)
self.decoder = nn.Sequential(
nn.Linear(self.z_dimensions, hidden_layer_size),
nn.ReLU(),
nn.Linear(hidden_layer_size, self.perceptual_size)
)
def encode(self, x):
y = self.perceptual_net(x)
y = y.view(y.size(0),-1)
y = self.encoder(y)
mu = self.mu(y)
logvar = self.logvar(y)
return mu, logvar
def loss(self, output, x):
rec_y, z, mu, logvar = output
y = self.perceptual_net(x)
y = y.view(y.size(0),-1)
REC = F.mse_loss(rec_y, y, reduction='mean')
if self.variational:
KLD = -1 * torch.mean(1 + logvar - mu.pow(2) - logvar.exp())
return REC + self.gamma*KLD, REC, KLD
else:
return [REC]
class PerceptualFeatureToImgCVAE(TemplateVAE):
'''
A CVAE that encodes perceptual features and reconstructs the images
Trained with perceptual loss
F-I-PS in the paper
Args:
input_size (int,int): The height and width of the input image
acceptable sizes are 64+16*n
z_dimensions (int): The number of latent dimensions in the encoding
variational (bool): Whether the model is variational or not
gamma (float): The weight of the KLD loss
perceptual_net: Which feature extraction and perceptual net to use
'''
def __init__(self, input_size=(64,64), z_dimensions=32,
variational=True, gamma=20.0, perceptual_net=None
):
super().__init__()
#Parameter check
if (input_size[0] - 64) % 16 != 0 or (input_size[1] - 64) % 16 != 0:
raise ValueError(
f'Input_size is {input_size}, but must be 64+16*N'
)
assert perceptual_net != None, \
'For PerceptualFeatureToImgCVAE, perceptual_net cannot be None'
#Attributes
self.input_size = input_size
self.z_dimensions = z_dimensions
self.variational = variational
self.gamma = gamma
self.perceptual_net = perceptual_net
inp = torch.rand((1,3,input_size[0],input_size[1]))
out = self.perceptual_net(
inp.to(next(perceptual_net.parameters()).device)
)
self.perceptual_size = out.numel()
self.perceptual_loss = True
hidden_layer_size = int(min(self.perceptual_size/2, 2048))
self.encoder = nn.Sequential(
nn.Linear(self.perceptual_size, hidden_layer_size),
nn.ReLU(),
)
self.mu = nn.Linear(hidden_layer_size, self.z_dimensions)
self.logvar = nn.Linear(hidden_layer_size, self.z_dimensions)
g = lambda x: int((x-64)/16)+1
deconv_flat_size = g(input_size[0]) * g(input_size[1]) * 1024
self.dense = nn.Linear(self.z_dimensions, deconv_flat_size)
self.decoder = _create_coder(
[1024,128,64,32,3], [5,5,6,6], [2,2,2,2],
nn.ConvTranspose2d,
[nn.ReLU,nn.ReLU,nn.ReLU,nn.Sigmoid],
batch_norms=[True,True,True,False]
)
self.relu = nn.ReLU()
def encode(self, x):
y = self.perceptual_net(x)
y = y.view(y.size(0),-1)
y = self.encoder(y)
mu = self.mu(y)
logvar = self.logvar(y)
return mu, logvar
def decode(self, z):
y = self.dense(z)
y = self.relu(y)
y = y.view(
y.size(0), 1024,
int((self.input_size[0]-64)/16)+1,
int((self.input_size[1]-64)/16)+1
)
y = self.decoder(y)
return y
class FeatureToImgCVAE(PerceptualFeatureToImgCVAE):
'''
A CVAE that encodes perceptual features and reconstructs the images
Trained with pixel-wise loss
F-I-PW in the paper
Args:
input_size (int,int): The height and width of the input image
acceptable sizes are 64+16*n
z_dimensions (int): The number of latent dimensions in the encoding
variational (bool): Whether the model is variational or not
gamma (float): The weight of the KLD loss
perceptual_net: Which feature extraction net to use
'''
def loss(self, output, x):
rec_x, z, mu, logvar = output
x = x.reshape(x.size(0), -1)
rec_x = rec_x.view(x.size(0), -1)
REC = F.mse_loss(rec_x, x, reduction='mean')
if self.variational:
KLD = -1 * torch.mean(1 + logvar - mu.pow(2) - logvar.exp())
return REC + self.gamma*KLD, REC, KLD
else:
return [REC]