-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
159 lines (132 loc) · 6.83 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
import numpy as np
from pathlib import Path
import cv2
from lpips import LPIPS
import pandas as pd
from tqdm import tqdm
from pytorch_lightning.loggers import CometLogger
from dotmap import DotMap
from utils.metrics import compute_psnr, compute_ssim, compute_lpips, compute_vmaf
from data.synthetic_video_dataset import SyntheticVideoDataset
from utils.utils import PROJECT_ROOT, device
def test(args: DotMap, net: nn.Module, logger: CometLogger):
"""
Test the model on the synthetic dataset test split.
Args:
args: Arguments
net: Network to be tested
logger: Comet ML logger
"""
data_base_path = Path(args.data_base_path)
test_path = data_base_path / "test"
gt_videos_path = test_path / "gt" / "videos"
results_path = PROJECT_ROOT / "experiments" / args.experiment_name / "results"
results_path.mkdir(parents=True, exist_ok=True)
videos_path = results_path / Path("videos")
restored_videos_path = videos_path / "restored"
restored_videos_path.mkdir(parents=True, exist_ok=True)
combined_videos_path = videos_path / "combined"
combined_videos_path.mkdir(parents=True, exist_ok=True)
test_dataset = SyntheticVideoDataset(test_path, num_input_frames=args.num_input_frames,
num_reference_frames=args.num_reference_frames, crop_mode="center",
patch_size=args.test_patch_size)
net.eval()
net.to(device)
dataloader = DataLoader(test_dataset, batch_size=1, shuffle=False, num_workers=args.num_workers, pin_memory=True)
lpips_net = LPIPS(pretrained=True, net='alex').to(device)
count_videos = 0
count_frames = 0
if not args.no_vmaf:
column_names = ["Name", "PSNR", "SSIM", "LPIPS", "VMAF"]
metrics_list = ["PSNR", "SSIM", "LPIPS", "VMAF"]
else:
column_names = ["Name", "PSNR", "SSIM", "LPIPS"]
metrics_list = ["PSNR", "SSIM", "LPIPS"]
single_metrics_results = {}
total_metrics_results = {metric: 0 for metric in metrics_list}
output_csv = [[]]
output_csv_path = results_path / "metrics_results.csv"
last_clip = ""
restored_video_writer = cv2.VideoWriter()
combined_video_writer = cv2.VideoWriter()
for batch in tqdm(dataloader):
img_name = batch["img_name"]
imgs_lq = batch["imgs_lq"].to(device, non_blocking=True)
imgs_ref = batch["imgs_ref"].to(device, non_blocking=True)
imgs_gt = batch["imgs_gt"]
with torch.no_grad(), torch.cuda.amp.autocast():
output = torch.clamp(net(imgs_lq, imgs_ref), 0, 1)
output = output[:, args.num_input_frames // 2].permute(0, 2, 3, 1).cpu().numpy()
for i in range(output.shape[0]):
video_clip = Path(img_name[i]).parent
(results_path / Path(video_clip)).mkdir(parents=True, exist_ok=True)
restored = (output[i] * 255).astype(np.uint8)
restored = restored[..., ::-1] # RGB -> BGR
input = imgs_lq[i, args.num_input_frames // 2].permute(1, 2, 0).cpu().numpy()
input = (input * 255).astype(np.uint8)
input = input[..., ::-1] # RGB -> BGR
gt = imgs_gt[i, args.num_input_frames // 2].permute(1, 2, 0).cpu().numpy()
gt = (gt * 255).astype(np.uint8)
gt = gt[..., ::-1] # RGB -> BGR
if video_clip != last_clip:
restored_video_writer.release()
combined_video_writer.release()
if last_clip != "":
gt_video_path = gt_videos_path / f"{last_clip}.mp4"
if not args.no_vmaf:
single_metrics_results["VMAF"] = compute_vmaf(restored_video_path, gt_video_path,
width=restored.shape[0], height=restored.shape[1])
for metric in single_metrics_results.keys():
if metric != "VMAF":
single_metrics_results[metric] /= count_frames
total_metrics_results[metric] += single_metrics_results[metric]
output_csv_row = list(single_metrics_results.values())
output_csv_row.insert(0, last_clip)
output_csv.append(output_csv_row)
last_clip = video_clip
restored_video_path = restored_videos_path / f"{last_clip}.mp4"
combined_video_path = combined_videos_path / f"{last_clip}.mp4"
restored_video_writer = cv2.VideoWriter(str(restored_video_path),
cv2.VideoWriter_fourcc(*'mp4v'), 60,
restored.shape[0:2])
combined_shape = (restored.shape[0] * 3, restored.shape[1])
combined_video_writer = cv2.VideoWriter(str(combined_video_path),
cv2.VideoWriter_fourcc(*'mp4v'), 60,
combined_shape)
single_metrics_results = {metric: 0 for metric in metrics_list}
count_videos += 1
count_frames = 0
restored_video_writer.write(restored)
combined = np.hstack((input, restored, gt))
combined_video_writer.write(combined)
single_metrics_results["PSNR"] += compute_psnr(restored, gt)
single_metrics_results["SSIM"] += compute_ssim(restored, gt)
single_metrics_results["LPIPS"] += compute_lpips(restored, gt, lpips_net, device)
count_frames += 1
cv2.imwrite(f"{results_path}/{img_name[i]}.jpg", restored)
restored_video_writer.release()
combined_video_writer.release()
# Compute metrics of last video
gt_video_path = gt_videos_path / f"{last_clip}.mp4"
if not args.no_vmaf:
single_metrics_results["VMAF"] = compute_vmaf(restored_video_path, gt_video_path,
width=restored.shape[0], height=restored.shape[1])
for metric in single_metrics_results.keys():
if metric != "VMAF":
single_metrics_results[metric] /= count_frames
total_metrics_results[metric] += single_metrics_results[metric]
output_csv_row = list(single_metrics_results.values())
output_csv_row.insert(0, last_clip)
output_csv.append(output_csv_row)
for metric in total_metrics_results.keys():
total_metrics_results[metric] /= count_videos
logger.experiment.log_metric(metric, total_metrics_results[metric])
output_csv_row = list(total_metrics_results.values())
output_csv_row.insert(0, "Total")
output_csv.append(output_csv_row)
df = pd.DataFrame(output_csv)
df.columns = column_names
df.to_csv(output_csv_path, index=False)