aboutsummaryrefslogtreecommitdiff
path: root/losses.py
diff options
context:
space:
mode:
authorLogan Bauman <logan_bauman@brown.edu>2022-05-06 23:32:17 -0400
committerLogan Bauman <logan_bauman@brown.edu>2022-05-06 23:32:17 -0400
commitd802c988a57d6afe4fca979384ba377ecc7edb66 (patch)
treebf604e5da1bee0f2bf1ef16cc67df9a61dede2fa /losses.py
parent6e5f2d1a62f4f3bf0e87829082b2120ca440ddf0 (diff)
hi
Diffstat (limited to 'losses.py')
-rw-r--r--losses.py37
1 files changed, 27 insertions, 10 deletions
diff --git a/losses.py b/losses.py
index 7198ebf4..5564ca33 100644
--- a/losses.py
+++ b/losses.py
@@ -1,8 +1,10 @@
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. """
@@ -11,17 +13,26 @@ class YourModel(tf.keras.Model):
self.content_image = transform.resize(content_image, tf.shape(style_image), anti_aliasing=True)
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 = 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(np.float32), trainable=True)
+
self.alpha = hp.alpha
self.beta = hp.beta
- print(self.x.shape)
+ self.photo_layers = None
+ self.art_layers = None
+
- print(self.content_image.shape, self.style_image.shape)
+
+ #(self.x.shape)
+
+ #print(self.content_image.shape, self.style_image.shape)
self.optimizer = tf.keras.optimizers.Adam()
@@ -88,14 +99,20 @@ 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)
+ return (self.alpha * content_l) + (self.beta * style_l)
def content_loss(self, photo_layers, input_layers):
L_content = tf.constant(0.0)