Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Kautenja committed Feb 10, 2018
1 parent 079997e commit dd7bbd8
Show file tree
Hide file tree
Showing 23 changed files with 299 additions and 368 deletions.
8 changes: 4 additions & 4 deletions VGG19-classification.ipynb

Large diffs are not rendered by default.

100 changes: 43 additions & 57 deletions content-reconstruction.ipynb

Large diffs are not rendered by default.

File renamed without changes.
75 changes: 17 additions & 58 deletions neural_stylization/loss_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@
from keras import backend as K


def content_loss(content, combination):
"""
Return the content loss between the content and combinations tensors.
Args:
content: the output of a layer for the content image
combination: the output of a layer for the combination image
Returns:
the loss between `content` and `combination`
"""
# squared euclidean distance, exactly how it is in the paper
return 0.5 * K.sum(K.square(combination - content))


def gram(matrix):
"""
Return a gram matrix for the given input matrix.
Expand All @@ -24,22 +40,6 @@ def gram(matrix):
return g


def content_loss(content, combination):
"""
Return the content loss between the content and combinations tensors.
Args:
content: the output of a layer for the content image
combination: the output of a layer for the combination image
Returns:
the loss between `content` and `combination`
"""
# squared euclidean distance, exactly how it is in the paper
return 0.5 * K.sum(K.square(combination - content))


def style_loss(style, combination):
"""
Return the style loss between the style and combinations tensors.
Expand All @@ -63,45 +63,4 @@ def style_loss(style, combination):
return K.sum(K.square(gram(style) - gram(combination))) / (4 * Nl_2 * Ml_2)


def total_variation_loss(combination, kind='anisotropic'):
"""
Return the total variation loss for the combination image.
Args:
combination: the combination tensor to return the variation loss of
kind: the kind of total variation loss to use (default 'anisotropic')
Returns:
the total variation loss of the combination tensor
"""
# store the dimensions for indexing from thee combination
h, w = combination.shape[1], combination.shape[2]
if kind == 'anisotropic':
# take the absolute value between this image, and the image one pixel
# down, and one pixel to the right. take the absolute value as
# specified by anisotropic loss
a = K.abs(combination[:, :h-1, :w-1, :] - combination[:, 1:, :w-1, :])
b = K.abs(combination[:, :h-1, :w-1, :] - combination[:, :h-1, 1:, :])
# add up all the differences
return K.sum(a + b)
elif kind == 'isotropic':
# take the absolute value between this image, and the image one pixel
# down, and one pixel to the right. take the square root as specified
# by isotropic loss
a = K.square(combination[:, :h-1, :w-1, :] - combination[:, 1:, :w-1, :])
b = K.square(combination[:, :h-1, :w-1, :] - combination[:, :h-1, 1:, :])
# take the vector square root of all the pixel differences, then sum
# them all up
return K.sum(K.pow(a + b, 2))
else:
# kind can only be two values, raise an error on unexpected kind value
raise ValueError("`kind` should be 'anisotropic' or 'isotropic'")


# explicitly export the public API
__all__ = [
'content_loss',
'style_loss',
'total_variation_loss'
]
__all__ = ['content_loss', 'style_loss']
2 changes: 1 addition & 1 deletion neural_stylization/optimizers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Black Box optimizers for the project."""
"""Black Box optimization methods."""
from .sgd import SGD
from .l_bfgs import L_BFGS
3 changes: 2 additions & 1 deletion neural_stylization/optimizers/l_bfgs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""An interface to the L-BFGS Algorithm."""
import numpy as np
from typing import Callable
from tqdm import tqdm
from scipy.optimize import fmin_l_bfgs_b


Expand Down Expand Up @@ -75,7 +76,7 @@ def loss_and_gradients(X):
# assign the custom method to self for loss / gradient calculation
self.loss_and_gradients = loss_and_gradients

