Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. for ii in range(1, steps+1):
  2.  
  3. # get the features from your target image
  4. target_features = get_features(target, vgg)
  5.  
  6. # the content loss
  7. content_loss = torch.mean((target_features['conv4_2'] - content_features['conv4_2'])**2)
  8.  
  9. # the style loss
  10. # initialize the style loss to 0
  11. style_loss = 0
  12. # then add to it for each layer's gram matrix loss
  13. for layer in style_weights:
  14. # get the "target" style representation for the layer
  15. target_feature = target_features[layer]
  16. target_gram = gram_matrix(target_feature)
  17. _, d, h, w = target_feature.shape
  18. # get the "style" style representation
  19. style_gram = style_grams[layer]
  20. # the style loss for one layer, weighted appropriately
  21. layer_style_loss = style_weights[layer] * torch.mean((target_gram - style_gram)**2)
  22. # add to the style loss
  23. style_loss += layer_style_loss / (d * h * w)
  24.  
  25. # calculate the *total* loss
  26. total_loss = content_weight * content_loss + style_weight * style_loss
  27.  
  28. # update your target image
  29. optimizer.zero_grad()
  30. total_loss.backward()
  31. optimizer.step()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement