Example #1
0
def eval_elbo(seq_file, file_list, model_file, preproc_file, output_file,
              ubm_type, **kwargs):

    sr_args = SR.filter_eval_args(**kwargs)

    if preproc_file is not None:
        preproc = TransformList.load(preproc_file)
    else:
        preproc = None

    sr = SR(seq_file,
            file_list,
            batch_size=1,
            shuffle_seqs=False,
            preproc=preproc,
            **sr_args)

    t1 = time.time()

    if ubm_type == 'diag-gmm':
        model = DiagGMM.load(model_file)
    else:
        model = DiagGMM.load_from_kaldi(model_file)
    model.initialize()

    elbo = np.zeros((sr.num_seqs, ), dtype=float_cpu())
    num_frames = np.zeros((sr.num_seqs, ), dtype=int)
    keys = []
    for i in xrange(sr.num_seqs):
        x, key = sr.read_next_seq()
        keys.append(key)
        elbo[i] = model.elbo(x)
        num_frames[i] = x.shape[0]

    num_total_frames = np.sum(num_frames)
    total_elbo = np.sum(elbo)
    total_elbo_norm = total_elbo / num_total_frames
    logging.info('Extract elapsed time: %.2f' % (time.time() - t1))
    s = 'Total ELBO: %f\nELBO_NORM %f' % (total_elbo, total_elbo_norm)
    logging.info(s)

    with open(output_file, 'w') as f:
        f.write(s)
Example #2
0
def extract_ivector(seq_file, file_list, gmm_file, model_file, preproc_file, output_path,
                    qy_only, **kwargs):

    set_float_cpu('float32')
    
    sr_args = SR.filter_eval_args(**kwargs)
    
    if preproc_file is not None:
        preproc = TransformList.load(preproc_file)
    else:
        preproc = None

    gmm = DiagGMM.load_from_kaldi(gmm_file)
        
    sr = SR(seq_file, file_list, batch_size=1,
            shuffle_seqs=False,
            preproc=preproc, **sr_args)
    
    t1 = time.time()

    # if qy_only:
    #     model = TVAEY.load(model_file)
    # else:
    model = TVAEYZ.load(model_file)
        
    model.build(max_seq_length=sr.max_batch_seq_length)
            
    y = np.zeros((sr.num_seqs, model.y_dim), dtype=float_keras())
    xx = np.zeros((1, sr.max_batch_seq_length, model.x_dim), dtype=float_keras())
    rr = np.zeros((1, sr.max_batch_seq_length, model.r_dim), dtype=float_keras())
    keys = []
    for i in xrange(sr.num_seqs):
        ti1 = time.time()
        x, key = sr.read_next_seq()
        ti2 = time.time()
        r = gmm.compute_z(x)
        ti3 = time.time()
        logging.info('Extracting i-vector %d/%d for %s, num_frames: %d' % (i, sr.num_seqs, key, x.shape[0]))
        keys.append(key)
        xx[:,:,:] = 0
        rr[:,:,:] = 0
        xx[0,:x.shape[0]] = x
        rr[0,:x.shape[0]] = r
        y[i] = model.compute_qy_x([xx, rr], batch_size=1)[0]
        ti4 = time.time()
        logging.info('Elapsed time i-vector %d/%d for %s, total: %.2f read: %.2f, gmm: %.2f, vae: %.2f' %
                     (i, sr.num_seqs, key, ti4-ti1, ti2-ti1, ti3-ti2, ti4-ti3))
            
    logging.info('Extract elapsed time: %.2f' % (time.time() - t1))
    
    hw = HypDataWriter(output_path)
    hw.write(keys, '', y)
Example #3
0
def init_ubm(seq_file, train_list, x_dim, num_comp,
             output_path, **kwargs):

    if seq_file is None:
        model = DiagGMM(x_dim=x_dim, num_comp=1)
        model.initialize()
        model.save(output_path)

        
    sr_args = SR.filter_args(**kwargs)
    sr = SR(seq_file, train_list, batch_size=1, **sr_args)
