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
15 changes: 7 additions & 8 deletions test_code/cartoonize.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import guided_filter
from tqdm import tqdm


tf.compat.v1.disable_eager_execution()

def resize_crop(image):
h, w, c = np.shape(image)
Expand All @@ -23,19 +23,19 @@ def resize_crop(image):


def cartoonize(load_folder, save_folder, model_path):
input_photo = tf.placeholder(tf.float32, [1, None, None, 3])
input_photo = tf.compat.v1.placeholder(tf.float32, [1, None, None, 3])
network_out = network.unet_generator(input_photo)
final_out = guided_filter.guided_filter(input_photo, network_out, r=1, eps=5e-3)

all_vars = tf.trainable_variables()
all_vars = tf.compat.v1.trainable_variables()
gene_vars = [var for var in all_vars if 'generator' in var.name]
saver = tf.train.Saver(var_list=gene_vars)
saver = tf.compat.v1.train.Saver(var_list=gene_vars)

config = tf.ConfigProto()
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
sess = tf.compat.v1.Session(config=config)

sess.run(tf.global_variables_initializer())
sess.run(tf.compat.v1.global_variables_initializer())
saver.restore(sess, tf.train.latest_checkpoint(model_path))
name_list = os.listdir(load_folder)
for name in tqdm(name_list):
Expand Down Expand Up @@ -65,4 +65,3 @@ def cartoonize(load_folder, save_folder, model_path):
cartoonize(load_folder, save_folder, model_path)



20 changes: 10 additions & 10 deletions test_code/guided_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ def tf_box_filter(x, r):
weight = 1/(k_size**2)
box_kernel = weight*np.ones((k_size, k_size, ch, 1))
box_kernel = np.array(box_kernel).astype(np.float32)
output = tf.nn.depthwise_conv2d(x, box_kernel, [1, 1, 1, 1], 'SAME')
output = tf.nn.depthwise_conv2d(input=x, filter=box_kernel, strides=[1, 1, 1, 1], padding='SAME')
return output



def guided_filter(x, y, r, eps=1e-2):

x_shape = tf.shape(x)
x_shape = tf.shape(input=x)
#y_shape = tf.shape(y)

N = tf_box_filter(tf.ones((1, x_shape[1], x_shape[2], 1), dtype=x.dtype), r)
Expand All @@ -43,9 +43,9 @@ def fast_guided_filter(lr_x, lr_y, hr_x, r=1, eps=1e-8):

#assert lr_x.shape.ndims == 4 and lr_y.shape.ndims == 4 and hr_x.shape.ndims == 4

lr_x_shape = tf.shape(lr_x)
lr_x_shape = tf.shape(input=lr_x)
#lr_y_shape = tf.shape(lr_y)
hr_x_shape = tf.shape(hr_x)
hr_x_shape = tf.shape(input=hr_x)

N = tf_box_filter(tf.ones((1, lr_x_shape[1], lr_x_shape[2], 1), dtype=lr_x.dtype), r)

Expand All @@ -57,8 +57,8 @@ def fast_guided_filter(lr_x, lr_y, hr_x, r=1, eps=1e-8):
A = cov_xy / (var_x + eps)
b = mean_y - A * mean_x

mean_A = tf.image.resize_images(A, hr_x_shape[1: 3])
mean_b = tf.image.resize_images(b, hr_x_shape[1: 3])
mean_A = tf.image.resize(A, hr_x_shape[1: 3])
mean_b = tf.image.resize(b, hr_x_shape[1: 3])

output = mean_A * hr_x + mean_b

Expand All @@ -69,17 +69,17 @@ def fast_guided_filter(lr_x, lr_y, hr_x, r=1, eps=1e-8):
import cv2
from tqdm import tqdm

