-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfvd_external.py
168 lines (143 loc) · 4.95 KB
/
fvd_external.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
160
161
162
163
164
165
166
167
168
import os
import random
import argparse
import glob
from tqdm import tqdm
import numpy as np
import torch
from decord import VideoReader
from common_metrics_on_video_quality import calculate_fvd
def load_videos(
dir, has_subset=False, resolution=256, frames=17, pos="frame", num_videos=2048
):
if not has_subset:
videos = os.listdir(dir)
videos = [os.path.join(dir, v) for v in videos if v.endswith("mp4")]
else:
videos = glob.glob(f"{dir}/*/*.avi")
random.shuffle(videos)
if num_videos != -1:
videos = videos[:num_videos]
video_data = []
for v in tqdm(videos):
if v.endswith(".gif"):
new_v = v.replace("gif", "mp4")
os.system(f"ffmpeg -i {v} {new_v} -loglevel quiet")
v = new_v
vr = VideoReader(v, width=resolution, height=resolution)
sampled_frms = (
vr.get_batch(np.arange(0, len(vr), 1, dtype=int)).asnumpy().astype(np.uint8)
) # T H W 3
assert len(vr) >= frames
if len(vr) > frames:
if pos == "first":
sampled_frms = sampled_frms[:frames]
elif pos == "last":
sampled_frms = sampled_frms[-frames:]
else:
center = len(vr) // 2
if frames % 2 == 0:
frame_indices = np.array(
range(center - frames // 2, center + frames // 2)
).astype(int)
else:
frame_indices = np.array(
range(center - frames // 2, center + frames // 2 + 1)
).astype(int)
sampled_frms = sampled_frms[frame_indices]
vid_frm_array = (
torch.from_numpy(sampled_frms).float().permute(0, 3, 1, 2)
) / 255.0 # T, 3, H, W
video_data.append(vid_frm_array)
video_data = torch.stack(video_data, dim=0)
return video_data # torch.zeros(NUMBER_OF_VIDEOS, VIDEO_LENGTH, CHANNEL, SIZE, SIZE, requires_grad=False)
def calculate_fvd_github(gen_dir, gt_dir, resolution=256, frames=17, sampling="center"):
num_videos_gt = len(os.listdir(gt_dir))
num_videos_gen = len(os.listdir(gen_dir))
# assert num_videos_gt == num_videos_gen
num_videos = min(num_videos_gt, num_videos_gen)
print(
f"num_video_gt: {num_videos_gt}, num_video_gen: {num_videos_gen},num_videos(min of the 2): {num_videos}"
)
if False:
gt_videos = load_videos(
gt_dir,
has_subset=True,
resolution=resolution,
frames=frames,
pos=sampling,
num_videos=num_videos,
)
else:
gt_videos = load_videos(
gt_dir,
has_subset=False,
resolution=resolution,
frames=frames,
pos=sampling,
num_videos=num_videos,
)
gen_videos = load_videos(
gen_dir,
has_subset=False,
resolution=resolution,
frames=frames,
pos=sampling,
num_videos=num_videos,
)
# print(gen_videos.shape, gt_videos.shape)
results = calculate_fvd(gt_videos, gen_videos, device="cuda", method="videogpt")
print(results)
fvd16 = results["value"][16]
print(f"fvd16: {fvd16}")
return dict(fvd=fvd16)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", type=str, default="k600", choices=["k600", "ucf"])
parser.add_argument(
"--gen_dir", default="./data/unittest_ffs_tokenizer_titok/gt", type=str
)
parser.add_argument(
"--gt_dir",
default="./data/unittest_ffs_tokenizer_titok/reconstructed",
type=str,
)
parser.add_argument("--split", type=str, choices=["train", "test"])
parser.add_argument("--frames", type=int, default=17)
parser.add_argument("--resolution", type=int, default=64)
parser.add_argument(
"--sampling", type=str, default="center", choices=["first", "last", "center"]
)
parser.add_argument("--num_videos", type=int, default=2048)
args = parser.parse_args()
gen_dir = args.gen_dir
gt_dir = args.gt_dir
if args.dataset == "ucf":
gt_videos = load_videos(
gt_dir,
has_subset=True,
resolution=args.resolution,
frames=args.frames,
pos=args.sampling,
num_videos=args.num_videos,
)
else:
gt_videos = load_videos(
gt_dir,
has_subset=False,
resolution=args.resolution,
frames=args.frames,
pos=args.sampling,
num_videos=args.num_videos,
)
gen_videos = load_videos(
gen_dir,
has_subset=False,
resolution=args.resolution,
frames=args.frames,
pos=args.sampling,
num_videos=args.num_videos,
)
# print(gen_videos.shape, gt_videos.shape)
results = calculate_fvd(gt_videos, gen_videos, device="cuda", method="videogpt")
print(results)