def load_jhu_feat_segm_fixed_len(feats_dir, files, min_len, max_len, floatX='float32', start_from_zero=False, suffix='fea', rng=np.random): # First we need to loop through the files and check the minimum available lenght #n_avl_samp = [] #for f in files: #n_avl_samp.append( pytel.htk.readhtk_header(feats_dir + '/' + f + '.' + suffix)[0] ) n_avl_samp = np.zeros((len(files), 1), dtype=int) for i, f in enumerate(files): n_avl_samp[i] = pytel.htk.readhtk_header(feats_dir + '/' + f + '.' + suffix)[0] min_n_avl_samp = np.min(n_avl_samp) max_len = np.min( [max_len, min_n_avl_samp + 1] ) # Need to add 1 because max_len because the intervall is [min_len, max_len) n_sel_samp = rng.randint(min_len, max_len) # not [min_len, max_len] #print min_n_avl_samp #print max_len #print min_len #feats = np.zeros((len(files), 512), dtype=floatX) for i, f in enumerate(files): # The start_from_zero option is mainly for debugging/development if start_from_zero: start = 0 else: last_possible_start = n_avl_samp[i] - n_sel_samp start = rng.randint( 0, last_possible_start + 1 ) # This means the intervall is [0,last_possible_start + 1) = [0, last_possible_start] end = start + n_sel_samp #feats.append( readhtk_segment(feats_dir + '/'+ f + '.' + suffix, start, end)[np.newaxis,:,:] ) if i == 0: feats_tmp = readhtk_segment(feats_dir + '/' + f + '.' + suffix, start, end)[np.newaxis, :, :] feats = np.zeros((len(files), n_sel_samp, feats_tmp.shape[2]), dtype=floatX) feats[i, :, :] = readhtk_segment( feats_dir + '/' + f + '.' + suffix, start, end)[np.newaxis, :, :] else: feats[i, :, :] = readhtk_segment( feats_dir + '/' + f + '.' + suffix, start, end)[np.newaxis, :, :] #return np.vstack(feats) return feats
def load_and_proc_feats_segm(feats_dir, files, frame_step, min_len, max_len, floatX='float32'): feats_o = [] for f in files: n_avl_samp = pytel.htk.readhtk_header(feats_dir + '/' + f + '.fea')[0] n_sel_samp = np.random.randint(min_len, max_len) if (n_avl_samp <= n_sel_samp): feats_o.append(readhtk(feats_dir + '/' + f + '.fea')) else: # Randomly select the starting point start = np.random.randint(0, n_avl_samp - n_sel_samp) end = start + n_sel_samp feats_o.append( readhtk_segment(feats_dir + '/' + f + '.fea', start, end)) idx = np.cumsum( np.vstack([len(feats_o[i]) for i in range(0, len(feats_o))])) idx = np.insert(idx, 0, 0) # Need these as arrays feats_o = np.vstack(feats_o[i] for i in range(0, len(feats_o))) return [feats_o, idx.astype(floatX)]
def load_jhu_feat_segm(feats_dir, files, min_len, max_len, floatX='float32', rng=np.random, start_from_zero=False): feats = [] for f in files: n_avl_samp = pytel.htk.readhtk_header(feats_dir + '/' + f + '.fea')[0] n_sel_samp = rng.randint(min_len, max_len) # +1) if (n_avl_samp <= n_sel_samp): feats.append(readhtk(feats_dir + '/' + f + '.fea')) else: if start_from_zero: start = 0 else: start = rng.randint(0, n_avl_samp - n_sel_samp + 1) end = start + n_sel_samp feats.append( readhtk_segment(feats_dir + '/' + f + '.fea', start, end)) idx = np.cumsum(np.vstack([len(feats[i]) for i in range(0, len(feats))])) idx = np.insert(idx, 0, 0) return np.vstack(feats)[np.newaxis, :, :], idx
def check_data_exists(ivec_dir, stats_dir, feat_dir, f_name, ivec_suffix=None, stats_suffix=None, feat_suffix=None, stats_clean=False): # The MISSING info is a bit misleading since it will be printed if an earlier check has failed. missing=False if (ivec_dir != None): if (ivec_suffix==None): missing = not (os.path.isfile( ivec_dir + '/' + f_name + '.i.gz' ) or os.path.isfile( ivec_dir + '/' + f_name + '.xvec' ) ) if missing: log.info ("MISSING: " + ivec_dir + '/' + f_name + '.i.gz') else: missing = not os.path.isfile( ivec_dir + '/' + f_name + "." + ivec_suffix) if missing: log.info ("MISSING: " + ivec_dir + '/' + f_name + "." + ivec_suffix ) if (stats_dir != None): st_f_name = f_name if stats_clean: st_f_name = st_f_name.replace("reverb/", "clean/") st_f_name = st_f_name.replace("music/", "clean/") st_f_name = st_f_name.replace("noise/", "clean/") st_f_name = st_f_name.replace("babble/", "clean/") if (stats_suffix==None): missing = missing or not os.path.isfile( stats_dir + '/' + st_f_name + '.h5') if missing: log.info ("MISSING: " + stats_dir + '/' + st_f_name + '.h5') elif (stats_suffix=='fea'): missing = missing or not os.path.isfile( stats_dir + '/' + st_f_name + '.fea') if missing: log.info ("MISSING: " + stats_dir + '/' + st_f_name + '.fea') try: tmp_h = readhtk_segment(stats_dir + '/' + st_f_name + '.fea', 0, 1)[np.newaxis,:,:] except Exception as e: log.info ("Failed readhtk_segment: " + stats_dir + '/' + st_f_name + '.fea') log.info(str(e)) mising = True else: missing = missing or not os.path.isfile( stats_dir + '/' + st_f_name + "." + stats_suffix) if missing: log.info ("MISSING: " + stats_dir + '/' + st_f_name + "." + stats_suffix) if (feat_dir != None): if (feat_suffix==None): missing = missing or not os.path.isfile( feat_dir + '/'+ f_name + '.fea' ) if missing: log.info ("MISSING: " + feat_dir + '/'+ f_name + '.fea') else: missing = missing or not os.path.isfile( feat_dir + '/'+ f_name + "." + feats_suffix) if missing: log.info ("MISSING: " + feat_dir + '/'+ f_name + "." + feats_suffix) return missing
def load_jhu_feat_lab_vad(feats_dir, lab_dir, vad_dir, files, max_frame=-1, feat_is_segm=False, floatX='float32'): if (max_frame == -1): feats = [readhtk(feats_dir + '/' + f + '.fea') for f in files] else: n_avl_samp = [ pytel.htk.readhtk_header(feats_dir + '/' + f + '.fea')[0] for f in files ] feats = [ readhtk_segment(feats_dir + '/' + f + '.fea', start=0, end=np.min((max_frame, n_avl_samp[i]))) for i, f in enumerate(files) ] new_files = [fix_names_2(f) for f in files] lab = [] for i, nf in enumerate(new_files): if feat_is_segm: f, start, end = grep_times(nf) else: f = nf start = 0 end = feats[i].shape[0] l = np.genfromtxt(lab_dir + f + ".hp") if (vad_dir != None): v = pytel.htk.read_lab_to_bool_vec(vad_dir + '/' + f + '.vad', length=len(l), true_label='speech') l = l[v] l = l[start:end] if (len(l) != feats[i].shape[0]): log.warning( "WARNING: The length of lab is %d but lenght of features is %d. Is VAD missing on lab? " % (len(l), feats[i].shape[0])) lab.append(l[:, np.newaxis]) idx = np.cumsum( np.vstack([len(feats[i]) for i in range(0, len(feats))])) idx = np.insert(idx, 0, 0) #print feats print(lab[2].shape) return np.vstack(feats)[np.newaxis, :, :], idx, np.vstack(lab)[ np.newaxis, :, :]
def load_feats_segm_vad_time_comb(vad_dir, feats_dir, files, frame_step, min_len, max_len, floatX='float32'): feats_o = [] vad = [] for f in files: n_avl_samp_tot = pytel.htk.readhtk_header(feats_dir + '/' + f + '.fea')[0] vad_tmp = pytel.htk.read_lab_to_bool_vec(vad_dir + '/' + f + '.lab.gz', true_label='sp', length=-n_avl_samp_tot) assert (len(vad_tmp) == n_avl_samp_tot) n_avl_samp = np.sum(vad_tmp) # The speech time n_sel_samp = np.random.randint(min_len, max_len) if (n_avl_samp <= n_sel_samp): feats_o.append(readhtk(feats_dir + '/' + f + '.fea')) vad.append(vad_tmp) else: # Randomly select the starting point start = np.random.randint(0, n_avl_samp - n_sel_samp) end = start + n_sel_samp # Need to find the start and end index idx = np.cumsum(vad_tmp) start = int(np.where(np.cumsum(vad_tmp) == start)[0]) end = int(np.where(np.cumsum(vad_tmp) == end)[0]) feats_o.append( readhtk_segment(feats_dir + '/' + f + '.fea', start, end)) vad.append(vad_tmp[start:end]) idx = np.cumsum(np.vstack([len(feats_o[i]) for i in range(0, len(vad))])) idx = np.insert(idx, 0, 0) # Need these as arrays feats_o = np.vstack(feats_o[i] for i in range(0, len(feats_o))) vad = np.hstack(vad[i] for i in range(0, len(vad))) return [ np.hstack((feats_o, vad[:, np.newaxis].astype(floatX))), idx.astype(floatX) ]
def load_jhu_feat(feats_dir, files, floatX='float32', max_frame=-1): if (max_frame == -1): feats = [readhtk(feats_dir + '/' + f + '.fea') for f in files] else: n_avl_samp = [ pytel.htk.readhtk_header(feats_dir + '/' + f + '.fea')[0] for f in files ] feats = [ readhtk_segment(feats_dir + '/' + f + '.fea', start=0, end=np.min((max_frame, n_avl_samp[i]))) for i, f in enumerate(files) ] idx = np.cumsum(np.vstack([len(feats[i]) for i in range(0, len(feats))])) idx = np.insert(idx, 0, 0) return np.vstack(feats)[np.newaxis, :, :], idx
def load_jhu_feat_segm_fixed_len_plus_lab(feats_dir, lab_dir, files, min_len, max_len, min_len_B, max_len_B, floatX='float32', start_from_zero=False, suffix='fea', rng=np.random, segm_B_clean=1, segm_A=True, vad_dir=None, ab_same=False): # First we need to loop through the files and check the minimum available lenght n_avl_samp = np.zeros((len(files), 1), dtype=int) for i, f in enumerate(files): n_avl_samp[i] = pytel.htk.readhtk_header(feats_dir + '/' + f + '.' + suffix)[0] min_n_avl_samp = np.min(n_avl_samp) max_len = np.min( [max_len, min_n_avl_samp + 1] ) # Need to add 1 because max_len because the intervall is [min_len, max_len) if segm_A: n_sel_samp = rng.randint(min_len, max_len) # not [min_len, max_len] if ab_same: n_sel_samp_B = n_sel_samp else: n_sel_samp_B = rng.randint(min_len, max_len) # not [min_len, max_len] if (segm_B_clean >= 1): lab = [np.genfromtxt(lab_dir + fix_names(f) + ".hp") for f in files] else: lab = [np.genfromtxt(lab_dir + f + ".hp") for f in files] if (vad_dir != None): lab = [ lab[i][pytel.htk.read_lab_to_bool_vec( vad_dir + '/' + fix_names(f) + '.vad', true_label='speech', length=len(lab[i]))] for i, f in enumerate(files) ] # Sanity check if (len(lab[0]) != n_avl_samp[0]): log.warning( "WARNING: The length of lab is %d but lenght of features is %d. Is VAD missing on lab? " % (len(lab[0]), n_avl_samp[0])) n_failed = 0 for i, f in enumerate(files): segm_b_f = f if (segm_B_clean >= 2): segm_b_f = fix_names(segm_b_f) try: # The start_from_zero option is mainly for debugging/development if start_from_zero: if segm_A: start = 0 start_B = 0 else: if segm_A: last_possible_start = n_avl_samp[i] - n_sel_samp start = rng.randint( 0, last_possible_start + 1 ) # This means the intervall is [0,last_possible_start + 1) # = [0, last_possible_start] if ab_same: start_B = start else: last_possible_start_B = n_avl_samp[i] - n_sel_samp_B start_B = rng.randint(0, last_possible_start_B + 1) # - " - if segm_A: end = start + n_sel_samp if ab_same: end_B = end else: end_B = start_B + n_sel_samp_B if ((i - n_failed) == 0): if segm_A: feats_tmp = readhtk_segment( feats_dir + '/' + f + '.' + suffix, start, end)[np.newaxis, :, :] feats = np.zeros( (len(files), n_sel_samp, feats_tmp.shape[2]), dtype=floatX) feats[i - n_failed, :, :] = readhtk_segment( feats_dir + '/' + f + '.' + suffix, start, end)[np.newaxis, :, :] feats_tmp_B = readhtk_segment( feats_dir + '/' + segm_b_f + '.' + suffix, start_B, end_B)[np.newaxis, :, :] feats_B = np.zeros( (len(files), n_sel_samp_B, feats_tmp_B.shape[2]), dtype=floatX) feats_B[i - n_failed, :, :] = readhtk_segment( feats_dir + '/' + segm_b_f + '.' + suffix, start_B, end_B)[np.newaxis, :, :] lab[i - n_failed] = lab[i][start_B:end_B] else: if segm_A: feats[i - n_failed, :, :] = readhtk_segment( feats_dir + '/' + f + '.' + suffix, start, end)[np.newaxis, :, :] feats_B[i - n_failed, :, :] = readhtk_segment( feats_dir + '/' + segm_b_f + '.' + suffix, start_B, end_B)[np.newaxis, :, :] lab[i - n_failed] = lab[i][start_B:end_B] except Exception as e: log.warning("Failed to load file %s, %s", f) log.warning(e.__doc__) log.warning(e.message) n_failed += 1 lab = np.vstack(lab[0:i - n_failed + 1]) if n_failed > 0: if segm_A: return [feats[0:-n_failed], feats_B[0:-n_failed], lab[0:-n_failed]] else: return [feats_B[0:-n_failed], lab[0:-n_failed]] else: if segm_A: return [feats, feats_B, lab] else: return [feats_B, lab]
def load_jhu_feat_segm_fixed_len_plus_bottle_n_2(feats_dir, feats_dir_bn, files, min_len, max_len, min_len_2, max_len_2, floatX='float32', start_from_zero=False, suffix='fea', rng=np.random, bn_context=30, bn_clean=False): # First we need to loop through the files and check the minimum available lenght n_avl_samp = np.zeros((len(files), 1), dtype=int) for i, f in enumerate(files): n_avl_samp[i] = pytel.htk.readhtk_header(feats_dir + '/' + f + '.' + suffix)[0] min_n_avl_samp = np.min(n_avl_samp) max_len = np.min( [max_len, min_n_avl_samp + 1] ) # Need to add 1 because max_len because the intervall is [min_len, max_len) n_sel_samp = rng.randint(min_len, max_len) # not [min_len, max_len] n_sel_samp_2 = rng.randint(min_len, max_len) # not [min_len, max_len] n_failed = 0 for i, f in enumerate(files): bn_f = f if (bn_clean): bn_f = bn_f.replace("reverb/", "clean/") bn_f = bn_f.replace("music/", "clean/") bn_f = bn_f.replace("noise/", "clean/") bn_f = bn_f.replace("babble/", "clean/") try: # The start_from_zero option is mainly for debugging/development if start_from_zero: start = 0 start_2 = 0 else: last_possible_start = n_avl_samp[i] - n_sel_samp start = rng.randint( 0, last_possible_start + 1 ) # This means the intervall is [0,last_possible_start + 1) # = [0, last_possible_start] last_possible_start_2 = n_avl_samp[i] - n_sel_samp_2 start_2 = rng.randint(0, last_possible_start_2 + 1) # -"- end = start + n_sel_samp end_2 = start_2 + n_sel_samp_2 if ((i - n_failed) == 0): feats_tmp = readhtk_segment(feats_dir + '/' + f + '.' + suffix, start, end)[np.newaxis, :, :] feats = np.zeros((len(files), n_sel_samp, feats_tmp.shape[2]), dtype=floatX) feats[i - n_failed, :, :] = readhtk_segment( feats_dir + '/' + f + '.' + suffix, start, end)[np.newaxis, :, :] # Why not setting it to feat_tmp? bn_feats_tmp = readhtk_segment( feats_dir_bn + '/' + bn_f + '.' + suffix, start_2, end_2 - bn_context)[np.newaxis, :, :] bn_feats = np.zeros((len(files), n_sel_samp_2 - bn_context, bn_feats_tmp.shape[2]), dtype=floatX) bn_feats[i - n_failed, :, :] = readhtk_segment( feats_dir_bn + '/' + bn_f + '.' + suffix, start_2, end_2 - bn_context)[np.newaxis, :, :] else: feats[i - n_failed, :, :] = readhtk_segment( feats_dir + '/' + bn_f + '.' + suffix, start, end)[np.newaxis, :, :] bn_feats[i - n_failed, :, :] = readhtk_segment( feats_dir_bn + '/' + bn_f + '.' + suffix, start_2, end_2 - bn_context)[np.newaxis, :, :] except: log.warning("Failed to load file %s, %s", f, bn_f) n_failed += 1 if n_failed > 0: return [feats[0:-n_failed], bn_feats[0:-n_failed]] else: return [feats, bn_feats]