Ejemplo n.º 1
0
    def __init__(self, ds_path):
        super(SADreader, self).__init__()
        indices = os.path.join(ds_path, 'indices_sad')
        assert os.path.isfile(
            indices), "Cannot find indices at path: %s" % indices
        self.indices = F.MmapDict(indices, read_only=True)

        data = os.path.join(ds_path, 'sad')
        assert os.path.isfile(data), "Cannot find SAD at path: %s" % data
        self.data = F.MmapData(data, read_only=True)
Ejemplo n.º 2
0
def prepare_dnn_data(recipe, feat, utt_length, seed=87654321):
    """
  Return
  ------
  train_feeder : Feeder for training
  valid_feeder : Feeder for validating
  test_ids : Test indices
  test_dat : Data array
  all_speakers : list of all speaker in training set
  """
    # Load dataset
    frame_length = int(utt_length / FRAME_SHIFT)
    ds = F.Dataset(os.path.join(PATH_ACOUSTIC_FEAT, recipe), read_only=True)
    X = ds[feat]
    train_indices = {name: ds['indices'][name] for name in TRAIN_DATA.keys()}
    test_indices = {
        name: start_end
        for name, start_end in ds['indices'].items() if name not in TRAIN_DATA
    }
    train_indices, valid_indices = train_valid_test_split(x=list(
        train_indices.items()),
                                                          train=0.9,
                                                          inc_test=False,
                                                          seed=seed)
    all_speakers = sorted(set(TRAIN_DATA.values()))
    n_speakers = max(all_speakers) + 1
    print("#Train files:", ctext(len(train_indices), 'cyan'))
    print("#Valid files:", ctext(len(valid_indices), 'cyan'))
    print("#Test files:", ctext(len(test_indices), 'cyan'))
    print("#Speakers:", ctext(n_speakers, 'cyan'))
    recipes = [
        F.recipes.Sequencing(frame_length=frame_length,
                             step_length=frame_length,
                             end='pad',
                             pad_value=0,
                             pad_mode='post',
                             data_idx=0),
        F.recipes.Name2Label(lambda name: TRAIN_DATA[name], ref_idx=0),
        F.recipes.LabelOneHot(nb_classes=n_speakers, data_idx=1)
    ]
    train_feeder = F.Feeder(data_desc=F.IndexedData(data=X,
                                                    indices=train_indices),
                            batch_mode='batch',
                            ncpu=7,
                            buffer_size=12)
    valid_feeder = F.Feeder(data_desc=F.IndexedData(data=X,
                                                    indices=valid_indices),
                            batch_mode='batch',
                            ncpu=2,
                            buffer_size=4)
    train_feeder.set_recipes(recipes)
    valid_feeder.set_recipes(recipes)
    print(train_feeder)
    # ====== cache the test data ====== #
    cache_dat = os.path.join(PATH_EXP,
                             'test_%s_%d.dat' % (feat, int(utt_length)))
    cache_ids = os.path.join(PATH_EXP,
                             'test_%s_%d.ids' % (feat, int(utt_length)))
    # validate cache files
    if os.path.exists(cache_ids):
        with open(cache_ids, 'rb') as f:
            ids = pickle.load(f)
        if len(ids) != len(test_indices):
            os.remove(cache_ids)
            if os.path.exists(cache_dat):
                os.remove(cache_dat)
    elif os.path.exists(cache_dat):
        os.remove(cache_dat)
    # caching
    if not os.path.exists(cache_dat):
        dat = F.MmapData(cache_dat,
                         dtype='float16',
                         shape=(0, frame_length, X.shape[1]))
        ids = {}
        prog = Progbar(target=len(test_indices))
        s = 0
        for name, (start, end) in test_indices.items():
            y = X[start:end]
            y = segment_axis(y,
                             axis=0,
                             frame_length=frame_length,
                             step_length=frame_length,
                             end='pad',
                             pad_value=0,
                             pad_mode='post')
            dat.append(y)
            # update indices
            ids[name] = (s, s + len(y))
            s += len(y)
            # update progress
            prog.add(1)
        dat.flush()
        dat.close()
        with open(cache_ids, 'wb') as f:
            pickle.dump(ids, f)
    # ====== re-load ====== #
    dat = F.MmapData(cache_dat, read_only=True)
    with open(cache_ids, 'rb') as f:
        ids = pickle.load(f)
    # ====== save some sample ====== #
    sample_path = os.path.join(PATH_EXP,
                               'test_%s_%d.pdf' % (feat, int(utt_length)))
    V.plot_figure(nrow=9, ncol=6)
    for i, (name, (start, end)) in enumerate(
            sampling_iter(it=sorted(ids.items(), key=lambda x: x[0]),
                          k=12,
                          seed=87654321)):
        x = dat[start:end][:].astype('float32')
        ax = V.plot_spectrogram(x[np.random.randint(0, len(x))].T,
                                ax=(12, 1, i + 1),
                                title='')
        ax.set_title(name)
    V.plot_save(sample_path)
    return (train_feeder, valid_feeder, ids, dat, all_speakers)
