diff options
-rw-r--r-- | __pycache__/hyperparameters.cpython-38.pyc | bin | 369 -> 344 bytes | |||
-rw-r--r-- | __pycache__/losses.cpython-38.pyc | bin | 4473 -> 4664 bytes | |||
-rw-r--r-- | content.jpg | bin | 0 -> 45172 bytes | |||
-rw-r--r-- | data/content.jpeg | bin | 0 -> 68587 bytes | |||
-rw-r--r-- | data/extratrain/otto.jpeg | bin | 0 -> 50249 bytes | |||
-rw-r--r-- | data/extratrain/star.jpeg | bin | 0 -> 540085 bytes | |||
-rw-r--r-- | data/style.jpeg | bin | 0 -> 65581 bytes | |||
-rw-r--r-- | hyperparameters.py | 8 | ||||
-rw-r--r-- | losses.py | 49 | ||||
-rw-r--r-- | main.py | 7 | ||||
-rw-r--r-- | save.jpg | bin | 30377 -> 19949 bytes | |||
-rw-r--r-- | style.jpg | bin | 0 -> 43386 bytes |
12 files changed, 44 insertions, 20 deletions
diff --git a/__pycache__/hyperparameters.cpython-38.pyc b/__pycache__/hyperparameters.cpython-38.pyc Binary files differindex 11bc2070..12ab00e4 100644 --- a/__pycache__/hyperparameters.cpython-38.pyc +++ b/__pycache__/hyperparameters.cpython-38.pyc diff --git a/__pycache__/losses.cpython-38.pyc b/__pycache__/losses.cpython-38.pyc Binary files differindex d583a985..d25c70d0 100644 --- a/__pycache__/losses.cpython-38.pyc +++ b/__pycache__/losses.cpython-38.pyc diff --git a/content.jpg b/content.jpg Binary files differnew file mode 100644 index 00000000..163629cd --- /dev/null +++ b/content.jpg diff --git a/data/content.jpeg b/data/content.jpeg Binary files differnew file mode 100644 index 00000000..398d20ea --- /dev/null +++ b/data/content.jpeg diff --git a/data/extratrain/otto.jpeg b/data/extratrain/otto.jpeg Binary files differnew file mode 100644 index 00000000..9dbb4461 --- /dev/null +++ b/data/extratrain/otto.jpeg diff --git a/data/extratrain/star.jpeg b/data/extratrain/star.jpeg Binary files differnew file mode 100644 index 00000000..106713da --- /dev/null +++ b/data/extratrain/star.jpeg diff --git a/data/style.jpeg b/data/style.jpeg Binary files differnew file mode 100644 index 00000000..0c4015df --- /dev/null +++ b/data/style.jpeg diff --git a/hyperparameters.py b/hyperparameters.py index 63e51b91..75528742 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 = 100 +num_epochs = 500 """ 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 = 1e2 momentum = 0.01 -alpha = 1e-5 +alpha = 1 -beta = 1e-2 +beta = 100 @@ -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] @@ -1,7 +1,9 @@ import os import sys import argparse + import tensorflow as tf +from skimage import transform import hyperparameters as hp from losses import YourModel @@ -37,7 +39,8 @@ def parse_args(): return parser.parse_args() def train(model): - for _ in range(hp.num_epochs): + for i in range(hp.num_epochs): + print('batch', i) model.train_step() def main(): @@ -51,7 +54,7 @@ def main(): content_image = imread(ARGS.content) style_image = imread(ARGS.style) - style_image = transform.resize(style_image, content_image.shape) + style_image = transform.resize(style_image, content_image.shape).astype('uint8') 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) Binary files differdiff --git a/style.jpg b/style.jpg Binary files differnew file mode 100644 index 00000000..105ac2d1 --- /dev/null +++ b/style.jpg |