repo
stringlengths
2
99
file
stringlengths
13
225
code
stringlengths
0
18.3M
file_length
int64
0
18.3M
avg_line_length
float64
0
1.36M
max_line_length
int64
0
4.26M
extension_type
stringclasses
1 value
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-fixed-sig/01/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.a_softmax as nii_a_softmax import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) ## FOR MODEL class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 1 # output class self.v_out_class = 1 self.m_transform = [] self.m_output_act = [] self.m_frontend = [] #self.m_a_softmax = [] for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (self.spec_fb_dim // 16) * 32, 160), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(80, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_emb = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
15,079
34.233645
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-fixed-sig/06/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.a_softmax as nii_a_softmax import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) ## FOR MODEL class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 1 # output class self.v_out_class = 1 self.m_transform = [] self.m_output_act = [] self.m_frontend = [] #self.m_a_softmax = [] for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (self.spec_fb_dim // 16) * 32, 160), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(80, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_emb = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
15,079
34.233645
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-fixed-sig/03/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.a_softmax as nii_a_softmax import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) ## FOR MODEL class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 1 # output class self.v_out_class = 1 self.m_transform = [] self.m_output_act = [] self.m_frontend = [] #self.m_a_softmax = [] for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (self.spec_fb_dim // 16) * 32, 160), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(80, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_emb = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
15,079
34.233645
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-fixed-sig/02/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.a_softmax as nii_a_softmax import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) ## FOR MODEL class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 1 # output class self.v_out_class = 1 self.m_transform = [] self.m_output_act = [] self.m_frontend = [] #self.m_a_softmax = [] for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (self.spec_fb_dim // 16) * 32, 160), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(80, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_emb = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
15,079
34.233645
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-fixed-p2s/05/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.p2sgrad as nii_p2sgrad import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) ## a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output class self.v_out_class = 2 self.m_transform = [] self.m_output_act = [] self.m_frontend = [] self.m_angle = [] for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (lfb_dim // 16) * 32, 160), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(80, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_angle = torch_nn.ModuleList(self.m_angle) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output embedding from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_score = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros([batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
15,262
34.331019
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-fixed-p2s/04/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.p2sgrad as nii_p2sgrad import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) ## a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output class self.v_out_class = 2 self.m_transform = [] self.m_output_act = [] self.m_frontend = [] self.m_angle = [] for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (lfb_dim // 16) * 32, 160), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(80, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_angle = torch_nn.ModuleList(self.m_angle) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output embedding from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_score = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros([batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
15,262
34.331019
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-fixed-p2s/01/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.p2sgrad as nii_p2sgrad import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) ## a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output class self.v_out_class = 2 self.m_transform = [] self.m_output_act = [] self.m_frontend = [] self.m_angle = [] for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (lfb_dim // 16) * 32, 160), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(80, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_angle = torch_nn.ModuleList(self.m_angle) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output embedding from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_score = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros([batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
15,262
34.331019
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-fixed-p2s/06/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.p2sgrad as nii_p2sgrad import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) ## a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output class self.v_out_class = 2 self.m_transform = [] self.m_output_act = [] self.m_frontend = [] self.m_angle = [] for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (lfb_dim // 16) * 32, 160), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(80, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_angle = torch_nn.ModuleList(self.m_angle) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output embedding from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_score = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros([batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
15,262
34.331019
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-fixed-p2s/03/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.p2sgrad as nii_p2sgrad import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) ## a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output class self.v_out_class = 2 self.m_transform = [] self.m_output_act = [] self.m_frontend = [] self.m_angle = [] for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (lfb_dim // 16) * 32, 160), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(80, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_angle = torch_nn.ModuleList(self.m_angle) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output embedding from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_score = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros([batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
15,262
34.331019
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-fixed-p2s/02/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.p2sgrad as nii_p2sgrad import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) ## a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output class self.v_out_class = 2 self.m_transform = [] self.m_output_act = [] self.m_frontend = [] self.m_angle = [] for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (lfb_dim // 16) * 32, 160), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(80, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_angle = torch_nn.ModuleList(self.m_angle) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output embedding from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_score = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros([batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
15,262
34.331019
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-lstmsum-oc/05/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.oc_softmax as nii_ocsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32), nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_ocsoftmax.OCAngleLayer(self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return out_score_neg else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_ocsoftmax.OCSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,561
33.970787
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-lstmsum-oc/04/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.oc_softmax as nii_ocsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32), nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_ocsoftmax.OCAngleLayer(self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return out_score_neg else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_ocsoftmax.OCSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,561
33.970787
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-lstmsum-oc/01/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.oc_softmax as nii_ocsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32), nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_ocsoftmax.OCAngleLayer(self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return out_score_neg else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_ocsoftmax.OCSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,561
33.970787
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-lstmsum-oc/06/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.oc_softmax as nii_ocsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32), nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_ocsoftmax.OCAngleLayer(self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return out_score_neg else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_ocsoftmax.OCSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,561
33.970787
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-lstmsum-oc/03/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.oc_softmax as nii_ocsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32), nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_ocsoftmax.OCAngleLayer(self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return out_score_neg else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_ocsoftmax.OCSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,561
33.970787
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-lstmsum-oc/02/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.oc_softmax as nii_ocsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32), nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_ocsoftmax.OCAngleLayer(self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return out_score_neg else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_ocsoftmax.OCSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,561
33.970787
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-p2s/05/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.p2sgrad as nii_p2sgrad import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros( [batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,863
33.487239
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-p2s/04/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.p2sgrad as nii_p2sgrad import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros( [batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,863
33.487239
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-p2s/01/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.p2sgrad as nii_p2sgrad import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros( [batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,863
33.487239
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-p2s/06/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.p2sgrad as nii_p2sgrad import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros( [batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,863
33.487239
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-p2s/03/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.p2sgrad as nii_p2sgrad import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros( [batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,863
33.487239
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-p2s/02/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.p2sgrad as nii_p2sgrad import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros( [batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,863
33.487239
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/rawnet2/05/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torchaudio import torch.nn.functional as torch_nn_func import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import sandbox.eval_asvspoof as nii_asvspoof import sandbox.block_rawnet as nii_rawnet __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, prj_conf, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std( in_dim,out_dim,args, prj_conf, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_pth = prj_conf.optional_argument[0] self.protocol_parser = nii_asvspoof.protocol_parse_general(protocol_pth) # Model self.m_filter_num = 20 self.m_filter_len = 1025 self.m_res_ch_1 = 128 self.m_res_ch_2 = 512 self.m_gru_node = 256 self.m_gru_layer = 3 self.m_emb_dim = 64 self.m_num_class = 2 self.m_rawnet = nii_rawnet.RawNet( self.m_filter_num, self.m_filter_len, in_dim, prj_conf.wav_samp_rate, self.m_res_ch_1, self.m_res_ch_2, self.m_gru_node, self.m_gru_layer, self.m_emb_dim, self.m_num_class ) # segment length = self.m_seg_len = prj_conf.truncate_seq if self.m_seg_len is None: # use default segment length self.m_seg_len = 64600 print("RawNet uses a default segment length {:d}".format( self.m_seg_len)) # done return def prepare_mean_std(self, in_dim, out_dim, args, prj_conf, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: # log prob logprob = self.m_rawnet.forward(x) # target target = self._get_target(filenames) target_vec = torch.tensor( target, device=x.device, dtype=torch.long) return [logprob, target_vec, True] else: if x.shape[1] < self.m_seg_len: # too short, no need to split scores = self.m_rawnet.inference(x) else: # split input into segments num_seq = x.shape[1] // self.m_seg_len scores = [] for idx in range(num_seq): stime = idx * self.m_seg_len etime = idx * self.m_seg_len + self.m_seg_len scores.append(self.m_rawnet.inference(x[:, stime:etime])) # average scores # (batch, num_classes, seg_num) -> (batch, num_classes) scores = torch.stack(scores, dim=2).mean(dim=-1) targets = self._get_target(filenames) for filename, target, score in zip(filenames, targets, scores): print("Output, %s, %d, %f" % (filename, target, score[-1])) return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.CrossEntropyLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
6,838
32.038647
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/rawnet2/04/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torchaudio import torch.nn.functional as torch_nn_func import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import sandbox.eval_asvspoof as nii_asvspoof import sandbox.block_rawnet as nii_rawnet __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, prj_conf, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std( in_dim,out_dim,args, prj_conf, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_pth = prj_conf.optional_argument[0] self.protocol_parser = nii_asvspoof.protocol_parse_general(protocol_pth) # Model self.m_filter_num = 20 self.m_filter_len = 1025 self.m_res_ch_1 = 128 self.m_res_ch_2 = 512 self.m_gru_node = 256 self.m_gru_layer = 3 self.m_emb_dim = 64 self.m_num_class = 2 self.m_rawnet = nii_rawnet.RawNet( self.m_filter_num, self.m_filter_len, in_dim, prj_conf.wav_samp_rate, self.m_res_ch_1, self.m_res_ch_2, self.m_gru_node, self.m_gru_layer, self.m_emb_dim, self.m_num_class ) # segment length = self.m_seg_len = prj_conf.truncate_seq if self.m_seg_len is None: # use default segment length self.m_seg_len = 64600 print("RawNet uses a default segment length {:d}".format( self.m_seg_len)) # done return def prepare_mean_std(self, in_dim, out_dim, args, prj_conf, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: # log prob logprob = self.m_rawnet.forward(x) # target target = self._get_target(filenames) target_vec = torch.tensor( target, device=x.device, dtype=torch.long) return [logprob, target_vec, True] else: if x.shape[1] < self.m_seg_len: # too short, no need to split scores = self.m_rawnet.inference(x) else: # split input into segments num_seq = x.shape[1] // self.m_seg_len scores = [] for idx in range(num_seq): stime = idx * self.m_seg_len etime = idx * self.m_seg_len + self.m_seg_len scores.append(self.m_rawnet.inference(x[:, stime:etime])) # average scores # (batch, num_classes, seg_num) -> (batch, num_classes) scores = torch.stack(scores, dim=2).mean(dim=-1) targets = self._get_target(filenames) for filename, target, score in zip(filenames, targets, scores): print("Output, %s, %d, %f" % (filename, target, score[-1])) return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.CrossEntropyLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
6,838
32.038647
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/rawnet2/01/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torchaudio import torch.nn.functional as torch_nn_func import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import sandbox.eval_asvspoof as nii_asvspoof import sandbox.block_rawnet as nii_rawnet __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, prj_conf, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std( in_dim,out_dim,args, prj_conf, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_pth = prj_conf.optional_argument[0] self.protocol_parser = nii_asvspoof.protocol_parse_general(protocol_pth) # Model self.m_filter_num = 20 self.m_filter_len = 1025 self.m_res_ch_1 = 128 self.m_res_ch_2 = 512 self.m_gru_node = 256 self.m_gru_layer = 3 self.m_emb_dim = 64 self.m_num_class = 2 self.m_rawnet = nii_rawnet.RawNet( self.m_filter_num, self.m_filter_len, in_dim, prj_conf.wav_samp_rate, self.m_res_ch_1, self.m_res_ch_2, self.m_gru_node, self.m_gru_layer, self.m_emb_dim, self.m_num_class ) # segment length = self.m_seg_len = prj_conf.truncate_seq if self.m_seg_len is None: # use default segment length self.m_seg_len = 64600 print("RawNet uses a default segment length {:d}".format( self.m_seg_len)) # done return def prepare_mean_std(self, in_dim, out_dim, args, prj_conf, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: # log prob logprob = self.m_rawnet.forward(x) # target target = self._get_target(filenames) target_vec = torch.tensor( target, device=x.device, dtype=torch.long) return [logprob, target_vec, True] else: if x.shape[1] < self.m_seg_len: # too short, no need to split scores = self.m_rawnet.inference(x) else: # split input into segments num_seq = x.shape[1] // self.m_seg_len scores = [] for idx in range(num_seq): stime = idx * self.m_seg_len etime = idx * self.m_seg_len + self.m_seg_len scores.append(self.m_rawnet.inference(x[:, stime:etime])) # average scores # (batch, num_classes, seg_num) -> (batch, num_classes) scores = torch.stack(scores, dim=2).mean(dim=-1) targets = self._get_target(filenames) for filename, target, score in zip(filenames, targets, scores): print("Output, %s, %d, %f" % (filename, target, score[-1])) return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.CrossEntropyLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
6,838
32.038647
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/rawnet2/06/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torchaudio import torch.nn.functional as torch_nn_func import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import sandbox.eval_asvspoof as nii_asvspoof import sandbox.block_rawnet as nii_rawnet __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, prj_conf, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std( in_dim,out_dim,args, prj_conf, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_pth = prj_conf.optional_argument[0] self.protocol_parser = nii_asvspoof.protocol_parse_general(protocol_pth) # Model self.m_filter_num = 20 self.m_filter_len = 1025 self.m_res_ch_1 = 128 self.m_res_ch_2 = 512 self.m_gru_node = 256 self.m_gru_layer = 3 self.m_emb_dim = 64 self.m_num_class = 2 self.m_rawnet = nii_rawnet.RawNet( self.m_filter_num, self.m_filter_len, in_dim, prj_conf.wav_samp_rate, self.m_res_ch_1, self.m_res_ch_2, self.m_gru_node, self.m_gru_layer, self.m_emb_dim, self.m_num_class ) # segment length = self.m_seg_len = prj_conf.truncate_seq if self.m_seg_len is None: # use default segment length self.m_seg_len = 64600 print("RawNet uses a default segment length {:d}".format( self.m_seg_len)) # done return def prepare_mean_std(self, in_dim, out_dim, args, prj_conf, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: # log prob logprob = self.m_rawnet.forward(x) # target target = self._get_target(filenames) target_vec = torch.tensor( target, device=x.device, dtype=torch.long) return [logprob, target_vec, True] else: if x.shape[1] < self.m_seg_len: # too short, no need to split scores = self.m_rawnet.inference(x) else: # split input into segments num_seq = x.shape[1] // self.m_seg_len scores = [] for idx in range(num_seq): stime = idx * self.m_seg_len etime = idx * self.m_seg_len + self.m_seg_len scores.append(self.m_rawnet.inference(x[:, stime:etime])) # average scores # (batch, num_classes, seg_num) -> (batch, num_classes) scores = torch.stack(scores, dim=2).mean(dim=-1) targets = self._get_target(filenames) for filename, target, score in zip(filenames, targets, scores): print("Output, %s, %d, %f" % (filename, target, score[-1])) return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.CrossEntropyLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
6,838
32.038647
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/rawnet2/03/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torchaudio import torch.nn.functional as torch_nn_func import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import sandbox.eval_asvspoof as nii_asvspoof import sandbox.block_rawnet as nii_rawnet __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, prj_conf, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std( in_dim,out_dim,args, prj_conf, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_pth = prj_conf.optional_argument[0] self.protocol_parser = nii_asvspoof.protocol_parse_general(protocol_pth) # Model self.m_filter_num = 20 self.m_filter_len = 1025 self.m_res_ch_1 = 128 self.m_res_ch_2 = 512 self.m_gru_node = 256 self.m_gru_layer = 3 self.m_emb_dim = 64 self.m_num_class = 2 self.m_rawnet = nii_rawnet.RawNet( self.m_filter_num, self.m_filter_len, in_dim, prj_conf.wav_samp_rate, self.m_res_ch_1, self.m_res_ch_2, self.m_gru_node, self.m_gru_layer, self.m_emb_dim, self.m_num_class ) # segment length = self.m_seg_len = prj_conf.truncate_seq if self.m_seg_len is None: # use default segment length self.m_seg_len = 64600 print("RawNet uses a default segment length {:d}".format( self.m_seg_len)) # done return def prepare_mean_std(self, in_dim, out_dim, args, prj_conf, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: # log prob logprob = self.m_rawnet.forward(x) # target target = self._get_target(filenames) target_vec = torch.tensor( target, device=x.device, dtype=torch.long) return [logprob, target_vec, True] else: if x.shape[1] < self.m_seg_len: # too short, no need to split scores = self.m_rawnet.inference(x) else: # split input into segments num_seq = x.shape[1] // self.m_seg_len scores = [] for idx in range(num_seq): stime = idx * self.m_seg_len etime = idx * self.m_seg_len + self.m_seg_len scores.append(self.m_rawnet.inference(x[:, stime:etime])) # average scores # (batch, num_classes, seg_num) -> (batch, num_classes) scores = torch.stack(scores, dim=2).mean(dim=-1) targets = self._get_target(filenames) for filename, target, score in zip(filenames, targets, scores): print("Output, %s, %d, %f" % (filename, target, score[-1])) return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.CrossEntropyLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
6,838
32.038647
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/rawnet2/02/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torchaudio import torch.nn.functional as torch_nn_func import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import sandbox.eval_asvspoof as nii_asvspoof import sandbox.block_rawnet as nii_rawnet __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, prj_conf, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std( in_dim,out_dim,args, prj_conf, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_pth = prj_conf.optional_argument[0] self.protocol_parser = nii_asvspoof.protocol_parse_general(protocol_pth) # Model self.m_filter_num = 20 self.m_filter_len = 1025 self.m_res_ch_1 = 128 self.m_res_ch_2 = 512 self.m_gru_node = 256 self.m_gru_layer = 3 self.m_emb_dim = 64 self.m_num_class = 2 self.m_rawnet = nii_rawnet.RawNet( self.m_filter_num, self.m_filter_len, in_dim, prj_conf.wav_samp_rate, self.m_res_ch_1, self.m_res_ch_2, self.m_gru_node, self.m_gru_layer, self.m_emb_dim, self.m_num_class ) # segment length = self.m_seg_len = prj_conf.truncate_seq if self.m_seg_len is None: # use default segment length self.m_seg_len = 64600 print("RawNet uses a default segment length {:d}".format( self.m_seg_len)) # done return def prepare_mean_std(self, in_dim, out_dim, args, prj_conf, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: # log prob logprob = self.m_rawnet.forward(x) # target target = self._get_target(filenames) target_vec = torch.tensor( target, device=x.device, dtype=torch.long) return [logprob, target_vec, True] else: if x.shape[1] < self.m_seg_len: # too short, no need to split scores = self.m_rawnet.inference(x) else: # split input into segments num_seq = x.shape[1] // self.m_seg_len scores = [] for idx in range(num_seq): stime = idx * self.m_seg_len etime = idx * self.m_seg_len + self.m_seg_len scores.append(self.m_rawnet.inference(x[:, stime:etime])) # average scores # (batch, num_classes, seg_num) -> (batch, num_classes) scores = torch.stack(scores, dim=2).mean(dim=-1) targets = self._get_target(filenames) for filename, target, score in zip(filenames, targets, scores): print("Output, %s, %d, %f" % (filename, target, score[-1])) return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.CrossEntropyLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
6,838
32.038647
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-attention-oc/05/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.oc_softmax as nii_ocsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) # self.model_debug = False # self.flag_validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # spectrogram dim (base component) self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part for output layer self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((self.spec_fb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((self.spec_fb_dim//16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_ocsoftmax.OCAngleLayer(self.v_emd_dim) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # batch_size batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([x.shape[0], self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models * batch_size batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return out_score_neg else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_ocsoftmax.OCSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,526
33.504444
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-attention-oc/04/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.oc_softmax as nii_ocsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) # self.model_debug = False # self.flag_validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # spectrogram dim (base component) self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part for output layer self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((self.spec_fb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((self.spec_fb_dim//16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_ocsoftmax.OCAngleLayer(self.v_emd_dim) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # batch_size batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([x.shape[0], self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models * batch_size batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return out_score_neg else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_ocsoftmax.OCSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,526
33.504444
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-attention-oc/01/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.oc_softmax as nii_ocsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) # self.model_debug = False # self.flag_validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # spectrogram dim (base component) self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part for output layer self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((self.spec_fb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((self.spec_fb_dim//16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_ocsoftmax.OCAngleLayer(self.v_emd_dim) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # batch_size batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([x.shape[0], self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models * batch_size batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return out_score_neg else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_ocsoftmax.OCSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,526
33.504444
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-attention-oc/06/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.oc_softmax as nii_ocsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) # self.model_debug = False # self.flag_validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # spectrogram dim (base component) self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part for output layer self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((self.spec_fb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((self.spec_fb_dim//16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_ocsoftmax.OCAngleLayer(self.v_emd_dim) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # batch_size batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([x.shape[0], self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models * batch_size batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return out_score_neg else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_ocsoftmax.OCSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,526
33.504444
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-attention-oc/03/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.oc_softmax as nii_ocsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) # self.model_debug = False # self.flag_validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # spectrogram dim (base component) self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part for output layer self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((self.spec_fb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((self.spec_fb_dim//16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_ocsoftmax.OCAngleLayer(self.v_emd_dim) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # batch_size batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([x.shape[0], self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models * batch_size batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return out_score_neg else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_ocsoftmax.OCSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,526
33.504444
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-attention-oc/02/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.oc_softmax as nii_ocsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) # self.model_debug = False # self.flag_validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # spectrogram dim (base component) self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part for output layer self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((self.spec_fb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((self.spec_fb_dim//16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_ocsoftmax.OCAngleLayer(self.v_emd_dim) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # batch_size batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([x.shape[0], self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models * batch_size batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return out_score_neg else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_ocsoftmax.OCSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,526
33.504444
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-lstmsum-p2s/05/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.p2sgrad as nii_p2sgrad import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32), nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((self.spec_fb_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros( [batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
15,414
33.640449
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-lstmsum-p2s/04/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.p2sgrad as nii_p2sgrad import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32), nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((self.spec_fb_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros( [batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
15,414
33.640449
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-lstmsum-p2s/01/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.p2sgrad as nii_p2sgrad import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32), nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((self.spec_fb_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros( [batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
15,414
33.640449
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-lstmsum-p2s/06/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.p2sgrad as nii_p2sgrad import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32), nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((self.spec_fb_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros( [batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
15,414
33.640449
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-lstmsum-p2s/03/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.p2sgrad as nii_p2sgrad import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32), nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((self.spec_fb_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros( [batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
15,414
33.640449
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-lstmsum-p2s/02/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.p2sgrad as nii_p2sgrad import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32), nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((self.spec_fb_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros( [batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
15,414
33.640449
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-oc/05/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.oc_softmax as nii_ocsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) # self.model_debug = False # self.flag_validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part for output layer self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_ocsoftmax.OCAngleLayer(self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # batch_size batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([x.shape[0], self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models * batch_size batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return out_score_neg else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_ocsoftmax.OCSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,258
33.600907
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-oc/04/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.oc_softmax as nii_ocsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) # self.model_debug = False # self.flag_validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part for output layer self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_ocsoftmax.OCAngleLayer(self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # batch_size batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([x.shape[0], self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models * batch_size batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return out_score_neg else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_ocsoftmax.OCSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,258
33.600907
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-oc/01/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.oc_softmax as nii_ocsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) # self.model_debug = False # self.flag_validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part for output layer self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_ocsoftmax.OCAngleLayer(self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # batch_size batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([x.shape[0], self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models * batch_size batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return out_score_neg else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_ocsoftmax.OCSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,258
33.600907
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-oc/06/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.oc_softmax as nii_ocsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) # self.model_debug = False # self.flag_validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part for output layer self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_ocsoftmax.OCAngleLayer(self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # batch_size batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([x.shape[0], self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models * batch_size batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return out_score_neg else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_ocsoftmax.OCSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,258
33.600907
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-oc/03/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.oc_softmax as nii_ocsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) # self.model_debug = False # self.flag_validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part for output layer self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_ocsoftmax.OCAngleLayer(self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # batch_size batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([x.shape[0], self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models * batch_size batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return out_score_neg else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_ocsoftmax.OCSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,258
33.600907
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-oc/02/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.oc_softmax as nii_ocsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) # self.model_debug = False # self.flag_validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part for output layer self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_ocsoftmax.OCAngleLayer(self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # batch_size batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([x.shape[0], self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models * batch_size batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return out_score_neg else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_ocsoftmax.OCSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,258
33.600907
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-fixed-am/05/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.am_softmax as nii_amsoftmax import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output class self.v_out_class = 2 #### # create network #### self.m_transform = [] self.m_output_act = [] self.m_frontend = [] self.m_angle = [] for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (self.spec_fb_dim // 16) * 32, 512), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(256, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_angle = torch_nn.ModuleList(self.m_angle) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_emb = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device).long() target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) score = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], score.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,919
34.221239
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-fixed-am/04/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.am_softmax as nii_amsoftmax import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output class self.v_out_class = 2 #### # create network #### self.m_transform = [] self.m_output_act = [] self.m_frontend = [] self.m_angle = [] for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (self.spec_fb_dim // 16) * 32, 512), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(256, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_angle = torch_nn.ModuleList(self.m_angle) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_emb = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device).long() target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) score = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], score.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,919
34.221239
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-fixed-am/01/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.am_softmax as nii_amsoftmax import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output class self.v_out_class = 2 #### # create network #### self.m_transform = [] self.m_output_act = [] self.m_frontend = [] self.m_angle = [] for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (self.spec_fb_dim // 16) * 32, 512), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(256, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_angle = torch_nn.ModuleList(self.m_angle) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_emb = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device).long() target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) score = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], score.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,918
34.297118
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-fixed-am/06/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.am_softmax as nii_amsoftmax import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output class self.v_out_class = 2 #### # create network #### self.m_transform = [] self.m_output_act = [] self.m_frontend = [] self.m_angle = [] for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (self.spec_fb_dim // 16) * 32, 512), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(256, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_angle = torch_nn.ModuleList(self.m_angle) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_emb = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device).long() target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) score = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], score.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,919
34.221239
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-fixed-am/03/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.am_softmax as nii_amsoftmax import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output class self.v_out_class = 2 #### # create network #### self.m_transform = [] self.m_output_act = [] self.m_frontend = [] self.m_angle = [] for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (self.spec_fb_dim // 16) * 32, 512), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(256, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_angle = torch_nn.ModuleList(self.m_angle) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_emb = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device).long() target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) score = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], score.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,919
34.221239
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-fixed-am/02/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.am_softmax as nii_amsoftmax import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output class self.v_out_class = 2 #### # create network #### self.m_transform = [] self.m_output_act = [] self.m_frontend = [] self.m_angle = [] for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (self.spec_fb_dim // 16) * 32, 512), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(256, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_angle = torch_nn.ModuleList(self.m_angle) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_emb = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device).long() target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) score = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], score.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,919
34.221239
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfcc-lcnn-attention-sig/05/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfcc_dim = [20] self.lfcc_with_delta = True # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors # here, the embedding is just the activation before sigmoid() self.v_emd_dim = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfcc_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfcc_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfcc_with_delta: lfcc_dim = lfcc_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfcc_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfcc_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFCC(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfcc_dim[idx], with_energy=True) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,332
33.537349
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfcc-lcnn-attention-sig/04/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfcc_dim = [20] self.lfcc_with_delta = True # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors # here, the embedding is just the activation before sigmoid() self.v_emd_dim = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfcc_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfcc_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfcc_with_delta: lfcc_dim = lfcc_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfcc_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfcc_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFCC(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfcc_dim[idx], with_energy=True) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,332
33.537349
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfcc-lcnn-attention-sig/01/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfcc_dim = [20] self.lfcc_with_delta = True # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors # here, the embedding is just the activation before sigmoid() self.v_emd_dim = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfcc_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfcc_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfcc_with_delta: lfcc_dim = lfcc_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfcc_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfcc_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFCC(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfcc_dim[idx], with_energy=True) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,332
33.537349
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfcc-lcnn-attention-sig/06/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfcc_dim = [20] self.lfcc_with_delta = True # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors # here, the embedding is just the activation before sigmoid() self.v_emd_dim = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfcc_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfcc_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfcc_with_delta: lfcc_dim = lfcc_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfcc_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfcc_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFCC(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfcc_dim[idx], with_energy=True) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,332
33.537349
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfcc-lcnn-attention-sig/03/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfcc_dim = [20] self.lfcc_with_delta = True # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors # here, the embedding is just the activation before sigmoid() self.v_emd_dim = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfcc_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfcc_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfcc_with_delta: lfcc_dim = lfcc_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfcc_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfcc_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFCC(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfcc_dim[idx], with_energy=True) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,332
33.537349
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfcc-lcnn-attention-sig/02/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfcc_dim = [20] self.lfcc_with_delta = True # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors # here, the embedding is just the activation before sigmoid() self.v_emd_dim = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfcc_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfcc_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfcc_with_delta: lfcc_dim = lfcc_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfcc_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfcc_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFCC(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfcc_dim[idx], with_energy=True) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,332
33.537349
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-sig/05/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors # here, the embedding is just the activation before sigmoid() self.v_emd_dim = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,437
33.623501
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-sig/04/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors # here, the embedding is just the activation before sigmoid() self.v_emd_dim = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,437
33.623501
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-sig/01/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors # here, the embedding is just the activation before sigmoid() self.v_emd_dim = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,437
33.623501
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-sig/06/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors # here, the embedding is just the activation before sigmoid() self.v_emd_dim = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,437
33.623501
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-sig/03/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors # here, the embedding is just the activation before sigmoid() self.v_emd_dim = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,437
33.623501
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-sig/02/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors # here, the embedding is just the activation before sigmoid() self.v_emd_dim = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,437
33.623501
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-lstmsum-am/05/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.am_softmax as nii_amsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32), nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((self.spec_fb_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,944
34.043956
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-lstmsum-am/04/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.am_softmax as nii_amsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32), nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((self.spec_fb_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,944
34.043956
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-lstmsum-am/01/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.am_softmax as nii_amsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32), nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((self.spec_fb_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,944
34.043956
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-lstmsum-am/06/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.am_softmax as nii_amsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32), nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((self.spec_fb_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,944
34.043956
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-lstmsum-am/03/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.am_softmax as nii_amsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32), nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((self.spec_fb_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,944
34.043956
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/spec2-lcnn-lstmsum-am/02/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.am_softmax as nii_amsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class TrainableLinearFb(nii_nn.LinearInitialized): """Linear layer initialized with linear filter bank """ def __init__(self, fn, sr, filter_num): super(TrainableLinearFb, self).__init__( nii_front_end.linear_fb(fn, sr, filter_num)) return def forward(self, x): return torch.log10( torch.pow(super(TrainableLinearFb, self).forward(x), 2) + torch.finfo(torch.float32).eps) class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # self.spec_with_delta = False self.spec_fb_dim = 60 # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n) in enumerate(zip( self.v_truncate_lens, self.fft_n)): fft_n_bins = fft_n // 2 + 1 self.m_transform.append( torch_nn.Sequential( TrainableLinearFb(fft_n,self.m_target_sr,self.spec_fb_dim), torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32), nii_nn.BLSTMLayer((self.spec_fb_dim//16) * 32, (self.spec_fb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((self.spec_fb_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.Spectrogram(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,944
34.043956
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-lstmsum-sig/05/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors # here, the embedding is just the activation before sigmoid() self.v_emd_dim = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32), nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32, self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).mean(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,695
33.824645
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-lstmsum-sig/04/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors # here, the embedding is just the activation before sigmoid() self.v_emd_dim = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32), nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32, self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).mean(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,695
33.824645
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-lstmsum-sig/01/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors # here, the embedding is just the activation before sigmoid() self.v_emd_dim = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32), nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32, self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).mean(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,695
33.824645
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-lstmsum-sig/06/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors # here, the embedding is just the activation before sigmoid() self.v_emd_dim = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32), nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32, self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).mean(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,695
33.824645
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-lstmsum-sig/03/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors # here, the embedding is just the activation before sigmoid() self.v_emd_dim = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32), nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32, self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).mean(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,695
33.824645
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-lstmsum-sig/02/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors # here, the embedding is just the activation before sigmoid() self.v_emd_dim = 1 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32), nii_nn.BLSTMLayer((lfb_dim//16) * 32, (lfb_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32, self.v_emd_dim) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).mean(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, feature_vec, inference=False): """ """ # feature_vec is [batch * submodel, 1] if inference: return feature_vec.squeeze(1) else: return torch.sigmoid(feature_vec).squeeze(1) def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = torch_nn.BCELoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,695
33.824645
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfcc-lcnn-attention-p2s/05/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.p2sgrad as nii_p2sgrad import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfcc_dim = [20] self.lfcc_with_delta = True # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfcc_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfcc_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfcc_with_delta: lfcc_dim = lfcc_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfcc_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfcc_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.LFCC(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfcc_dim[idx], with_energy=True) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros( [batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,758
33.403263
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfcc-lcnn-attention-p2s/04/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.p2sgrad as nii_p2sgrad import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfcc_dim = [20] self.lfcc_with_delta = True # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfcc_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfcc_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfcc_with_delta: lfcc_dim = lfcc_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfcc_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfcc_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.LFCC(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfcc_dim[idx], with_energy=True) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros( [batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,758
33.403263
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfcc-lcnn-attention-p2s/01/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.p2sgrad as nii_p2sgrad import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfcc_dim = [20] self.lfcc_with_delta = True # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfcc_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfcc_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfcc_with_delta: lfcc_dim = lfcc_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfcc_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfcc_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.LFCC(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfcc_dim[idx], with_energy=True) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros( [batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,758
33.403263
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfcc-lcnn-attention-p2s/06/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.p2sgrad as nii_p2sgrad import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfcc_dim = [20] self.lfcc_with_delta = True # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfcc_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfcc_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfcc_with_delta: lfcc_dim = lfcc_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfcc_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfcc_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.LFCC(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfcc_dim[idx], with_energy=True) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros( [batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,758
33.403263
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfcc-lcnn-attention-p2s/03/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.p2sgrad as nii_p2sgrad import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfcc_dim = [20] self.lfcc_with_delta = True # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfcc_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfcc_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfcc_with_delta: lfcc_dim = lfcc_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfcc_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfcc_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.LFCC(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfcc_dim[idx], with_energy=True) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros( [batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,758
33.403263
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfcc-lcnn-attention-p2s/02/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.p2sgrad as nii_p2sgrad import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) self.model_debug = False self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfcc_dim = [20] self.lfcc_with_delta = True # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfcc_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfcc_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfcc_with_delta: lfcc_dim = lfcc_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfcc_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfcc_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_p2sgrad.P2SActivationLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.LFCC(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfcc_dim[idx], with_energy=True) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_score = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_score return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] # compute score through p2sgrad layer out_score = torch.zeros( [batch_size * self.v_submodels, self.v_out_class], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): tmp_score = m_score(x[idx * batch_size : (idx+1) * batch_size]) out_score[idx * batch_size : (idx+1) * batch_size] = tmp_score if inference: # output_score [:, 1] corresponds to the positive class return out_score[:, 1] else: return out_score def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=scores.dtype) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_p2sgrad.P2SGradLoss() def compute(self, outputs, target): """ """ loss = self.m_loss(outputs[0], outputs[1]) return loss if __name__ == "__main__": print("Definition of model")
14,758
33.403263
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfcc-lcnn-lstmsum-am/05/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.am_softmax as nii_amsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfcc_dim = [20] self.lfcc_with_delta = True # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfcc_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfcc_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfcc_with_delta: lfcc_dim = lfcc_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((lfcc_dim//16) * 32, (lfcc_dim//16) * 32), nii_nn.BLSTMLayer((lfcc_dim//16) * 32, (lfcc_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((lfcc_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.LFCC(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfcc_dim[idx], with_energy=True) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,513
34.020316
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfcc-lcnn-lstmsum-am/04/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.am_softmax as nii_amsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfcc_dim = [20] self.lfcc_with_delta = True # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfcc_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfcc_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfcc_with_delta: lfcc_dim = lfcc_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((lfcc_dim//16) * 32, (lfcc_dim//16) * 32), nii_nn.BLSTMLayer((lfcc_dim//16) * 32, (lfcc_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((lfcc_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.LFCC(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfcc_dim[idx], with_energy=True) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,513
34.020316
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfcc-lcnn-lstmsum-am/01/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.am_softmax as nii_amsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfcc_dim = [20] self.lfcc_with_delta = True # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfcc_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfcc_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfcc_with_delta: lfcc_dim = lfcc_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((lfcc_dim//16) * 32, (lfcc_dim//16) * 32), nii_nn.BLSTMLayer((lfcc_dim//16) * 32, (lfcc_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((lfcc_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.LFCC(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfcc_dim[idx], with_energy=True) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,513
34.020316
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfcc-lcnn-lstmsum-am/06/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.am_softmax as nii_amsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfcc_dim = [20] self.lfcc_with_delta = True # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfcc_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfcc_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfcc_with_delta: lfcc_dim = lfcc_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((lfcc_dim//16) * 32, (lfcc_dim//16) * 32), nii_nn.BLSTMLayer((lfcc_dim//16) * 32, (lfcc_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((lfcc_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.LFCC(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfcc_dim[idx], with_energy=True) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,513
34.020316
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfcc-lcnn-lstmsum-am/03/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.am_softmax as nii_amsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfcc_dim = [20] self.lfcc_with_delta = True # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfcc_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfcc_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfcc_with_delta: lfcc_dim = lfcc_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((lfcc_dim//16) * 32, (lfcc_dim//16) * 32), nii_nn.BLSTMLayer((lfcc_dim//16) * 32, (lfcc_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((lfcc_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.LFCC(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfcc_dim[idx], with_energy=True) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,513
34.020316
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfcc-lcnn-lstmsum-am/02/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.am_softmax as nii_amsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFCC dim (base component) self.lfcc_dim = [20] self.lfcc_with_delta = True # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # self.m_before_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part on training self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfcc_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfcc_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfcc_with_delta: lfcc_dim = lfcc_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_before_pooling.append( torch_nn.Sequential( nii_nn.BLSTMLayer((lfcc_dim//16) * 32, (lfcc_dim//16) * 32), nii_nn.BLSTMLayer((lfcc_dim//16) * 32, (lfcc_dim//16) * 32) ) ) self.m_output_act.append( torch_nn.Linear((lfcc_dim // 16) * 32, self.v_emd_dim) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class) ) self.m_frontend.append( nii_front_end.LFCC(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfcc_dim[idx], with_energy=True) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_angle = torch_nn.ModuleList(self.m_angle) self.m_before_pooling = torch_nn.ModuleList(self.m_before_pooling) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_be_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_before_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pass through LSTM then summing hidden_features_lstm = m_be_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output((hidden_features_lstm + hidden_features).sum(1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,513
34.020316
80
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-fixed-am/05/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.am_softmax as nii_amsoftmax import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output class self.v_out_class = 2 #### # create network #### self.m_transform = [] self.m_output_act = [] self.m_frontend = [] self.m_angle = [] for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (lfb_dim // 16) * 32, 512), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(256, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class, s=10, m=0.35) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_angle = torch_nn.ModuleList(self.m_angle) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_emb = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device).long() target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) score = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], score.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,716
34.319101
90
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-fixed-am/04/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.am_softmax as nii_amsoftmax import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output class self.v_out_class = 2 #### # create network #### self.m_transform = [] self.m_output_act = [] self.m_frontend = [] self.m_angle = [] for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (lfb_dim // 16) * 32, 512), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(256, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class, s=10, m=0.35) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_angle = torch_nn.ModuleList(self.m_angle) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_emb = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device).long() target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) score = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], score.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,716
34.319101
90
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-fixed-am/01/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.am_softmax as nii_amsoftmax import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output class self.v_out_class = 2 #### # create network #### self.m_transform = [] self.m_output_act = [] self.m_frontend = [] self.m_angle = [] for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (lfb_dim // 16) * 32, 512), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(256, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class, s=10, m=0.35) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_angle = torch_nn.ModuleList(self.m_angle) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_emb = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device).long() target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) score = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], score.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,716
34.319101
90
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-fixed-am/06/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.am_softmax as nii_amsoftmax import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output class self.v_out_class = 2 #### # create network #### self.m_transform = [] self.m_output_act = [] self.m_frontend = [] self.m_angle = [] for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (lfb_dim // 16) * 32, 512), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(256, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class, s=10, m=0.35) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_angle = torch_nn.ModuleList(self.m_angle) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_emb = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device).long() target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) score = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], score.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,716
34.319101
90
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-fixed-am/03/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.am_softmax as nii_amsoftmax import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output class self.v_out_class = 2 #### # create network #### self.m_transform = [] self.m_output_act = [] self.m_frontend = [] self.m_angle = [] for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (lfb_dim // 16) * 32, 512), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(256, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class, s=10, m=0.35) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_angle = torch_nn.ModuleList(self.m_angle) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_emb = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device).long() target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) score = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], score.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,716
34.319101
90
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-fixed-am/02/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_modules.am_softmax as nii_amsoftmax import core_scripts.data_io.seq_info as nii_seq_tk import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) #self.model_debug = False #self.validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training # target data protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # working sampling rate, torchaudio is used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating self.amp_floor = 0.00001 # manual choose the first 600 frames in the data self.v_truncate_lens = [10 * 16 * 750 // x for x in self.frame_hops] # number of sub-models self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output class self.v_out_class = 2 #### # create network #### self.m_transform = [] self.m_output_act = [] self.m_frontend = [] self.m_angle = [] for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]) ) ) self.m_output_act.append( torch_nn.Sequential( torch_nn.Dropout(0.7), torch_nn.Linear((trunc_len // 16) * (lfb_dim // 16) * 32, 512), nii_nn.MaxFeatureMap2D(), torch_nn.Linear(256, self.v_emd_dim) ) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class, s=10, m=0.35) ) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_angle = torch_nn.ModuleList(self.m_angle) # output # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.zeros([in_dim]) out_m = torch.ones([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features fs: frame shift fl: frame length fn: fft points trunc_len: number of frames per file (by truncating) datalength: original length of data """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # permute to (batch, fft_bin, frame_length) x_sp_amp = x_sp_amp.permute(0, 2, 1) # make sure the buffer is long enough x_sp_amp_buff = torch.zeros( [x_sp_amp.shape[0], x_sp_amp.shape[1], trunc_len], dtype=x_sp_amp.dtype, device=x_sp_amp.device) # for batch of data, handle the padding and trim independently fs = self.frame_hops[idx] for fileidx in range(x_sp_amp.shape[0]): # roughtly this is the number of frames true_frame_num = datalength[fileidx] // fs if true_frame_num > trunc_len: # trim randomly pos = torch.rand([1]) * (true_frame_num-trunc_len) pos = torch.floor(pos[0]).long() tmp = x_sp_amp[fileidx, :, pos:trunc_len+pos] x_sp_amp_buff[fileidx] = tmp else: rep = int(np.ceil(trunc_len / true_frame_num)) tmp = x_sp_amp[fileidx, :, 0:true_frame_num].repeat(1, rep) x_sp_amp_buff[fileidx] = tmp[:, 0:trunc_len] # permute to (batch, frame_length, fft_bin) x_sp_amp = x_sp_amp_buff.permute(0, 2, 1) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([batch_size * self.v_submodels, self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_output) in enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_output_act)): # extract feature (stft spectrogram) x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. flatten and transform through output function tmp_emb = m_output(torch.flatten(hidden_features, 1)) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): #with torch.no_grad(): # vad_waveform = self.m_vad(x.squeeze(-1)) # vad_waveform = self.m_vad(torch.flip(vad_waveform, dims=[1])) # if vad_waveform.shape[-1] > 0: # x = torch.flip(vad_waveform, dims=[1]).unsqueeze(-1) # else: # pass filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device).long() target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec, True] else: feature_vec = self._compute_embedding(x, datalength) score = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], score.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,716
34.319101
90
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-am/05/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.am_softmax as nii_amsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) # self.model_debug = False # self.flag_validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part for output layer self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class, s=10, m=0.35) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # batch_size batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([x.shape[0], self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models * batch_size batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,326
33.755102
90
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-am/04/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.am_softmax as nii_amsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) # self.model_debug = False # self.flag_validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part for output layer self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class, s=10, m=0.35) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # batch_size batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([x.shape[0], self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models * batch_size batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,326
33.755102
90
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-am/01/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.am_softmax as nii_amsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) # self.model_debug = False # self.flag_validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part for output layer self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class, s=10, m=0.35) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # batch_size batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([x.shape[0], self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models * batch_size batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,326
33.755102
90
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-am/06/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.am_softmax as nii_amsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) # self.model_debug = False # self.flag_validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part for output layer self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class, s=10, m=0.35) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # batch_size batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([x.shape[0], self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models * batch_size batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,326
33.755102
90
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-am/03/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.am_softmax as nii_amsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) # self.model_debug = False # self.flag_validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part for output layer self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class, s=10, m=0.35) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # batch_size batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([x.shape[0], self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models * batch_size batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,326
33.755102
90
py
project-NN-Pytorch-scripts
project-NN-Pytorch-scripts-master/project/03-asvspoof-mega/lfb-lcnn-attention-am/02/model.py
#!/usr/bin/env python """ model.py Self defined model definition. Usage: """ from __future__ import absolute_import from __future__ import print_function import sys import numpy as np import torch import torch.nn as torch_nn import torch.nn.functional as torch_nn_func import sandbox.block_nn as nii_nn import sandbox.util_frontend as nii_front_end import core_scripts.other_tools.debug as nii_debug import core_scripts.data_io.seq_info as nii_seq_tk import core_modules.am_softmax as nii_amsoftmax import config as prj_conf __author__ = "Xin Wang" __email__ = "[email protected]" __copyright__ = "Copyright 2020, Xin Wang" ############## ## util ############## def protocol_parse(protocol_filepath): """ Parse protocol of ASVspoof2019 and get bonafide/spoof for each trial input: ----- protocol_filepath: string, path to the protocol file for convenience, I put train/dev/eval trials into a single protocol file output: ------- data_buffer: dic, data_bufer[filename] -> 1 (bonafide), 0 (spoof) """ data_buffer = {} temp_buffer = np.loadtxt(protocol_filepath, dtype='str') for row in temp_buffer: if row[-1] == 'bonafide': data_buffer[row[1]] = 1 else: data_buffer[row[1]] = 0 return data_buffer ############## ## FOR MODEL ############## class Model(torch_nn.Module): """ Model definition """ def __init__(self, in_dim, out_dim, args, mean_std=None): super(Model, self).__init__() ##### required part, no need to change ##### # mean std of input and output in_m, in_s, out_m, out_s = self.prepare_mean_std(in_dim,out_dim,\ args, mean_std) self.input_mean = torch_nn.Parameter(in_m, requires_grad=False) self.input_std = torch_nn.Parameter(in_s, requires_grad=False) self.output_mean = torch_nn.Parameter(out_m, requires_grad=False) self.output_std = torch_nn.Parameter(out_s, requires_grad=False) # a flag for debugging (by default False) # self.model_debug = False # self.flag_validation = False ##### #### # on input waveform and output target #### # Load protocol and prepare the target data for network training protocol_file = prj_conf.optional_argument[0] self.protocol_parser = protocol_parse(protocol_file) # Working sampling rate # torchaudio may be used to change sampling rate self.m_target_sr = 16000 #### # optional configs (not used) #### # re-sampling (optional) #self.m_resampler = torchaudio.transforms.Resample( # prj_conf.wav_samp_rate, self.m_target_sr) # vad (optional) #self.m_vad = torchaudio.transforms.Vad(sample_rate = self.m_target_sr) # flag for balanced class (temporary use) #self.v_flag = 1 #### # front-end configuration # multiple front-end configurations may be used # by default, use a single front-end #### # frame shift (number of waveform points) self.frame_hops = [160] # frame length self.frame_lens = [320] # FFT length self.fft_n = [512] # LFB dim (base component) self.lfb_dim = [60] self.lfb_with_delta = False # window type self.win = torch.hann_window # floor in log-spectrum-amplitude calculating (not used) self.amp_floor = 0.00001 # number of frames to be kept for each trial # no truncation self.v_truncate_lens = [None for x in self.frame_hops] # number of sub-models (by default, a single model) self.v_submodels = len(self.frame_lens) # dimension of embedding vectors self.v_emd_dim = 64 # output classes self.v_out_class = 2 #### # create network #### # 1st part of the classifier self.m_transform = [] # pooling layer self.m_pooling = [] # 2nd part of the classifier self.m_output_act = [] # front-end self.m_frontend = [] # final part for output layer self.m_angle = [] # it can handle models with multiple front-end configuration # by default, only a single front-end for idx, (trunc_len, fft_n, lfb_dim) in enumerate(zip( self.v_truncate_lens, self.fft_n, self.lfb_dim)): fft_n_bins = fft_n // 2 + 1 if self.lfb_with_delta: lfb_dim = lfb_dim * 3 self.m_transform.append( torch_nn.Sequential( torch_nn.Conv2d(1, 64, [5, 5], 1, padding=[2, 2]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 96, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 96, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(48, affine=False), torch_nn.Conv2d(48, 128, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch.nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Conv2d(64, 128, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(64, affine=False), torch_nn.Conv2d(64, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [1, 1], 1, padding=[0, 0]), nii_nn.MaxFeatureMap2D(), torch_nn.BatchNorm2d(32, affine=False), torch_nn.Conv2d(32, 64, [3, 3], 1, padding=[1, 1]), nii_nn.MaxFeatureMap2D(), torch_nn.MaxPool2d([2, 2], [2, 2]), torch_nn.Dropout(0.7) ) ) self.m_pooling.append( nii_nn.SelfWeightedPooling((lfb_dim // 16) * 32) ) self.m_output_act.append( torch_nn.Linear((lfb_dim // 16) * 32 * 2, self.v_emd_dim) ) self.m_angle.append( nii_amsoftmax.AMAngleLayer(self.v_emd_dim, self.v_out_class, s=10, m=0.35) ) self.m_frontend.append( nii_front_end.LFB(self.frame_lens[idx], self.frame_hops[idx], self.fft_n[idx], self.m_target_sr, self.lfb_dim[idx], with_energy=False, with_emphasis=True, with_delta=self.lfb_with_delta) ) self.m_frontend = torch_nn.ModuleList(self.m_frontend) self.m_transform = torch_nn.ModuleList(self.m_transform) self.m_output_act = torch_nn.ModuleList(self.m_output_act) self.m_pooling = torch_nn.ModuleList(self.m_pooling) self.m_angle = torch_nn.ModuleList(self.m_angle) # done return def prepare_mean_std(self, in_dim, out_dim, args, data_mean_std=None): """ prepare mean and std for data processing This is required for the Pytorch project, but not relevant to this code """ if data_mean_std is not None: in_m = torch.from_numpy(data_mean_std[0]) in_s = torch.from_numpy(data_mean_std[1]) out_m = torch.from_numpy(data_mean_std[2]) out_s = torch.from_numpy(data_mean_std[3]) if in_m.shape[0] != in_dim or in_s.shape[0] != in_dim: print("Input dim: {:d}".format(in_dim)) print("Mean dim: {:d}".format(in_m.shape[0])) print("Std dim: {:d}".format(in_s.shape[0])) print("Input dimension incompatible") sys.exit(1) if out_m.shape[0] != out_dim or out_s.shape[0] != out_dim: print("Output dim: {:d}".format(out_dim)) print("Mean dim: {:d}".format(out_m.shape[0])) print("Std dim: {:d}".format(out_s.shape[0])) print("Output dimension incompatible") sys.exit(1) else: in_m = torch.zeros([in_dim]) in_s = torch.ones([in_dim]) out_m = torch.zeros([out_dim]) out_s = torch.ones([out_dim]) return in_m, in_s, out_m, out_s def normalize_input(self, x): """ normalizing the input data This is required for the Pytorch project, but not relevant to this code """ return (x - self.input_mean) / self.input_std def normalize_target(self, y): """ normalizing the target data This is required for the Pytorch project, but not relevant to this code """ return (y - self.output_mean) / self.output_std def denormalize_output(self, y): """ denormalizing the generated output from network This is required for the Pytorch project, but not relevant to this code """ return y * self.output_std + self.output_mean def _front_end(self, wav, idx, trunc_len, datalength): """ simple fixed front-end to extract features input: ------ wav: waveform idx: idx of the trial in mini-batch trunc_len: number of frames to be kept after truncation datalength: list of data length in mini-batch output: ------- x_sp_amp: front-end featues, (batch, frame_num, frame_feat_dim) """ with torch.no_grad(): x_sp_amp = self.m_frontend[idx](wav.squeeze(-1)) # return return x_sp_amp def _compute_embedding(self, x, datalength): """ definition of forward method Assume x (batchsize, length, dim) Output x (batchsize * number_filter, output_dim) """ # resample if necessary #x = self.m_resampler(x.squeeze(-1)).unsqueeze(-1) # batch_size batch_size = x.shape[0] // self.v_submodels # buffer to store output scores from sub-models output_emb = torch.zeros([x.shape[0], self.v_emd_dim], device=x.device, dtype=x.dtype) # compute scores for each sub-models for idx, (fs, fl, fn, trunc_len, m_trans, m_pool, m_output) in \ enumerate( zip(self.frame_hops, self.frame_lens, self.fft_n, self.v_truncate_lens, self.m_transform, self.m_pooling, self.m_output_act)): # extract front-end feature x_sp_amp = self._front_end(x, idx, trunc_len, datalength) # compute scores # 1. unsqueeze to (batch, 1, frame_length, fft_bin) # 2. compute hidden features hidden_features = m_trans(x_sp_amp.unsqueeze(1)) # 3. (batch, channel, frame//N, feat_dim//N) -> # (batch, frame//N, channel * feat_dim//N) # where N is caused by conv with stride hidden_features = hidden_features.permute(0, 2, 1, 3).contiguous() frame_num = hidden_features.shape[1] hidden_features = hidden_features.view(batch_size, frame_num, -1) # 4. pooling hidden_features = m_pool(hidden_features) # 5. pass through the output layer tmp_emb = m_output(hidden_features) output_emb[idx * batch_size : (idx+1) * batch_size] = tmp_emb return output_emb def _compute_score(self, x, inference=False): """ """ # number of sub models * batch_size batch_size = x.shape[0] // self.v_submodels # buffer to save the scores # for non-target classes out_score_neg = torch.zeros( [x.shape[0], self.v_out_class], device=x.device, dtype=x.dtype) # for target classes out_score_pos = torch.zeros_like(out_score_neg) # compute scores for each sub-models for idx, m_score in enumerate(self.m_angle): s_idx = idx * batch_size e_idx = idx * batch_size + batch_size tmp_score = m_score(x[s_idx:e_idx], inference) out_score_neg[s_idx:e_idx] = tmp_score[0] out_score_pos[s_idx:e_idx] = tmp_score[1] if inference: return torch_nn_func.softmax(out_score_neg, dim=1)[:, 1] else: return out_score_neg, out_score_pos def _get_target(self, filenames): try: return [self.protocol_parser[x] for x in filenames] except KeyError: print("Cannot find target data for %s" % (str(filenames))) sys.exit(1) def forward(self, x, fileinfo): filenames = [nii_seq_tk.parse_filename(y) for y in fileinfo] datalength = [nii_seq_tk.parse_length(y) for y in fileinfo] if self.training: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec) # target target = self._get_target(filenames) target_vec = torch.tensor(target, device=x.device, dtype=torch.long) target_vec = target_vec.repeat(self.v_submodels) return [scores, target_vec] else: feature_vec = self._compute_embedding(x, datalength) scores = self._compute_score(feature_vec, True) target = self._get_target(filenames) print("Output, %s, %d, %f" % (filenames[0], target[0], scores.mean())) # don't write output score as a single file return None class Loss(): """ Wrapper to define loss function """ def __init__(self, args): """ """ self.m_loss = nii_amsoftmax.AMSoftmaxWithLoss() def compute(self, input_data, target): """loss = compute(input_data, target_data) Note: 1. input_data will be the output from Model.forward() input_data will be a tuple of [scores, target_vec] 2. we will not use target given by the system script we will use the target_vec in input_data[1] """ loss = self.m_loss(input_data[0], input_data[1]) return loss if __name__ == "__main__": print("Definition of model")
15,326
33.755102
90
py