diff options
author | David Doan <daviddoan@davids-mbp-3.devices.brown.edu> | 2022-05-07 18:55:47 -0400 |
---|---|---|
committer | David Doan <daviddoan@davids-mbp-3.devices.brown.edu> | 2022-05-07 18:55:47 -0400 |
commit | 79e999da9c3eff40006267b53bab0193242322df (patch) | |
tree | 3456585b93aa57b5010c3ecee5ff785f547be753 /losses.py | |
parent | 7a03723ffb9ac7613f3c5537a4d5c7cb9a4a3f3a (diff) |
changes
Diffstat (limited to 'losses.py')
-rw-r--r-- | losses.py | 17 |
1 files changed, 11 insertions, 6 deletions
@@ -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)) |