Skip to content

Commit

Permalink
Merge pull request #21 from LielinJiang/refine-code
Browse files Browse the repository at this point in the history
Refine application code
  • Loading branch information
LielinJiang committed Sep 10, 2020
2 parents 3eeaefa + bb71d1a commit 6ea2876
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 30 deletions.
8 changes: 2 additions & 6 deletions applications/DAIN/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import glob
import numpy as np
from imageio import imread, imsave
from tqdm import tqdm
import cv2

import paddle.fluid as fluid
Expand Down Expand Up @@ -175,8 +176,7 @@ def run(self):
if not os.path.exists(os.path.join(frame_path_combined, vidname)):
os.makedirs(os.path.join(frame_path_combined, vidname))

for i in range(frame_num - 1):
print(frames[i])
for i in tqdm(range(frame_num - 1)):
first = frames[i]
second = frames[i + 1]

Expand Down Expand Up @@ -208,12 +208,10 @@ def run(self):
assert (X0.shape[1] == X1.shape[1])
assert (X0.shape[2] == X1.shape[2])

print("size before padding ", X0.shape)
X0 = np.pad(X0, ((0,0), (padding_top, padding_bottom), \
(padding_left, padding_right)), mode='edge')
X1 = np.pad(X1, ((0,0), (padding_top, padding_bottom), \
(padding_left, padding_right)), mode='edge')
print("size after padding ", X0.shape)

X0 = np.expand_dims(X0, axis=0)
X1 = np.expand_dims(X1, axis=0)
Expand All @@ -233,8 +231,6 @@ def run(self):
proc_timer.update(time.time() - proc_end)
tot_timer.update(time.time() - end)
end = time.time()
print("*********** current image process time \t " +
str(time.time() - proc_end) + "s *********")

