Last active
          July 5, 2020 10:02 
        
      - 
      
 - 
        
Save neelriyer/947f2db71fb553c395927d5682fbdeaf to your computer and use it in GitHub Desktop.  
    Vide Restoration using Deep Learning
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | # adapted from: https://github.com/fastai/course-v3/blob/master/nbs/dl1/lesson7-superres.ipynb | |
| t = data.valid_ds[0][1].data | |
| t = torch.stack([t,t]) | |
| def gram_matrix(x): | |
| n,c,h,w = x.size() | |
| x = x.view(n, c, -1) | |
| return (x @ x.transpose(1,2))/(c*h*w) | |
| base_loss = F.l1_loss | |
| vgg_m = vgg16_bn(True).features.cuda().eval() | |
| requires_grad(vgg_m, False) | |
| blocks = [i-1 for i,o in enumerate(children(vgg_m)) if isinstance(o,nn.MaxPool2d)] | |
| blocks, [vgg_m[i] for i in blocks] | |
| class FeatureLoss(nn.Module): | |
| def __init__(self, m_feat, layer_ids, layer_wgts): | |
| super().__init__() | |
| self.m_feat = m_feat | |
| self.loss_features = [self.m_feat[i] for i in layer_ids] | |
| self.hooks = hook_outputs(self.loss_features, detach=False) | |
| self.wgts = layer_wgts | |
| self.metric_names = ['pixel',] + [f'feat_{i}' for i in range(len(layer_ids)) | |
| ] + [f'gram_{i}' for i in range(len(layer_ids))] | |
| def make_features(self, x, clone=False): | |
| self.m_feat(x) | |
| return [(o.clone() if clone else o) for o in self.hooks.stored] | |
| def forward(self, input, target): | |
| out_feat = self.make_features(target, clone=True) | |
| in_feat = self.make_features(input) | |
| self.feat_losses = [base_loss(input,target)] | |
| self.feat_losses += [base_loss(f_in, f_out)*w | |
| for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)] | |
| self.feat_losses += [base_loss(gram_matrix(f_in), gram_matrix(f_out))*w**2 * 5e3 | |
| for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)] | |
| self.metrics = dict(zip(self.metric_names, self.feat_losses)) | |
| return sum(self.feat_losses) | |
| def __del__(self): self.hooks.remove() | |
| feat_loss = FeatureLoss(vgg_m, blocks[2:5], [5,15,2]) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment