Skip to content

Commit

Permalink
Support for Keras 1.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
titu1994 committed Nov 28, 2016
1 parent 60537dd commit fcf2417
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 121 deletions.
61 changes: 31 additions & 30 deletions INetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import argparse
import warnings

from keras.models import Sequential
from keras.models import Model
from keras.layers import Input
from keras.layers.convolutional import Convolution2D, AveragePooling2D, MaxPooling2D
from keras import backend as K
from keras.utils.data_utils import get_file
Expand Down Expand Up @@ -249,11 +250,11 @@ def load_mask(mask_path, shape, return_mask_img=False):
pooltype = 1 if pooltype == "ave" else 0


def pooling_func():
def pooling_func(x):
if pooltype == 1:
return AveragePooling2D((2, 2), strides=(2, 2))
return AveragePooling2D((2, 2), strides=(2, 2))(x)
else:
return MaxPooling2D((2, 2), strides=(2, 2))
return MaxPooling2D((2, 2), strides=(2, 2))(x)


read_mode = "gray" if args.init_image == "gray" else "color"
Expand Down Expand Up @@ -286,39 +287,39 @@ def pooling_func():
else:
shape = (nb_tensors, img_width, img_height, 3)

# build the VGG16 network with our 3 images as input
first_layer = Convolution2D(64, 3, 3, activation='relu', name='conv1_1', border_mode='same')
first_layer.set_input(input_tensor, shape)
ip = Input(tensor=input_tensor, shape=shape)

model = Sequential()
model.add(first_layer)
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2', border_mode='same'))
model.add(pooling_func())
# build the VGG16 network with our 3 images as input
x = Convolution2D(64, 3, 3, activation='relu', name='conv1_1', border_mode='same')(ip)
x = Convolution2D(64, 3, 3, activation='relu', name='conv1_2', border_mode='same')(x)
x = pooling_func(x)

model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1', border_mode='same'))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2', border_mode='same'))
model.add(pooling_func())
x = Convolution2D(128, 3, 3, activation='relu', name='conv2_1', border_mode='same')(x)
x = Convolution2D(128, 3, 3, activation='relu', name='conv2_2', border_mode='same')(x)
x = pooling_func(x)

model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1', border_mode='same'))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2', border_mode='same'))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3', border_mode='same'))
x = Convolution2D(256, 3, 3, activation='relu', name='conv3_1', border_mode='same')(x)
x = Convolution2D(256, 3, 3, activation='relu', name='conv3_2', border_mode='same')(x)
x = Convolution2D(256, 3, 3, activation='relu', name='conv3_3', border_mode='same')(x)
if args.model == "vgg19":
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_4', border_mode='same'))
model.add(pooling_func())
x = Convolution2D(256, 3, 3, activation='relu', name='conv3_4', border_mode='same')(x)
x = pooling_func(x)

model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1', border_mode='same'))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2', border_mode='same'))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3', border_mode='same'))
x = Convolution2D(512, 3, 3, activation='relu', name='conv4_1', border_mode='same')(x)
x = Convolution2D(512, 3, 3, activation='relu', name='conv4_2', border_mode='same')(x)
x = Convolution2D(512, 3, 3, activation='relu', name='conv4_3', border_mode='same')(x)
if args.model == "vgg19":
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_4', border_mode='same'))
model.add(pooling_func())
x = Convolution2D(512, 3, 3, activation='relu', name='conv4_4', border_mode='same')(x)
x = pooling_func(x)

model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1', border_mode='same'))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_2', border_mode='same'))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_3', border_mode='same'))
x = Convolution2D(512, 3, 3, activation='relu', name='conv5_1', border_mode='same')(x)
x = Convolution2D(512, 3, 3, activation='relu', name='conv5_2', border_mode='same')(x)
x = Convolution2D(512, 3, 3, activation='relu', name='conv5_3', border_mode='same')(x)
if args.model == "vgg19":
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_4', border_mode='same'))
model.add(pooling_func())
x = Convolution2D(512, 3, 3, activation='relu', name='conv5_4', border_mode='same')(x)
x = pooling_func(x)

model = Model(ip, x)

if K.image_dim_ordering() == "th":
if args.model == "vgg19":
Expand Down Expand Up @@ -562,7 +563,7 @@ def grads(self, x):
improvement_threshold = float(args.min_improvement)

for i in range(num_iter):
print("Start of iteration", (i + 1))
print("Starting iteration %d of %d" % ((i + 1), num_iter))
start_time = time.time()

x, min_val, info = fmin_l_bfgs_b(evaluator.loss, x.flatten(), fprime=evaluator.grads, maxfun=20)
Expand Down
61 changes: 31 additions & 30 deletions Network.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import argparse
import warnings

