Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # content_loss
- def content_loss(layer_features):
- base_image_features = layer_features[0, :, :, :]
- combination_features = layer_features[2, :, :, :]
- return K.sum(K.square(combination_features - base_image_features))
- # Creating a gram matrix
- def gram_matrix(x):
- features = K.batch_flatten(K.permute_dimensions(x, (2, 0, 1)))
- gram = K.dot(features, K.transpose(features))
- return gram
- # Style_loss_per_layer
- def style_loss_per_layer(style, combination):
- S = gram_matrix(style)
- C = gram_matrix(combination)
- channels = 3
- size = resized_width * resized_height
- return K.sum(K.square(S - C)) / (4. * (channels ** 2) * (size ** 2))
- # Get the outputs of each key layer, through unique names
- outputs_dict = dict([(layer.name, layer.output) for layer in model.layers])
- # Total_style_loss
- def total_style_loss(feature_layers):
- loss = K.variable(0.)
- for layer_name in feature_layers:
- layer_features = outputs_dict[layer_name]
- style_reference_features = layer_features[1, :, :, :]
- combination_features = layer_features[2, :, :, :]
- s1 = style_loss_per_layer(style_reference_features, combination_features)
- loss += (style_weight / len(feature_layers)) * s1
- # total_variation_loss
- def total_variation_loss(x):
- a = K.square(x[:, :resized_width - 1, :resized_height - 1, :] - x[:, 1:, :resized_height - 1, :])
- b = K.square(x[:, :resized_width - 1, :resized_height - 1, :] - x[:, :resized_width - 1, 1:, :])
- return K.sum(K.pow(a + b, 1.25))
- return loss
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement