-
Notifications
You must be signed in to change notification settings - Fork 2
/
lightning_module.py
388 lines (310 loc) · 13.6 KB
/
lightning_module.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
# -*- coding: utf-8 -*-
"""
Example template for defining a system
"""
import logging as log
from argparse import ArgumentParser
from collections import OrderedDict
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms as transforms
from torch import optim
from torch.utils.data import DataLoader
from torch.utils.data.distributed import DistributedSampler
import pytorch_lightning as pl
from pytorch_lightning.core.lightning import LightningModule
from torchvision.utils import make_grid
from resfcn256 import ResFCN256
from WLP300dataset import PRNetDataset, ToTensor, ToNormalize, RescaleAndCrop, FlipH
from prnet_loss import WeightMaskLoss
#import pytorch_msssim
from losses import SSIM
import win32com.client as wincl
speak = wincl.Dispatch("SAPI.SpVoice")
# speak.Speak("")
from cv_plot import plot_kpt
import cv2
import numpy as np
#cv2.imshow("mask",mask_image)
#cv2.waitKey()
class LightningTemplateModel(LightningModule):
"""
Sample model to show how to define a template
"""
def __init__(self, hparams):
"""
Pass in parsed HyperOptArgumentParser to the model
:param hparams:
"""
# init superclass
super(LightningTemplateModel, self).__init__()
self.hparams = hparams
self.uv_kpt_ind_path = "uv_data/uv_kpt_ind.txt"
self.face_ind_path = "uv_data/face_ind.txt"
self.triangles_path = "uv_data/triangles.txt"
self.uv_kpt_ind = np.loadtxt(self.uv_kpt_ind_path).astype(np.int32) # 2 x 68 get kpt
self.face_ind = np.loadtxt(self.face_ind_path).astype(np.int32) # get valid vertices in the pos map
self.triangles = np.loadtxt(self.triangles_path).astype(np.int32) # ntri x 3
self.input_size=hparams.input_size
self.input_channels=1
if hparams.is_color:
self.input_channels=3
self.weights_img=cv2.imread('uv_data/uv_weight_mask_gdh.png')
self.weights_img=cv2.resize(self.weights_img,(self.input_size,self.input_size))
self.mask_image = np.zeros(shape=[self.input_size, self.input_size, self.input_channels], dtype=np.float32)
for i in range(self.input_size*self.input_size):
x=i//self.input_size
y=i%self.input_size
if self.weights_img[y,x,:].any()>0:
self.mask_image[y,x,:]=1.0
#self.trainer = pl.Trainer(logger=self.logger, accumulate_grad_batches=2,amp_level='O2', use_amp=False)
#self.trainer = pl.Trainer(default_save_path='./checkpoints/', logger=self.logger, amp_level='O2', use_amp=False, checkpoint_callback=checkpoint_callback)
self.batch_size = hparams.batch_size
#if you specify an example input, the summary will show input/output for each layer
# self.example_input_array = torch.rand(64, 3, 224, 224)
# build model
self.__build_model()
#self.ssim_loss = SSIM(mask_path="uv_data/uv_weight_mask_gdh.png", gauss="original")
self.ssim_loss = WeightMaskLoss(mask_path="uv_data/uv_weight_mask_gdh.png")
#self.ssim_loss = pytorch_msssim.SSIM(size_average=False)
# ---------------------
# MODEL SETUP
# ---------------------
def __build_model(self):
"""
Layout model
:return:
"""
self.model = ResFCN256(resolution_input=self.input_size)
# ---------------------
# TRAINING
# ---------------------
def forward(self, x):
"""
No special modification required for lightning, define as you normally would
:param x:
:return:
"""
x=self.model.forward(x)
return x
def loss(self, targets, outputs):
ssim = self.ssim_loss(targets, outputs)
return ssim
def on_epoch_end(self):
# do something when the epoch ends
torch.save(self.model,'model_converter/model.pth')
speak.Speak("Эпоха окончена")
def on_epoch_start(self):
speak.Speak("Эпоха начата")
def training_step(self, batch, batch_idx):
"""
Lightning calls this inside the training loop
:param batch:
:return:
"""
# self.train(True)
# forward pass
x, y = batch['origin'],batch['uv_map']
y_hat = self.forward(x)
# calculate loss
loss_val = self.loss(y, y_hat)
if (self.global_step % 500) == 0:
# self.logger.experiment.add_image('train_results',make_grid(y_hat), batch_idx)
map_gt, map_pred = make_grid(y), make_grid(y_hat)
gr=make_grid(self.show_batch(x, y_hat),normalize=True)
gr_gt=make_grid(self.show_batch(x, y),normalize=True)
self.logger.experiment.add_image('map_gt', map_gt, batch_idx)
self.logger.experiment.add_image('map_pred', map_pred, batch_idx)
self.logger.experiment.add_image('gt', gr_gt, batch_idx)
self.logger.experiment.add_image('pred', gr, batch_idx)
# in DP mode (default) make sure if result is scalar, there's another dim in the beginning
if self.trainer.use_dp or self.trainer.use_ddp2:
loss_val = loss_val.unsqueeze(0)
if (self.global_step % 500) == 0:
speak.Speak("Итерация "+str(self.global_step))
speak.Speak('Лосс {:.3f}'.format(loss_val))
tqdm_dict = {'train_loss': loss_val}
output = OrderedDict({
'loss': loss_val,
'progress_bar': tqdm_dict,
'log': tqdm_dict
})
# can also return just a scalar instead of a dict (return loss_val)
return output
def validation_step(self, batch, batch_idx):
"""
Lightning calls this inside the validation loop
:param batch:
:return:
"""
# self.train(False)
x, y = batch['origin'],batch['uv_map']
y_hat = self.forward(x)
loss_val = self.loss(y, y_hat)
if (batch_idx == 0):
#self.logger.experiment.add_image('val_results',make_grid(y_hat), batch_idx)
map_gt, map_pred = make_grid(y), make_grid(y_hat)
gr=make_grid(self.show_batch(x, y_hat),normalize=True)
gr_gt=make_grid(self.show_batch(x, y),normalize=True)
self.logger.experiment.add_image('val_map_gt', map_gt, batch_idx)
self.logger.experiment.add_image('val_map_pred', map_pred, batch_idx)
self.logger.experiment.add_image('val_gt', gr_gt, batch_idx)
self.logger.experiment.add_image('val_pred', gr, batch_idx)
# in DP mode (default) make sure if result is scalar, there's another dim in the beginning
if self.trainer.use_dp or self.trainer.use_ddp2:
loss_val = loss_val.unsqueeze(0)
output = OrderedDict({
'val_loss': loss_val,
})
# can also return just a scalar instead of a dict (return loss_val)
return output
def validation_end(self, outputs):
"""
Called at the end of validation to aggregate outputs
:param outputs: list of individual outputs of each validation step
:return:
"""
# if returned a scalar from validation_step, outputs is a list of tensor scalars
# we return just the average in this case (if we want)
# return torch.stack(outputs).mean()
val_loss_mean = 0
for output in outputs:
val_loss = output['val_loss']
# reduce manually when using dp
if self.trainer.use_dp or self.trainer.use_ddp2:
val_loss = torch.mean(val_loss)
val_loss_mean += val_loss
val_loss_mean /= len(outputs)
speak.Speak('Тест Лосс {:.3f}'.format(val_loss_mean))
tqdm_dict = {'val_loss': val_loss_mean}
result = {'progress_bar': tqdm_dict, 'log': tqdm_dict, 'val_loss': val_loss_mean}
# img=show_landmarks_batch(output['x'],output['y_hat'])
#self.logger.experiment.add_image('val_results',make_grid(img), 0)
return result
# ---------------------
# TRAINING SETUP
# ---------------------
def configure_optimizers(self):
"""
return whatever optimizers we want here
:return: list of optimizers
"""
optimizer = optim.Adam(self.parameters(), lr=self.hparams.learning_rate,betas=(0.5, 0.999))
scheduler = optim.lr_scheduler.ExponentialLR(optimizer, gamma=0.99)
return [optimizer], [scheduler]
def __dataloader(self, train):
data_dir = self.hparams.data_dir
dataset=PRNetDataset(root_dir=data_dir,
train=train,
transform=transforms.Compose([ FlipH(),RescaleAndCrop(),
ToTensor(),
ToNormalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]))
# when using multi-node (ddp) we need to add the datasampler
train_sampler = None
batch_size = self.hparams.batch_size
if self.use_ddp:
train_sampler = DistributedSampler(dataset)
should_shuffle = train_sampler is None
loader = DataLoader(
dataset=dataset,
drop_last=True,
batch_size=batch_size,
shuffle=should_shuffle,
sampler=train_sampler,
num_workers=2
)
return loader
@pl.data_loader
def train_dataloader(self):
log.info('Training data loader called.')
return self.__dataloader(train=True)
@pl.data_loader
def val_dataloader(self):
log.info('Validation data loader called.')
return self.__dataloader(train=False)
@pl.data_loader
def test_dataloader(self):
log.info('Test data loader called.')
return self.__dataloader(train=False)
@staticmethod
def add_model_specific_args(parent_parser, root_dir): # pragma: no cover
"""
Parameters you define here will be available to your model through self.hparams
:param parent_parser:
:param root_dir:
:return:
"""
parser = ArgumentParser(parents=[parent_parser])
# param overwrites
# parser.set_defaults(gradient_clip_val=5.0)
# network params
# use 500 for CPU, 50000 for GPU to see speed difference
parser.add_argument('--learning_rate', default=0.0001, type=float)
# training params (opt)
parser.add_argument('--optimizer_name', default='adam', type=str)
parser.add_argument('--batch_size', default=48, type=int)
return parser
def generate_uv_coords(self):
resolution = 256
uv_coords = np.meshgrid(range(resolution), range(resolution))
# uv_coords = np.transpose(np.array(uv_coords), [1, 2, 0])
uv_coords = np.reshape(uv_coords, [resolution ** 2, -1])
uv_coords = uv_coords[face_ind, :]
uv_coords = np.hstack((uv_coords[:, :2], np.zeros([uv_coords.shape[0], 1])))
return uv_coords
def get_landmarks(self,pos):
'''
Notice: original tensorflow version shape is [256, 256, 3] (H, W, C)
where our pytorch shape is [3, 256, 256] (C, H, W).
Args:
pos: the 3D position map. shape = (256, 256, 3).
Returns:
kpt: 68 3D landmarks. shape = (68, 3).
'''
kpt = pos[self.uv_kpt_ind[1, :], self.uv_kpt_ind[0, :], :]
return kpt
def get_vertices(self,pos):
'''
Args:
pos: the 3D position map. shape = (3, 256, 256).
Returns:
vertices: the vertices(point cloud). shape = (num of points, 3). n is about 40K here.
'''
all_vertices = np.reshape(pos, [256 ** 2, -1])
vertices = all_vertices[self.face_ind, :]
return vertices
# uv_coords = generate_uv_coords()
# -------------------------
#
# -------------------------
def show_batch(self,img, pos):
h,w = img.shape[2],img.shape[3]
batch_size=img.shape[0]
result=torch.Tensor(batch_size,3,h,w)
img_cpu = img.to("cpu").detach().numpy()*255
pos_cpu = pos.to("cpu").detach().numpy()*255
for i in range(min(img_cpu.shape[0],pos_cpu.shape[0])):
im = img_cpu[i,:,:,:]
im = im.squeeze()
im = im.transpose((1,2,0))
pos = pos_cpu[i,:,:,:]
pos = pos.squeeze()
pos = pos.transpose(1, 2, 0)
kpt = self.get_landmarks(pos)
im = plot_kpt(im, kpt)
#im=plot_landmarks(im, (annotation_cpu[i])*224)
im = cv2.cvtColor(im,cv2.COLOR_RGB2BGR)
im = torch.from_numpy(im).float()
im = im[np.newaxis, :]
im = im.permute(0, 3, 1, 2)
result[i,:,:,:]=im
return result
# -------------------------
#
# -------------------------
def show_res(self,im, pos):
kpt = self.get_landmarks(pos)
im = plot_kpt(im, kpt)
return im