aboutsummaryrefslogtreecommitdiff
path: root/losses.py
diff options
context:
space:
mode:
Diffstat (limited to 'losses.py')
-rw-r--r--losses.py17
1 files changed, 11 insertions, 6 deletions
diff --git a/losses.py b/losses.py
index 99b1eded..6405df6a 100644
--- a/losses.py
+++ b/losses.py
@@ -11,16 +11,19 @@ class YourModel(tf.keras.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, clip=False)
self.content_image = tf.expand_dims(self.content_image, axis=0)
+ # self.content_image = self.content_image.astype('uint8')
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, clip=False)
self.style_image = tf.expand_dims(self.style_image, axis=0)
+ # self.style_image = self.style_image.astype('uint8')
#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.content_image.shape[1], self.content_image.shape[2], self.content_image.shape[3]).astype(np.float32), trainable=True)
+ # self.x = self.x.astype('uint8')
self.alpha = hp.alpha
self.beta = hp.beta
@@ -121,7 +124,7 @@ class YourModel(tf.keras.Model):
for i in range(len(photo_layers)):
pl = photo_layers[i]
il = input_layers[i]
- L_content = tf.math.add(L_content, tf.reduce_mean(tf.square(pl - il)))
+ L_content = tf.math.add(L_content, tf.reduce_sum(tf.square(pl - il)))
#print('content loss', L_content)
return L_content
@@ -149,8 +152,10 @@ class YourModel(tf.keras.Model):
# get the loss per each lateral layer
# N depends on # of filters in the layer, M depends on hight and width of feature map
- M_l = art_layer.shape[0]
- N_l = art_layer.shape[1]
+ # M_l = art_layer.shape[0]
+ # N_l = art_layer.shape[1]
+ M_l = self.content_image.shape[0]*self.content_image.shape[1]
+ N_l = art_layer.shape[0]
# layer.filters might not work
E_l = 1/4 * (M_l**(-2)) *(N_l**(-2)) * tf.reduce_sum(tf.square(G_l - A_l))