diff options
Diffstat (limited to 'losses.py')
-rw-r--r-- | losses.py | 49 |
1 files changed, 35 insertions, 14 deletions
@@ -1,27 +1,40 @@ import tensorflow as tf +import numpy as np from tensorflow.keras.layers import \ Conv2D, AveragePooling2D from skimage import transform import hyperparameters as hp + class YourModel(tf.keras.Model): """ Your own neural network model. """ def __init__(self, content_image, style_image): #normalize these images to float values super(YourModel, self).__init__() - self.content_image = transform.resize(content_image, tf.shape(style_image), anti_aliasing=True) + self.content_image = transform.resize(content_image, tf.shape(style_image), anti_aliasing=True, preserve_range=True).astype('uint8') self.content_image = tf.expand_dims(self.content_image, axis=0) - + print(self.content_image) + #perhaps consider cropping to avoid distortion - self.style_image = transform.resize(style_image, tf.shape(style_image), anti_aliasing=True) + self.style_image = transform.resize(style_image, tf.shape(style_image), anti_aliasing=True, preserve_range=True).astype('uint8') self.style_image = tf.expand_dims(self.style_image, axis=0) - self.x = tf.Variable(tf.expand_dims(tf.random.uniform(tf.shape(content_image)), axis=0), trainable=True) + #self.x = tf.Variable(initial_value = self.content_image.numpy().astype(np.float32), trainable=True) + self.x = tf.Variable(initial_value = np.random.rand(self.content_image.shape[0], + self.content_image.shape[1], self.content_image.shape[2], self.content_image.shape[3]).astype('uint8'), trainable=True) + self.alpha = hp.alpha self.beta = hp.beta - print(self.content_image.shape, self.style_image.shape) + self.photo_layers = None + self.art_layers = None + - self.optimizer = tf.keras.optimizers.RMSprop(learning_rate=hp.learning_rate, momentum=hp.momentum) + + #(self.x.shape) + + #print(self.content_image.shape, self.style_image.shape) + + self.optimizer = tf.keras.optimizers.Adam(hp.learning_rate) self.vgg16 = [ # Block 1 @@ -86,17 +99,25 @@ class YourModel(tf.keras.Model): return x, layers def loss_fn(self, p, a, x): - _, photo_layers = self.call(p) - _, art_layers = self.call(a) - _, input_layers = self.call(x) - - content_l = self.content_loss(photo_layers, input_layers) - style_l = self.style_loss(art_layers, input_layers) + # print(p) + if(self.photo_layers == None): + _, self.photo_layers = self.call(p) + # print(a) + if(self.art_layers == None): + _, self.art_layers = self.call(a) + # print(x) + _, input_layers = self.call(x) + + + content_l = self.content_loss(self.photo_layers, input_layers) + style_l = self.style_loss(self.art_layers, input_layers) # Equation 7 - return (self.alpha * content_l) + (self.beta * style_l) + print('style_loss', style_l) + print('content_loss', content_l) + return (self.alpha * content_l) + (self.beta * style_l) def content_loss(self, photo_layers, input_layers): - L_content = tf.constant(0.0) + L_content = tf.constant(0.0).astype('uint8') for i in range(len(photo_layers)): pl = photo_layers[i] il = input_layers[i] |