loss.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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:[float]=[5.0,15.0,2.0], gram_wgt:float=5e3):
  8. super().__init__()
  9. self.gram_wgt = gram_wgt
  10. self.base_loss = F.l1_loss
  11. self.m_feat = models.vgg16_bn(True).features.cuda().eval()
  12. requires_grad(self.m_feat, False)
  13. blocks = [i-1 for i,o in enumerate(children(self.m_feat)) if isinstance(o,nn.MaxPool2d)]
  14. layer_ids = blocks[2:5]
  15. self.loss_features = [self.m_feat[i] for i in layer_ids]
  16. self.hooks = hook_outputs(self.loss_features, detach=False)
  17. self.wgts = layer_wgts
  18. self.metric_names = ['pixel',] + [f'feat_{i}' for i in range(len(layer_ids))
  19. ] + [f'gram_{i}' for i in range(len(layer_ids))]
  20. def _gram_matrix(self, x:torch.Tensor):
  21. n,c,h,w = x.size()
  22. x = x.view(n, c, -1)
  23. return (x @ x.transpose(1,2))/(c*h*w)
  24. def make_features(self, x:torch.Tensor, clone=False):
  25. self.m_feat(x)
  26. return [(o.clone() if clone else o) for o in self.hooks.stored]
  27. def forward(self, input:torch.Tensor, target:torch.Tensor):
  28. out_feat = self.make_features(target, clone=True)
  29. in_feat = self.make_features(input)
  30. self.feat_losses = [self.base_loss(f_in, f_out)*w
  31. for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)]
  32. self.feat_losses += [self.base_loss(input,target)]
  33. self.feat_losses += [self.base_loss(self._gram_matrix(f_in), self._gram_matrix(f_out))*w**2 * self.gram_wgt
  34. for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)]
  35. self.metrics = dict(zip(self.metric_names, self.feat_losses))
  36. return sum(self.feat_losses)
  37. def __del__(self):
  38. self.hooks.remove()
  39. class FeatureLoss2(nn.Module):
  40. def __init__(self, layer_wgts:[float]=[20.0,70.0,10.0], gram_wgt:float=5e3):
  41. super().__init__()
  42. self.gram_wgt = gram_wgt
  43. self.base_loss = F.l1_loss
  44. self.m_feat = models.vgg16_bn(True).features.cuda().eval()
  45. requires_grad(self.m_feat, False)
  46. blocks = [i-1 for i,o in enumerate(children(self.m_feat)) if isinstance(o,nn.MaxPool2d)]
  47. layer_ids = blocks[2:5]
  48. self.loss_features = [self.m_feat[i] for i in layer_ids]
  49. self.hooks = hook_outputs(self.loss_features, detach=False)
  50. self.wgts = layer_wgts
  51. self.metric_names = ['pixel',] + [f'feat_{i}' for i in range(len(layer_ids))
  52. ] + [f'gram_{i}' for i in range(len(layer_ids))]
  53. def _gram_matrix(self, x:torch.Tensor):
  54. n,c,h,w = x.size()
  55. x = x.view(n, c, -1)
  56. return (x @ x.transpose(1,2))/(c*h*w)
  57. def make_features(self, x:torch.Tensor, clone=False):
  58. self.m_feat(x)
  59. return [(o.clone() if clone else o) for o in self.hooks.stored]
  60. def forward(self, input:torch.Tensor, target:torch.Tensor):
  61. out_feat = self.make_features(target, clone=True)
  62. in_feat = self.make_features(input)
  63. self.feat_losses = [self.base_loss(f_in, f_out)*w
  64. for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)]
  65. self.feat_losses += [self.base_loss(input,target)*100]
  66. self.feat_losses += [self.base_loss(self._gram_matrix(f_in), self._gram_matrix(f_out))*w**2 * self.gram_wgt
  67. for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)]
  68. self.metrics = dict(zip(self.metric_names, self.feat_losses))
  69. return sum(self.feat_losses)
  70. def __del__(self):
  71. self.hooks.remove()
  72. #Includes wasserstein loss
  73. class FeatureLoss3(nn.Module):
  74. def __init__(self, layer_wgts=[5,15,2], wass_wgts=[3.0,0.7,0.01]):
  75. super().__init__()
  76. self.m_feat = models.vgg16_bn(True).features.cuda().eval()
  77. requires_grad(self.m_feat, False)
  78. blocks = [i-1 for i,o in enumerate(children(self.m_feat)) if isinstance(o,nn.MaxPool2d)]
  79. layer_ids = blocks[2:5]
  80. self.loss_features = [self.m_feat[i] for i in layer_ids]
  81. self.hooks = hook_outputs(self.loss_features, detach=False)
  82. self.wgts = layer_wgts
  83. self.wass_wgts = wass_wgts
  84. self.metric_names = ['pixel',] + [f'feat_{i}' for i in range(len(layer_ids))
  85. ] + [f'wass_{i}' for i in range(len(layer_ids))]
  86. self.base_loss = F.l1_loss
  87. def _make_features(self, x, clone=False):
  88. self.m_feat(x)
  89. return [(o.clone() if clone else o) for o in self.hooks.stored]
  90. def _calc_2_moments(self, tensor):
  91. chans = tensor.shape[1]
  92. tensor = tensor.view(1, chans, -1)
  93. n = tensor.shape[2]
  94. mu = tensor.mean(2)
  95. tensor = (tensor - mu[:,:,None]).squeeze(0)
  96. cov = torch.mm(tensor, tensor.t()) / float(n)
  97. return mu, cov
  98. def _get_style_vals(self, tensor):
  99. mean, cov = self._calc_2_moments(tensor)
  100. eigvals, eigvects = torch.symeig(cov, eigenvectors=True)
  101. eigroot_mat = torch.diag(torch.sqrt(eigvals.clamp(min=0)))
  102. root_cov = torch.mm(torch.mm(eigvects, eigroot_mat), eigvects.t())
  103. tr_cov = eigvals.clamp(min=0).sum()
  104. return mean, tr_cov, root_cov
  105. def _calc_l2wass_dist(self, mean_stl, tr_cov_stl, root_cov_stl, mean_synth, cov_synth):
  106. tr_cov_synth = torch.symeig(cov_synth, eigenvectors=True)[0].clamp(min=0).sum()
  107. mean_diff_squared = (mean_stl - mean_synth).pow(2).sum()
  108. cov_prod = torch.mm(torch.mm(root_cov_stl, cov_synth), root_cov_stl)
  109. var_overlap = torch.sqrt(torch.symeig(cov_prod, eigenvectors=True)[0].clamp(min=0)+1e-8).sum()
  110. dist = mean_diff_squared + tr_cov_stl + tr_cov_synth - 2*var_overlap
  111. return dist
  112. def _single_wass_loss(self, pred, targ):
  113. mean_test, tr_cov_test, root_cov_test = targ
  114. mean_synth, cov_synth = self._calc_2_moments(pred)
  115. loss = self._calc_l2wass_dist(mean_test, tr_cov_test, root_cov_test, mean_synth, cov_synth)
  116. return loss
  117. def forward(self, input, target):
  118. out_feat = self._make_features(target, clone=True)
  119. in_feat = self._make_features(input)
  120. self.feat_losses = [self.base_loss(input,target)]
  121. self.feat_losses += [self.base_loss(f_in, f_out)*w
  122. for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)]
  123. styles = [self._get_style_vals(i) for i in out_feat]
  124. self.feat_losses += [self._single_wass_loss(f_pred, f_targ)*w
  125. for f_pred, f_targ, w in zip(in_feat, styles, self.wass_wgts)]
  126. self.metrics = dict(zip(self.metric_names, self.feat_losses))
  127. return sum(self.feat_losses)
  128. def __del__(self): self.hooks.remove()
  129. #"Before activations" in ESRGAN paper
  130. class FeatureLoss4(nn.Module):
  131. def __init__(self, layer_wgts=[5,15,2]):
  132. super().__init__()
  133. self.m_feat = models.vgg16_bn(True).features.cuda().eval()
  134. requires_grad(self.m_feat, False)
  135. blocks = [i-2 for i,o in enumerate(children(self.m_feat)) if isinstance(o,nn.MaxPool2d)]
  136. layer_ids = blocks[2:5]
  137. self.loss_features = [self.m_feat[i] for i in layer_ids]
  138. self.hooks = hook_outputs(self.loss_features, detach=False)
  139. self.wgts = layer_wgts
  140. self.metric_names = ['pixel',] + [f'feat_{i}' for i in range(len(layer_ids))]
  141. self.base_loss = F.l1_loss
  142. def _make_features(self, x, clone=False):
  143. self.m_feat(x)
  144. return [(o.clone() if clone else o) for o in self.hooks.stored]
  145. def forward(self, input, target):
  146. out_feat = self._make_features(target, clone=True)
  147. in_feat = self._make_features(input)
  148. self.feat_losses = [self.base_loss(input,target)]
  149. self.feat_losses += [self.base_loss(f_in, f_out)*w
  150. for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)]
  151. self.metrics = dict(zip(self.metric_names, self.feat_losses))
  152. return sum(self.feat_losses)
  153. def __del__(self): self.hooks.remove()