from keras.models import Sequential
from keras.models import Model
from keras.layers import Input
from keras.layers.convolutional import Convolution2D, AveragePooling2D, MaxPooling2D
from keras import backend as K
from keras.utils.data_utils import get_file
Expand Down Expand Up @@ -247,11 +248,11 @@ def load_mask(mask_path, shape, return_mask_img=False):
pooltype = 1 if pooltype == "ave" else 0


def pooling_func():
def pooling_func(x):
if pooltype == 1:
return AveragePooling2D((2, 2), strides=(2, 2))
return AveragePooling2D((2, 2), strides=(2, 2))(x)
else:
return MaxPooling2D((2, 2), strides=(2, 2))
return MaxPooling2D((2, 2), strides=(2, 2))(x)


read_mode = "gray" if args.init_image == "gray" else "color"
Expand Down Expand Up @@ -284,39 +285,39 @@ def pooling_func():
else:
shape = (nb_tensors, img_width, img_height, 3)

# build the VGG16 network with our 3 images as input
first_layer = Convolution2D(64, 3, 3, activation='relu', name='conv1_1', border_mode='same')
first_layer.set_input(input_tensor, shape)
ip = Input(tensor=input_tensor, shape=shape)

model = Sequential()
model.add(first_layer)
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2', border_mode='same'))
model.add(pooling_func())
# build the VGG16 network with our 3 images as input
x = Convolution2D(64, 3, 3, activation='relu', name='conv1_1', border_mode='same')(ip)
x = Convolution2D(64, 3, 3, activation='relu', name='conv1_2', border_mode='same')(x)
x = pooling_func(x)

model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1', border_mode='same'))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2', border_mode='same'))
model.add(pooling_func())
x = Convolution2D(128, 3, 3, activation='relu', name='conv2_1', border_mode='same')(x)
x = Convolution2D(128, 3, 3, activation='relu', name='conv2_2', border_mode='same')(x)
x = pooling_func(x)

model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1', border_mode='same'))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2', border_mode='same'))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3', border_mode='same'))
x = Convolution2D(256, 3, 3, activation='relu', name='conv3_1', border_mode='same')(x)
x = Convolution2D(256, 3, 3, activation='relu', name='conv3_2', border_mode='same')(x)
x = Convolution2D(256, 3, 3, activation='relu', name='conv3_3', border_mode='same')(x)
if args.model == "vgg19":
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_4', border_mode='same'))
model.add(pooling_func())
x = Convolution2D(256, 3, 3, activation='relu', name='conv3_4', border_mode='same')(x)
x = pooling_func(x)

model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1', border_mode='same'))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2', border_mode='same'))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3', border_mode='same'))
x = Convolution2D(512, 3, 3, activation='relu', name='conv4_1', border_mode='same')(x)
x = Convolution2D(512, 3, 3, activation='relu', name='conv4_2', border_mode='same')(x)
x = Convolution2D(512, 3, 3, activation='relu', name='conv4_3', border_mode='same')(x)
if args.model == "vgg19":
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_4', border_mode='same'))
model.add(pooling_func())
x = Convolution2D(512, 3, 3, activation='relu', name='conv4_4', border_mode='same')(x)
x = pooling_func(x)

model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1', border_mode='same'))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_2', border_mode='same'))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_3', border_mode='same'))
x = Convolution2D(512, 3, 3, activation='relu', name='conv5_1', border_mode='same')(x)
x = Convolution2D(512, 3, 3, activation='relu', name='conv5_2', border_mode='same')(x)
x = Convolution2D(512, 3, 3, activation='relu', name='conv5_3', border_mode='same')(x)
if args.model == "vgg19":
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_4', border_mode='same'))
model.add(pooling_func())
x = Convolution2D(512, 3, 3, activation='relu', name='conv5_4', border_mode='same')(x)
x = pooling_func(x)

model = Model(ip, x)

if K.image_dim_ordering() == "th":
if args.model == "vgg19":
Expand Down Expand Up @@ -538,7 +539,7 @@ def grads(self, x):
improvement_threshold = float(args.min_improvement)

for i in range(num_iter):
print('Start of iteration', (i + 1))
print("Starting iteration %d of %d" % ((i + 1), num_iter))
start_time = time.time()

x, min_val, info = fmin_l_bfgs_b(evaluator.loss, x.flatten(), fprime=evaluator.grads, maxfun=20)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Neural Style Transfer & Neural Doodles
Implementation of Neural Style Transfer from the paper <a href="http://arxiv.org/abs/1508.06576">A Neural Algorithm of Artistic Style</a> in Keras 1.1.1
Implementation of Neural Style Transfer from the paper <a href="http://arxiv.org/abs/1508.06576">A Neural Algorithm of Artistic Style</a> in Keras 1.1.2

