-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdataset.py
187 lines (151 loc) · 7.78 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
177
178
179
180
181
182
183
184
185
186
187
from video_records import EpicKitchens55_VideoRecord, EpicKitchens100_VideoRecord
import torch.utils.data as data
import librosa
from PIL import Image
import os
import os.path
from pathlib import Path
import pandas as pd
import numpy as np
from numpy.random import randint
import pickle
class TBNDataSet(data.Dataset):
def __init__(self, dataset, list_file,
new_length, modality, image_tmpl,
visual_path=None, audio_path=None,
resampling_rate=44000,
num_segments=3, transform=None,
mode='train', use_audio_dict=True):
self.dataset = dataset
if audio_path is not None:
if not use_audio_dict:
self.audio_path = Path(audio_path)
else:
self.audio_path = pickle.load(open(audio_path, 'rb'))
self.visual_path = visual_path
self.list_file = list_file
self.num_segments = num_segments
self.new_length = new_length
self.modality = modality
self.image_tmpl = image_tmpl
self.transform = transform
self.mode = mode
self.resampling_rate = resampling_rate
self.use_audio_dict = use_audio_dict
if 'RGBDiff' in self.modality:
self.new_length['RGBDiff'] += 1 # Diff needs one more image to calculate diff
self._parse_list()
def _log_specgram(self, audio, window_size=10,
step_size=5, eps=1e-6):
nperseg = int(round(window_size * self.resampling_rate / 1e3))
noverlap = int(round(step_size * self.resampling_rate / 1e3))
spec = librosa.stft(audio, n_fft=511,
window='hann',
hop_length=noverlap,
win_length=nperseg,
pad_mode='constant')
spec = np.log(np.real(spec * np.conj(spec)) + eps)
return spec
def _extract_sound_feature(self, record, idx):
centre_sec = (record.start_frame + idx) / record.fps['Spec']
left_sec = centre_sec - 0.639
right_sec = centre_sec + 0.639
audio_fname = record.untrimmed_video_name + '.wav'
if not self.use_audio_dict:
samples, sr = librosa.core.load(self.audio_path / audio_fname,
sr=None, mono=True)
else:
audio_fname = record.untrimmed_video_name
samples = self.audio_path[audio_fname]
duration = samples.shape[0] / float(self.resampling_rate)
left_sample = int(round(left_sec * self.resampling_rate))
right_sample = int(round(right_sec * self.resampling_rate))
if left_sec < 0:
samples = samples[:int(round(self.resampling_rate * 1.279))]
elif right_sec > duration:
samples = samples[-int(round(self.resampling_rate * 1.279)):]
else:
samples = samples[left_sample:right_sample]
return self._log_specgram(samples)
def _load_data(self, modality, record, idx):
if modality == 'RGB' or modality == 'RGBDiff':
idx_untrimmed = record.start_frame + idx
return [Image.open(os.path.join(self.visual_path, record.untrimmed_video_name, self.image_tmpl[modality].format(idx_untrimmed))).convert('RGB')]
elif modality == 'Flow':
rgb2flow_fps_ratio = record.fps['Flow'] / float(record.fps['RGB'])
idx_untrimmed = int(np.floor((record.start_frame * rgb2flow_fps_ratio))) + idx
x_img = Image.open(os.path.join(self.visual_path, record.untrimmed_video_name, self.image_tmpl[modality].format('x', idx_untrimmed))).convert('L')
y_img = Image.open(os.path.join(self.visual_path, record.untrimmed_video_name, self.image_tmpl[modality].format('y', idx_untrimmed))).convert('L')
return [x_img, y_img]
elif modality == 'Spec':
spec = self._extract_sound_feature(record, idx)
return [Image.fromarray(spec)]
def _parse_list(self):
if self.dataset == 'epic-kitchens-55':
self.video_list = [EpicKitchens55_VideoRecord(tup) for tup in self.list_file.iterrows()]
elif self.dataset == 'epic-kitchens-100':
self.video_list = [EpicKitchens100_VideoRecord(tup) for tup in self.list_file.iterrows()]
def _sample_indices(self, record, modality):
"""
:param record: VideoRecord
:return: list
"""
average_duration = (record.num_frames[modality] - self.new_length[modality] + 1) // self.num_segments
if average_duration > 0:
offsets = np.multiply(list(range(self.num_segments)), average_duration) + randint(average_duration, size=self.num_segments)
# elif record.num_frames[modality] > self.num_segments:
# offsets = np.sort(randint(record.num_frames[modality] - self.new_length[modality] + 1, size=self.num_segments))
else:
offsets = np.zeros((self.num_segments,))
return offsets
def _get_val_indices(self, record, modality):
if record.num_frames[modality] > self.num_segments + self.new_length[modality] - 1:
tick = (record.num_frames[modality] - self.new_length[modality] + 1) / float(self.num_segments)
offsets = np.array([int(tick / 2.0 + tick * x) for x in range(self.num_segments)])
else:
offsets = np.zeros((self.num_segments,))
return offsets
def _get_test_indices(self, record, modality):
tick = (record.num_frames[modality] - self.new_length[modality] + 1) / float(self.num_segments)
offsets = np.array([int(tick / 2.0 + tick * x) for x in range(self.num_segments)])
return offsets
def __getitem__(self, index):
input = {}
record = self.video_list[index]
for m in self.modality:
if self.mode == 'train':
segment_indices = self._sample_indices(record, m)
elif self.mode == 'val':
segment_indices = self._get_val_indices(record, m)
elif self.mode == 'test':
segment_indices = self._get_test_indices(record, m)
# We implement a Temporal Binding Window (TBW) with size same as the action's length by:
# 1. Selecting different random indices (timestamps) for each modality within segments
# (this is similar to using a TBW with size same as the segment's size)
# 2. Shuffling randomly the segments of Flow, Audio (RGB is the anchor hence not shuffled)
# which binds data across segments, hence making the TBW same in size as the action.
# Example of an action with 90 frames across all modalities:
# 1. Synchronous selection of indices per segment:
# RGB: [12, 41, 80], Flow: [12, 41, 80], Audio: [12, 41, 80]
# 2. Asynchronous selection of indices per segment:
# RGB: [12, 41, 80], Flow: [9, 55, 88], Audio: [20, 33, 67]
# 3. Asynchronous selection of indices per action:
# RGB: [12, 41, 80], Flow: [88, 55, 9], Audio: [67, 20, 33]
if m != 'RGB' and self.mode == 'train':
np.random.shuffle(segment_indices)
img, label = self.get(m, record, segment_indices)
input[m] = img
return input, label
def get(self, modality, record, indices):
images = list()
for seg_ind in indices:
p = int(seg_ind)
for i in range(self.new_length[modality]):
seg_imgs = self._load_data(modality, record, p)
images.extend(seg_imgs)
if p < record.num_frames[modality]:
p += 1
process_data = self.transform[modality](images)
return process_data, record.label, record.metadata
def __len__(self):
return len(self.video_list)