for i in range(iterations):
for i in tqdm(range(iterations)):
# pass X through an iteration of LBFGS
X, min_val, info = fmin_l_bfgs_b(self.loss, X,
fprime=self.gradients,
Expand Down
3 changes: 2 additions & 1 deletion neural_stylization/optimizers/sgd.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""An implementation of a basic gradient descent algorithm."""
from typing import Callable
from tqdm import tqdm
import numpy as np


Expand Down Expand Up @@ -43,7 +44,7 @@ def minimize(self,
Returns: an optimized X about the loss and gradients given
"""
for i in range(iterations):
for i in tqdm(range(iterations)):
# pass the input through the loss function and generate gradients
loss_i, grads_i = loss_grads([X])
# move the input based on the gradients and learning rate
Expand Down
10 changes: 5 additions & 5 deletions neural_stylization/reconstruct_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from typing import Callable
from keras import backend as K
from .vgg19 import VGG_19
from .img_util import load_image
from .img_util import normalize
from .img_util import denormalize
from .img_util import image_to_matrix
from .img_util import matrix_to_image
from .util.img_util import load_image
from .util.img_util import normalize
from .util.img_util import denormalize
from .util.img_util import image_to_matrix
from .util.img_util import matrix_to_image
from .loss_functions import content_loss
from .optimizers.l_bfgs import L_BFGS

Expand Down
10 changes: 5 additions & 5 deletions neural_stylization/reconstruct_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from typing import Callable
from keras import backend as K
from .vgg19 import VGG_19
from .img_util import load_image
from .img_util import normalize
from .img_util import denormalize
from .img_util import image_to_matrix
from .img_util import matrix_to_image
from .util.img_util import load_image
from .util.img_util import normalize
from .util.img_util import denormalize
from .util.img_util import image_to_matrix
from .util.img_util import matrix_to_image
from .loss_functions import style_loss
from .optimizers.l_bfgs import L_BFGS

Expand Down
14 changes: 7 additions & 7 deletions neural_stylization/transfer_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from PIL import Image
from typing import Callable
from keras import backend as K
from neural_stylization.vgg19 import VGG_19
from neural_stylization.img_util import normalize
from neural_stylization.img_util import denormalize
from neural_stylization.img_util import load_image
from neural_stylization.img_util import image_to_matrix
from neural_stylization.img_util import matrix_to_image
from neural_stylization.loss_functions import content_loss, style_loss
from .vgg19 import VGG_19
from .util.img_util import normalize
from .util.img_util import denormalize
from .util.img_util import load_image
from .util.img_util import image_to_matrix
from .util.img_util import matrix_to_image
from .loss_functions import content_loss, style_loss


# the template for the class's repr method
Expand Down
1 change: 1 addition & 0 deletions neural_stylization/util/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Utility methods for the parent package."""
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ def build_callback(out_dir: str):
for file in glob('{}/*.png'.format(out_dir)):
os.remove(file)

from neural_stylization.img_util import denormalize
from neural_stylization.img_util import matrix_to_image
from .img_util import denormalize
from .img_util import matrix_to_image
from IPython import display

def denormalize_and_display(image, i) -> None:
"""
Expand All @@ -33,7 +34,6 @@ def denormalize_and_display(image, i) -> None:
Returns: None
"""
from IPython import display
# clear the existing output
display.clear_output(wait=True)
# denormalize the image and conver to binary
Expand All @@ -42,7 +42,5 @@ def denormalize_and_display(image, i) -> None:
image.save('{}/{}.png'.format(out_dir, i))
# display the image on the IPython front end
display.display(image)
# display the iteration beneat the image
display.display(i)

return denormalize_and_display
File renamed without changes.
File renamed without changes.
4 changes: 1 addition & 3 deletions neural_stylization/vgg19.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
from keras import backend as K


# the definition for a tensor type as defined by the keras documentation:
# https://keras.io/backend/
# scroll to the `is_keras_tensor` section for a description of this type
# the definition for a tensor type as defined by Keras
Tensor = Union[Input, Layer]


Expand Down
100 changes: 43 additions & 57 deletions style-reconstruction.ipynb

Large diffs are not rendered by default.

329 changes: 165 additions & 164 deletions style-transfer.ipynb

Large diffs are not rendered by default.

Binary file modified tex/img/transfer/kandinsky.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tex/img/transfer/monet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tex/img/transfer/scream.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tex/img/transfer/seated-nudes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tex/img/transfer/shipwreck.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tex/img/transfer/starry-starry-night.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit dd7bbd8

Please sign in to comment.