INetwork implements and focuses on certain improvements suggested in <a href="http://arxiv.org/abs/1605.04603">Improving the Neural Algorithm of Artistic Style</a>.

Expand Down
61 changes: 31 additions & 30 deletions script_helper/Script/INetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import argparse
import warnings

from keras.models import Sequential
from keras.models import Model
from keras.layers import Input
from keras.layers.convolutional import Convolution2D, AveragePooling2D, MaxPooling2D
from keras import backend as K
from keras.utils.data_utils import get_file
Expand Down Expand Up @@ -249,11 +250,11 @@ def load_mask(mask_path, shape, return_mask_img=False):
pooltype = 1 if pooltype == "ave" else 0


def pooling_func():
def pooling_func(x):
if pooltype == 1:
return AveragePooling2D((2, 2), strides=(2, 2))
return AveragePooling2D((2, 2), strides=(2, 2))(x)
else:
return MaxPooling2D((2, 2), strides=(2, 2))
return MaxPooling2D((2, 2), strides=(2, 2))(x)


read_mode = "gray" if args.init_image == "gray" else "color"
Expand Down Expand Up @@ -286,39 +287,39 @@ def pooling_func():
else:
shape = (nb_tensors, img_width, img_height, 3)

# build the VGG16 network with our 3 images as input
first_layer = Convolution2D(64, 3, 3, activation='relu', name='conv1_1', border_mode='same')
first_layer.set_input(input_tensor, shape)
ip = Input(tensor=input_tensor, shape=shape)

model = Sequential()
model.add(first_layer)
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2', border_mode='same'))
model.add(pooling_func())
# build the VGG16 network with our 3 images as input
x = Convolution2D(64, 3, 3, activation='relu', name='conv1_1', border_mode='same')(ip)
x = Convolution2D(64, 3, 3, activation='relu', name='conv1_2', border_mode='same')(x)
x = pooling_func(x)

model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1', border_mode='same'))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2', border_mode='same'))
model.add(pooling_func())
x = Convolution2D(128, 3, 3, activation='relu', name='conv2_1', border_mode='same')(x)
x = Convolution2D(128, 3, 3, activation='relu', name='conv2_2', border_mode='same')(x)
x = pooling_func(x)

model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1', border_mode='same'))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2', border_mode='same'))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3', border_mode='same'))
x = Convolution2D(256, 3, 3, activation='relu', name='conv3_1', border_mode='same')(x)
x = Convolution2D(256, 3, 3, activation='relu', name='conv3_2', border_mode='same')(x)
x = Convolution2D(256, 3, 3, activation='relu', name='conv3_3', border_mode='same')(x)
if args.model == "vgg19":
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_4', border_mode='same'))
model.add(pooling_func())
x = Convolution2D(256, 3, 3, activation='relu', name='conv3_4', border_mode='same')(x)
x = pooling_func(x)

model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1', border_mode='same'))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2', border_mode='same'))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3', border_mode='same'))
x = Convolution2D(512, 3, 3, activation='relu', name='conv4_1', border_mode='same')(x)
x = Convolution2D(512, 3, 3, activation='relu', name='conv4_2', border_mode='same')(x)
x = Convolution2D(512, 3, 3, activation='relu', name='conv4_3', border_mode='same')(x)
if args.model == "vgg19":
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_4', border_mode='same'))
model.add(pooling_func())
x = Convolution2D(512, 3, 3, activation='relu', name='conv4_4', border_mode='same')(x)
x = pooling_func(x)

model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1', border_mode='same'))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_2', border_mode='same'))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_3', border_mode='same'))
x = Convolution2D(512, 3, 3, activation='relu', name='conv5_1', border_mode='same')(x)
x = Convolution2D(512, 3, 3, activation='relu', name='conv5_2', border_mode='same')(x)
x = Convolution2D(512, 3, 3, activation='relu', name='conv5_3', border_mode='same')(x)
if args.model == "vgg19":
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_4', border_mode='same'))
model.add(pooling_func())
x = Convolution2D(512, 3, 3, activation='relu', name='conv5_4', border_mode='same')(x)
x = pooling_func(x)

model = Model(ip, x)

if K.image_dim_ordering() == "th":
if args.model == "vgg19":
Expand Down Expand Up @@ -562,7 +563,7 @@ def grads(self, x):
improvement_threshold = float(args.min_improvement)

for i in range(num_iter):
print("Start of iteration", (i + 1))
print("Starting iteration %d of %d" % ((i + 1), num_iter))
start_time = time.time()

x, min_val, info = fmin_l_bfgs_b(evaluator.loss, x.flatten(), fprime=evaluator.grads, maxfun=20)
Expand Down
Loading

0 comments on commit fcf2417

Please sign in to comment.