-
Notifications
You must be signed in to change notification settings - Fork 4
/
dataLoader_multiperson.py
executable file
·401 lines (339 loc) · 17.1 KB
/
dataLoader_multiperson.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import os, torch, numpy, cv2, random, glob, python_speech_features, json, math
from scipy.io import wavfile
from torchvision.transforms import RandomCrop
from operator import itemgetter
from torchvggish import vggish_input, vggish_params, mel_features
def overlap(audio, noiseAudio):
snr = [random.uniform(-5, 5)]
if len(noiseAudio) < len(audio):
shortage = len(audio) - len(noiseAudio)
noiseAudio = numpy.pad(noiseAudio, (0, shortage), 'wrap')
else:
noiseAudio = noiseAudio[:len(audio)]
noiseDB = 10 * numpy.log10(numpy.mean(abs(noiseAudio**2)) + 1e-4)
cleanDB = 10 * numpy.log10(numpy.mean(abs(audio**2)) + 1e-4)
noiseAudio = numpy.sqrt(10**((cleanDB - noiseDB - snr) / 10)) * noiseAudio
audio = audio + noiseAudio
return audio.astype(numpy.int16)
def load_audio(data, dataPath, numFrames, audioAug, audioSet=None):
dataName = data[0]
fps = float(data[2])
audio = audioSet[dataName]
if audioAug == True:
augType = random.randint(0, 1)
if augType == 1:
audio = overlap(dataName, audio, audioSet)
else:
audio = audio
# fps is not always 25, in order to align the visual, we modify the window and step in MFCC extraction process based on fps
audio = python_speech_features.mfcc(audio,
16000,
numcep=13,
winlen=0.025 * 25 / fps,
winstep=0.010 * 25 / fps)
maxAudio = int(numFrames * 4)
if audio.shape[0] < maxAudio:
shortage = maxAudio - audio.shape[0]
audio = numpy.pad(audio, ((0, shortage), (0, 0)), 'wrap')
audio = audio[:int(round(numFrames * 4)), :]
return audio
def load_single_audio(audio, fps, numFrames, audioAug=False):
audio = python_speech_features.mfcc(audio,
16000,
numcep=13,
winlen=0.025 * 25 / fps,
winstep=0.010 * 25 / fps)
maxAudio = int(numFrames * 4)
if audio.shape[0] < maxAudio:
shortage = maxAudio - audio.shape[0]
audio = numpy.pad(audio, ((0, shortage), (0, 0)), 'wrap')
audio = audio[:int(round(numFrames * 4)), :]
return audio
def load_visual(data, dataPath, numFrames, visualAug):
dataName = data[0]
videoName = data[0][:11]
faceFolderPath = os.path.join(dataPath, videoName, dataName)
faceFiles = glob.glob("%s/*.jpg" % faceFolderPath)
sortedFaceFiles = sorted(faceFiles,
key=lambda data: (float(data.split('/')[-1][:-4])),
reverse=False)
faces = []
H = 112
if visualAug == True:
new = int(H * random.uniform(0.7, 1))
x, y = numpy.random.randint(0, H - new), numpy.random.randint(0, H - new)
M = cv2.getRotationMatrix2D((H / 2, H / 2), random.uniform(-15, 15), 1)
augType = random.choice(['orig', 'flip', 'crop', 'rotate'])
else:
augType = 'orig'
for faceFile in sortedFaceFiles[:numFrames]:
face = cv2.imread(faceFile)
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
face = cv2.resize(face, (H, H))
if augType == 'orig':
faces.append(face)
elif augType == 'flip':
faces.append(cv2.flip(face, 1))
elif augType == 'crop':
faces.append(cv2.resize(face[y:y + new, x:x + new], (H, H)))
elif augType == 'rotate':
faces.append(cv2.warpAffine(face, M, (H, H)))
faces = numpy.array(faces)
return faces
def load_label(data, numFrames):
res = []
labels = data[3].replace('[', '').replace(']', '')
labels = labels.split(',')
for label in labels:
res.append(int(label))
res = numpy.array(res[:numFrames])
return res
class train_loader(object):
def __init__(self, cfg, trialFileName, audioPath, visualPath, num_speakers):
self.cfg = cfg
self.audioPath = audioPath
self.visualPath = visualPath
self.candidate_speakers = num_speakers
self.path = os.path.join(cfg.DATA.dataPathAVA, "csv")
self.entity_data = json.load(open(os.path.join(self.path, 'train_entity.json')))
self.ts_to_entity = json.load(open(os.path.join(self.path, 'train_ts.json')))
self.mixLst = open(trialFileName).read().splitlines()
self.list_length = len(self.mixLst)
random.shuffle(self.mixLst)
def load_single_audio(self, audio, fps, numFrames, audioAug=False, aug_audio=None):
if audioAug:
augType = random.randint(0, 1)
if augType == 1:
audio = overlap(audio, aug_audio)
else:
audio = audio
res = vggish_input.waveform_to_examples(audio, 16000, numFrames, fps, return_tensor=False)
return res
def load_visual_label_mask(self, videoName, entityName, target_ts, context_ts, visualAug=True):
faceFolderPath = os.path.join(self.visualPath, videoName, entityName)
faces = []
H = 112
if visualAug == True:
new = int(H * random.uniform(0.7, 1))
x, y = numpy.random.randint(0, H - new), numpy.random.randint(0, H - new)
M = cv2.getRotationMatrix2D((H / 2, H / 2), random.uniform(-15, 15), 1)
augType = random.choice(['orig', 'flip', 'crop', 'rotate'])
else:
augType = 'orig'
labels_dict = self.entity_data[videoName][entityName]
labels = numpy.zeros(len(target_ts))
mask = numpy.zeros(len(target_ts))
for i, time in enumerate(target_ts):
if time not in context_ts:
faces.append(numpy.zeros((H, H)))
else:
labels[i] = labels_dict[time]
mask[i] = 1
time = "%.2f" % float(time)
faceFile = os.path.join(faceFolderPath, str(time) + '.jpg')
face = cv2.imread(faceFile)
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
face = cv2.resize(face, (H, H))
if augType == 'orig':
faces.append(face)
elif augType == 'flip':
faces.append(cv2.flip(face, 1))
elif augType == 'crop':
faces.append(cv2.resize(face[y:y + new, x:x + new], (H, H)))
elif augType == 'rotate':
faces.append(cv2.warpAffine(face, M, (H, H)))
faces = numpy.array(faces)
return faces, labels, mask
def get_speaker_context(self, videoName, target_entity, all_ts, center_ts):
context_speakers = list(self.ts_to_entity[videoName][center_ts])
context = {}
chosen_speakers = []
context[target_entity] = all_ts
context_speakers.remove(target_entity)
num_frames = len(all_ts)
for candidate in context_speakers:
candidate_ts = self.entity_data[videoName][candidate]
shared_ts = set(all_ts).intersection(set(candidate_ts))
if (len(shared_ts) > (num_frames / 2)):
context[candidate] = shared_ts
chosen_speakers.append(candidate)
context_speakers = chosen_speakers
random.shuffle(context_speakers)
if not context_speakers:
context_speakers.insert(0, target_entity) # make sure is at 0
while len(context_speakers) < self.candidate_speakers:
context_speakers.append(random.choice(context_speakers))
elif len(context_speakers) < self.candidate_speakers:
context_speakers.insert(0, target_entity) # make sure is at 0
while len(context_speakers) < self.candidate_speakers:
context_speakers.append(random.choice(context_speakers[1:]))
else:
context_speakers.insert(0, target_entity) # make sure is at 0
context_speakers = context_speakers[:self.candidate_speakers]
assert set(context_speakers).issubset(set(list(context.keys()))), target_entity
assert target_entity in context_speakers, target_entity
return context_speakers, context
def __getitem__(self, index):
target_video = self.mixLst[index]
data = target_video.split('\t')
fps = float(data[2])
videoName = data[0][:11]
target_entity = data[0]
all_ts = list(self.entity_data[videoName][target_entity].keys())
numFrames = int(data[1])
assert numFrames == len(all_ts)
center_ts = all_ts[math.floor(numFrames / 2)]
# get context speakers which have more than half time overlapped with target speaker
context_speakers, context = self.get_speaker_context(videoName, target_entity, all_ts,
center_ts)
if self.cfg.TRAIN.AUDIO_AUG:
other_indices = list(range(0, index)) + list(range(index + 1, self.list_length))
augment_entity = self.mixLst[random.choice(other_indices)]
augment_data = augment_entity.split('\t')
augment_entity = augment_data[0]
augment_videoname = augment_data[0][:11]
aug_sr, aug_audio = wavfile.read(
os.path.join(self.audioPath, augment_videoname, augment_entity + '.wav'))
else:
aug_audio = None
audio_path = os.path.join(self.audioPath, videoName, target_entity + '.wav')
sr, audio = wavfile.read(os.path.join(self.audioPath, videoName, target_entity + '.wav'))
audio = self.load_single_audio(audio,
fps,
numFrames,
audioAug=self.cfg.TRAIN.AUDIO_AUG,
aug_audio=aug_audio)
visualFeatures, labels, masks = [], [], []
# target_label = list(self.entity_data[videoName][target_entity].values())
visual, target_labels, target_masks = self.load_visual_label_mask(
videoName, target_entity, all_ts, all_ts)
for idx, context_entity in enumerate(context_speakers):
if context_entity == target_entity:
label = target_labels
visualfeat = visual
mask = target_masks
else:
visualfeat, label, mask = self.load_visual_label_mask(videoName, context_entity,
all_ts,
context[context_entity])
visualFeatures.append(visualfeat)
labels.append(label)
masks.append(mask)
audio = torch.FloatTensor(audio)[None, :, :]
visualFeatures = torch.FloatTensor(numpy.array(visualFeatures))
audio_t = audio.shape[1]
video_t = visualFeatures.shape[1]
if audio_t != video_t * 4:
print(visualFeatures.shape, audio.shape, videoName, target_entity, numFrames)
labels = torch.LongTensor(numpy.array(labels))
masks = torch.LongTensor(numpy.array(masks))
return audio, visualFeatures, labels, masks
def __len__(self):
return len(self.mixLst)
class val_loader(object):
def __init__(self, cfg, trialFileName, audioPath, visualPath, num_speakers):
self.cfg = cfg
self.audioPath = audioPath
self.visualPath = visualPath
self.candidate_speakers = num_speakers
self.path = os.path.join(cfg.DATA.dataPathAVA, "csv")
self.entity_data = json.load(open(os.path.join(self.path, 'val_entity.json')))
self.ts_to_entity = json.load(open(os.path.join(self.path, 'val_ts.json')))
self.mixLst = open(trialFileName).read().splitlines()
def load_single_audio(self, audio, fps, numFrames, audioAug=False, aug_audio=None):
res = vggish_input.waveform_to_examples(audio, 16000, numFrames, fps, return_tensor=False)
return res
def load_visual_label_mask(self, videoName, entityName, target_ts, context_ts):
faceFolderPath = os.path.join(self.visualPath, videoName, entityName)
faces = []
H = 112
labels_dict = self.entity_data[videoName][entityName]
labels = numpy.zeros(len(target_ts))
mask = numpy.zeros(len(target_ts))
for i, time in enumerate(target_ts):
if time not in context_ts:
faces.append(numpy.zeros((H, H)))
else:
labels[i] = labels_dict[time]
mask[i] = 1
time = "%.2f" % float(time)
faceFile = os.path.join(faceFolderPath, str(time) + '.jpg')
face = cv2.imread(faceFile)
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
face = cv2.resize(face, (H, H))
faces.append(face)
faces = numpy.array(faces)
return faces, labels, mask
def get_speaker_context(self, videoName, target_entity, all_ts, center_ts):
context_speakers = list(self.ts_to_entity[videoName][center_ts])
context = {}
chosen_speakers = []
context[target_entity] = all_ts
context_speakers.remove(target_entity)
num_frames = len(all_ts)
for candidate in context_speakers:
candidate_ts = self.entity_data[videoName][candidate]
shared_ts = set(all_ts).intersection(set(candidate_ts))
context[candidate] = shared_ts
chosen_speakers.append(candidate)
# if (len(shared_ts) > (num_frames / 2)):
# context[candidate] = shared_ts
# chosen_speakers.append(candidate)
context_speakers = chosen_speakers
random.shuffle(context_speakers)
if not context_speakers:
context_speakers.insert(0, target_entity) # make sure is at 0
while len(context_speakers) < self.candidate_speakers:
context_speakers.append(random.choice(context_speakers))
elif len(context_speakers) < self.candidate_speakers:
context_speakers.insert(0, target_entity) # make sure is at 0
while len(context_speakers) < self.candidate_speakers:
context_speakers.append(random.choice(context_speakers[1:]))
else:
context_speakers.insert(0, target_entity) # make sure is at 0
context_speakers = context_speakers[:self.candidate_speakers]
assert set(context_speakers).issubset(set(list(context.keys()))), target_entity
return context_speakers, context
def __getitem__(self, index):
target_video = self.mixLst[index]
data = target_video.split('\t')
fps = float(data[2])
videoName = data[0][:11]
target_entity = data[0]
all_ts = list(self.entity_data[videoName][target_entity].keys())
numFrames = int(data[1])
# print(numFrames, len(all_ts))
assert numFrames == len(all_ts)
center_ts = all_ts[math.floor(numFrames / 2)]
# get context speakers which have more than half time overlapped with target speaker
context_speakers, context = self.get_speaker_context(videoName, target_entity, all_ts,
center_ts)
sr, audio = wavfile.read(os.path.join(self.audioPath, videoName, target_entity + '.wav'))
audio = self.load_single_audio(audio, fps, numFrames, audioAug=False)
visualFeatures, labels, masks = [], [], []
# target_label = list(self.entity_data[videoName][target_entity].values())
target_visual, target_labels, target_masks = self.load_visual_label_mask(
videoName, target_entity, all_ts, all_ts)
for idx, context_entity in enumerate(context_speakers):
if context_entity == target_entity:
label = target_labels
visualfeat = target_visual
mask = target_masks
else:
visualfeat, label, mask = self.load_visual_label_mask(videoName, context_entity,
all_ts,
context[context_entity])
visualFeatures.append(visualfeat)
labels.append(label)
masks.append(mask)
audio = torch.FloatTensor(audio)[None, :, :]
visualFeatures = torch.FloatTensor(numpy.array(visualFeatures))
audio_t = audio.shape[1]
video_t = visualFeatures.shape[1]
if audio_t != video_t * 4:
print(visualFeatures.shape, audio.shape, videoName, target_entity, numFrames)
labels = torch.LongTensor(numpy.array(labels))
masks = torch.LongTensor(numpy.array(masks))
return audio, visualFeatures, labels, masks
def __len__(self):
return len(self.mixLst)