input_photo = tf.placeholder(tf.float32, [1, None, None, 3])
input_photo = tf.compat.v1.placeholder(tf.float32, [1, None, None, 3])
#input_superpixel = tf.placeholder(tf.float32, [16, 256, 256, 3])
output = guided_filter(input_photo, input_photo, 5, eps=1)
image = cv2.imread('output_figure1/cartoon2.jpg')
image = image/127.5 - 1
image = np.expand_dims(image, axis=0)

config = tf.ConfigProto()
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
sess.run(tf.global_variables_initializer())
sess = tf.compat.v1.Session(config=config)
sess.run(tf.compat.v1.global_variables_initializer())

out = sess.run(output, feed_dict={input_photo: image})
out = (np.squeeze(out)+1)*127.5
Expand Down
14 changes: 7 additions & 7 deletions test_code/network.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import tensorflow as tf
import numpy as np
import tensorflow.contrib.slim as slim
import tf_slim as slim



def resblock(inputs, out_channel=32, name='resblock'):

with tf.variable_scope(name):
with tf.compat.v1.variable_scope(name):

x = slim.convolution2d(inputs, out_channel, [3, 3],
activation_fn=None, scope='conv1')
Expand All @@ -20,7 +20,7 @@ def resblock(inputs, out_channel=32, name='resblock'):


def unet_generator(inputs, channel=32, num_blocks=4, name='generator', reuse=False):
with tf.variable_scope(name, reuse=reuse):
with tf.compat.v1.variable_scope(name, reuse=reuse):

