summaryrefslogtreecommitdiff
path: root/Codes/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'Codes/utils.py')
-rw-r--r--Codes/utils.py86
1 files changed, 10 insertions, 76 deletions
diff --git a/Codes/utils.py b/Codes/utils.py
index efeab8e..8466d08 100644
--- a/Codes/utils.py
+++ b/Codes/utils.py
@@ -10,6 +10,15 @@ rng = np.random.RandomState(2017)
def np_load_frame(filename, resize_height, resize_width):
+ """
+ Load image path and convert it to numpy.ndarray. Notes that the color channels are BGR and the color space
+ is normalized from [0, 255] to [-1, 1].
+
+ :param filename: the full path of image
+ :param resize_height: resized height
+ :param resize_width: resized width
+ :return: numpy.ndarray
+ """
image_decoded = cv2.imread(filename)
image_resized = cv2.resize(image_decoded, (resize_width, resize_height))
image_resized = image_resized.astype(dtype=np.float32)
@@ -20,7 +29,7 @@ def np_load_frame(filename, resize_height, resize_width):
class DataLoader(object):
def __init__(self, video_folder, resize_height=256, resize_width=256):
self.dir = video_folder
- self.videos = {}
+ self.videos = OrderedDict()
self._resize_height = resize_height
self._resize_width = resize_width
self.setup()
@@ -83,15 +92,6 @@ class DataLoader(object):
return np.concatenate(batch, axis=2)
- # def get_video_clips(self, video_name, start, end):
- # video_idx = np.arange(start, end)
- # video_clip = np.empty(shape=[self._resize_height, self._resize_height, 3*len(video_idx)], dtype=np.float32)
- # for idx, v_idx in enumerate(video_idx):
- # filename = self.videos[video_name]['frame'][v_idx]
- # video_clip[..., idx*3:(idx+1)*3] = np_load_frame(filename, self._resize_height, self._resize_width)
- #
- # return video_clip
-
def log10(t):
"""
@@ -130,46 +130,6 @@ def psnr_error(gen_frames, gt_frames):
return tf.reduce_mean(batch_errors)
-def sharp_diff_error(gen_frames, gt_frames, channels=3):
- """
- Computes the Sharpness Difference error between the generated images and the ground truth
- images.
-
- @param gen_frames: A tensor of shape [batch_size, height, width, 3]. The frames generated by the
- generator model.
- @param gt_frames: A tensor of shape [batch_size, height, width, 3]. The ground-truth frames for
- each frame in gen_frames.
- @param channels: The number of channels, 3 is RGB and 1 is Gray, default is 3.
-
- @return: A scalar tensor. The Sharpness Difference error over each frame in the batch.
- """
- shape = tf.shape(gen_frames)
- num_pixels = tf.to_float(shape[1] * shape[2] * shape[3])
-
- # gradient difference
- # create filters [-1, 1] and [[1],[-1]] for diffing to the left and down respectively.
- # TODO: Could this be simplified with one filter [[-1, 2], [0, -1]]?
- pos = tf.constant(np.identity(channels), dtype=tf.float32)
- neg = -1 * pos
- filter_x = tf.expand_dims(tf.stack([neg, pos]), 0) # [-1, 1]
- filter_y = tf.stack([tf.expand_dims(pos, 0), tf.expand_dims(neg, 0)]) # [[1],[-1]]
- strides = [1, 1, 1, 1] # stride of (1, 1)
- padding = 'SAME'
-
- gen_dx = tf.abs(tf.nn.conv2d(gen_frames, filter_x, strides, padding=padding))
- gen_dy = tf.abs(tf.nn.conv2d(gen_frames, filter_y, strides, padding=padding))
- gt_dx = tf.abs(tf.nn.conv2d(gt_frames, filter_x, strides, padding=padding))
- gt_dy = tf.abs(tf.nn.conv2d(gt_frames, filter_y, strides, padding=padding))
-
- gen_grad_sum = gen_dx + gen_dy
- gt_grad_sum = gt_dx + gt_dy
-
- grad_diff = tf.abs(gt_grad_sum - gen_grad_sum)
-
- batch_errors = 10 * log10(1 / ((1 / num_pixels) * tf.reduce_sum(grad_diff, [1, 2, 3])))
- return tf.reduce_mean(batch_errors)
-
-
def diff_mask(gen_frames, gt_frames, min_value=-1, max_value=1):
# normalize to [0, 1]
delta = max_value - min_value
@@ -197,31 +157,5 @@ def save(saver, sess, logdir, step):
print('The checkpoint has been created.')
-# if __name__ == '__main__':
-# os.environ['CUDA_DEVICES_ORDER'] = "PCI_BUS_ID"
-# os.environ['CUDA_VISIBLE_DEVICES'] = '0'
-#
-# data_loader = DataLoader('/home/liuwen/ssd/datasets/avenue/training/frames')
-# dataset, epoch_size = data_loader(10, 4, 1, 3, 1)
-#
-# # debug
-# iteration = dataset.make_one_shot_iterator()
-# batch_video_clip_tensor = iteration.get_next()
-#
-# config = tf.ConfigProto()
-# config.gpu_options.allow_growth = True
-# with tf.Session(config=config) as sess:
-# # batch_video_clip = sess.run(next(it))
-#
-# for i in range(100):
-# batch_video_clip = sess.run(batch_video_clip_tensor)
-# # print(batch_video_clip.shape)
-#
-# for vid, video_clip in enumerate(batch_video_clip):
-# for fid, frame in enumerate(video_clip):
-# print(i, vid, fid)
-# cv2.imshow('visualization', frame + 0.5)
-# cv2.waitKey(100)
-