y_ = [
np.transpose(
Expand Down
7 changes: 4 additions & 3 deletions applications/DAIN/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def update(self, val, n=1):


def dump_frames_ffmpeg(vid_path, outpath, r=None, ss=None, t=None):
ffmpeg = ['ffmpeg ', ' -loglevel ', ' error ']
ffmpeg = ['ffmpeg ', ' -y -loglevel ', ' error ']
vid_name = vid_path.split('/')[-1].split('.')[0]
out_full_path = os.path.join(outpath, vid_name)

Expand Down Expand Up @@ -66,7 +66,7 @@ def dump_frames_ffmpeg(vid_path, outpath, r=None, ss=None, t=None):


def frames_to_video_ffmpeg(framepath, videopath, r):
ffmpeg = ['ffmpeg ', ' -loglevel ', ' error ']
ffmpeg = ['ffmpeg ', ' -y -loglevel ', ' error ']
cmd = ffmpeg + [
' -r ', r, ' -f ', ' image2 ', ' -i ', framepath, ' -vcodec ',
' libx264 ', ' -pix_fmt ', ' yuv420p ', ' -crf ', ' 16 ', videopath
Expand Down Expand Up @@ -99,7 +99,8 @@ def combine_frames(input, interpolated, combined, num_frames):
for k in range(num_frames):
src = frames2[i * num_frames + k]
dst = os.path.join(
combined, '{:08d}.png'.format(i * (num_frames + 1) + k + 1))
combined,
'{:08d}.png'.format(i * (num_frames + 1) + k + 1))
shutil.copy2(src, dst)
except Exception as e:
print(e)
Expand Down
2 changes: 0 additions & 2 deletions applications/DeOldify/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,8 @@ def custom_conv_layer(ni: int,
stride=stride,
padding=padding)
if norm_type == 'Weight':
print('use weight norm')
conv = nn.utils.weight_norm(conv)
elif norm_type == 'Spectral':
# pass
conv = Spectralnorm(conv)
layers = [conv]
if use_activ:
Expand Down
6 changes: 3 additions & 3 deletions applications/DeOldify/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@


def frames_to_video_ffmpeg(framepath, videopath, r):
ffmpeg = ['ffmpeg ', ' -loglevel ', ' error ']
ffmpeg = ['ffmpeg ', ' -y -loglevel ', ' error ']
cmd = ffmpeg + [
' -r ', r, ' -f ', ' image2 ', ' -i ', framepath, ' -vcodec ',
' libx264 ', ' -pix_fmt ', ' yuv420p ', ' -crf ', ' 16 ', videopath
Expand Down Expand Up @@ -139,7 +139,7 @@ def run(self):


def dump_frames_ffmpeg(vid_path, outpath, r=None, ss=None, t=None):
ffmpeg = ['ffmpeg ', ' -loglevel ', ' error ']
ffmpeg = ['ffmpeg ', ' -y -loglevel ', ' error ']
vid_name = vid_path.split('/')[-1].split('.')[0]
out_full_path = os.path.join(outpath, 'frames_input')

Expand Down Expand Up @@ -169,7 +169,7 @@ def dump_frames_ffmpeg(vid_path, outpath, r=None, ss=None, t=None):


if __name__ == '__main__':
paddle.enable_imperative()
paddle.disable_static()
args = parser.parse_args()

predictor = DeOldifyPredictor(args.input,
Expand Down
17 changes: 8 additions & 9 deletions applications/EDVR/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

import numpy as np


def read_img(path, size=None, is_gt=False):
"""read image by cv2
return: Numpy float32, HWC, BGR, [0,1]"""
# print('debug:', path)
img = cv2.imread(path, cv2.IMREAD_UNCHANGED)

img = img.astype(np.float32) / 255.
if img.ndim == 2:
img = np.expand_dims(img, axis=2)

if img.shape[2] > 3:
img = img[:, :, :3]
return img
img = img[:, :, :3]
return img


def get_test_neighbor_frames(crt_i, N, max_n, padding='new_info'):
"""Generate an index list for reading N frames from a sequence of images
Expand Down Expand Up @@ -62,15 +63,14 @@ def get_test_neighbor_frames(crt_i, N, max_n, padding='new_info'):
else:
add_idx = i
return_l.append(add_idx)
# name_b = '{:08d}'.format(crt_i)
# name_b = '{:08d}'.format(crt_i)
return return_l


class EDVRDataset:
def __init__(self, frame_paths):
self.frames = frame_paths


def __getitem__(self, index):
indexs = get_test_neighbor_frames(index, 5, len(self.frames))
frame_list = []
Expand All @@ -79,12 +79,11 @@ def __getitem__(self, index):
frame_list.append(img)

img_LQs = np.stack(frame_list, axis=0)
print('img:', img_LQs.shape)
# BGR to RGB, HWC to CHW, numpy to tensor
img_LQs = img_LQs[:, :, :, [2, 1, 0]]
img_LQs = np.transpose(img_LQs, (0, 3, 1, 2)).astype('float32')

return img_LQs, self.frames[index]

def __len__(self):
return len(self.frames)
return len(self.frames)
10 changes: 5 additions & 5 deletions applications/EDVR/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import paddle.fluid as fluid
import cv2

from tqdm import tqdm
from data import EDVRDataset
from paddle.utils.download import get_path_from_url

Expand All @@ -52,7 +53,6 @@ def parse_args():


def get_img(pred):
print('pred shape', pred.shape)
pred = pred.squeeze()
pred = np.clip(pred, a_min=0., a_max=1.0)
pred = pred * 255
Expand All @@ -72,7 +72,7 @@ def save_img(img, framename):


def dump_frames_ffmpeg(vid_path, outpath, r=None, ss=None, t=None):
ffmpeg = ['ffmpeg ', ' -loglevel ', ' error ']
ffmpeg = ['ffmpeg ', ' -y -loglevel ', ' error ']
vid_name = vid_path.split('/')[-1].split('.')[0]
out_full_path = os.path.join(outpath, 'frames_input')

Expand Down Expand Up @@ -102,7 +102,7 @@ def dump_frames_ffmpeg(vid_path, outpath, r=None, ss=None, t=None):


def frames_to_video_ffmpeg(framepath, videopath, r):
ffmpeg = ['ffmpeg ', ' -loglevel ', ' error ']
ffmpeg = ['ffmpeg ', ' -y -loglevel ', ' error ']
cmd = ffmpeg + [
' -r ', r, ' -f ', ' image2 ', ' -i ', framepath, ' -vcodec ',
' libx264 ', ' -pix_fmt ', ' yuv420p ', ' -crf ', ' 16 ', videopath
Expand Down Expand Up @@ -164,7 +164,7 @@ def run(self):

periods = []
cur_time = time.time()
for infer_iter, data in enumerate(dataset):
for infer_iter, data in enumerate(tqdm(dataset)):
data_feed_in = [data[0]]

infer_outs = self.exe.run(
Expand All @@ -185,7 +185,7 @@ def run(self):
period = cur_time - prev_time
periods.append(period)

print('Processed {} samples'.format(infer_iter + 1))
# print('Processed {} samples'.format(infer_iter + 1))
frame_pattern_combined = os.path.join(pred_frame_path, '%08d.png')
vid_out_path = os.path.join(self.output,
'{}_edvr_out.mp4'.format(base_name))
Expand Down
4 changes: 2 additions & 2 deletions applications/RealSR/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


def frames_to_video_ffmpeg(framepath, videopath, r):
ffmpeg = ['ffmpeg ', ' -loglevel ', ' error ']
ffmpeg = ['ffmpeg ', ' -y -loglevel ', ' error ']
cmd = ffmpeg + [
' -r ', r, ' -f ', ' image2 ', ' -i ', framepath, ' -vcodec ',
' libx264 ', ' -pix_fmt ', ' yuv420p ', ' -crf ', ' 16 ', videopath
Expand Down Expand Up @@ -110,7 +110,7 @@ def run(self):


def dump_frames_ffmpeg(vid_path, outpath, r=None, ss=None, t=None):
ffmpeg = ['ffmpeg ', ' -loglevel ', ' error ']
ffmpeg = ['ffmpeg ', ' -y -loglevel ', ' error ']
vid_name = vid_path.split('/')[-1].split('.')[0]
out_full_path = os.path.join(outpath, 'frames_input')

Expand Down

0 comments on commit 6ea2876

Please sign in to comment.