Example #4
0
def compute_gmm_post(seq_file, file_list, model_file, preproc_file,
                     output_path, num_comp, **kwargs):

    sr_args = SR.filter_eval_args(**kwargs)

    if preproc_file is not None:
        preproc = TransformList.load(preproc_file)
    else:
        preproc = None

    gmm = DiagGMM.load_from_kaldi(model_file)

    sr = SR(seq_file,
            file_list,
            batch_size=1,
            shuffle_seqs=False,
            preproc=preproc,
            **sr_args)

    t1 = time.time()

    logging.info(time.time() - t1)
    index = np.zeros((sr.num_seqs, num_comp), dtype=int)

    hw = HypDataWriter(output_path)
    for i in xrange(sr.num_seqs):
        x, key = sr.read_next_seq()
        logging.info('Extracting i-vector %d/%d for %s, num_frames: %d' %
                     (i, sr.num_seqs, key, x.shape[0]))
        r = gmm.compute_z(x)
        r_s, index = to_sparse(r, num_comp)
        if i == 0:
            r2 = to_dense(r_s, index, r.shape[1])
            logging.degug(np.sort(r[0, :])[-12:])
            logging.degug(np.sort(r2[0, :])[-12:])
            logging.degug(np.argsort(r[0, :])[-12:])
            logging.degug(np.argsort(r2[0, :])[-12:])

        hw.write([key], '.r', [r_s])
        hw.write([key], '.index', [index])

    logging.info('Extract elapsed time: %.2f' % (time.time() - t1))
def extract_ivector(seq_file, file_list, gmm_file, model_file, preproc_file,
                    output_path, qy_only, **kwargs):

    set_float_cpu('float32')

    sr_args = SR.filter_eval_args(**kwargs)

    if preproc_file is not None:
        preproc = TransformList.load(preproc_file)
    else:
        preproc = None

    gmm = DiagGMM.load_from_kaldi(gmm_file)

    sr = SR(seq_file,
            file_list,
            batch_size=1,
            shuffle_seqs=False,
            preproc=preproc,
            **sr_args)

    t1 = time.time()

    # if qy_only:
    #     model = TVAEY.load(model_file)
    # else:
    model = TVAEYZ.load(model_file)

    #model.build(max_seq_length=sr.max_batch_seq_length)
    #model.build(max_seq_length=1)
    model.x_dim = 60
    model.r_dim = 2048
    model.y_dim = 400

    y = np.zeros((sr.num_seqs, model.y_dim), dtype=float_keras())
    xx = np.zeros((1, sr.max_batch_seq_length, model.x_dim),
                  dtype=float_keras())
    rr = np.zeros((1, sr.max_batch_seq_length, model.r_dim),
                  dtype=float_keras())
    keys = []

    xp = Input(shape=(
        sr.max_batch_seq_length,
        model.x_dim,
    ))
    rp = Input(shape=(
        sr.max_batch_seq_length,
        model.r_dim,
    ))
    qy_param = model.qy_net([xp, rp])
    qy_net = Model([xp, rp], qy_param)
    for i in xrange(sr.num_seqs):
        ti1 = time.time()
        x, key = sr.read_next_seq()
        ti2 = time.time()
        r = gmm.compute_z(x)
        ti3 = time.time()
        logging.info('Extracting i-vector %d/%d for %s, num_frames: %d' %
                     (i, sr.num_seqs, key, x.shape[0]))
        keys.append(key)
        # xp = Input(shape=(x.shape[0], model.x_dim,))
        # rp = Input(shape=(x.shape[0], model.r_dim,))
        # qy_param = model.qy_net([xp, rp])
        ti5 = time.time()
        xx[:, :, :] = 0
        rr[:, :, :] = 0
        xx[0, :x.shape[0]] = x
        rr[0, :x.shape[0]] = r
        # x = np.expand_dims(x, axis=0)
        # r = np.expand_dims(r, axis=0)
        # qy_net = Model([xp, rp], qy_param)
        y[i] = qy_net.predict([xx, rr], batch_size=1)[0]
        # del qy_net
        # y[i] = model.compute_qy_x2([x, r], batch_size=1)[0]
        #for i in xrange(10):
        #gc.collect()
        ti4 = time.time()
        logging.info(
            'Elapsed time i-vector %d/%d for %s, total: %.2f read: %.2f, gmm: %.2f, vae: %.2f qy: %.2f'
            % (i, sr.num_seqs, key, ti4 - ti1, ti2 - ti1, ti3 - ti2, ti4 - ti5,
               ti5 - ti3))

        # print('Elapsed time i-vector %d/%d for %s, total: %.2f read: %.2f, gmm: %.2f, vae: %.2f' %
        #       (i, sr.num_seqs, key, ti4-ti1, ti2-ti1, ti3-ti2, ti4-ti3))

    logging.info('Extract elapsed time: %.2f' % (time.time() - t1))

    hw = HypDataWriter(output_path)
    hw.write(keys, '', y)
