forked from chaoyuaw/pytorch-coviar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
176 lines (133 loc) · 5.52 KB
/
dataset.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
169
170
171
172
173
174
175
176
"""
Definition of PyTorch "Dataset" that iterates through compressed videos
and return compressed representations (I-frames, motion vectors,
or residuals) for training or testing.
"""
import os
import os.path
import random
import numpy as np
import torch
import torch.utils.data as data
from coviar import get_num_frames
from coviar import load
from transforms import color_aug
GOP_SIZE = 12
def clip_and_scale(img, size):
return (img * (127.5 / size)).astype(np.int32)
def get_seg_range(n, num_segments, seg, representation):
if representation in ['residual', 'mv']:
n -= 1
seg_size = float(n - 1) / num_segments
seg_begin = int(np.round(seg_size * seg))
seg_end = int(np.round(seg_size * (seg+1)))
if seg_end == seg_begin:
seg_end = seg_begin + 1
if representation in ['residual', 'mv']:
# Exclude the 0-th frame, because it's an I-frmae.
return seg_begin + 1, seg_end + 1
return seg_begin, seg_end
def get_gop_pos(frame_idx, representation):
gop_index = frame_idx // GOP_SIZE
gop_pos = frame_idx % GOP_SIZE
if representation in ['residual', 'mv']:
if gop_pos == 0:
gop_index -= 1
gop_pos = GOP_SIZE - 1
else:
gop_pos = 0
return gop_index, gop_pos
class CoviarDataSet(data.Dataset):
def __init__(self, data_root, data_name,
video_list,
representation,
transform,
num_segments,
is_train,
accumulate):
self._data_root = data_root
self._data_name = data_name
self._num_segments = num_segments
self._representation = representation
self._transform = transform
self._is_train = is_train
self._accumulate = accumulate
self._input_mean = torch.from_numpy(
np.array([0.485, 0.456, 0.406]).reshape((1, 3, 1, 1))).float()
self._input_std = torch.from_numpy(
np.array([0.229, 0.224, 0.225]).reshape((1, 3, 1, 1))).float()
self._load_list(video_list)
def _load_list(self, video_list):
self._video_list = []
with open(video_list, 'r') as f:
for line in f:
video, _, label = line.strip().split()
video_path = os.path.join(self._data_root, video[:-4] + '.mp4')
self._video_list.append((
video_path,
int(label),
get_num_frames(video_path)))
print('%d videos loaded.' % len(self._video_list))
def _get_train_frame_index(self, num_frames, seg):
# Compute the range of the segment.
seg_begin, seg_end = get_seg_range(num_frames, self._num_segments, seg,
representation=self._representation)
# Sample one frame from the segment.
v_frame_idx = random.randint(seg_begin, seg_end - 1)
return get_gop_pos(v_frame_idx, self._representation)
def _get_test_frame_index(self, num_frames, seg):
if self._representation in ['mv', 'residual']:
num_frames -= 1
seg_size = float(num_frames - 1) / self._num_segments
v_frame_idx = int(np.round(seg_size * (seg + 0.5)))
if self._representation in ['mv', 'residual']:
v_frame_idx += 1
return get_gop_pos(v_frame_idx, self._representation)
def __getitem__(self, index):
if self._representation == 'mv':
representation_idx = 1
elif self._representation == 'residual':
representation_idx = 2
else:
representation_idx = 0
if self._is_train:
video_path, label, num_frames = random.choice(self._video_list)
else:
video_path, label, num_frames = self._video_list[index]
frames = []
for seg in range(self._num_segments):
if self._is_train:
gop_index, gop_pos = self._get_train_frame_index(num_frames, seg)
else:
gop_index, gop_pos = self._get_test_frame_index(num_frames, seg)
img = load(video_path, gop_index, gop_pos,
representation_idx, self._accumulate)
if img is None:
print('Error: loading video %s failed.' % video_path)
img = np.zeros((256, 256, 2)) if self._representation == 'mv' else np.zeros((256, 256, 3))
else:
if self._representation == 'mv':
img = clip_and_scale(img, 20)
img += 128
img = (np.minimum(np.maximum(img, 0), 255)).astype(np.uint8)
elif self._representation == 'residual':
img += 128
img = (np.minimum(np.maximum(img, 0), 255)).astype(np.uint8)
if self._representation == 'iframe':
img = color_aug(img)
# BGR to RGB. (PyTorch uses RGB according to doc.)
img = img[..., ::-1]
frames.append(img)
frames = self._transform(frames)
frames = np.array(frames)
frames = np.transpose(frames, (0, 3, 1, 2))
input = torch.from_numpy(frames).float() / 255.0
if self._representation == 'iframe':
input = (input - self._input_mean) / self._input_std
elif self._representation == 'residual':
input = (input - 0.5) / self._input_std
elif self._representation == 'mv':
input = (input - 0.5)
return input, label
def __len__(self):
return len(self._video_list)