def __data_generation(self, dict_dir, flag, batch): anchor_feats = np.empty( (self.batch_size, self.inp_dim[0], self.inp_dim[1])) positive_feats = np.empty( (self.batch_size, self.inp_dim[0], self.inp_dim[1])) negative_feats = np.empty( (self.batch_size, self.inp_dim[0], self.inp_dim[1])) target_feats = np.empty((self.batch_size, self.target_dim)) dict_dir_current = dict_dir + '/' + flag + '/' for ind, utt in enumerate(batch): speaker = utt.strip().split("#")[0] neg_speaker = utt.strip().split("#")[1] anchor = utt.strip().split("#")[2] positive = utt.strip().split("#")[3] negative = utt.strip().split("#")[4] ch = int(utt.strip().split("#")[5]) neg_ch = int(utt.strip().split("#")[6]) dict_pos = get_dictionaries(dict_dir_current + str(speaker), ch) anchor_feats[ind, :] = dict_pos[anchor] positive_feats[ind, :] = dict_pos[positive] dict_neg = get_dictionaries(dict_dir_current + str(neg_speaker), neg_ch) negative_feats[ind, :] = dict_neg[negative] target_feats[ind, :] = float(0) del dict_pos, dict_neg return anchor_feats, positive_feats, negative_feats, target_feats
def load_DataGenerators(self, dictionary_dir, wavs_dict, negative_wavs, speaker_list_current, flag, model_current, graph, speaker_list_val): while True: #print(flag) triplets = [] triplets_miniepoch = [] st = time.time() print("Current speaker list for " + str(flag) + " =", speaker_list_current) for speaker in speaker_list_current: print(speaker) start = time.time() dict_dir_current = dictionary_dir + '/' + flag + '/' dict_dir_current_speaker = dict_dir_current + str(speaker) print(dict_dir_current_speaker) num_chunks = int(len(os.listdir(dict_dir_current_speaker))) if flag == 'train': chunk = random.randint(0, num_chunks - 1) else: chunk = 0 #print("chunk",chunk) tmp_dictionary = get_dictionaries(dict_dir_current_speaker, chunk) #print(len(tmp_dictionary.keys())) anchor_positive_pairs = list(C(tmp_dictionary.keys(), 2)) negative_wav_ids = list(negative_wavs[speaker]) random.shuffle(negative_wav_ids) #print((anchor_positive_pairs)) tmp = negative_sampling(speaker, chunk, anchor_positive_pairs, tmp_dictionary, negative_wav_ids, wavs_dict, dictionary_dir, flag, graph, model_current, speaker_list_val) print("Done negative sampling for speaker ", speaker) #print(tmp) del tmp_dictionary triplets_miniepoch.append(tmp) #print(time.time() - start, "seconds") #print(triplets_miniepoch) #print("Found triplets for mini epoch") triplets = [t for p in triplets_miniepoch for t in p] random.shuffle(triplets) if flag == 'train': f = open("triplets_mep0.pickle", 'wb') pickle.dump(triplets, f) f.close() print("SAVED TRIPLETS") if flag == 'train': f = open( "/home/rperi/exp_DAE/SpeakerTurnLossHistory/triplets.txt", 'a') f.writelines(str(triplets) + "\n") f.close() num_triplets = len(triplets) print("Num Triplets = ", num_triplets) batch_size = int(self.batch_size) num_batches = int(num_triplets / batch_size) #print("num batches", num_batches) for b in range(num_batches): current_batch = list(triplets[b * batch_size:(b + 1) * batch_size]) l = K.get_value(model_current.optimizer.lr) print(l) anc, pos, neg, tar = self.__data_generation( dictionary_dir, flag, current_batch) yield [anc, pos, neg], tar del triplets, triplets_miniepoch
def generate(self, dict_dir, list_chunks_input, \ num_chunks_per_load, num_chunks, trainFlag): #saved_model = load_model(model_dir + 'weights-improvement-01-18.54.hdf5') #saved_model.summary() while True: num_loads = int(len(list_chunks_input) / num_chunks_per_load) for loads in range(num_loads): print("Train Flag = " + str(trainFlag)) print(str(loads + 1) + " / " + str(num_loads) + "loads") start_time = time.time() if (loads + 1) * num_chunks_per_load <= num_chunks: chunks = list_chunks_input[loads * num_chunks_per_load:(loads + 1) * num_chunks_per_load] print('chunks ' + str((loads + 1) * num_chunks_per_load) + " / " + str(num_chunks)) else: chunks = list_chunks_input[loads * num_chunks_per_load:] print('chunks ' + str(num_chunks) + " / " + str(num_chunks)) print(chunks) input_dicts, target_dicts, len_wav_list_inp \ = get_dictionaries(dict_dir, chunks, trainFlag) print("Time taken to load " + str(num_chunks_per_load) + " dictionaries = " + str(time.time() - start_time) + " seconds") for idx, input_dict in enumerate(input_dicts): target_dict = target_dicts[idx] wav_list_inp = list(input_dict.keys()) input_list_frames = [] output_list_frames = [] for idx, inp in enumerate(wav_list_inp): target = '_'.join(inp.split('_')[0:4]) num_frames_per_wav = input_dict[inp].shape[0] for frame in range(num_frames_per_wav): input_list_frames.append(inp + "#" + str(frame)) output_list_frames.append( str(target) + "#" + str(frame)) # Randomize indices indices = np.arange(len(input_list_frames)) if self.shuffle == True: np.random.shuffle(indices) num_batches = int(floor(len(indices) / self.batch_size)) for b in range(num_batches): input_ids_currentbatch = [ input_list_frames[k] for k in indices[b * self.batch_size:(b + 1) * self.batch_size] ] output_ids_currentbatch = [ output_list_frames[k] for k in indices[b * self.batch_size:(b + 1) * self.batch_size] ] # Generate Data x, y = self.__data_generation(input_ids_currentbatch, output_ids_currentbatch, input_dict, target_dict) yield x, y
def load_DataGenerators(self, dictionary_dir, speaker_list, flag, model_current, graph, model_dir): while True: print(flag) num_speakers_per_miniepoch = int(self.num_speakers_per_miniepoch) print("num_speakers = ", len(speaker_list)) num_miniepochs = int(ceil(len(speaker_list)/num_speakers_per_miniepoch)) error_flag = 0 wavs_dict = defaultdict() wavs_all = [] negative_wavs = defaultdict() for speaker in speaker_list: f = open("/home/rperi/exp_DAE/data/featsscp/clean" + "/" + flag + "/" + speaker + '/feats_merge.scp', 'r') # open("/home/raghuveer/tmp_data/DAE_exp/clean/" "/" + flag + "/" + speaker + '/wav.scp', 'r') # open("/home/rperi/exp_DAE/data/clean/" + "/" + flag + "/" + speaker + '/wav.scp', 'r') # open("/home/raghuveer/tmp_data/DAE_exp/clean/train/" + speaker + '/wav.scp', 'r') lines = f.readlines() wavs_dict[speaker] = [x.strip().split(" ")[0] for x in lines] wavs_all.append(list(wavs_dict[speaker])) f.close() wavs_all = [x for t in wavs_all for x in t] #print(wavs_all) for speaker in speaker_list: #print(speaker) negative_wavs[speaker] = [x for x in wavs_all if x not in list(wavs_dict[speaker])] print("num_mini_epochs=",num_miniepochs) for mini_epoch in range(num_miniepochs): #if (mini_epoch + 1) % 2 == 0: #K.clear_session() #model_current = load_model(model_dir + "/saved_model_latest.h5") #graph = tf.get_default_graph() triplets = [] triplets_miniepoch = [] speaker_list_current = speaker_list[mini_epoch*num_speakers_per_miniepoch: (mini_epoch+1)*num_speakers_per_miniepoch] st = time.time() print("mini epoch " + str(mini_epoch) + " / " + str(num_miniepochs)) print("Current speaker list = ", speaker_list_current) for speaker in speaker_list_current: print(speaker) #start = time.time() dict_dir_current = dictionary_dir + '/' + flag + '/' dict_dir_current_speaker = dict_dir_current + str(speaker) print(dict_dir_current_speaker) num_chunks = int(len(os.listdir(dict_dir_current_speaker))) chunk = random.randint(0, num_chunks-1) #print("chunk",chunk) tmp_dictionary = get_dictionaries(dict_dir_current_speaker, chunk) #print(len(tmp_dictionary.keys())) anchor_positive_pairs = list(C(tmp_dictionary.keys(), 2)) negative_wav_ids = list(negative_wavs[speaker]) random.shuffle(negative_wav_ids) #print((anchor_positive_pairs)) tmp = negative_sampling(speaker, chunk, anchor_positive_pairs, tmp_dictionary, negative_wav_ids, wavs_dict, dictionary_dir, flag, graph, model_current, speaker_list) print("Done negative sampling for speaker ",speaker) del tmp_dictionary triplets_miniepoch.append(tmp) #print(time.time() - start, "seconds") #print(triplets_miniepoch) print("Found triplets for mini epoch", mini_epoch) triplets = [t for p in triplets_miniepoch for t in p] random.shuffle(triplets) if flag == 'train': f = open("/home/rperi/exp_DAE/SpeakerTurnLossHistory/triplets.txt", 'a') f.writelines(str(triplets) + "\n") f.close() num_triplets = len(triplets) print("Num Triplets = ",num_triplets) batch_size = int(self.batch_size) num_batches = int(num_triplets/batch_size) print("num batches",num_batches) for b in range(num_batches): current_batch = list(triplets[b*batch_size:(b+1)*batch_size]) anc, pos, neg, tar = self.__data_generation(dictionary_dir, flag, current_batch) yield [anc, pos, neg], tar del triplets, triplets_miniepoch print("Time fof mini epoch = ", str(time.time() - st)) model_current.save(model_dir + "/saved_model_latest.h5") del negative_wavs, wavs_dict