Ejemplo n.º 3
0
        args.stat:
        print('========= Extracting statistics for: "%s" =========' % name)
        gmm.transform_to_disk(X=ds[FEAT],
                              indices=files,
                              pathZ=z_path,
                              pathF=f_path,
                              name_path=l_path,
                              dtype='float32',
                              device='cpu',
                              ncpu=None,
                              override=True)
    # load the statistics in MmapData
    y_true[name] = [
        fn_label(i) for i in np.genfromtxt(fname=l_path, dtype=str)
    ]
    stats[name] = (F.MmapData(path=z_path, read_only=True),
                   F.MmapData(path=f_path, read_only=True))
for name, x in stats.items():
    print(ctext(name + ':', 'cyan'), x)
# ===========================================================================
# Training T-matrix
# ===========================================================================
if not os.path.exists(TMAT_PATH) or args.tmat:
    tmat = ml.Tmatrix(tv_dim=TV_DIM,
                      gmm=gmm,
                      niter=TV_NITER,
                      dtype=TV_DTYPE,
                      batch_size_cpu='auto',
                      batch_size_gpu='auto',
                      device='gpu',
                      ncpu=1,
Ejemplo n.º 4
0
X, train, test = prepare_ivec_data(args.recipe, FEAT)
# ===========================================================================
# Training I-vector model
# ===========================================================================
ivec = ml.Ivector(path=MODEL_PATH,
                  nmix=args.nmix,
                  tv_dim=args.tdim,
                  niter_gmm=16,
                  niter_tmat=16,
                  downsample=2,
                  stochastic_downsample=True,
                  device='gpu',
                  name="VoxCelebIvec")
ivec.fit(X, indices=train, extract_ivecs=True, keep_stats=False)
# ====== extract train i-vector ====== #
I_train = F.MmapData(ivec.ivec_path, read_only=True)
name_train = np.genfromtxt(ivec.name_path, dtype=str)
print("Train i-vectors:", ctext(I_train, 'cyan'))
# save train i-vectors to csv
prog = Progbar(target=len(name_train),
               print_report=True,
               print_summary=True,
               name="Saving train i-vectors")
with open(TRAIN_PATH, 'w') as f_train:
    for i, name in enumerate(name_train):
        spk = TRAIN_DATA[name]
        vec = I_train[i]
        f_train.write('\t'.join([str(spk)] + [str(v) for v in vec]) + '\n')
        prog.add(1)
# ====== extract test i-vector ====== #
test = sorted(test.items(), key=lambda x: x[0])
Ejemplo n.º 5
0
    ]
    _ = []
    for ids in all_indices:
        _ += list(ids.keys())
    assert len(_) == len(set(_)), "Overlap indices name"
    # ====== initialize ====== #
    out_data = None
    out_indices = {}
    start = 0
    curr_nfile = 0
    prog = Progbar(target=sum(len(i) for i in all_indices),
                   print_summary=True,
                   name=outpath)

    for i, path in enumerate(inpath):
        in_data = F.MmapData(path=os.path.join(path, feat_name),
                             read_only=True)
        in_indices = all_indices[i]
        # initialize
        if out_data is None:
            out_data = F.MmapData(path=os.path.join(outpath, feat_name),
                                  dtype=in_data.dtype,
                                  shape=(0, ) + in_data.shape[1:],
                                  read_only=False)
        # copy data
        for name, (s, e) in list(in_indices.items()):
            X = in_data[s:e]
            out_data.append(X)
            out_indices[name] = (start, start + X.shape[0])
            start += X.shape[0]
            # update progress
            prog['name'] = name[:48]
Ejemplo n.º 6
0
                  stochastic_downsample=GMM_STOCHASTIC,
                  device='gpu',
                  ncpu=1,
                  gpu_factor_gmm=80,
                  gpu_factor_tmat=3,
                  dtype=DTYPE,
                  seed=1234,
                  name=None)
ivec.fit(X=X, indices=train, extract_ivecs=True, keep_stats=True)
ivec.transform(X=X,
               indices=test,
               save_ivecs=True,
               keep_stats=True,
               name='test')
# ====== i-vector ====== #
I_train = F.MmapData(path=ivec.get_i_path(None), read_only=True)
F_train = F.MmapData(path=ivec.get_f_path(None), read_only=True)
name_train = np.genfromtxt(ivec.get_name_path(None), dtype=str)

I_test = F.MmapData(path=ivec.get_i_path(name='test'), read_only=True)
F_test = F.MmapData(path=ivec.get_f_path(name='test'), read_only=True)
name_test = np.genfromtxt(ivec.get_name_path(name='test'), dtype=str)
# ===========================================================================
# I-vector
# ===========================================================================
X_train = I_train
X_test = I_test
# ====== cosine scoring ====== #
print(ctext("==== '%s'" % "Ivec cosine-scoring", 'cyan'))
scorer = ml.Scorer(centering=True, wccn=True, lda=True, method='cosine')
scorer.fit(X=X_train, y=y_train)