Update python/run_onnx.py
Browse files- python/run_onnx.py +130 -129
python/run_onnx.py
CHANGED
|
@@ -1,129 +1,130 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import cv2
|
| 3 |
-
import time
|
| 4 |
-
import torch
|
| 5 |
-
import argparse
|
| 6 |
-
import numpy as np
|
| 7 |
-
from tqdm import tqdm
|
| 8 |
-
|
| 9 |
-
import common
|
| 10 |
-
import imgproc
|
| 11 |
-
import onnxruntime as ort
|
| 12 |
-
|
| 13 |
-
torch.manual_seed(1)
|
| 14 |
-
|
| 15 |
-
parser = argparse.ArgumentParser()
|
| 16 |
-
parser.add_argument("--model", type=str, default="edsr_baseline_x2_1.onnx", help="onnx model path")
|
| 17 |
-
parser.add_argument('--scale', nargs='+', type=int, default=[2], help='super resolution scale')
|
| 18 |
-
parser.add_argument("--dir_demo", type=str, default='../video/test_1920x1080.mp4', help="demo image directory")
|
| 19 |
-
parser.add_argument('--rgb_range', type=int, default=255, help='maximum value of RGB')
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
self.
|
| 32 |
-
self.
|
| 33 |
-
self.
|
| 34 |
-
self.
|
| 35 |
-
self.
|
| 36 |
-
self.
|
| 37 |
-
self.
|
| 38 |
-
self.
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
(
|
| 53 |
-
|
| 54 |
-
int(scale * vidcap.get(cv2.
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
lr_y_image, = common.
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
sr_image =
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
print('
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
t.
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import time
|
| 4 |
+
import torch
|
| 5 |
+
import argparse
|
| 6 |
+
import numpy as np
|
| 7 |
+
from tqdm import tqdm
|
| 8 |
+
|
| 9 |
+
import common
|
| 10 |
+
import imgproc
|
| 11 |
+
import onnxruntime as ort
|
| 12 |
+
|
| 13 |
+
torch.manual_seed(1)
|
| 14 |
+
|
| 15 |
+
parser = argparse.ArgumentParser()
|
| 16 |
+
parser.add_argument("--model", type=str, default="edsr_baseline_x2_1.onnx", help="onnx model path")
|
| 17 |
+
parser.add_argument('--scale', nargs='+', type=int, default=[2], help='super resolution scale')
|
| 18 |
+
parser.add_argument("--dir_demo", type=str, default='../video/test_1920x1080.mp4', help="demo image directory")
|
| 19 |
+
parser.add_argument('--rgb_range', type=int, default=255, help='maximum value of RGB')
|
| 20 |
+
parser.add_argument('--arch', type=str, default='espcn', help='model architecture (options: edsr、espcn)')
|
| 21 |
+
|
| 22 |
+
def quantize(img, rgb_range):
|
| 23 |
+
pixel_range = 255 / rgb_range
|
| 24 |
+
return np.round(np.clip(img * pixel_range, 0, 255)) / pixel_range
|
| 25 |
+
|
| 26 |
+
def from_numpy(x):
|
| 27 |
+
return x if isinstance(x, np.ndarray) else np.array(x)
|
| 28 |
+
|
| 29 |
+
class VideoTester():
|
| 30 |
+
def __init__(self, scale, my_model, dir_demo, rgb_range=255, cuda=True, arch='espcn'):
|
| 31 |
+
self.scale = scale
|
| 32 |
+
self.rgb_range = rgb_range
|
| 33 |
+
self.providers = ['CUDAExecutionProvider'] if cuda else ['CPUExecutionProvider']
|
| 34 |
+
self.session = ort.InferenceSession(my_model, providers=self.providers)
|
| 35 |
+
self.output_names = [x.name for x in self.session.get_outputs()]
|
| 36 |
+
self.input_name = self.session.get_inputs()[0].name
|
| 37 |
+
self.dir_demo = dir_demo
|
| 38 |
+
self.filename, _ = os.path.splitext(os.path.basename(dir_demo))
|
| 39 |
+
self.arch = arch
|
| 40 |
+
|
| 41 |
+
def test(self):
|
| 42 |
+
torch.set_grad_enabled(False)
|
| 43 |
+
if not os.path.exists('experiment'):
|
| 44 |
+
os.makedirs('experiment')
|
| 45 |
+
for idx_scale, scale in enumerate(self.scale):
|
| 46 |
+
vidcap = cv2.VideoCapture(self.dir_demo)
|
| 47 |
+
total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 48 |
+
|
| 49 |
+
vidwri = cv2.VideoWriter(
|
| 50 |
+
os.path.join('experiment', ('{}_x{}.avi'.format(self.filename, scale))),
|
| 51 |
+
cv2.VideoWriter_fourcc(*'XVID'),
|
| 52 |
+
vidcap.get(cv2.CAP_PROP_FPS),
|
| 53 |
+
(
|
| 54 |
+
int(scale * vidcap.get(cv2.CAP_PROP_FRAME_WIDTH)),
|
| 55 |
+
int(scale * vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 56 |
+
)
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
total_times = 0
|
| 60 |
+
tqdm_test = tqdm(range(total_frames), ncols=80)
|
| 61 |
+
|
| 62 |
+
if self.arch == 'edsr':
|
| 63 |
+
for _ in tqdm_test:
|
| 64 |
+
success, lr = vidcap.read()
|
| 65 |
+
if not success: break
|
| 66 |
+
start_time = time.time()
|
| 67 |
+
lr_y_image, = common.set_channel(lr, n_channels=3)
|
| 68 |
+
lr_y_image, = common.np_prepare(lr_y_image, rgb_range=self.rgb_range)
|
| 69 |
+
|
| 70 |
+
sr = self.session.run(self.output_names, {self.input_name: lr_y_image})
|
| 71 |
+
end_time = time.time()
|
| 72 |
+
total_times += end_time - start_time
|
| 73 |
+
|
| 74 |
+
if isinstance(sr, (list, tuple)):
|
| 75 |
+
sr = from_numpy(sr[0]) if len(sr) == 1 else [from_numpy(x) for x in sr]
|
| 76 |
+
else:
|
| 77 |
+
sr = from_numpy(sr)
|
| 78 |
+
|
| 79 |
+
sr = quantize(sr, self.rgb_range).squeeze(0)
|
| 80 |
+
normalized = sr * 255 / self.rgb_range
|
| 81 |
+
ndarr = normalized.transpose(1, 2, 0).astype(np.uint8)
|
| 82 |
+
vidwri.write(ndarr)
|
| 83 |
+
|
| 84 |
+
elif self.arch == 'espcn':
|
| 85 |
+
for _ in tqdm_test:
|
| 86 |
+
success, lr = vidcap.read()
|
| 87 |
+
if not success: break
|
| 88 |
+
start_time = time.time()
|
| 89 |
+
|
| 90 |
+
lr_y_image, lr_cb_image, lr_cr_image = imgproc.preprocess_one_frame(lr)
|
| 91 |
+
bic_cb_image = cv2.resize(lr_cb_image,
|
| 92 |
+
(int(lr_cb_image.shape[1] * scale),
|
| 93 |
+
int(lr_cb_image.shape[0] * scale)),
|
| 94 |
+
interpolation=cv2.INTER_CUBIC)
|
| 95 |
+
bic_cr_image = cv2.resize(lr_cr_image,
|
| 96 |
+
(int(lr_cr_image.shape[1] * scale),
|
| 97 |
+
int(lr_cr_image.shape[0] * scale)),
|
| 98 |
+
interpolation=cv2.INTER_CUBIC)
|
| 99 |
+
|
| 100 |
+
sr = self.session.run(self.output_names, {self.input_name: lr_y_image})
|
| 101 |
+
end_time = time.time()
|
| 102 |
+
total_times += end_time - start_time
|
| 103 |
+
|
| 104 |
+
if isinstance(sr, (list, tuple)):
|
| 105 |
+
sr = from_numpy(sr[0]) if len(sr) == 1 else [from_numpy(x) for x in sr]
|
| 106 |
+
else:
|
| 107 |
+
sr = from_numpy(sr)
|
| 108 |
+
|
| 109 |
+
ndarr = imgproc.array_to_image(sr)
|
| 110 |
+
sr_y_image = ndarr.astype(np.float32) / 255.0
|
| 111 |
+
sr_ycbcr_image = cv2.merge([sr_y_image[:, :, 0], bic_cb_image, bic_cr_image])
|
| 112 |
+
sr_image = imgproc.ycbcr_to_bgr(sr_ycbcr_image)
|
| 113 |
+
sr_image = np.clip(sr_image* 255.0, 0 , 255).astype(np.uint8)
|
| 114 |
+
vidwri.write(sr_image)
|
| 115 |
+
|
| 116 |
+
print('Total time: {:.3f} seconds for {} frames'.format(total_times, total_frames))
|
| 117 |
+
print('Average time: {:.3f} seconds for each frame'.format(total_times / total_frames))
|
| 118 |
+
|
| 119 |
+
vidcap.release()
|
| 120 |
+
vidwri.release()
|
| 121 |
+
|
| 122 |
+
torch.set_grad_enabled(True)
|
| 123 |
+
|
| 124 |
+
def main():
|
| 125 |
+
args = parser.parse_args()
|
| 126 |
+
t = VideoTester(args.scale, args.model, args.dir_demo, arch=args.arch)
|
| 127 |
+
t.test()
|
| 128 |
+
|
| 129 |
+
if __name__ == '__main__':
|
| 130 |
+
main()
|