forked from cfosco/deepfake_detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
195 lines (155 loc) · 6.01 KB
/
datasets.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os
import os.path
import numpy as np
import torch.utils.data as data
from numpy.random import randint
from PIL import Image
class Record(object):
"""Represents a record.
A record has the following properties:
path (str): path to file.
label (int): primary label associated with video.
labels (list[int]): all labels associated with video.
"""
def __init__(self, path, label):
self._path = path
self._label = label
@property
def path(self):
return self._path
@property
def label(self):
return int(self._label)
def todict(self):
return {'label': self.label, 'path': self.path}
def __hash__(self):
return hash(self.path)
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.path == other.path
else:
return False
class VideoRecord(object):
"""Represents a video record.
A video record has the following properties:
path (str): path to directory containing frames.
num_frames (int): number of frames in path dir.
label (int): primary label associated with video.
labels (list[int]): all labels associated with video.
"""
def __init__(self, row):
self._data = row
@property
def path(self):
return self._data[0]
@property
def num_frames(self):
return int(self._data[1])
@property
def label(self):
return int(self._data[2])
@property
def labels(self):
return [int(label) for label in self._data[2:]]
def todict(self):
return {'labels': self.labels, 'num_frames': self.num_frames,
'path': self.path, 'label': self.label}
def __hash__(self):
return hash((self.path, self.num_frames))
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.path == other.path
else:
return False
class VideoDataset(data.Dataset):
def __init__(self, root, list_file, num_frames=1, image_tmpl='img_{:06d}.jpg',
sampler=None, transform=None, target_transform=None):
self.root = root
self.list_file = list_file
self.num_frames = num_frames
self.sampler = sampler
self.image_tmpl = image_tmpl
self.transform = transform
self.target_transform = target_transform
self._parse_list()
def _load_image(self, directory, idx):
filename_tmpl = os.path.join(self.root, directory, self.image_tmpl)
try:
return Image.open(filename_tmpl.format(idx)).convert('RGB')
except Exception:
print('Error loading image:', filename_tmpl.format(idx))
return Image.open(filename_tmpl.format(1)).convert('RGB')
def _parse_list(self):
# check the frame number is large >3:
# usualy it is [video_id, num_frames, class_idx]
tmp = [x.strip().split(' ') for x in open(self.list_file)]
tmp = [item for item in tmp if int(item[1]) >= 3]
self.video_list = [VideoRecord(item) for item in tmp]
print('Video number: {}'.format(len(self.video_list)))
def _sample_indices(self, record):
"""Sample frame indices.
:param record: VideoRecord
:return: list
Args:
record (TYPE): Description.
Returns:
TYPE: Description.
"""
average_duration = (record.num_frames - self.new_length + 1) * 1.0 / self.num_frames
if average_duration > 0:
offsets = np.multiply(list(range(self.num_frames)), average_duration) + \
np.random.uniform(0, average_duration, size=self.num_frames)
offsets = np.floor(offsets)
elif record.num_frames > self.num_frames:
offsets = np.sort(randint(record.num_frames - self.new_length + 1, size=self.num_frames))
else:
offsets = np.zeros((self.num_frames,))
return offsets + 1
def _get_val_indices(self, record):
if record.num_frames > self.num_frames + self.new_length - 1:
tick = (record.num_frames - self.new_length + 1) * 1.0 / self.num_frames
offsets = np.array([int(tick / 2.0 + tick * x) for x in range(self.num_frames)])
else:
offsets = np.zeros((self.num_frames,))
return offsets + 1
def _get_test_indices(self, record):
tick = (record.num_frames - self.new_length + 1) * 1.0 / self.num_frames
offsets = np.array([int(tick / 2.0 + tick * x) for x in range(self.num_segments)])
return offsets + 1
def __getitem__(self, index):
record = self.video_list[index]
# Check this is a legit video folder
test_filename = os.path.join(self.root, record.path, self.image_tmpl.format(1))
while not os.path.exists(test_filename):
print('Could not find: {}'.format(test_filename))
index = np.random.randint(len(self.video_list))
# Try another video.
record = self.video_list[index]
if self.sampler is None:
frame_indices = self._sample_indices(record)
return self.get(record, frame_indices)
def get(self, record, indices):
images = list()
for seg_ind in indices:
p = int(seg_ind)
for i in range(self.new_length):
seg_imgs = self._load_image(record.path, p)
images.extend(seg_imgs)
if p < record.num_frames:
p += 1
label = record.label
if self.transform is not None:
images = self.transform(images)
if self.target_transform is not None:
label = self.target_transform(label)
return images, label
def __len__(self):
return len(self.video_list)
if __name__ == '__main__':
# TODO: Finish implementing this!
root = ''
list_file = ''
transform = None
dataset = VideoDataset(root, list_file, transform=transform)
for i, (frames, label) in enumerate(dataset):
print(i, frames.shape, label)