-
Notifications
You must be signed in to change notification settings - Fork 31
/
msrvtt_loader.py
129 lines (116 loc) · 4.26 KB
/
msrvtt_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
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
import pickle
class MSRVTT_DataLoader(Dataset):
"""MSRVTT Video-Text loader."""
def __init__(
self,
data,
video_root='',
num_clip=4,
fps=16,
num_frames=32,
size=224,
crop_only=False,
center_crop=True,
token_to_word_path='data/dict.npy',
max_words=30,
):
"""
Args:
"""
assert isinstance(size, int)
self.data = pd.read_csv(data)
self.video_root = video_root
self.size = size
self.num_frames = num_frames
self.fps = fps
self.num_clip = num_clip
self.num_sec = self.num_frames / float(self.fps)
self.crop_only = crop_only
self.center_crop = center_crop
self.max_words = max_words
self.word_to_token = {}
token_to_word = np.load(os.path.join(os.path.dirname(__file__), token_to_word_path))
for i, t in enumerate(token_to_word):
self.word_to_token[t] = i + 1
def __len__(self):
return len(self.data)
def _get_video(self, video_path, start, end, num_clip):
video = th.zeros(num_clip, 3, self.num_frames, self.size, self.size)
start_ind = np.linspace(start, max(start, end-self.num_sec - 0.4), num_clip)
for i, s in enumerate(start_ind):
video[i] = self._get_video_start(video_path, s)
return video
def _get_video_start(self, video_path, start):
start_seek = start
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)
)
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).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 _get_duration(self, video_path):
probe = ffmpeg.probe(video_path)
return probe['format']['duration']
def __getitem__(self, idx):
video_id = self.data['video_id'].values[idx]
cap = self.data['sentence'].values[idx]
video_path = os.path.join(self.video_root, video_id + '.mp4')
duration = self._get_duration(video_path)
text = self.words_to_ids(cap)
video = self._get_video(video_path, 0, float(duration), self.num_clip)
return {'video': video, 'text': text}