File size: 14,189 Bytes
18dd6ad |
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 |
import copy
import logging
from typing import Dict, Tuple
import pandas as pd
import pytorch_lightning as ptl
import torch
import torch.nn as nn
import torch.nn.functional as F
# from torch.utils.data import DistributedSampler
# from annotator.lama.saicinpainting.evaluation import make_evaluator
# from annotator.lama.saicinpainting.training.data.datasets import make_default_train_dataloader, make_default_val_dataloader
# from annotator.lama.saicinpainting.training.losses.adversarial import make_discrim_loss
# from annotator.lama.saicinpainting.training.losses.perceptual import PerceptualLoss, ResNetPL
from annotator.lama.saicinpainting.training.modules import make_generator #, make_discriminator
# from annotator.lama.saicinpainting.training.visualizers import make_visualizer
from annotator.lama.saicinpainting.utils import add_prefix_to_keys, average_dicts, set_requires_grad, flatten_dict, \
get_has_ddp_rank
LOGGER = logging.getLogger(__name__)
def make_optimizer(parameters, kind='adamw', **kwargs):
if kind == 'adam':
optimizer_class = torch.optim.Adam
elif kind == 'adamw':
optimizer_class = torch.optim.AdamW
else:
raise ValueError(f'Unknown optimizer kind {kind}')
return optimizer_class(parameters, **kwargs)
def update_running_average(result: nn.Module, new_iterate_model: nn.Module, decay=0.999):
with torch.no_grad():
res_params = dict(result.named_parameters())
new_params = dict(new_iterate_model.named_parameters())
for k in res_params.keys():
res_params[k].data.mul_(decay).add_(new_params[k].data, alpha=1 - decay)
def make_multiscale_noise(base_tensor, scales=6, scale_mode='bilinear'):
batch_size, _, height, width = base_tensor.shape
cur_height, cur_width = height, width
result = []
align_corners = False if scale_mode in ('bilinear', 'bicubic') else None
for _ in range(scales):
cur_sample = torch.randn(batch_size, 1, cur_height, cur_width, device=base_tensor.device)
cur_sample_scaled = F.interpolate(cur_sample, size=(height, width), mode=scale_mode, align_corners=align_corners)
result.append(cur_sample_scaled)
cur_height //= 2
cur_width //= 2
return torch.cat(result, dim=1)
class BaseInpaintingTrainingModule(ptl.LightningModule):
def __init__(self, config, use_ddp, *args, predict_only=False, visualize_each_iters=100,
average_generator=False, generator_avg_beta=0.999, average_generator_start_step=30000,
average_generator_period=10, store_discr_outputs_for_vis=False,
**kwargs):
super().__init__(*args, **kwargs)
LOGGER.info('BaseInpaintingTrainingModule init called')
self.config = config
self.generator = make_generator(config, **self.config.generator)
self.use_ddp = use_ddp
if not get_has_ddp_rank():
LOGGER.info(f'Generator\n{self.generator}')
# if not predict_only:
# self.save_hyperparameters(self.config)
# self.discriminator = make_discriminator(**self.config.discriminator)
# self.adversarial_loss = make_discrim_loss(**self.config.losses.adversarial)
# self.visualizer = make_visualizer(**self.config.visualizer)
# self.val_evaluator = make_evaluator(**self.config.evaluator)
# self.test_evaluator = make_evaluator(**self.config.evaluator)
#
# if not get_has_ddp_rank():
# LOGGER.info(f'Discriminator\n{self.discriminator}')
#
# extra_val = self.config.data.get('extra_val', ())
# if extra_val:
# self.extra_val_titles = list(extra_val)
# self.extra_evaluators = nn.ModuleDict({k: make_evaluator(**self.config.evaluator)
# for k in extra_val})
# else:
# self.extra_evaluators = {}
#
# self.average_generator = average_generator
# self.generator_avg_beta = generator_avg_beta
# self.average_generator_start_step = average_generator_start_step
# self.average_generator_period = average_generator_period
# self.generator_average = None
# self.last_generator_averaging_step = -1
# self.store_discr_outputs_for_vis = store_discr_outputs_for_vis
#
# if self.config.losses.get("l1", {"weight_known": 0})['weight_known'] > 0:
# self.loss_l1 = nn.L1Loss(reduction='none')
#
# if self.config.losses.get("mse", {"weight": 0})['weight'] > 0:
# self.loss_mse = nn.MSELoss(reduction='none')
#
# if self.config.losses.perceptual.weight > 0:
# self.loss_pl = PerceptualLoss()
#
# # if self.config.losses.get("resnet_pl", {"weight": 0})['weight'] > 0:
# # self.loss_resnet_pl = ResNetPL(**self.config.losses.resnet_pl)
# # else:
# # self.loss_resnet_pl = None
#
# self.loss_resnet_pl = None
self.visualize_each_iters = visualize_each_iters
LOGGER.info('BaseInpaintingTrainingModule init done')
def configure_optimizers(self):
discriminator_params = list(self.discriminator.parameters())
return [
dict(optimizer=make_optimizer(self.generator.parameters(), **self.config.optimizers.generator)),
dict(optimizer=make_optimizer(discriminator_params, **self.config.optimizers.discriminator)),
]
def train_dataloader(self):
kwargs = dict(self.config.data.train)
if self.use_ddp:
kwargs['ddp_kwargs'] = dict(num_replicas=self.trainer.num_nodes * self.trainer.num_processes,
rank=self.trainer.global_rank,
shuffle=True)
dataloader = make_default_train_dataloader(**self.config.data.train)
return dataloader
def val_dataloader(self):
res = [make_default_val_dataloader(**self.config.data.val)]
if self.config.data.visual_test is not None:
res = res + [make_default_val_dataloader(**self.config.data.visual_test)]
else:
res = res + res
extra_val = self.config.data.get('extra_val', ())
if extra_val:
res += [make_default_val_dataloader(**extra_val[k]) for k in self.extra_val_titles]
return res
def training_step(self, batch, batch_idx, optimizer_idx=None):
self._is_training_step = True
return self._do_step(batch, batch_idx, mode='train', optimizer_idx=optimizer_idx)
def validation_step(self, batch, batch_idx, dataloader_idx):
extra_val_key = None
if dataloader_idx == 0:
mode = 'val'
elif dataloader_idx == 1:
mode = 'test'
else:
mode = 'extra_val'
extra_val_key = self.extra_val_titles[dataloader_idx - 2]
self._is_training_step = False
return self._do_step(batch, batch_idx, mode=mode, extra_val_key=extra_val_key)
def training_step_end(self, batch_parts_outputs):
if self.training and self.average_generator \
and self.global_step >= self.average_generator_start_step \
and self.global_step >= self.last_generator_averaging_step + self.average_generator_period:
if self.generator_average is None:
self.generator_average = copy.deepcopy(self.generator)
else:
update_running_average(self.generator_average, self.generator, decay=self.generator_avg_beta)
self.last_generator_averaging_step = self.global_step
full_loss = (batch_parts_outputs['loss'].mean()
if torch.is_tensor(batch_parts_outputs['loss']) # loss is not tensor when no discriminator used
else torch.tensor(batch_parts_outputs['loss']).float().requires_grad_(True))
log_info = {k: v.mean() for k, v in batch_parts_outputs['log_info'].items()}
self.log_dict(log_info, on_step=True, on_epoch=False)
return full_loss
def validation_epoch_end(self, outputs):
outputs = [step_out for out_group in outputs for step_out in out_group]
averaged_logs = average_dicts(step_out['log_info'] for step_out in outputs)
self.log_dict({k: v.mean() for k, v in averaged_logs.items()})
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
# standard validation
val_evaluator_states = [s['val_evaluator_state'] for s in outputs if 'val_evaluator_state' in s]
val_evaluator_res = self.val_evaluator.evaluation_end(states=val_evaluator_states)
val_evaluator_res_df = pd.DataFrame(val_evaluator_res).stack(1).unstack(0)
val_evaluator_res_df.dropna(axis=1, how='all', inplace=True)
LOGGER.info(f'Validation metrics after epoch #{self.current_epoch}, '
f'total {self.global_step} iterations:\n{val_evaluator_res_df}')
for k, v in flatten_dict(val_evaluator_res).items():
self.log(f'val_{k}', v)
# standard visual test
test_evaluator_states = [s['test_evaluator_state'] for s in outputs
if 'test_evaluator_state' in s]
test_evaluator_res = self.test_evaluator.evaluation_end(states=test_evaluator_states)
test_evaluator_res_df = pd.DataFrame(test_evaluator_res).stack(1).unstack(0)
test_evaluator_res_df.dropna(axis=1, how='all', inplace=True)
LOGGER.info(f'Test metrics after epoch #{self.current_epoch}, '
f'total {self.global_step} iterations:\n{test_evaluator_res_df}')
for k, v in flatten_dict(test_evaluator_res).items():
self.log(f'test_{k}', v)
# extra validations
if self.extra_evaluators:
for cur_eval_title, cur_evaluator in self.extra_evaluators.items():
cur_state_key = f'extra_val_{cur_eval_title}_evaluator_state'
cur_states = [s[cur_state_key] for s in outputs if cur_state_key in s]
cur_evaluator_res = cur_evaluator.evaluation_end(states=cur_states)
cur_evaluator_res_df = pd.DataFrame(cur_evaluator_res).stack(1).unstack(0)
cur_evaluator_res_df.dropna(axis=1, how='all', inplace=True)
LOGGER.info(f'Extra val {cur_eval_title} metrics after epoch #{self.current_epoch}, '
f'total {self.global_step} iterations:\n{cur_evaluator_res_df}')
for k, v in flatten_dict(cur_evaluator_res).items():
self.log(f'extra_val_{cur_eval_title}_{k}', v)
def _do_step(self, batch, batch_idx, mode='train', optimizer_idx=None, extra_val_key=None):
if optimizer_idx == 0: # step for generator
set_requires_grad(self.generator, True)
set_requires_grad(self.discriminator, False)
elif optimizer_idx == 1: # step for discriminator
set_requires_grad(self.generator, False)
set_requires_grad(self.discriminator, True)
batch = self(batch)
total_loss = 0
metrics = {}
if optimizer_idx is None or optimizer_idx == 0: # step for generator
total_loss, metrics = self.generator_loss(batch)
elif optimizer_idx is None or optimizer_idx == 1: # step for discriminator
if self.config.losses.adversarial.weight > 0:
total_loss, metrics = self.discriminator_loss(batch)
if self.get_ddp_rank() in (None, 0) and (batch_idx % self.visualize_each_iters == 0 or mode == 'test'):
if self.config.losses.adversarial.weight > 0:
if self.store_discr_outputs_for_vis:
with torch.no_grad():
self.store_discr_outputs(batch)
vis_suffix = f'_{mode}'
if mode == 'extra_val':
vis_suffix += f'_{extra_val_key}'
self.visualizer(self.current_epoch, batch_idx, batch, suffix=vis_suffix)
metrics_prefix = f'{mode}_'
if mode == 'extra_val':
metrics_prefix += f'{extra_val_key}_'
result = dict(loss=total_loss, log_info=add_prefix_to_keys(metrics, metrics_prefix))
if mode == 'val':
result['val_evaluator_state'] = self.val_evaluator.process_batch(batch)
elif mode == 'test':
result['test_evaluator_state'] = self.test_evaluator.process_batch(batch)
elif mode == 'extra_val':
result[f'extra_val_{extra_val_key}_evaluator_state'] = self.extra_evaluators[extra_val_key].process_batch(batch)
return result
def get_current_generator(self, no_average=False):
if not no_average and not self.training and self.average_generator and self.generator_average is not None:
return self.generator_average
return self.generator
def forward(self, batch: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
"""Pass data through generator and obtain at leas 'predicted_image' and 'inpainted' keys"""
raise NotImplementedError()
def generator_loss(self, batch) -> Tuple[torch.Tensor, Dict[str, torch.Tensor]]:
raise NotImplementedError()
def discriminator_loss(self, batch) -> Tuple[torch.Tensor, Dict[str, torch.Tensor]]:
raise NotImplementedError()
def store_discr_outputs(self, batch):
out_size = batch['image'].shape[2:]
discr_real_out, _ = self.discriminator(batch['image'])
discr_fake_out, _ = self.discriminator(batch['predicted_image'])
batch['discr_output_real'] = F.interpolate(discr_real_out, size=out_size, mode='nearest')
batch['discr_output_fake'] = F.interpolate(discr_fake_out, size=out_size, mode='nearest')
batch['discr_output_diff'] = batch['discr_output_real'] - batch['discr_output_fake']
def get_ddp_rank(self):
return self.trainer.global_rank if (self.trainer.num_nodes * self.trainer.num_processes) > 1 else None
|