Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. # content_loss
  2.  
  3. def content_loss(layer_features):
  4. base_image_features = layer_features[0, :, :, :]
  5. combination_features = layer_features[2, :, :, :]
  6. return K.sum(K.square(combination_features - base_image_features))
  7.  
  8.  
  9. # Creating a gram matrix
  10.  
  11. def gram_matrix(x):
  12. features = K.batch_flatten(K.permute_dimensions(x, (2, 0, 1)))
  13. gram = K.dot(features, K.transpose(features))
  14. return gram
  15.  
  16.  
  17. # Style_loss_per_layer
  18.  
  19. def style_loss_per_layer(style, combination):
  20. S = gram_matrix(style)
  21. C = gram_matrix(combination)
  22. channels = 3
  23. size = resized_width * resized_height
  24. return K.sum(K.square(S - C)) / (4. * (channels ** 2) * (size ** 2))
  25.  
  26.  
  27. # Get the outputs of each key layer, through unique names
  28.  
  29. outputs_dict = dict([(layer.name, layer.output) for layer in model.layers])
  30.  
  31. # Total_style_loss
  32.  
  33. def total_style_loss(feature_layers):
  34. loss = K.variable(0.)
  35. for layer_name in feature_layers:
  36. layer_features = outputs_dict[layer_name]
  37. style_reference_features = layer_features[1, :, :, :]
  38. combination_features = layer_features[2, :, :, :]
  39. s1 = style_loss_per_layer(style_reference_features, combination_features)
  40. loss += (style_weight / len(feature_layers)) * s1
  41.  
  42. # total_variation_loss
  43.  
  44. def total_variation_loss(x):
  45. a = K.square(x[:, :resized_width - 1, :resized_height - 1, :] - x[:, 1:, :resized_height - 1, :])
  46. b = K.square(x[:, :resized_width - 1, :resized_height - 1, :] - x[:, :resized_width - 1, 1:, :])
  47. return K.sum(K.pow(a + b, 1.25))
  48. return loss
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement