-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.py
445 lines (387 loc) · 16.6 KB
/
model.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
from dataset import SkeletonTrainDataset, SkeletonValDataset,SkeletonTestDataset
from torch.utils.data.distributed import DistributedSampler
from collections import OrderedDict
from argparse import ArgumentParser
import logging as log
import pytorch_lightning as ptl
import math
import torch
import torchvision.models as models
import torchvision.transforms as transforms
from torchvision.datasets import MNIST
from torch.utils.data import DataLoader
from components import *
from settings import args
import os
import sys
sys.path.append(os.getcwd())
class BasicBlock(nn.Module):
expansion = 1
#inplanes其实就是channel,叫法不同
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = nn.BatchNorm2d(planes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.bn2 = nn.BatchNorm2d(planes)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
#把shortcut那的channel的维度统一
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
block = BasicBlock
# ==================Definition Start - Stage 2======================
class skeletonVAE(ptl.LightningModule):
def __init__(self,args):
super(skeletonVAE, self).__init__()
self.hparams = args
self.train_batch_nb = self.hparams.batch_size
self.vaL_batch_nb = 1
self.latent_dim = self.hparams.latent_dim
self.input_size = self.hparams.input_size
self.cond_f_dims = self.hparams.cond_f_dims
self.loss_weight = self.hparams.kld_loss_weight # will multiply kld loss
self.input_size = self.hparams.input_size
self.cond_size = self.hparams.cond_size
in_channels = self.hparams.in_channels
hidden_dims = self.hparams.hidden_dims
extract_layers = self.hparams.extract_layers
# input is C,H*W, make the size same by Linear
self.res_in_channels = 64
self.embed_condition = nn.Linear(
self.cond_f_dims, self.input_size * self.input_size)
# this is for feature fusion
self.embed_data = nn.Conv2d(in_channels, in_channels, kernel_size=1)
if hidden_dims == 0:
hidden_dims = [16, 32, 64, 128, 256, 512] # TODO need to be finetuned
if extract_layers == 0:
extract_layers = [2, 2, 2, 2] # for condition feature extractor
enc_in_channels = in_channels + 1 # add condition embeded
########## encoder, img_size/2 for each conv, but feature_num *2 #####
modules = []
for h_dim in hidden_dims:
modules.append(
nn.Sequential(
nn.Conv2d(enc_in_channels, out_channels=h_dim,
kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(h_dim),
nn.LeakyReLU())
)
enc_in_channels = h_dim
self.encoder = nn.Sequential(*modules)
self.fcFactor = int(math.pow(self.input_size/(2**len(hidden_dims)),2))
self.fc_mu = nn.Linear(hidden_dims[-1] * self.fcFactor, self.latent_dim)
self.fc_var = nn.Linear(hidden_dims[-1] * self.fcFactor, self.latent_dim)
######### decoder #########
modules = []
self.decoder_input = nn.Linear(
self.latent_dim + self.cond_f_dims, hidden_dims[-1] * self.fcFactor)
self.dec_hidden_dim = hidden_dims[-1]
hidden_dims.reverse() # list order reverse.
for i in range(len(hidden_dims) - 1):
modules.append(
nn.Sequential(nn.ConvTranspose2d(hidden_dims[i],hidden_dims[i + 1],kernel_size=3,stride=2,padding=1,output_padding=1),
nn.BatchNorm2d(hidden_dims[i + 1]),
nn.LeakyReLU())
)
self.decoder = nn.Sequential(*modules)
# final layer
self.final_layer = nn.Sequential(
nn.ConvTranspose2d(hidden_dims[-1],
hidden_dims[-1],
kernel_size=3,
stride=2,
padding=1,
output_padding=1),
nn.BatchNorm2d(hidden_dims[-1]),
nn.LeakyReLU(),
nn.Conv2d(hidden_dims[-1], out_channels=1,
kernel_size=3, padding=1),
nn.Tanh()
)
########### feature extractor for condition image #########
af_size = math.ceil(self.cond_size / 32 - 7 + 1)
self.feature_extracter = nn.Sequential(nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False),
# size/2
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1), # size/2
self._make_layer(block, 64, extract_layers[0]),
self._make_layer(block,128,extract_layers[1],stride=2),
# size/2
self._make_layer(block,256,extract_layers[2],stride=2),
# size/2
self._make_layer(block,512,extract_layers[3],stride=2),
# size/2
nn.AvgPool2d(7, stride=1),
)
# transfer feature map b,512,h,w -> b,cond_f_dims
self.feature_mapping = nn.Linear(
512 * block.expansion * af_size * af_size,
self.cond_f_dims)
def forward(self, x, c): # input, condition
# TODO now encoder-decoder structure, whether to use U-net
# AND where should we add the condition? beginning or mid-term. NOW FOR INPUTS
# here is flatten for c,w*h, then conv, relu
cond_vector = self.feature_extract(c) # maybe 64 or 128
embedded_condition = self.embed_condition(cond_vector) # bn,cond_dim -> bn, input_size*input*size
embedded_condition = embedded_condition.view(
-1, self.input_size, self.input_size).unsqueeze(1) # bn, input_size*input*size -> bn, 1, input_size, input_size
embedded_input = self.embed_data(x)
x_ = torch.cat([embedded_input, embedded_condition],
dim=1) # concat on channel dim. bn, 2, input_size, input_size
mu, log_var = self.encode(x_)
z = self.reparameterize(mu, log_var)
z = torch.cat([z, cond_vector], dim=1) # TODO data augment
output = self.decode(z)
return [output, mu, log_var]
def decode(self, z):
'''
forward subpart. decodes the latent variable+condition to output
:param z: bn, 2*cond_dim
:return:
'''
dec_input = self.decoder_input(z)
rsln = int(math.sqrt(self.fcFactor))
dec_input = dec_input.view(-1, self.dec_hidden_dim, rsln, rsln)
dec_feature = self.decoder(dec_input)
output = self.final_layer(dec_feature)
return output
def encode(self, x):
'''
forward subpart. encodes the input through self.encoder. generate latent variable.
:param x: [N,C,H,W]
:return: latent variables
'''
result = self.encoder(x)
# [N x C x H x W] -> [N,CHW]
result = torch.flatten(result, start_dim=1) # bn, 2^5
# Split the result into mu and var components
# of the latent Gaussian distribution
mu = self.fc_mu(result)
log_var = self.fc_var(result)
return [mu, log_var]
def feature_extract(self, c):
'''
use pretrained model to extract information of condition image.
:param c: condition image.
:return: feature extracted
'''
# suppose c [b,C,H,W]
feature_map = self.feature_extracter(c)
feature_map = torch.flatten(feature_map, start_dim=1)
feature_vector = self.feature_mapping(feature_map)
return feature_vector
def reparameterize(self, mu, logvar):
"""
Will a single z be enough ti compute the expectation
for the loss??
:param mu: (Tensor) Mean of the latent Gaussian
:param logvar: (Tensor) Standard deviation of the latent Gaussian
:return:
"""
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
return eps * std + mu
def _make_layer(self, block, channels, blocks, stride=1):
# downsample 主要用来处理H(x)=F(x)+x中F(x)和xchannel维度不匹配问题
downsample = None
# self.inplanes为上个box_block的输出channel,planes为当前box_block块的输入channel
if stride != 1 or self.res_in_channels != channels * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.res_in_channels, channels * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(channels * block.expansion),
)
layers = list()
layers.append(
block(
self.res_in_channels,
channels,
stride,
downsample))
self.res_in_channels = channels * block.expansion
for i in range(1, blocks):
layers.append(block(self.res_in_channels, channels))
return nn.Sequential(*layers)
def my_loss(self, infer_out, y):
# TODO construct loss, y_hat is the predicted
recons = infer_out[0] # output
mu = infer_out[1]
log_var = infer_out[2]
recons_loss = F.mse_loss(recons, y)
kld_loss = torch.mean(-0.5 * torch.sum(1 +
log_var - mu ** 2 - log_var.exp(), dim=1), dim=0)
loss = recons_loss + self.loss_weight * kld_loss
return loss, recons_loss, -kld_loss
def training_step(self, batch, batch_nb):
x, c, y = batch['real'],batch['condition'],batch['anime']
y_hat, mu, log_var = self.forward(x, c)
infer_out = [y_hat, mu, log_var]
loss, recons_loss, kld_loss = self.my_loss(infer_out, y)
tensorboard_logs = {'Train_avg_loss': loss,'Train_Reconstruction_Loss': recons_loss, 'Train_KLD': -kld_loss}
return {'loss': loss, 'log': tensorboard_logs}
#
# return {
# 'loss': loss,
# 'Train_Reconstruction_Loss': recons_loss,
# 'Train_KLD': -kld_loss}
def validation_step(self, batch, batch_nb):
x, c, y = batch['real'],batch['condition'],batch['anime']
y_hat, mu, log_var = self.forward(x, c)
infer_out = [y_hat, mu, log_var]
loss, recons_loss, kld_loss = self.my_loss(infer_out, y)
return {
'val_loss': loss,
'val_Reconstruction_Loss': recons_loss,
'val_KLD': -kld_loss}
def validation_end(self, outputs):
avg_loss = torch.stack([x['val_loss'] for x in outputs]).mean()
avg_recon_loss = torch.stack(
[x['val_Reconstruction_Loss'] for x in outputs]).mean()
avg_kld_loss = torch.stack([x['val_KLD'] for x in outputs]).mean()
tensorboard_logs = {'val_avg_loss': avg_loss, 'val_Recon_Loss': avg_recon_loss, 'val_KLD': -avg_kld_loss}
return {'avg_val_loss': avg_loss, 'log': tensorboard_logs}
# return {
# 'avg_val_loss': avg_loss,
# 'avg_val_recon_Loss': avg_recon_loss,
# 'avg_val_KLD': -avg_kld_loss}
def test_step(self, batch, batch_nb):
name, x, c = batch['name'], batch['real'], batch['condition']
infer_out, _, _ = self.forward(x, c)
return {'output': infer_out, 'name':name}
def test_end(self, outputs):
import cv2
[cv2.imwrite(os.path.join(self.hparams.dataset_vae, 'Output', x['name']), x['output']) for x in outputs]
return
def configure_optimizers(self):
# TODO if we want to finetune, add para.requires_grad = False in the
# separent part of model : lambda p: p.requires_grad,self.parameters()
return torch.optim.Adam(self.parameters(), lr=self.hparams.lr_vae)
def __dataloader(self, train, infer=False):
# init data generators
transform = transforms.Compose([transforms.ToPILImage(),
transforms.Resize((256,256)),
transforms.ToTensor(),
transforms.Normalize((0.5,), (1.0,))
])
if infer:
dataset = SkeletonTestDataset(self.hparams.dataset_vae,transform=transform)
elif train:
dataset = SkeletonTrainDataset(self.hparams.dataset_vae,None,None,transform=transform)
else:
dataset = SkeletonValDataset(self.hparams.dataset_vae,transform)
# TODO whether we should transform back, here I consider that we keep the 256*256 then use it in the next train stage. Finally we transform back?
# when using multi-node (ddp) we need to add the datasampler
train_sampler = None
batch_size = self.hparams.batch_size if not infer else 1
if self.use_ddp:
train_sampler = DistributedSampler(dataset)
should_shuffle = train_sampler is None
if infer: should_shuffle = False
loader = DataLoader(
dataset=dataset,
batch_size=batch_size,
shuffle=should_shuffle,
sampler=train_sampler,
num_workers=0
)
return loader
@ptl.data_loader
def train_dataloader(self):
log.info('Training data loader called.')
return self.__dataloader(train=True)
@ptl.data_loader
def val_dataloader(self):
log.info('Validation data loader called.')
return self.__dataloader(train=False)
@ptl.data_loader
def test_dataloader(self):
log.info('Test data loader called.')
return self.__dataloader(train=False,infer=True)
# @staticmethod
# def add_model_specific_args(): # pragma: no cover
# """
# Parameters you define here will be available to your model through self.hparams
# :param parent_parser:
# :return:
# """
# parser = ArgumentParser()
# return parser
# ==================Definition Start - Stage 3======================
class Generator(nn.Module):
def __init__(self):
# TODO change model
super(Generator, self).__init__()
preprocess = nn.Sequential(
nn.Linear(128, 4 * 4 * 4 * args.dim),
nn.ReLU(True),
)
block1 = nn.Sequential(
nn.ConvTranspose2d(4 * args.dim, 2 * args.dim, 5),
nn.ReLU(True),
)
block2 = nn.Sequential(
nn.ConvTranspose2d(2 * args.dim, args.dim, 5),
nn.ReLU(True),
)
deconv_out = nn.ConvTranspose2d(args.dim, 1, 8, stride=2)
self.block1 = block1
self.block2 = block2
self.deconv_out = deconv_out
self.preprocess = preprocess
self.sigmoid = nn.Sigmoid()
def forward(self, input):
# TODO change model
output = self.preprocess(input)
output = output.view(-1, 4 * args.dim, 4, 4)
# print output.size()
output = self.block1(output)
# print output.size()
output = output[:, :, :7, :7]
# print output.size()
output = self.block2(output)
# print output.size()
output = self.deconv_out(output)
output = self.sigmoid(output)
# print output.size()
return output.view(-1, args.output_dim)
class Discriminator(nn.Module):
def __init__(self):
# TODO change model
super(Discriminator, self).__init__()
main = nn.Sequential(
nn.Conv2d(1, args.dim, 5, stride=2, padding=2),
# nn.Linear(OUTPUT_DIM, 4*4*4*args.dim),
nn.ReLU(True),
nn.Conv2d(args.dim, 2 * args.dim, 5, stride=2, padding=2),
# nn.Linear(4*4*4*DIM, 4*4*4*DIM),
nn.ReLU(True),
nn.Conv2d(2 * args.dim, 4 * args.dim, 5, stride=2, padding=2),
# nn.Linear(4*4*4*DIM, 4*4*4*DIM),
nn.ReLU(True),
# nn.Linear(4*4*4*DIM, 4*4*4*DIM),
# nn.LeakyReLU(True),
# nn.Linear(4*4*4*DIM, 4*4*4*DIM),
# nn.LeakyReLU(True),
)
self.main = main
self.output = nn.Linear(4 * 4 * 4 * args.dim, 1)
def forward(self, input):
# TODO change model
input = input.view(-1, 1, 28, 28)
out = self.main(input)
out = out.view(-1, 4 * 4 * 4 * args.dim)
out = self.output(out)
return out.view(-1)