Мой код передачи нейронного стиля выводит плохое изображение
Я пытаюсь построить модель передачи нейронного стиля, я следую за этой статьей в среде " https://medium.com/tensorflow/neural-style-transfer-creating-art-with-deep-learning-using-tf-keras-and-eager-execution-7d541ac31398 "
но выходное изображение является полным провалом
Я использую модель VGG19.
Я пытался изменить скорость обучения до 100, пока. скорость обучения 1,0 дает полностью черное изображение.
вес потери контента и стиля также был скорректирован, вес потери контента = 100 вес стиля = 10
def content_loss(input_img, content_img):
""" returns content loss
content loss = 0.5 * (input_img - content_img)^2
input_img = generated image
content_img = original content image
"""
input_img = tf.reshape(input_img, [input_img.shape[1], input_img.shape[2], input_img.shape[3]])
loss = -tf.reduce_mean(tf.square(content_img - input_img))
loss = tf.reshape(loss, (1,1))
return loss
def gram_matrix(input_mat):
""" returns gram matrix
* gram matrix is a dot product of the matrix to calulate the similarities across the feature maps
* convert the matrices to vectors for height and width
* dot product will result in a matrix of dimensions nxn where n is the no. of feature maps
"""
# length*width
lw = input_mat.shape[1]*input_mat.shape[2]
channels = input_mat.shape[3]
# reshape vector to convert it to vector
vector = tf.reshape(input_mat, [lw, channels])
gram_matrix = tf.matmul(tf.transpose(vector), vector)
# normalization by dividing the pixel intensity by the width
return gram_matrix/tf.cast(input_mat.shape[0], dtype=tf.float32)
def style_loss(input_img, style_img):
""" returns style loss
* style loss is a mean squared difference of gram matrix of style image and generated image,
input_img = generated image
style_img = original style image
"""
# calculating gram matrix
style_gram = gram_matrix(style_img)
input_gram = gram_matrix(input_img)
# calculating loss
style_l = -tf.reduce_mean(tf.square(style_gram - input_gram))
style_l = tf.reshape(style_l, (1,1))
return style_l
пожалуйста, прокомментируйте, если вам нужно больше деталей. Благодарю.