This file contains 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
# GRADED FUNCTION: triplet_loss | |
def triplet_loss(y_true, y_pred, alpha = 0.2): | |
""" | |
Implementation of the triplet loss as defined by formula (3) | |
Arguments: | |
y_true -- true labels, required when you define a loss in Keras, you don't need it in this function. | |
y_pred -- python list containing three objects: | |
anchor -- the encodings for the anchor images, of shape (None, 128) |
This file contains 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
from keras.models import Sequential | |
from keras.layers import Conv2D, ZeroPadding2D, Activation, Input, concatenate | |
from keras.models import Model | |
from keras.layers.normalization import BatchNormalization | |
from keras.layers.pooling import MaxPooling2D, AveragePooling2D | |
from keras.layers.merge import Concatenate | |
from keras.layers.core import Lambda, Flatten, Dense | |
from keras.initializers import glorot_uniform | |
from keras.engine.topology import Layer | |
from keras import backend as K |
This file contains 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
def model_nn(sess, input_image, num_iterations = 200): | |
# Initialize global variables (you need to run the session on the initializer) | |
### START CODE HERE ### (1 line) | |
sess.run(tf.global_variables_initializer()) | |
### END CODE HERE ### | |
# Run the noisy input image (initial generated image) through the model. Use assign(). | |
### START CODE HERE ### (1 line) | |
sess.run(model['input'].assign(input_image)) |
This file contains 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
# define optimizer (1 line) | |
optimizer = tf.train.AdamOptimizer(2.0) | |
# define train_step (1 line) | |
train_step = optimizer.minimize(J) |
This file contains 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
###content cost | |
# Assign the content image to be the input of the VGG model. | |
sess.run(model['input'].assign(content_image)) | |
# Select the output tensor of layer conv4_2 | |
out = model['conv4_2'] | |
# Set a_C to be the hidden layer activation from the layer we have selected | |
a_C = sess.run(out) |
This file contains 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
def total_cost(J_content, J_style, alpha = 10, beta = 40): | |
""" | |
Computes the total cost function | |
Arguments: | |
J_content -- content cost coded above | |
J_style -- style cost coded above | |
alpha -- hyperparameter weighting the importance of the content cost | |
beta -- hyperparameter weighting the importance of the style cost | |
This file contains 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
#Assigning the weight value. It can be changed. Here, sum of the value of lambda = 1 | |
STYLE_LAYERS = [ | |
('conv1_1', 0.2), | |
('conv2_1', 0.2), | |
('conv3_1', 0.2), | |
('conv4_1', 0.2), | |
('conv5_1', 0.2)] | |
# style cost function | |
def compute_style_cost(model, STYLE_LAYERS): |
This file contains 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
# GRADED FUNCTION: compute_layer_style_cost | |
def compute_layer_style_cost(a_S, a_G): | |
""" | |
Arguments: | |
a_S -- tensor of dimension (1, n_H, n_W, n_C), hidden layer activations representing style of the image S | |
a_G -- tensor of dimension (1, n_H, n_W, n_C), hidden layer activations representing style of the image G | |
Returns: | |
J_style_layer -- tensor representing a scalar value, style cost defined above by equation (2) |
This file contains 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
# Style matrix is also called gram matrix. For our simplicity, we define it as gram matrix | |
def gram_matrix(A): | |
""" | |
Argument: | |
A -- matrix of shape (n_C, n_H*n_W) | |
Returns: | |
GA -- Gram matrix of A, of shape (n_C, n_C) | |
""" |
This file contains 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
def compute_content_cost(a_C, a_G): | |
""" | |
Computes the content cost | |
Arguments: | |
a_C -- tensor of dimension (1, n_H, n_W, n_C), hidden layer activations representing content of the image C | |
a_G -- tensor of dimension (1, n_H, n_W, n_C), hidden layer activations representing content of the image G | |
Returns: | |
J_content -- scalar that you compute using equation 1 above. |
NewerOlder