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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import torch
|
| 3 |
+
import os
|
| 4 |
+
import sys
|
| 5 |
+
import numpy as np
|
| 6 |
+
import soundfile as sf
|
| 7 |
+
import torch.utils.data as data
|
| 8 |
+
import librosa
|
| 9 |
+
import cv2 as cv
|
| 10 |
+
import tqdm
|
| 11 |
+
from tools import audioread, audiowrite, cal_SISNR, load_model
|
| 12 |
+
# import fast_bss_eval
|
| 13 |
+
import pdb
|
| 14 |
+
import math
|
| 15 |
+
import torch.nn.functional as F
|
| 16 |
+
import scipy.io.wavfile as wavfile
|
| 17 |
+
|
| 18 |
+
sys.path.append('/mnt/users/hccl.local/wwu/SEMO-621/data_preparation/')
|
| 19 |
+
from Visual_perturb import *
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class dataset_wwu_multispk(data.Dataset):
|
| 29 |
+
def __init__(self,partition
|
| 30 |
+
):
|
| 31 |
+
self.if_fixed =False
|
| 32 |
+
self.max_length=6
|
| 33 |
+
self.sampling_rate = 16000
|
| 34 |
+
self.four2six_length = False
|
| 35 |
+
self.minibatch =[]
|
| 36 |
+
self.audio_direc = '/mnt/users/hccl.local/wwu/Voxceleb2/muse/audio_clean/'
|
| 37 |
+
self.text_direc = '/home/export/base/sc100138/sc100138/online1/wenxuan_tse_data/audio_clean_text/'
|
| 38 |
+
self.visual_direc = '/mnt/users/hccl.local/wwu/Voxceleb2/origin/video/'
|
| 39 |
+
self.mixture_direc = '/mnt/Corpus-Upload/Voxceleb2/origin/mixture_3spk/'
|
| 40 |
+
|
| 41 |
+
self.partition = partition
|
| 42 |
+
self.mix_lst_path = '/mnt/users/hccl.local/wwu/mamba_may20/mixture_data_list_3mix.csv'
|
| 43 |
+
self.C=3
|
| 44 |
+
self.batch_size = 3
|
| 45 |
+
self.normMean = 0.4161
|
| 46 |
+
self.normStd = 0.1688
|
| 47 |
+
self.fps = 25
|
| 48 |
+
# import pdb;pdb.set_trace()
|
| 49 |
+
|
| 50 |
+
mix_lst=open(self.mix_lst_path).read().splitlines()
|
| 51 |
+
mix_lst=list(filter(lambda x: x.split(',')[0]==self.partition, mix_lst))[:]
|
| 52 |
+
# import pdb;pdb.set_trace()
|
| 53 |
+
assert (self.batch_size%self.C) == 0, "input batch_size should be multiples of mixture speakers"
|
| 54 |
+
|
| 55 |
+
self.batch_size = int(self.batch_size/self.C )
|
| 56 |
+
#sorted by mix_audio duration length!!!
|
| 57 |
+
# import pdb;pdb.set_trace()
|
| 58 |
+
# sorted_mix_lst = sorted(mix_lst, key=lambda data: float(data.split(',')[-1]), reverse=True)[:6]
|
| 59 |
+
sorted_mix_lst = sorted(mix_lst, key=lambda data: float(data.split(',')[-1]), reverse=True)
|
| 60 |
+
# import pdb;pdb.set_trace()
|
| 61 |
+
start = 0
|
| 62 |
+
while True:
|
| 63 |
+
end = min(len(sorted_mix_lst), start + self.batch_size)
|
| 64 |
+
self.minibatch.append(sorted_mix_lst[start:end])
|
| 65 |
+
if end == len(sorted_mix_lst):
|
| 66 |
+
break
|
| 67 |
+
start = end
|
| 68 |
+
def __len__(self):
|
| 69 |
+
return len(self.minibatch)
|
| 70 |
+
def _audio_norm(self,audio):
|
| 71 |
+
return np.divide(audio, np.max(np.abs(audio)))
|
| 72 |
+
def __getitem__(self, index):
|
| 73 |
+
# import pdb;pdb.set_trace()
|
| 74 |
+
batch_lst = self.minibatch[index]
|
| 75 |
+
# import pdb;pdb.set_trace()
|
| 76 |
+
min_length = self.max_length
|
| 77 |
+
for _ in range(len(batch_lst)):
|
| 78 |
+
if float(batch_lst[_].split(",")[-1]) < min_length:
|
| 79 |
+
min_length = float(batch_lst[_].split(",")[-1])
|
| 80 |
+
# import pdb;pdb.set_trace()
|
| 81 |
+
visuals=[]
|
| 82 |
+
audios = []
|
| 83 |
+
texts = []
|
| 84 |
+
file_base_path = []
|
| 85 |
+
mixtures = []
|
| 86 |
+
# import pdb;pdb.set_trace()
|
| 87 |
+
for line in batch_lst:
|
| 88 |
+
mixture_path=self.mixture_direc+self.partition+'/'+ line.replace(',','_').replace('/','_')+'.wav'
|
| 89 |
+
# import pdb;pdb.set_trace()
|
| 90 |
+
line=line.split(',')
|
| 91 |
+
for c in range(self.C):
|
| 92 |
+
# each line has 2 sample, batch =2, return same mixaudio, so return mix audio twice!!!!!!
|
| 93 |
+
_, mixture_speech = wavfile.read(mixture_path)
|
| 94 |
+
mix_audio_data = self._audio_norm(mixture_speech[:int(min_length * self.sampling_rate)])
|
| 95 |
+
# mix_log_feat = self.compute_logfilter(mix_audio_data)
|
| 96 |
+
# mixtures.append(mix_log_feat)
|
| 97 |
+
mixtures.append(mix_audio_data)
|
| 98 |
+
|
| 99 |
+
clean_speech_path=self.audio_direc+line[c*4+1]+'/'+line[c*4+2]+'/'+line[c*4+3]+'.wav'
|
| 100 |
+
file_base_path.append(line[c*4+1]+'/'+line[c*4+2]+'/'+line[c*4+3])
|
| 101 |
+
# import pdb;pdb.set_trace()
|
| 102 |
+
_, clean_speech = wavfile.read(clean_speech_path)
|
| 103 |
+
# print(len(clean_speech),clean_speech_path)
|
| 104 |
+
audio_data = self._audio_norm(clean_speech[:int(min_length * self.sampling_rate)])
|
| 105 |
+
# text_path = self.text_direc+line[c*4+1]+'/'+line[c*4+2]+'/'+line[c*4+3]+'.txt'
|
| 106 |
+
# with open(text_path, 'r', encoding='utf-8') as file:
|
| 107 |
+
# text = file.readlines()[0].split('\n')[0]
|
| 108 |
+
text = ''
|
| 109 |
+
# print(text)
|
| 110 |
+
# import pdb;pdb.set_trace()
|
| 111 |
+
|
| 112 |
+
# #print(audio_data)
|
| 113 |
+
# log_feat = self.compute_logfilter(audio_data)
|
| 114 |
+
audios.append(audio_data)
|
| 115 |
+
texts.append(text)
|
| 116 |
+
# audios.append([])
|
| 117 |
+
|
| 118 |
+
if self.partition != 'test':
|
| 119 |
+
visual_path = (
|
| 120 |
+
self.visual_direc
|
| 121 |
+
+ 'train'
|
| 122 |
+
+ "/"
|
| 123 |
+
+ line[2 + c * 4]
|
| 124 |
+
+ "/"
|
| 125 |
+
+ line[3 + c * 4]
|
| 126 |
+
+ ".mp4"
|
| 127 |
+
)
|
| 128 |
+
else:
|
| 129 |
+
|
| 130 |
+
visual_path = (
|
| 131 |
+
self.visual_direc
|
| 132 |
+
+ line[1 + c * 4]
|
| 133 |
+
+ "/"
|
| 134 |
+
+ line[2 + c * 4]
|
| 135 |
+
+ "/"
|
| 136 |
+
+ line[3 + c * 4]
|
| 137 |
+
+ ".mp4"
|
| 138 |
+
)
|
| 139 |
+
# import pdb;pdb.set_trace()
|
| 140 |
+
captureObj = cv.VideoCapture(visual_path)
|
| 141 |
+
|
| 142 |
+
# import pdb;pdb.set_trace()
|
| 143 |
+
roiSequence = []
|
| 144 |
+
clean_roiSequence = []
|
| 145 |
+
roiSize = 112
|
| 146 |
+
start = 0
|
| 147 |
+
while captureObj.isOpened():
|
| 148 |
+
ret, frame = captureObj.read()
|
| 149 |
+
if ret == True:
|
| 150 |
+
grayed = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
|
| 151 |
+
grayed = grayed / 255
|
| 152 |
+
grayed = cv.resize(grayed, (roiSize * 2, roiSize * 2))
|
| 153 |
+
roi = grayed[
|
| 154 |
+
int(roiSize - (roiSize / 2)) : int(
|
| 155 |
+
roiSize + (roiSize / 2)
|
| 156 |
+
),
|
| 157 |
+
int(roiSize - (roiSize / 2)) : int(
|
| 158 |
+
roiSize + (roiSize / 2)
|
| 159 |
+
),
|
| 160 |
+
]
|
| 161 |
+
clean_roiSequence.append(roi)
|
| 162 |
+
else:
|
| 163 |
+
break
|
| 164 |
+
captureObj.release()
|
| 165 |
+
# import pdb;pdb.set_trace()
|
| 166 |
+
clean_visual = np.asarray(clean_roiSequence)
|
| 167 |
+
clean_visual = clean_visual[0 : int(min_length * self.fps), ...]
|
| 168 |
+
clean_visual = (clean_visual - self.normMean) / self.normStd
|
| 169 |
+
if clean_visual.shape[0] < int(min_length * self.fps):
|
| 170 |
+
clean_visual = np.pad(
|
| 171 |
+
clean_visual,
|
| 172 |
+
(
|
| 173 |
+
(
|
| 174 |
+
0,
|
| 175 |
+
int(min_length * self.fps)
|
| 176 |
+
- clean_visual.shape[0],
|
| 177 |
+
),
|
| 178 |
+
(0, 0),
|
| 179 |
+
(0, 0),
|
| 180 |
+
),
|
| 181 |
+
mode="edge",
|
| 182 |
+
)
|
| 183 |
+
visuals.append(clean_visual)
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
assert np.asarray(mixtures).shape == np.asarray(audios).shape
|
| 190 |
+
# import pdb;pdb.set_trace()
|
| 191 |
+
return np.asarray(mixtures),np.asarray(audios),np.asarray(visuals), texts,file_base_path
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
def main(args):
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
# speaker id assignment
|
| 201 |
+
mix_lst = open(args.mix_lst_path).read().splitlines()
|
| 202 |
+
train_lst = list(filter(lambda x: x.split(",")[0] == "test", mix_lst))
|
| 203 |
+
IDs = 0
|
| 204 |
+
speaker_dict = {}
|
| 205 |
+
for line in train_lst:
|
| 206 |
+
for i in range(2):
|
| 207 |
+
ID = line.split(",")[i * 4 + 2]
|
| 208 |
+
if ID not in speaker_dict:
|
| 209 |
+
speaker_dict[ID] = IDs
|
| 210 |
+
IDs += 1
|
| 211 |
+
args.speaker_dict = speaker_dict
|
| 212 |
+
args.speakers = len(speaker_dict)
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
datasets = dataset_wwu_multispk('test')
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
|