def evaluate(net, data_loader): """ compute the eer equal error rate :param net: network :param data_loader: test dataset, contains audio files in a numpy array format :return eer """ net.reset() target_scores = [] non_target_scores = [] for data in tqdm(data_loader): sample_input, output = data[0], data[1] sample_input = whiten(sample_input) xo = gate_mfcc(net, sample_input) if output == 1: target_scores.append(xo) else: non_target_scores.append(xo) target_scores = np.array(target_scores) non_target_scores = np.array(non_target_scores) pmiss, pfa = rocch(target_scores, non_target_scores) eer = rocch2eer(pmiss, pfa) return eer
def eval_genome(genome, config, batch_data): """ Most important part of NEAT since it is here that we adapt NEAT to our problem. We tell what is the phenotype of a genome and how to calculate its fitness (same idea than a loss) :param config: config from the config file :param genome: one genome to get evaluated :param batch_data: data to use to evaluate the genomes :return fitness: returns the fitness of the genome this version is intented to use ParallelEvaluator and should be much faster """ net = neat.nn.RecurrentNetwork.create(genome, config) mse = 0 for data in batch_data: inputs, output = data[0], data[1] inputs = whiten(inputs) net.reset() mask, score = gate_activation(net, inputs) selected_score = score[mask] if selected_score.size == 0: xo = 0.5 else: xo = np.sum(selected_score) / selected_score.size mse += (xo - output)**2 return 1 / (1 + mse)
def evaluate(net, data_loader): """ compute the eer equal error rate :param net: list of networks :param data_loader: dataset, contains audio files or extracted features in a numpy array format :return eer """ nb_net = len(net) target_scores_sum = [] non_target_scores_sum = [] target_scores_prod = [] non_target_scores_prod = [] target_scores_min = [] non_target_scores_min = [] target_scores_max = [] non_target_scores_max = [] target_scores_median = [] non_target_scores_median = [] for data in tqdm(data_loader): for i in range(nb_net): net[i].reset() sample_input, output = data[0], data[1] sample_input = whiten(sample_input) xo = np.zeros(nb_net) for i in range(nb_net): xo[i] = gate_mfcc(net[i], sample_input) if output == 1: target_scores_sum.append(xo.mean()) target_scores_prod.append(xo.prod()) target_scores_min.append(xo.min()) target_scores_max.append(xo.max()) target_scores_median.append(np.median(xo)) else: non_target_scores_sum.append(xo.mean()) non_target_scores_prod.append(xo.prod()) non_target_scores_min.append(xo.min()) non_target_scores_max.append(xo.max()) non_target_scores_median.append(np.median(xo)) eer_sum = compute_eer(target_scores_sum, non_target_scores_sum) eer_prod = compute_eer(target_scores_prod, non_target_scores_prod) eer_min = compute_eer(target_scores_min, non_target_scores_min) eer_max = compute_eer(target_scores_max, non_target_scores_max) eer_median = compute_eer(target_scores_median, non_target_scores_median) return eer_sum, eer_prod, eer_min, eer_max, eer_median
def eval_genome(genome, config, batch_data): """ Most important part of NEAT since it is here that we adapt NEAT to our problem. We tell what is the phenotype of a genome and how to calculate its fitness (same idea than a loss) :param config: config from the config file :param genome: one genome to get evaluated :param batch_data: data to use to evaluate the genomes :return fitness: returns the fitness of the genome this version is intented to use ParallelEvaluator and should be much faster """ net = neat.nn.RecurrentNetwork.create(genome, config) target_scores = [] non_target_scores = [] l_s_n = np.zeros(len(batch_data)) for data in batch_data: inputs, output = data[0], data[1] inputs = whiten(inputs) net.reset() """ mask, score = gate_mfcc(net, inputs) selected_score = score[mask] if selected_score.size == 0: xo = 0.5 else: xo = np.sum(selected_score) / selected_score.size """ xo = gate_mfcc(net, inputs) if output == 1: target_scores.append(xo) else: non_target_scores.append(xo) target_scores = np.array(target_scores) non_target_scores = np.array(non_target_scores) for i in range(batch_size // 2): l_s_n[i] = (non_target_scores >= target_scores[i]).sum() / (batch_size // 2) for i in range(non_target_scores.size): l_s_n[i + batch_size // 2] = ( target_scores <= non_target_scores[i]).sum() / (batch_size // 2) return 1 - l_s_n
def eval_genome(genome, config, batch_data): """ Most important part of NEAT since it is here that we adapt NEAT to our problem. We tell what is the phenotype of a genome and how to calculate its fitness (same idea than a loss) :param config: config from the config file :param genome: one genome to get evaluated :param batch_data: data to use to evaluate the genomes :return fitness: returns the fitness of the genome this version is intented to use ParallelEvaluator and should be much faster """ net = neat.nn.RecurrentNetwork.create(genome, config) target_scores = [] non_target_scores = [] for data in batch_data: inputs, output = data[0], data[1] inputs = whiten(inputs) net.reset() """ score_weight, score = gate_activation(net, inputs) selected_score = score[mask] selected_score = gate_activation(net, inputs) if selected_score.size == 0: xo = 0.5 else: xo = np.sum(selected_score) / selected_score.size """ xo = gate_mfcc(net, inputs) if output == 1: target_scores.append(xo) else: non_target_scores.append(xo) target_scores = np.array(target_scores) non_target_scores = np.array(non_target_scores) pmiss, pfa = rocch(target_scores, non_target_scores) eer = rocch2eer(pmiss, pfa) return 2*(.5 - eer)
def eval_genomes(genomes, config_, batch_data): """ Most important part of NEAT since it is here that we adapt NEAT to our problem. We tell what is the phenotype of a genome and how to calculate its fitness (same idea than a loss) Used for single processing :param config_: config from the config file :param genomes: list of all the genomes to get evaluated :param batch_data: data to use to evaluate the genomes """ for _, genome in tqdm(genomes): net = neat.nn.RecurrentNet.create(genome, config_) mse = 0 for data in batch_data: inputs, output = data[0], data[1] inputs = whiten(inputs) net.reset() mask, score = gate_activation(net, inputs[0]) selected_score = score[mask] if selected_score.size == 0: xo = 0.5 else: xo = np.sum(selected_score) / selected_score.size mse += (xo - output)**2 genome.fitness = 1 / (1 + mse)
def eer_gc(genome, config, validation_set): """ function to use for selecting the grand xhampion of each generation :param genome: genome one genome to get evaluated :param config: file configuration file :param validation_set: ASVDataset data use for validation :return: """ net = neat.nn.RecurrentNetwork.create(genome, config) target_scores = [] non_target_scores = [] for data in tqdm(validation_set): inputs, output = data[0], data[1] inputs = whiten(inputs) net.reset() mask, score = gate_activation(net, inputs) selected_score = score[mask] if selected_score.size == 0: xo = 0.5 else: xo = np.sum(selected_score) / selected_score.size if output == 1: target_scores.append(xo) else: non_target_scores.append(xo) target_scores = np.array(target_scores) non_target_scores = np.array(non_target_scores) pmiss, pfa = rocch(target_scores, non_target_scores) eer = rocch2eer(pmiss, pfa) return eer
def read_file(self, meta): tmp_path = meta.path data_x, sample_rate = sf.read(tmp_path) data_y = meta.key if self.mfcc: data_x = librosa.feature.mfcc(y=data_x, sr=sample_rate, n_mfcc=24, n_fft=self.n_fft) if self.chroma_cqt: data_x = librosa.feature.chroma_cqt(y=data_x, sr=sample_rate, n_chroma=24) if self.chroma_stft: data_x = librosa.feature.chroma_stft(y=data_x, sr=sample_rate, n_chroma=24, n_fft=self.n_fft) if self.m_mfcc: data_x = mfcc(data_x, num_cep=24, nfft=self.n_fft) if self.lfcc: data_x = lfcc(data_x, fs=sample_rate, num_ceps=20, pre_emph=0, pre_emph_coeff=0.97, win_len=0.030, win_hop=0.015, win_type="hamming", nfilts=70, nfft=1024, low_freq=0, high_freq=8000, scale="constant", dct_type=2, use_energy=False, lifter=22, normalize=0) if self.standardize: data_x = whiten(data_x) if self.mrf: copy_data_x = data_x[:] data_x = librosa.feature.chroma_stft(y=data_x, sr=sample_rate, n_chroma=24, n_fft=self.n_fft[0]) if self.standardize: data_x = whiten(data_x) for index_n_fft in range(1, len(self.n_fft)): fft_data_x = librosa.feature.chroma_stft( y=copy_data_x, sr=sample_rate, n_chroma=24, n_fft=self.n_fft[index_n_fft]) if self.standardize: fft_data_x = whiten(fft_data_x) data_x = np.concatenate((data_x, fft_data_x)) # to make all data to have the same length if self.fragment_length: if data_x.size < self.fragment_length: nb_iter = self.fragment_length // data_x.size + 1 data_x = np.tile(data_x, nb_iter) begin = np.random.randint(0, data_x.size - self.fragment_length) return data_x[begin:begin + self.fragment_length], float(data_y) else: return data_x, float(data_y)
n_generation = 300 # number of generations train_loader = ASVDataset(length=None, is_train=True, is_eval=False, index_list=index_train) test_loader = ASVDataset(length=None, is_train=False, is_eval=False, index_list=index_train) trainloader = [] for data in train_loader: inputs, output = data[0], data[2] inputs = np.ravel(librosa.feature.mfcc(y=inputs, sr=SAMPLING_RATE)) inputs = whiten(inputs) trainloader.append((inputs, output)) testloader = [] for data in test_loader: inputs, output = data[0], data[2] inputs = np.ravel(librosa.feature.mfcc(y=inputs, sr=SAMPLING_RATE)) inputs = whiten(inputs) testloader.append((inputs, output)) def eval_genomes(genomes, config_): """ Most important part of NEAT since it is here that we adapt NEAT to our problem. We tell what is the phenotype of a genome and how to calculate its fitness (same idea than a loss) :param config_: config from the config file