Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added lib/images/style/west-elm-brightcolors.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
140 changes: 140 additions & 0 deletions src/custom_vgg19V2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import os
import tensorflow as tf
import numpy as np
import inspect
import urllib.request

VGG_MEAN = [103.939, 116.779, 123.68]
data = None
dir_path = os.path.dirname(os.path.realpath(__file__))
weights_name = dir_path + "/../lib/weights/vgg19.npy"
weights_url = "https://www.dropbox.com/s/68opci8420g7bcl/vgg19.npy?dl=1"


class Vgg19:
def __init__(self, vgg19_npy_path=None):
global data

if vgg19_npy_path is None:
path = inspect.getfile(Vgg19)
path = os.path.abspath(os.path.join(path, os.pardir))
path = os.path.join(path, weights_name)

if os.path.exists(path):
vgg19_npy_path = path
else:
print("VGG19 weights were not found in the project directory")

answer = 0
while answer is not 'y' and answer is not 'N':
answer = input("Would you like to download the 548 MB file? [y/N] ").replace(" ", "")

# Download weights if yes, else exit the program
if answer == 'y':
print("Downloading. Please be patient...")
urllib.request.urlretrieve(weights_url, weights_name)
vgg19_npy_path = path
elif answer == 'N':
print("Exiting the program..")
exit(0)

if data is None:
data = np.load(vgg19_npy_path, encoding='latin1', allow_pickle=True)
self.data_dict = data.item()
print("VGG19 weights loaded")

else:
self.data_dict = data.item()

def build(self, rgb, shape):
rgb_scaled = rgb * 255.0
num_channels = shape[2]
channel_shape = shape
channel_shape[2] = 1

# Convert RGB to BGR
red, green, blue = tf.split(axis=3, num_or_size_splits=3, value=rgb_scaled)

assert red.get_shape().as_list()[1:] == channel_shape
assert green.get_shape().as_list()[1:] == channel_shape
assert blue.get_shape().as_list()[1:] == channel_shape

bgr = tf.concat(axis=3, values=[
blue - VGG_MEAN[0],
green - VGG_MEAN[1],
red - VGG_MEAN[2],
])

shape[2] = num_channels
assert bgr.get_shape().as_list()[1:] == shape

self.conv1_1 = self.conv_layer(bgr, "conv1_1")
self.conv1_2 = self.conv_layer(self.conv1_1, "conv1_2")
self.pool1 = self.avg_pool(self.conv1_2, 'pool1')

self.conv2_1 = self.conv_layer(self.pool1, "conv2_1")
self.conv2_2 = self.conv_layer(self.conv2_1, "conv2_2")
self.pool2 = self.avg_pool(self.conv2_2, 'pool2')

self.conv3_1 = self.conv_layer(self.pool2, "conv3_1")
self.conv3_2 = self.conv_layer(self.conv3_1, "conv3_2")
self.conv3_3 = self.conv_layer(self.conv3_2, "conv3_3")
self.conv3_4 = self.conv_layer(self.conv3_3, "conv3_4")
self.pool3 = self.avg_pool(self.conv3_4, 'pool3')

self.conv4_1 = self.conv_layer(self.pool3, "conv4_1")
self.conv4_2 = self.conv_layer(self.conv4_1, "conv4_2")
self.conv4_3 = self.conv_layer(self.conv4_2, "conv4_3")
self.conv4_4 = self.conv_layer(self.conv4_3, "conv4_4")
self.pool4 = self.avg_pool(self.conv4_4, 'pool4')

self.conv5_1 = self.conv_layer(self.pool4, "conv5_1")
self.conv5_2 = self.conv_layer(self.conv5_1, "conv5_2")
self.conv5_3 = self.conv_layer(self.conv5_2, "conv5_3")
self.conv5_4 = self.conv_layer(self.conv5_3, "conv5_4")

self.data_dict = None

def avg_pool(self, bottom, name):
return tf.nn.avg_pool2d(input=bottom, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)

def max_pool(self, bottom, name):
return tf.nn.max_pool2d(input=bottom, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)

def conv_layer(self, bottom, name):
with tf.compat.v1.variable_scope(name):
filt = self.get_conv_filter(name)

conv = tf.nn.conv2d(input=bottom, filters=filt, strides=[1, 1, 1, 1], padding='SAME')

conv_biases = self.get_bias(name)
bias = tf.nn.bias_add(conv, conv_biases)

relu = tf.nn.relu(bias)
return relu

def fc_layer(self, bottom, name):
with tf.compat.v1.variable_scope(name):
shape = bottom.get_shape().as_list()
dim = 1
for d in shape[1:]:
dim *= d
x = tf.reshape(bottom, [-1, dim])

weights = self.get_fc_weight(name)
biases = self.get_bias(name)

# Fully connected layer. Note that the '+' operation automatically
# broadcasts the biases.
fc = tf.nn.bias_add(tf.matmul(x, weights), biases)

return fc

def get_conv_filter(self, name):
return tf.constant(self.data_dict[name][0], name="filter")

def get_bias(self, name):
return tf.constant(self.data_dict[name][1], name="biases")

def get_fc_weight(self, name):
return tf.constant(self.data_dict[name][0], name="weights")
26 changes: 26 additions & 0 deletions src/report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
TensorFlow 2.0 Upgrade Script
-----------------------------
Converted 1 files
Detected 0 issues that require attention
--------------------------------------------------------------------------------
================================================================================
Detailed log follows:

================================================================================
--------------------------------------------------------------------------------
Processing file 'custom_vgg19.py'
outputting to 'custom_vgg19V2.py'
--------------------------------------------------------------------------------

99:15: INFO: Added keywords to args of function 'tf.nn.avg_pool'
99:15: INFO: Renamed keyword argument for tf.nn.avg_pool from value to input
99:15: INFO: Renamed 'tf.nn.avg_pool' to 'tf.nn.avg_pool2d'
102:15: INFO: Added keywords to args of function 'tf.nn.max_pool'
102:15: INFO: Renamed keyword argument for tf.nn.max_pool from value to input
102:15: INFO: Renamed 'tf.nn.max_pool' to 'tf.nn.max_pool2d'
105:13: INFO: Renamed 'tf.variable_scope' to 'tf.compat.v1.variable_scope'
108:19: INFO: Added keywords to args of function 'tf.nn.conv2d'
108:19: INFO: Renamed keyword argument for tf.nn.conv2d from filter to filters
117:13: INFO: Renamed 'tf.variable_scope' to 'tf.compat.v1.variable_scope'
--------------------------------------------------------------------------------

7 changes: 5 additions & 2 deletions src/style_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import numpy as np
import os
import tensorflow as tf

# import tensorflow.compat.v1 as tf

import time
import utils
from functools import reduce
Expand Down Expand Up @@ -134,8 +137,8 @@ def parse_args():
STYLE_PATH = os.path.realpath(args.style)
OUT_PATH = os.path.realpath(args.out)


with tf.Session() as sess:
with tf.compat.v1.Session() as sess:
# with tf.Session() as sess:
parse_args()

# Initialize and process photo image to be used for our content
Expand Down
Loading