def extract_ivector(seq_file, file_list, gmm_file, model_file, preproc_file,
                    output_path, qy_only, max_length, **kwargs):

    set_float_cpu('float32')

    sr_args = SR.filter_eval_args(**kwargs)

    if preproc_file is not None:
        preproc = TransformList.load(preproc_file)
    else:
        preproc = None

    gmm = DiagGMM.load_from_kaldi(gmm_file)

    sr = SR(seq_file,
            file_list,
            batch_size=1,
            shuffle_seqs=False,
            preproc=preproc,
            **sr_args)

    t1 = time.time()

    # if qy_only:
    #     model = TVAEY.load(model_file)
    # else:
    model = TVAEYZ.load(model_file)

    #model.build(max_seq_length=sr.max_batch_seq_length)
    model.build(max_seq_length=1)

    max_length = np.minimum(sr.max_batch_seq_length, max_length)

    y = np.zeros((sr.num_seqs, model.y_dim), dtype=float_keras())
    xx = np.zeros((1, max_length, model.x_dim), dtype=float_keras())
    rr = np.zeros((1, max_length, model.r_dim), dtype=float_keras())
    keys = []

    xp = Input(shape=(
        max_length,
        model.x_dim,
    ))
    rp = Input(shape=(
        max_length,
        model.r_dim,
    ))
    qy_param = model.qy_net([xp, rp])
    qy_net = Model([xp, rp], qy_param)

    for i in xrange(sr.num_seqs):
        ti1 = time.time()
        x, key = sr.read_next_seq()
        ti2 = time.time()
        r = gmm.compute_z(x)
        ti3 = time.time()
        logging.info('Extracting i-vector %d/%d for %s, num_frames: %d' %
                     (i, sr.num_seqs, key, x.shape[0]))
        keys.append(key)
        xx[:, :, :] = 0
        rr[:, :, :] = 0

        if x.shape[0] <= max_length:
            xx[0, :x.shape[0]] = x
            rr[0, :x.shape[0]] = r
            y[i] = qy_net.predict([xx, rr], batch_size=1)[0]
        else:
            num_batches = int(np.ceil(x.shape[0] / max_length))
            for j in xrange(num_batches - 1):
                start = j * max_length
                xx[0] = x[start:start + max_length]
                rr[0] = r[start:start + max_length]
                y[i] += qy_net.predict([xx, rr], batch_size=1)[0].ravel()
            xx[0] = x[-max_length:]
            rr[0] = r[-max_length:]
            y[i] += qy_net.predict([xx, rr], batch_size=1)[0].ravel()
            y[i] /= num_batches

        ti4 = time.time()
        logging.info(
            'Elapsed time i-vector %d/%d for %s, total: %.2f read: %.2f, gmm: %.2f, vae: %.2f'
            %
            (i, sr.num_seqs, key, ti4 - ti1, ti2 - ti1, ti3 - ti2, ti4 - ti3))

    logging.info('Extract elapsed time: %.2f' % (time.time() - t1))

    hw = HypDataWriter(output_path)
    hw.write(keys, '', y)
