Пример #1
0
def train_plda(iv_file, train_list, val_list, preproc_file,
               epochs, ml_md, md_epochs,
               output_path, **kwargs):
    
    if preproc_file is not None:
        preproc = TransformList.load(preproc_file)
    else:
        preproc = None

    vcr_args = VCR.filter_args(**kwargs)
    vcr_train = VCR(iv_file, train_list, preproc, **vcr_args)
    x, class_ids = vcr_train.read()

    x_val = None
    class_ids_val = None
    if val_list is not None:
        vcr_val = VCR(iv_file, val_list, preproc, **vcr_args)
        x_val, class_ids_val = vcr_val.read()
        
    t1 = time.time()

    plda_args = F.filter_train_args(**kwargs)
    model = F.create_plda(**plda_args)
    elbos = model.fit(x, class_ids, x_val=x_val, class_ids_val=class_ids_val,
                      epochs=epochs, ml_md=ml_md, md_epochs=md_epochs)

    logging.info('Elapsed time: %.2f s.' % (time.time()-t1))
    
    model.save(output_path)

    elbo = np.vstack(elbos)
    num = np.arange(epochs)
    elbo = np.vstack((num, elbo)).T
    elbo_path=os.path.splitext(output_path)[0] + '.csv'
    np.savetxt(elbo_path, elbo, delimiter=',')
Пример #2
0
def train_be(iv_file, train_list, adapt_iv_file, adapt_list, lda_dim,
             plda_type, y_dim, z_dim, epochs, ml_md, md_epochs, w_mu, w_B, w_W,
             output_path, **kwargs):

    # Read data
    logging.info('loading data')
    vcr_args = VCR.filter_args(**kwargs)
    vcr_train = VCR(iv_file, train_list, None, **vcr_args)
    x, class_ids = vcr_train.read()

    # Train LDA
    logging.info('train LDA')
    t1 = time.time()
    lda = LDA(lda_dim=lda_dim, name='lda')
    lda.fit(x, class_ids)

    x_lda = lda.predict(x)
    logging.info('LDA elapsed time: %.2f s.' % (time.time() - t1))

    # Train centering and whitening
    logging.info('train length norm')
    t1 = time.time()
    lnorm = LNorm(name='lnorm')
    lnorm.fit(x_lda)

    x_ln = lnorm.predict(x_lda)
    logging.info('length norm elapsed time: %.2f s.' % (time.time() - t1))

    # Train PLDA
    logging.info('train PLDA')
    t1 = time.time()
    plda = F.create_plda(plda_type, y_dim=y_dim, z_dim=z_dim, name='plda')
    elbo = plda.fit(x_ln,
                    class_ids,
                    epochs=epochs,
                    ml_md=ml_md,
                    md_epochs=md_epochs)
    logging.info('PLDA elapsed time: %.2f s.' % (time.time() - t1))

    # Save models
    logging.info('saving models')
    preproc = TransformList(lda)
    preproc.append(lnorm)

    if not os.path.exists(output_path):
        os.makedirs(ouput_path)

    preproc.save(output_path + '/lda_lnorm.h5')
    plda.save(output_path + '/plda.h5')

    num = np.arange(epochs)
    elbo = np.vstack((num, elbo)).T
    np.savetxt(output_path + '/elbo.csv', elbo, delimiter=',')

    #adaptation
    vcr = VCR(adapt_iv_file, adapt_list, None)
    x, class_ids = vcr.read()
    x_lda = lda.predict(x)
    lnorm.update_T = False
    lnorm.fit(x_lda)

    preproc = TransformList(lda)
    preproc.append(lnorm)

    preproc.save(output_path + '/lda_lnorm_adapt.h5')

    x_ln = lnorm.predict(x_lda)

    plda_adapt = plda.copy()

    elbo = plda.fit(x_ln, class_ids, epochs=epochs)
    plda_adapt.weighted_avg_model(plda, w_mu, w_B, w_W)
    plda_adapt.save(output_path + '/plda_adapt.h5')

    num = np.arange(epochs)
    elbo = np.vstack((num, elbo)).T
    np.savetxt(output_path + '/elbo_adapt.csv', elbo, delimiter=',')