loss.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. from fastai import *
  2. from fastai.core import *
  3. from fastai.torch_core import *
  4. from fastai.callbacks import hook_outputs
  5. import torchvision.models as models
  6. class FeatureLoss(nn.Module):
  7. def __init__(self, layer_wgts=[20, 70, 10]):
  8. super().__init__()
  9. self.m_feat = models.vgg16_bn(True).features.cuda().eval()
  10. requires_grad(self.m_feat, False)
  11. blocks = [
  12. i - 1
  13. for i, o in enumerate(children(self.m_feat))
  14. if isinstance(o, nn.MaxPool2d)
  15. ]
  16. layer_ids = blocks[2:5]
  17. self.loss_features = [self.m_feat[i] for i in layer_ids]
  18. self.hooks = hook_outputs(self.loss_features, detach=False)
  19. self.wgts = layer_wgts
  20. self.metric_names = ['pixel'] + [f'feat_{i}' for i in range(len(layer_ids))]
  21. self.base_loss = F.l1_loss
  22. def _make_features(self, x, clone=False):
  23. self.m_feat(x)
  24. return [(o.clone() if clone else o) for o in self.hooks.stored]
  25. def forward(self, input, target):
  26. out_feat = self._make_features(target, clone=True)
  27. in_feat = self._make_features(input)
  28. self.feat_losses = [self.base_loss(input, target)]
  29. self.feat_losses += [
  30. self.base_loss(f_in, f_out) * w
  31. for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)
  32. ]
  33. self.metrics = dict(zip(self.metric_names, self.feat_losses))
  34. return sum(self.feat_losses)
  35. def __del__(self):
  36. self.hooks.remove()
  37. # Includes wasserstein loss
  38. class WassFeatureLoss(nn.Module):
  39. def __init__(self, layer_wgts=[5, 15, 2], wass_wgts=[3.0, 0.7, 0.01]):
  40. super().__init__()
  41. self.m_feat = models.vgg16_bn(True).features.cuda().eval()
  42. requires_grad(self.m_feat, False)
  43. blocks = [
  44. i - 1
  45. for i, o in enumerate(children(self.m_feat))
  46. if isinstance(o, nn.MaxPool2d)
  47. ]
  48. layer_ids = blocks[2:5]
  49. self.loss_features = [self.m_feat[i] for i in layer_ids]
  50. self.hooks = hook_outputs(self.loss_features, detach=False)
  51. self.wgts = layer_wgts
  52. self.wass_wgts = wass_wgts
  53. self.metric_names = (
  54. ['pixel']
  55. + [f'feat_{i}' for i in range(len(layer_ids))]
  56. + [f'wass_{i}' for i in range(len(layer_ids))]
  57. )
  58. self.base_loss = F.l1_loss
  59. def _make_features(self, x, clone=False):
  60. self.m_feat(x)
  61. return [(o.clone() if clone else o) for o in self.hooks.stored]
  62. def _calc_2_moments(self, tensor):
  63. chans = tensor.shape[1]
  64. tensor = tensor.view(1, chans, -1)
  65. n = tensor.shape[2]
  66. mu = tensor.mean(2)
  67. tensor = (tensor - mu[:, :, None]).squeeze(0)
  68. # Prevents nasty bug that happens very occassionally- divide by zero. Why such things happen?
  69. if n == 0:
  70. return None, None
  71. cov = torch.mm(tensor, tensor.t()) / float(n)
  72. return mu, cov
  73. def _get_style_vals(self, tensor):
  74. mean, cov = self._calc_2_moments(tensor)
  75. if mean is None:
  76. return None, None, None
  77. eigvals, eigvects = torch.symeig(cov, eigenvectors=True)
  78. eigroot_mat = torch.diag(torch.sqrt(eigvals.clamp(min=0)))
  79. root_cov = torch.mm(torch.mm(eigvects, eigroot_mat), eigvects.t())
  80. tr_cov = eigvals.clamp(min=0).sum()
  81. return mean, tr_cov, root_cov
  82. def _calc_l2wass_dist(
  83. self, mean_stl, tr_cov_stl, root_cov_stl, mean_synth, cov_synth
  84. ):
  85. tr_cov_synth = torch.symeig(cov_synth, eigenvectors=True)[0].clamp(min=0).sum()
  86. mean_diff_squared = (mean_stl - mean_synth).pow(2).sum()
  87. cov_prod = torch.mm(torch.mm(root_cov_stl, cov_synth), root_cov_stl)
  88. var_overlap = torch.sqrt(
  89. torch.symeig(cov_prod, eigenvectors=True)[0].clamp(min=0) + 1e-8
  90. ).sum()
  91. dist = mean_diff_squared + tr_cov_stl + tr_cov_synth - 2 * var_overlap
  92. return dist
  93. def _single_wass_loss(self, pred, targ):
  94. mean_test, tr_cov_test, root_cov_test = targ
  95. mean_synth, cov_synth = self._calc_2_moments(pred)
  96. loss = self._calc_l2wass_dist(
  97. mean_test, tr_cov_test, root_cov_test, mean_synth, cov_synth
  98. )
  99. return loss
  100. def forward(self, input, target):
  101. out_feat = self._make_features(target, clone=True)
  102. in_feat = self._make_features(input)
  103. self.feat_losses = [self.base_loss(input, target)]
  104. self.feat_losses += [
  105. self.base_loss(f_in, f_out) * w
  106. for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)
  107. ]
  108. styles = [self._get_style_vals(i) for i in out_feat]
  109. if styles[0][0] is not None:
  110. self.feat_losses += [
  111. self._single_wass_loss(f_pred, f_targ) * w
  112. for f_pred, f_targ, w in zip(in_feat, styles, self.wass_wgts)
  113. ]
  114. self.metrics = dict(zip(self.metric_names, self.feat_losses))
  115. return sum(self.feat_losses)
  116. def __del__(self):
  117. self.hooks.remove()