-
Notifications
You must be signed in to change notification settings - Fork 31
/
video_loader.py
158 lines (147 loc) · 5.69 KB
/
video_loader.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
import torch as th
from torch.utils.data import Dataset
import pandas as pd
import os
import numpy as np
import random
import ffmpeg
import time
import re
class HT100M_DataLoader(Dataset):
"""HowTo100M Video-Text loader."""
def __init__(
self,
csv,
video_root='',
caption_root='',
min_time=4.0,
fps=16,
num_frames=16,
size=224,
crop_only=False,
center_crop=True,
benchmark=False,
token_to_word_path='data/dict.npy',
max_words=20,
num_candidates=1,
random_left_right_flip=False,
):
"""
Args:
"""
assert isinstance(size, int)
self.csv = pd.read_csv(os.path.join(os.path.dirname(__file__), csv))
self.video_root = video_root
self.caption_root = caption_root
self.min_time = min_time
self.size = size
self.num_frames = num_frames
self.fps = fps
self.num_sec = self.num_frames / float(self.fps)
self.crop_only = crop_only
self.center_crop = center_crop
self.benchmark = benchmark
self.max_words = max_words
token_to_word = np.load(os.path.join(os.path.dirname(__file__), token_to_word_path))
self.word_to_token = {}
for i, t in enumerate(token_to_word):
self.word_to_token[t] = i + 1
self.num_candidates = num_candidates
self.random_flip = random_left_right_flip
def __len__(self):
return len(self.csv)
def _get_video(self, video_path, start, end):
start_seek = random.randint(start, int(max(start, end - self.num_sec)))
cmd = (
ffmpeg
.input(video_path, ss=start_seek, t=self.num_sec + 0.1)
.filter('fps', fps=self.fps)
)
if self.center_crop:
aw, ah = 0.5, 0.5
else:
aw, ah = random.uniform(0, 1), random.uniform(0, 1)
if self.crop_only:
cmd = (
cmd.crop('(iw - {})*{}'.format(self.size, aw),
'(ih - {})*{}'.format(self.size, ah),
str(self.size), str(self.size))
)
else:
cmd = (
cmd.crop('(iw - min(iw,ih))*{}'.format(aw),
'(ih - min(iw,ih))*{}'.format(ah),
'min(iw,ih)',
'min(iw,ih)')
.filter('scale', self.size, self.size)
)
if self.random_flip and random.uniform(0, 1) > 0.5:
cmd = cmd.hflip()
out, _ = (
cmd.output('pipe:', format='rawvideo', pix_fmt='rgb24')
.run(capture_stdout=True, quiet=True)
)
video = np.frombuffer(out, np.uint8).reshape([-1, self.size, self.size, 3])
video = th.from_numpy(video)
video = video.permute(3, 0, 1, 2)
if video.shape[1] < self.num_frames:
zeros = th.zeros((3, self.num_frames - video.shape[1], self.size, self.size), dtype=th.uint8)
video = th.cat((video, zeros), axis=1)
return video[:, :self.num_frames]
def _split_text(self, sentence):
w = re.findall(r"[\w']+", str(sentence))
return w
def _words_to_token(self, words):
words = [self.word_to_token[word] for word in words if word in self.word_to_token]
if words:
we = self._zero_pad_tensor_token(th.LongTensor(words), self.max_words)
return we
else:
return th.zeros(self.max_words, dtype=th.long)
def _zero_pad_tensor_token(self, tensor, size):
if len(tensor) >= size:
return tensor[:size]
else:
zero = th.zeros(size - len(tensor)).long()
return th.cat((tensor, zero), dim=0)
def words_to_ids(self, x):
return self._words_to_token(self._split_text(x))
def _find_nearest_candidates(self, caption, ind):
start, end = ind, ind
diff = caption['end'][end] - caption['start'][start]
n_candidate = 1
while n_candidate < self.num_candidates:
if start == 0:
return 0
elif end == len(caption) - 1:
return start - (self.num_candidates - n_candidate)
elif caption['end'][end] - caption['start'][start - 1] < caption['end'][end + 1] - caption['start'][start]:
start -= 1
else:
end += 1
n_candidate += 1
return start
def _get_text(self, caption):
cap = pd.read_csv(caption)
ind = random.randint(0, len(cap) - 1)
if self.num_candidates == 1:
words = self.words_to_ids(cap['text'].values[ind])
else:
words = th.zeros(self.num_candidates, self.max_words, dtype=th.long)
cap_start = self._find_nearest_candidates(cap, ind)
for i in range(self.num_candidates):
words[i] = self.words_to_ids(cap['text'].values[max(0, min(len(cap['text']) - 1, cap_start + i))])
start, end = cap['start'].values[ind], cap['end'].values[ind]
#TODO: May need to be improved for edge cases.
if end - start < self.min_time:
diff = self.min_time - end + start
start = max(0, start - diff / 2)
end = start + self.min_time
return words, int(start), int(end)
def __getitem__(self, idx):
video_file = self.csv['video_path'][idx]
video_id = video_file.split('.')[0]
video_path = os.path.join(self.video_root, video_file)
text, start, end = self._get_text(os.path.join(self.caption_root, video_id + '.csv'))
video = self._get_video(video_path, start, end)
return {'video': video, 'text': text}