x0 = slim.convolution2d(inputs, channel, [7, 7], activation_fn=None)
x0 = tf.nn.leaky_relu(x0)
Expand All @@ -41,15 +41,15 @@ def unet_generator(inputs, channel=32, num_blocks=4, name='generator', reuse=Fal
x2 = slim.convolution2d(x2, channel*2, [3, 3], activation_fn=None)
x2 = tf.nn.leaky_relu(x2)

h1, w1 = tf.shape(x2)[1], tf.shape(x2)[2]
x3 = tf.image.resize_bilinear(x2, (h1*2, w1*2))
h1, w1 = tf.shape(input=x2)[1], tf.shape(input=x2)[2]
x3 = tf.image.resize(x2, (h1*2, w1*2), method=tf.image.ResizeMethod.BILINEAR)
x3 = slim.convolution2d(x3+x1, channel*2, [3, 3], activation_fn=None)
x3 = tf.nn.leaky_relu(x3)
x3 = slim.convolution2d(x3, channel, [3, 3], activation_fn=None)
x3 = tf.nn.leaky_relu(x3)

h2, w2 = tf.shape(x3)[1], tf.shape(x3)[2]
x4 = tf.image.resize_bilinear(x3, (h2*2, w2*2))
h2, w2 = tf.shape(input=x3)[1], tf.shape(input=x3)[2]
x4 = tf.image.resize(x3, (h2*2, w2*2), method=tf.image.ResizeMethod.BILINEAR)
x4 = slim.convolution2d(x4+x0, channel, [3, 3], activation_fn=None)
x4 = tf.nn.leaky_relu(x4)
x4 = slim.convolution2d(x4, 3, [7, 7], activation_fn=None)
Expand Down
Empty file modified test_code/saved_models/checkpoint
100644 → 100755
Empty file.
Empty file modified test_code/saved_models/model-33999.data-00000-of-00001
100644 → 100755
Empty file.
Empty file modified test_code/saved_models/model-33999.index
100644 → 100755
Empty file.
Empty file modified test_code/test_images/actress2.jpg
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified test_code/test_images/china6.jpg
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified test_code/test_images/food16.jpg
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified test_code/test_images/food6.jpg
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified test_code/test_images/liuyifei4.jpg
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified test_code/test_images/london1.jpg
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified test_code/test_images/mountain4.jpg
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified test_code/test_images/mountain5.jpg
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified test_code/test_images/national_park1.jpg
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified test_code/test_images/party5.jpg
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified test_code/test_images/party7.jpg
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions train_code/guided_filter.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ def tf_box_filter(x, r):
weight = 1/((2*r+1)**2)
box_kernel = weight*np.ones((2*r+1, 2*r+1, ch, 1))
box_kernel = np.array(box_kernel).astype(np.float32)
output = tf.nn.depthwise_conv2d(x, box_kernel, [1, 1, 1, 1], 'SAME')
output = tf.nn.depthwise_conv2d(input=x, filter=box_kernel, strides=[1, 1, 1, 1], padding='SAME')
return output



def guided_filter(x, y, r, eps=1e-2):

x_shape = tf.shape(x)
x_shape = tf.shape(input=x)
#y_shape = tf.shape(y)

N = tf_box_filter(tf.ones((1, x_shape[1], x_shape[2], 1), dtype=x.dtype), r)
Expand Down
185 changes: 92 additions & 93 deletions train_code/layers.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,93 +1,92 @@
'''
CVPR 2020 submission, Paper ID 6791
Source code for 'Learning to Cartoonize Using White-Box Cartoon Representations'
'''


import tensorflow as tf
import numpy as np
import tensorflow.contrib.slim as slim



def adaptive_instance_norm(content, style, epsilon=1e-5):

c_mean, c_var = tf.nn.moments(content, axes=[1, 2], keep_dims=True)
s_mean, s_var = tf.nn.moments(style, axes=[1, 2], keep_dims=True)
c_std, s_std = tf.sqrt(c_var + epsilon), tf.sqrt(s_var + epsilon)

return s_std * (content - c_mean) / c_std + s_mean



def spectral_norm(w, iteration=1):
w_shape = w.shape.as_list()
w = tf.reshape(w, [-1, w_shape[-1]])

u = tf.get_variable("u", [1, w_shape[-1]],
initializer=tf.random_normal_initializer(), trainable=False)

u_hat = u
v_hat = None
for i in range(iteration):
"""
power iteration
Usually iteration = 1 will be enough
"""
v_ = tf.matmul(u_hat, tf.transpose(w))
v_hat = tf.nn.l2_normalize(v_)

u_ = tf.matmul(v_hat, w)
u_hat = tf.nn.l2_normalize(u_)

u_hat = tf.stop_gradient(u_hat)
v_hat = tf.stop_gradient(v_hat)

sigma = tf.matmul(tf.matmul(v_hat, w), tf.transpose(u_hat))

with tf.control_dependencies([u.assign(u_hat)]):
w_norm = w / sigma
w_norm = tf.reshape(w_norm, w_shape)

return w_norm


def conv_spectral_norm(x, channel, k_size, stride=1, name='conv_snorm'):
with tf.variable_scope(name):
w = tf.get_variable("kernel", shape=[k_size[0], k_size[1], x.get_shape()[-1], channel])
b = tf.get_variable("bias", [channel], initializer=tf.constant_initializer(0.0))

x = tf.nn.conv2d(input=x, filter=spectral_norm(w), strides=[1, stride, stride, 1], padding='SAME') + b

return x



def self_attention(inputs, name='attention', reuse=False):
with tf.variable_scope(name, reuse=reuse):
h, w = tf.shape(inputs)[1], tf.shape(inputs)[2]
bs, _, _, ch = inputs.get_shape().as_list()
f = slim.convolution2d(inputs, ch//8, [1, 1], activation_fn=None)
g = slim.convolution2d(inputs, ch//8, [1, 1], activation_fn=None)
s = slim.convolution2d(inputs, 1, [1, 1], activation_fn=None)
f_flatten = tf.reshape(f, shape=[f.shape[0], -1, f.shape[-1]])
g_flatten = tf.reshape(g, shape=[g.shape[0], -1, g.shape[-1]])
beta = tf.matmul(f_flatten, g_flatten, transpose_b=True)
beta = tf.nn.softmax(beta)

s_flatten = tf.reshape(s, shape=[s.shape[0], -1, s.shape[-1]])
att_map = tf.matmul(beta, s_flatten)
att_map = tf.reshape(att_map, shape=[bs, h, w, 1])
gamma = tf.get_variable("gamma", [1], initializer=tf.constant_initializer(0.0))
output = att_map * gamma + inputs

return att_map, output



if __name__ == '__main__':
pass




'''
CVPR 2020 submission, Paper ID 6791
Source code for 'Learning to Cartoonize Using White-Box Cartoon Representations'
'''


import tensorflow as tf
import numpy as np
import tf_slim as slim



def adaptive_instance_norm(content, style, epsilon=1e-5):

c_mean, c_var = tf.nn.moments(x=content, axes=[1, 2], keepdims=True)
s_mean, s_var = tf.nn.moments(x=style, axes=[1, 2], keepdims=True)
c_std, s_std = tf.sqrt(c_var + epsilon), tf.sqrt(s_var + epsilon)

return s_std * (content - c_mean) / c_std + s_mean



def spectral_norm(w, iteration=1):
w_shape = w.shape.as_list()
w = tf.reshape(w, [-1, w_shape[-1]])

u = tf.compat.v1.get_variable("u", [1, w_shape[-1]],
initializer=tf.compat.v1.random_normal_initializer(), trainable=False)

u_hat = u
v_hat = None
for i in range(iteration):
"""
power iteration
Usually iteration = 1 will be enough
"""
v_ = tf.matmul(u_hat, tf.transpose(a=w))
v_hat = tf.nn.l2_normalize(v_)

u_ = tf.matmul(v_hat, w)
u_hat = tf.nn.l2_normalize(u_)

u_hat = tf.stop_gradient(u_hat)
v_hat = tf.stop_gradient(v_hat)

sigma = tf.matmul(tf.matmul(v_hat, w), tf.transpose(a=u_hat))

with tf.control_dependencies([u.assign(u_hat)]):
w_norm = w / sigma
w_norm = tf.reshape(w_norm, w_shape)

return w_norm


def conv_spectral_norm(x, channel, k_size, stride=1, name='conv_snorm'):
with tf.compat.v1.variable_scope(name):
w = tf.compat.v1.get_variable("kernel", shape=[k_size[0], k_size[1], x.get_shape()[-1], channel])
b = tf.compat.v1.get_variable("bias", [channel], initializer=tf.compat.v1.constant_initializer(0.0))

x = tf.nn.conv2d(input=x, filters=spectral_norm(w), strides=[1, stride, stride, 1], padding='SAME') + b

return x



def self_attention(inputs, name='attention', reuse=False):
with tf.compat.v1.variable_scope(name, reuse=reuse):
h, w = tf.shape(input=inputs)[1], tf.shape(input=inputs)[2]
bs, _, _, ch = inputs.get_shape().as_list()
f = slim.convolution2d(inputs, ch//8, [1, 1], activation_fn=None)
g = slim.convolution2d(inputs, ch//8, [1, 1], activation_fn=None)
s = slim.convolution2d(inputs, 1, [1, 1], activation_fn=None)
f_flatten = tf.reshape(f, shape=[f.shape[0], -1, f.shape[-1]])
g_flatten = tf.reshape(g, shape=[g.shape[0], -1, g.shape[-1]])
beta = tf.matmul(f_flatten, g_flatten, transpose_b=True)
beta = tf.nn.softmax(beta)

s_flatten = tf.reshape(s, shape=[s.shape[0], -1, s.shape[-1]])
att_map = tf.matmul(beta, s_flatten)
att_map = tf.reshape(att_map, shape=[bs, h, w, 1])
gamma = tf.compat.v1.get_variable("gamma", [1], initializer=tf.compat.v1.constant_initializer(0.0))
output = att_map * gamma + inputs

return att_map, output



if __name__ == '__main__':
pass



Loading