-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataloader.py
313 lines (276 loc) · 12.8 KB
/
dataloader.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
# -*- coding: utf-8 -*-
# modified from:
# Author: Yuan Gong (MIT)
# with some functions borrowed from https://github.com/yuangongnd/cav-mae
import csv
import json
import os.path
import torchaudio
import numpy as np
import torch
import torch.nn.functional
from torch.utils.data import Dataset
import random
import torchvision.transforms as T
from PIL import Image
import PIL
def make_index_dict(label_csv):
index_lookup = {}
with open(label_csv, 'r') as f:
csv_reader = csv.DictReader(f)
line_count = 0
for row in csv_reader:
index_lookup[row['mid']] = row['index']
line_count += 1
return index_lookup
def make_name_dict(label_csv):
name_lookup = {}
with open(label_csv, 'r') as f:
csv_reader = csv.DictReader(f)
line_count = 0
for row in csv_reader:
name_lookup[row['index']] = row['display_name']
line_count += 1
return name_lookup
def lookup_list(index_list, label_csv):
label_list = []
table = make_name_dict(label_csv)
for item in index_list:
label_list.append(table[item])
return label_list
def preemphasis(signal,coeff=0.97):
"""perform preemphasis on the input signal.
:param signal: The signal to filter.
:param coeff: The preemphasis coefficient. 0 is none, default 0.97.
:returns: the filtered signal.
"""
return np.append(signal[0],signal[1:]-coeff*signal[:-1])
class AudiosetDataset(Dataset):
def __init__(self, dataset_json_file, audio_conf, label_csv=None, rt_idx=False):
"""
Dataset that manages audio recordings
:param audio_conf: Dictionary containing the audio loading and preprocessing settings
:param dataset_json_file
"""
self.datapath = dataset_json_file
with open(dataset_json_file, 'r') as fp:
data_json = json.load(fp)
self.data = data_json['data']
self.data = self.pro_data(self.data)
print('Dataset has {:d} samples'.format(self.data.shape[0]))
self.num_samples = self.data.shape[0]
self.audio_conf = audio_conf
self.label_smooth = self.audio_conf.get('label_smooth', 0.0)
# print('Using Label Smoothing: ' + str(self.label_smooth))
self.melbins = self.audio_conf.get('num_mel_bins')
self.freqm = self.audio_conf.get('freqm', 0)
self.timem = self.audio_conf.get('timem', 0)
# print('now using following mask: {:d} freq, {:d} time'.format(self.audio_conf.get('freqm'), self.audio_conf.get('timem')))
self.mixup = self.audio_conf.get('mixup', 0)
# print('now using mix-up with rate {:f}'.format(self.mixup))
self.dataset = self.audio_conf.get('dataset')
print('now process ' + self.dataset)
# dataset spectrogram mean and std, used to normalize the input
self.norm_mean = self.audio_conf.get('mean')
self.norm_std = self.audio_conf.get('std')
# skip_norm is a flag that if you want to skip normalization to compute the normalization stats using src/get_norm_stats.py, if Ture, input normalization will be skipped for correctly calculating the stats.
# set it as True ONLY when you are getting the normalization stats.
self.skip_norm = self.audio_conf.get('skip_norm') if self.audio_conf.get('skip_norm') else False
# if self.skip_norm:
# print('now skip normalization (use it ONLY when you are computing the normalization stats).')
# else:
# print('use dataset mean {:.3f} and std {:.3f} to normalize the input.'.format(self.norm_mean, self.norm_std))
# if add noise for data augmentation
self.noise = self.audio_conf.get('noise', False)
# if self.noise == True:
# print('now use noise augmentation')
# else:
# print('not use noise augmentation')
self.index_dict = make_index_dict(label_csv)
self.label_num = len(self.index_dict)
# print('number of classes is {:d}'.format(self.label_num))
self.target_length = self.audio_conf.get('target_length')
# train or eval
self.mode = self.audio_conf.get('mode')
# print('now in {:s} mode.'.format(self.mode))
# set the frame to use in the eval mode, default value for training is -1 which means random frame
self.frame_use = self.audio_conf.get('frame_use', -1)
# by default, 10 frames are used
self.total_frame = self.audio_conf.get('total_frame', 10)
# print('now use frame {:d} from total {:d} frames'.format(self.frame_use, self.total_frame))
# by default, all models use 224*224, other resolutions are not tested
self.im_res = self.audio_conf.get('im_res', 224)
self.rt_idx = rt_idx
# print('now using {:d} * {:d} image input'.format(self.im_res, self.im_res))
self.preprocess = T.Compose([
T.Resize(self.im_res, interpolation=PIL.Image.BICUBIC),
T.CenterCrop(self.im_res),
T.ToTensor(),
T.Normalize(
mean=[0.4850, 0.4560, 0.4060],
std=[0.2290, 0.2240, 0.2250]
)])
# change python list to numpy array to avoid memory leak.
def pro_data(self, data_json):
for i in range(len(data_json)):
data_json[i] = [data_json[i]['wav'], data_json[i]['labels'], data_json[i]['video_id'], data_json[i]['video_path']]
data_np = np.array(data_json, dtype=str)
return data_np
# reformat numpy data to original json format, make it compatible with old code
def decode_data(self, np_data):
datum = {}
datum['wav'] = np_data[0]
datum['labels'] = np_data[1]
datum['video_id'] = np_data[2]
datum['video_path'] = np_data[3]
return datum
def get_image(self, filename, filename2=None, mix_lambda=1):
if filename2 == None:
img = Image.open(filename)
image_tensor = self.preprocess(img)
return image_tensor
else:
img1 = Image.open(filename)
image_tensor1 = self.preprocess(img1)
img2 = Image.open(filename2)
image_tensor2 = self.preprocess(img2)
image_tensor = mix_lambda * image_tensor1 + (1 - mix_lambda) * image_tensor2
return image_tensor
def _wav2fbank(self, filename, filename2=None, mix_lambda=-1):
# no mixup
if filename2 == None:
waveform, sr = torchaudio.load(filename)
waveform = waveform - waveform.mean()
# mixup
else:
waveform1, sr = torchaudio.load(filename)
waveform2, _ = torchaudio.load(filename2)
waveform1 = waveform1 - waveform1.mean()
waveform2 = waveform2 - waveform2.mean()
if waveform1.shape[1] != waveform2.shape[1]:
if waveform1.shape[1] > waveform2.shape[1]:
# padding
temp_wav = torch.zeros(1, waveform1.shape[1])
temp_wav[0, 0:waveform2.shape[1]] = waveform2
waveform2 = temp_wav
else:
# cutting
waveform2 = waveform2[0, 0:waveform1.shape[1]]
mix_waveform = mix_lambda * waveform1 + (1 - mix_lambda) * waveform2
waveform = mix_waveform - mix_waveform.mean()
try:
fbank = torchaudio.compliance.kaldi.fbank(waveform, htk_compat=True, sample_frequency=sr, use_energy=False, window_type='hanning', num_mel_bins=self.melbins, dither=0.0, frame_shift=10)
except:
fbank = torch.zeros([512, 128]) + 0.01
print('there is a loading error in file {}'.format(filename))
target_length = self.target_length
n_frames = fbank.shape[0]
p = target_length - n_frames
# cut and pad
if p > 0:
m = torch.nn.ZeroPad2d((0, 0, 0, p))
fbank = m(fbank)
elif p < 0:
fbank = fbank[0:target_length, :]
return fbank
def randselect_img(self, video_id, video_path):
if self.mode == 'eval':
# if not specified, use the middle frame
if self.frame_use == -1:
frame_idx = int((self.total_frame) / 2)
else:
frame_idx = self.frame_use
else:
frame_idx = random.randint(0, 9)
while os.path.exists(video_path + '/frame_' + str(frame_idx) + '/' + video_id + '.jpg') == False and frame_idx >= 1:
print(video_path + '/frame_' + str(frame_idx) + '/' + video_id + '.jpg')
print('frame {:s} {:d} does not exist'.format(video_id, frame_idx))
frame_idx -= 1
exit()
out_path = video_path + '/frame_' + str(frame_idx) + '/' + video_id + '.jpg'
#print(out_path)
return out_path
def __getitem__(self, index):
if random.random() < self.mixup:
datum = self.data[index]
datum = self.decode_data(datum)
mix_sample_idx = random.randint(0, self.num_samples-1)
mix_datum = self.data[mix_sample_idx]
mix_datum = self.decode_data(mix_datum)
# get the mixed fbank
mix_lambda = np.random.beta(10, 10)
# try:
fbank = self._wav2fbank(datum['wav'], mix_datum['wav'], mix_lambda)
# except:
# fbank = torch.zeros([self.target_length, 128]) + 0.01
# print('there is an error in loading audio')
try:
image = self.get_image(self.randselect_img(datum['video_id'], datum['video_path']), self.randselect_img(mix_datum['video_id'], datum['video_path']), mix_lambda)
except:
image = torch.zeros([3, self.im_res, self.im_res]) + 0.01
print('there is an error in loading image')
label_indices = np.zeros(self.label_num) + (self.label_smooth / self.label_num)
for label_str in datum['labels'].split(','):
label_indices[int(self.index_dict[label_str])] += mix_lambda * (1.0 - self.label_smooth)
for label_str in mix_datum['labels'].split(','):
try:
label_indices[int(self.index_dict[label_str])] += (1.0 - mix_lambda) * (1.0 - self.label_smooth)
except:
print(label_str)
print(self.index_dict)
print(111)
exit()
label_indices = torch.FloatTensor(label_indices)
else:
datum = self.data[index]
# print(datum)
datum = self.decode_data(datum)
# label smooth for negative samples, epsilon/label_num
label_indices = np.zeros(self.label_num) + (self.label_smooth / self.label_num)
# try:
fbank = self._wav2fbank(datum['wav'], None, 0)
# except:
# fbank = torch.zeros([self.target_length, 128]) + 0.01
# print('there is an error in loading audio')
try:
image = self.get_image(self.randselect_img(datum['video_id'], datum['video_path']), None, 0)
except:
image = torch.zeros([3, self.im_res, self.im_res]) + 0.01
print('there is an error in loading image')
for label_str in datum['labels'].split(','):
try:
label_indices[int(self.index_dict[label_str])] = 1.0 - self.label_smooth
except:
print(datum)
print(label_str)
# print(self.index_dict)
print(222)
exit()
label_indices = torch.FloatTensor(label_indices)
# SpecAug, not do for eval set
freqm = torchaudio.transforms.FrequencyMasking(self.freqm)
timem = torchaudio.transforms.TimeMasking(self.timem)
fbank = torch.transpose(fbank, 0, 1)
fbank = fbank.unsqueeze(0)
if self.freqm != 0:
fbank = freqm(fbank)
if self.timem != 0:
fbank = timem(fbank)
fbank = fbank.squeeze(0)
fbank = torch.transpose(fbank, 0, 1)
# normalize the input for both training and test
if self.skip_norm == False:
fbank = (fbank - self.norm_mean) / (self.norm_std)
# skip normalization the input ONLY when you are trying to get the normalization stats.
else:
pass
if self.noise == True:
fbank = fbank + torch.rand(fbank.shape[0], fbank.shape[1]) * np.random.rand() / 10
fbank = torch.roll(fbank, np.random.randint(-self.target_length, self.target_length), 0)
# fbank shape is [time_frame_num, frequency_bins], e.g., [1024, 128]
if not self.rt_idx:
return fbank, image, label_indices
else:
return fbank, image, label_indices, index
def __len__(self):
return self.num_samples