aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--__pycache__/hyperparameters.cpython-38.pycbin341 -> 332 bytes
-rw-r--r--__pycache__/losses.cpython-38.pycbin4409 -> 4546 bytes
-rw-r--r--hyperparameters.py6
-rw-r--r--losses.py37
-rw-r--r--main.py12
-rw-r--r--save.jpgbin39903 -> 46064 bytes
6 files changed, 40 insertions, 15 deletions
diff --git a/__pycache__/hyperparameters.cpython-38.pyc b/__pycache__/hyperparameters.cpython-38.pyc
index e1a90bd4..7d32eefa 100644
--- a/__pycache__/hyperparameters.cpython-38.pyc
+++ b/__pycache__/hyperparameters.cpython-38.pyc
Binary files differ
diff --git a/__pycache__/losses.cpython-38.pyc b/__pycache__/losses.cpython-38.pyc
index 71b86245..66e565ec 100644
--- a/__pycache__/losses.cpython-38.pyc
+++ b/__pycache__/losses.cpython-38.pyc
Binary files differ
diff --git a/hyperparameters.py b/hyperparameters.py
index b03db017..460543dc 100644
--- a/hyperparameters.py
+++ b/hyperparameters.py
@@ -9,17 +9,17 @@ Number of epochs. If you experiment with more complex networks you
might need to increase this. Likewise if you add regularization that
slows training.
"""
-num_epochs = 5000
+num_epochs = 1000
"""
A critical parameter that can dramatically affect whether training
succeeds or fails. The value for this depends significantly on which
optimizer is used. Refer to the default learning rate parameter
"""
-learning_rate = 3e-2
+learning_rate = 1e-2
momentum = 0.01
alpha = 1e-2
-beta = 1e-5
+beta = 1e-4
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)
diff --git a/main.py b/main.py
index 60574e63..a074f71d 100644
--- a/main.py
+++ b/main.py
@@ -1,7 +1,9 @@
import os
import sys
import argparse
+import cv2
import tensorflow as tf
+from skimage import transform
import hyperparameters as hp
from losses import YourModel
@@ -50,9 +52,15 @@ def main():
print('this is',ARGS.content)
content_image = imread(ARGS.content)
- content_image = np.resize(content_image, (255, 255, 3))
+
+
style_image = imread(ARGS.style)
- style_image = np.resize(style_image, (255, 255, 3))
+ cv2.imshow('hi1', style_image)
+ cv2.waitKey(0)
+
+ style_image = transform.resize(style_image, content_image.shape)
+ cv2.imshow('hi2', style_image)
+ cv2.waitKey(0)
my_model = YourModel(content_image=content_image, style_image=style_image)
my_model.vgg16.build([1, 255, 255, 3])
my_model.vgg16.load_weights('vgg16_imagenet.h5', by_name=True)
diff --git a/save.jpg b/save.jpg
index 86b5f854..f4f49d6e 100644
--- a/save.jpg
+++ b/save.jpg
Binary files differ