Example #7
0
def train_tvae(seq_file, train_list, val_list, gmm_file, decoder_file, qy_file,
               qz_file, init_path, epochs, batch_size, preproc_file,
               output_path, num_samples_y, num_samples_z, px_form, qy_form,
               qz_form, min_kl, **kwargs):

    set_float_cpu(float_keras())

    sr_args = SR.filter_args(**kwargs)
    sr_val_args = SR.filter_val_args(**kwargs)
    opt_args = KOF.filter_args(**kwargs)
    cb_args = KCF.filter_args(**kwargs)

    if preproc_file is not None:
        preproc = TransformList.load(preproc_file)
    else:
        preproc = None

    gmm = DiagGMM.load_from_kaldi(gmm_file)

    sr = SR(seq_file,
            train_list,
            batch_size=batch_size,
            preproc=preproc,
            **sr_args)
    max_length = sr.max_batch_seq_length
    gen_val = None
    if val_list is not None:
        sr_val = SR(seq_file,
                    val_list,
                    batch_size=batch_size,
                    preproc=preproc,
                    shuffle_seqs=False,
                    seq_split_mode='sequential',
                    seq_split_overlap=0,
                    reset_rng=True,
                    **sr_val_args)
        max_length = max(max_length, sr_val.max_batch_seq_length)
        gen_val = data_generator(sr_val, gmm, max_length)

    gen_train = data_generator(sr, gmm, max_length)

    t1 = time.time()

    if init_path is None:
        decoder = load_model_arch(decoder_file)
        qy = load_model_arch(qy_file)

        # if qz_file is None:
        #     vae = TVAEY(qy, decoder, px_cond_form=px_form,
        #                 qy_form=qy_form, min_kl=min_kl)
        #     vae.build(num_samples=num_samples_y,
        #               max_seq_length = max_length)
        # else:
        qz = load_model_arch(qz_file)
        vae = TVAEYZ(qy,
                     qz,
                     decoder,
                     px_cond_form=px_form,
                     qy_form=qy_form,
                     qz_form=qz_form,
                     min_kl=min_kl)
    else:
        vae = TVAEYZ.load(init_path)

    vae.build(num_samples_y=num_samples_y,
              num_samples_z=num_samples_z,
              max_seq_length=max_length)
    logging.info(time.time() - t1)

    cb = KCF.create_callbacks(vae, output_path, **cb_args)
    opt = KOF.create_optimizer(**opt_args)

    h = vae.fit_generator(gen_train,
                          x_val=gen_val,
                          steps_per_epoch=sr.num_batches,
                          validation_steps=sr_val.num_batches,
                          optimizer=opt,
                          epochs=epochs,
                          callbacks=cb,
                          max_queue_size=10)

    # if vae.x_chol is not None:
    #     x_chol = np.array(K.eval(vae.x_chol))
    #     logging.info(x_chol[:4,:4])

    logging.info('Train elapsed time: %.2f' % (time.time() - t1))

    vae.save(output_path + '/model')
    sr_val.reset()
    y_val, sy_val, z_val, srz_val = vae.encoder_net.predict_generator(
        gen_val, steps=400)

    from scipy import linalg as la
    yy = y_val - np.mean(y_val, axis=0)
    cy = np.dot(yy.T, yy) / yy.shape[0]
    l, v = la.eigh(cy)
    np.savetxt(output_path + '/l1.txt', l)

    sr_val.reset()
    y_val2, sy_val2 = vae.qy_net.predict_generator(gen_val, steps=400)
    yy = y_val2 - np.mean(y_val, axis=0)
    cy = np.dot(yy.T, yy) / yy.shape[0]
    l, v = la.eigh(cy)
    np.savetxt(output_path + '/l2.txt', l)

    logging.info(y_val - y_val2)