From 89ebbf8d033d495af1f3ea5ddb113084f7007fca Mon Sep 17 00:00:00 2001 From: Gia Thuan Lam Date: Mon, 27 Apr 2020 02:23:16 +0200 Subject: [PATCH] make code compatible with new tools (tf2, imageio, skimage) --- evaluate.py | 14 +++++++------- src/transform.py | 11 +++++------ src/utils.py | 10 +++++----- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/evaluate.py b/evaluate.py index 6ddb0260..2abae652 100644 --- a/evaluate.py +++ b/evaluate.py @@ -26,12 +26,12 @@ def ffwd_video(path_in, path_out, checkpoint_dir, device_t='/gpu:0', batch_size= ffmpeg_params=None) g = tf.Graph() - soft_config = tf.ConfigProto(allow_soft_placement=True) + soft_config = tf.compat.v1.ConfigProto(allow_soft_placement=True) soft_config.gpu_options.allow_growth = True with g.as_default(), g.device(device_t), \ - tf.Session(config=soft_config) as sess: + tf.compat.v1.Session(config=soft_config) as sess: batch_shape = (batch_size, video_clip.size[1], video_clip.size[0], 3) - img_placeholder = tf.placeholder(tf.float32, shape=batch_shape, + img_placeholder = tf.compat.v1.placeholder(tf.float32, shape=batch_shape, name='img_placeholder') preds = transform.net(img_placeholder) @@ -82,16 +82,16 @@ def ffwd(data_in, paths_out, checkpoint_dir, device_t='/gpu:0', batch_size=4): g = tf.Graph() batch_size = min(len(paths_out), batch_size) curr_num = 0 - soft_config = tf.ConfigProto(allow_soft_placement=True) + soft_config = tf.compat.v1.ConfigProto(allow_soft_placement=True) soft_config.gpu_options.allow_growth = True with g.as_default(), g.device(device_t), \ - tf.Session(config=soft_config) as sess: + tf.compat.v1.Session(config=soft_config) as sess: batch_shape = (batch_size,) + img_shape - img_placeholder = tf.placeholder(tf.float32, shape=batch_shape, + img_placeholder = tf.compat.v1.placeholder(tf.float32, shape=batch_shape, name='img_placeholder') preds = transform.net(img_placeholder) - saver = tf.train.Saver() + saver = tf.compat.v1.train.Saver() if os.path.isdir(checkpoint_dir): ckpt = tf.train.get_checkpoint_state(checkpoint_dir) if ckpt and ckpt.model_checkpoint_path: diff --git a/src/transform.py b/src/transform.py index 4ba73991..7b3a2146 100644 --- a/src/transform.py +++ b/src/transform.py @@ -29,8 +29,7 @@ def _conv_layer(net, num_filters, filter_size, strides, relu=True): def _conv_tranpose_layer(net, num_filters, filter_size, strides): weights_init = _conv_init_vars(net, num_filters, filter_size, transpose=True) - - batch_size, rows, cols, in_channels = [i.value for i in net.get_shape()] + batch_size, rows, cols, in_channels = [i for i in net.get_shape()] new_rows, new_cols = int(rows * strides), int(cols * strides) # new_shape = #tf.pack([tf.shape(net)[0], new_rows, new_cols, num_filters]) @@ -47,9 +46,9 @@ def _residual_block(net, filter_size=3): return net + _conv_layer(tmp, 128, filter_size, 1, relu=False) def _instance_norm(net, train=True): - batch, rows, cols, channels = [i.value for i in net.get_shape()] + batch, rows, cols, channels = [i for i in net.get_shape()] var_shape = [channels] - mu, sigma_sq = tf.nn.moments(net, [1,2], keep_dims=True) + mu, sigma_sq = tf.nn.moments(net, [1,2], keepdims=True) shift = tf.Variable(tf.zeros(var_shape)) scale = tf.Variable(tf.ones(var_shape)) epsilon = 1e-3 @@ -57,11 +56,11 @@ def _instance_norm(net, train=True): return scale * normalized + shift def _conv_init_vars(net, out_channels, filter_size, transpose=False): - _, rows, cols, in_channels = [i.value for i in net.get_shape()] + _, rows, cols, in_channels = [i for i in net.get_shape()] if not transpose: weights_shape = [filter_size, filter_size, in_channels, out_channels] else: weights_shape = [filter_size, filter_size, out_channels, in_channels] - weights_init = tf.Variable(tf.truncated_normal(weights_shape, stddev=WEIGHTS_INIT_STDEV, seed=1), dtype=tf.float32) + weights_init = tf.Variable(tf.compat.v1.truncated_normal(weights_shape, stddev=WEIGHTS_INIT_STDEV, seed=1), dtype=tf.float32) return weights_init diff --git a/src/utils.py b/src/utils.py index 36080f2f..82d3a49b 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,23 +1,23 @@ -import scipy.misc, numpy as np, os, sys +import imageio, skimage.transform, numpy as np, os, sys def save_img(out_path, img): img = np.clip(img, 0, 255).astype(np.uint8) - scipy.misc.imsave(out_path, img) + imageio.imsave(out_path, img) def scale_img(style_path, style_scale): scale = float(style_scale) - o0, o1, o2 = scipy.misc.imread(style_path, mode='RGB').shape + o0, o1, o2 = imageio.imread(style_path, pilmode='RGB').shape scale = float(style_scale) new_shape = (int(o0 * scale), int(o1 * scale), o2) style_target = _get_img(style_path, img_size=new_shape) return style_target def get_img(src, img_size=False): - img = scipy.misc.imread(src, mode='RGB') # misc.imresize(, (256, 256, 3)) + img = imageio.imread(src, pilmode='RGB') # misc.imresize(, (256, 256, 3)) if not (len(img.shape) == 3 and img.shape[2] == 3): img = np.dstack((img,img,img)) if img_size != False: - img = scipy.misc.imresize(img, img_size) + img = skimage.transform.imresize(img, img_size) return img def exists(p, msg):