Exemple #1
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)
Exemple #2
0
def extract_ivector(seq_file, file_list, 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

    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)

    logging.info(time.time() - t1)
    logging.info(model.y_dim)
    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())
    keys = []
    for i in xrange(sr.num_seqs):
        x, key = sr.read_next_seq()
        logging.info('Extracting i-vector %d/%d for %s\n' %
                     (i, sr.num_seqs, key))
        keys.append(key)
        xx[:, :, :] = 0
        xx[0, :x.shape[0]] = x
        y[i] = model.compute_qy_x(xx, batch_size=1)[0]

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

    hw = HypDataWriter(output_path)
    hw.write(keys, '', y)
Exemple #3
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)
Exemple #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_embed(seq_file, file_list, model_file, preproc_file, output_path,
                  max_length, layer_names, **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

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

    t1 = time.time()

    model = SeqEmbed.load(model_file)
    model.build()
    print(layer_names)
    model.build_embed(layer_names)
    y_dim = model.embed_dim

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

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

    for i in xrange(sr.num_seqs):
        ti1 = time.time()
        x, key = sr.read_next_seq()
        ti2 = time.time()
        print('Extracting embeddings %d/%d for %s, num_frames: %d' %
              (i, sr.num_seqs, key, x.shape[0]))
        keys.append(key)
        xx[:, :, :] = 0

        if x.shape[0] <= max_length:
            xx[0, :x.shape[0]] = x
            y[i] = model.predict_embed(xx, batch_size=1)
        else:
            num_chunks = int(np.ceil(float(x.shape[0]) / max_length))
            chunk_size = int(np.ceil(float(x.shape[0]) / num_chunks))
            for j in xrange(num_chunks - 1):
                start = j * chunk_size
                xx[0, :chunk_size] = x[start:start + chunk_size]
                y[i] += model.predict_embed(xx, batch_size=1).ravel()
            xx[0, :chunk_size] = x[-chunk_size:]
            y[i] += model.predict_embed(xx, batch_size=1).ravel()
            y[i] /= num_chunks

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

    print('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)