예제 #1
0
파일: gs.c 프로젝트: 4auka/cmusphinx
gs_t *
gs_read(const char *file, logmath_t *logmath)
{

    int32 code_id;
    int32 m_id, s_id, c_id;
    bitvec_t *bv;
    gs_t *gs;

    E_INFO("Reading gaussian selector map: %s\n", file);
    gs = (gs_t *) ckd_calloc(1, sizeof(gs_t));
    if (gs == NULL)
        E_FATAL("Cannot allocate gs\n");

    if ((gs->fp = fopen(file, "rb")) == NULL)
        E_FATAL("gs_read(%s,rb) failed\n", file);

    gs->logmath = logmath;
    gs->n_mgau = gs_fread_int32(gs);

    E_INFO("The number of mixtures of gaussian: %d\n", gs->n_mgau);
    gs->n_feat = gs_fread_int32(gs);
    E_INFO("The number of features stream: %d\n", gs->n_feat);
    gs->n_density = gs_fread_int32(gs);
    E_INFO("The number of density: %d\n", gs->n_density);
    gs->n_code = gs_fread_int32(gs);
    E_INFO("The number of code word: %d\n", gs->n_code);
    gs->n_featlen = gs_fread_int32(gs);
    E_INFO("The feature length: %d\n", gs->n_featlen);

    gs->n_mbyte = bitvec_size(gs->n_density) * sizeof(bitvec_t);
    E_INFO("The number of byte to read: %d\n", gs->n_mbyte);

    /* allocate the bit vector here */
    bv = bitvec_alloc(gs->n_density);


    /* allocate memory for the data structure */
    /* n_code * n_featlen */
    gs->codeword =
        (float32 **) ckd_calloc_2d(gs->n_code, gs->n_featlen,
                                   sizeof(float32));
    /* n_mgau * n_feat * n_code */
    /*Hack ! assume feature stream to be only 1 */
    gs->codemap =
        (uint32 ***) ckd_calloc_3d(gs->n_mgau, gs->n_feat, gs->n_code,
                                   sizeof(uint32));
    gs->mgau_sl = (int32 *) ckd_calloc(gs->n_density + 1, sizeof(int32));

    for (code_id = 0; code_id < gs->n_code; code_id++) {
        for (c_id = 0; c_id < gs->n_featlen; c_id++) {
            gs->codeword[code_id][c_id] = gs_fread_float32(gs);
        }
        for (m_id = 0; m_id < gs->n_mgau; m_id++) {
            for (s_id = 0; s_id < gs->n_feat; s_id++) {
                /*The writer currently doesn't support the byte order */
                gs_fread_bitvec_t(bv, gs);
                gs->codemap[m_id][s_id][code_id] = *bv;
            }
        }
    }
    return gs;
}
예제 #2
0
파일: gs.c 프로젝트: 4auka/cmusphinx
int
gs_display(char *file, gs_t * gs)
{
    int32 i;
    int32 code_id;
    int32 m_id, s_id, c_id;
    float32 tmp;
    bitvec_t *bv;

    E_INFO("Reading gaussian selector map: %s\n", file);
    gs = (gs_t *) ckd_calloc(1, sizeof(gs_t));

    if ((gs->fp = fopen(file, "rb")) == NULL)
        E_FATAL("fopen(%s,rb) failed\n", file);

    gs->n_mgau = gs_fread_int32(gs);

    E_INFO("The number of mixtures of gaussian: %d\n", gs->n_mgau);
    gs->n_feat = gs_fread_int32(gs);
    E_INFO("The number of features stream: %d\n", gs->n_feat);
    gs->n_density = gs_fread_int32(gs);
    E_INFO("The number of density: %d\n", gs->n_density);
    gs->n_code = gs_fread_int32(gs);
    E_INFO("The number of code word: %d\n", gs->n_code);
    gs->n_featlen = gs_fread_int32(gs);
    E_INFO("The feature length: %d\n", gs->n_featlen);
    gs->n_mbyte = bitvec_size(gs->n_density) * sizeof(bitvec_t);
    E_INFO("The number of byte to read: %d\n", gs->n_mbyte);

    /* allocate the bit vector here */
    bv = bitvec_alloc(gs->n_density);

    /*  for(i=0;i<gs->n_code;i++) */
    for (code_id = 0; code_id < gs->n_code; code_id++) {
        printf("Code idx: %d\n", code_id);
        for (c_id = 0; c_id < gs->n_featlen; c_id++) {
            tmp = gs_fread_float32(gs);
            printf("%f ", tmp);
        }
        printf("\n");
        for (m_id = 0; m_id < gs->n_mgau; m_id++) {
            for (s_id = 0; s_id < gs->n_feat; s_id++) {
                /*The writer currently doesn't support the byte order */
                gs_fread_bitvec_t(bv, gs);
                printf("%d %d ", m_id, s_id);

                for (i = 0; i < gs->n_density; i++) {
                    if (bitvec_is_set(bv, i)) {
                        printf("%d ", i);
                    }
                }
                printf("\n");
            }
        }
    }

    printf("\n");
    /*  bitvec_free(bv); */
    /* destroy the bit vector here */

    gs_free(gs);
    return 1;
}
/**
 * Build net from phone HMMs
 */
static int
phmm_build(allphone_search_t * allphs)
{
    phmm_t *p, **pid2phmm;
    bin_mdef_t *mdef;
    int32 lrc_size;
    uint32 *lc, *rc;
    s3pid_t pid;
    s3cipid_t ci;
    s3cipid_t *filler;
    int n_phmm, n_link;
    int i, nphone;

    mdef = ((ps_search_t *) allphs)->acmod->mdef;
    allphs->ci_phmm =
        (phmm_t **) ckd_calloc(bin_mdef_n_ciphone(mdef), sizeof(phmm_t *));
    pid2phmm =
        (phmm_t **) ckd_calloc(bin_mdef_n_phone(mdef), sizeof(phmm_t *));

    /* For each unique ciphone/triphone entry in mdef, create a PHMM node */
    n_phmm = 0;
    nphone = allphs->ci_only ? bin_mdef_n_ciphone(mdef) : bin_mdef_n_phone(mdef);
    E_INFO("Building PHMM net of %d phones\n", nphone);
    for (pid = 0; pid < nphone; pid++) {
        if ((p = phmm_lookup(allphs, pid)) == NULL) {
            //not found, should be created
            p = (phmm_t *) ckd_calloc(1, sizeof(*p));
            hmm_init(allphs->hmmctx, &(p->hmm), FALSE,
                     mdef_pid2ssid(mdef, pid), mdef->phone[pid].tmat);
            p->pid = pid;
            p->ci = bin_mdef_pid2ci(mdef, pid);
            p->succlist = NULL;
            p->next = allphs->ci_phmm[p->ci];
            allphs->ci_phmm[p->ci] = p;
            n_phmm++;
        }
        pid2phmm[pid] = p;
    }

    /* Fill out bitvecs of each PHMM node, alloc continuous memory chunk for context bitvectors */
    lrc_size = bitvec_size(bin_mdef_n_ciphone(mdef));
    lc = ckd_calloc(n_phmm * 2 * lrc_size, sizeof(bitvec_t));
    rc = lc + (n_phmm * lrc_size);
    for (ci = 0; ci < mdef->n_ciphone; ci++) {
        for (p = allphs->ci_phmm[ci]; p; p = p->next) {
            p->lc = lc;
            lc += lrc_size;
            p->rc = rc;
            rc += lrc_size;
        }
    }

    /* Fill out lc and rc bitmaps (remember to map all fillers to each other!!) */
    filler =
        (s3cipid_t *) ckd_calloc(bin_mdef_n_ciphone(mdef) + 1,
                                 sizeof(s3cipid_t));

    /* Connect fillers */
    i = 0;
    for (ci = 0; ci < bin_mdef_n_ciphone(mdef); ci++) {
        p = pid2phmm[ci];
        bitvec_set_all(p->lc, bin_mdef_n_ciphone(mdef));
        bitvec_set_all(p->rc, bin_mdef_n_ciphone(mdef));
        if (mdef->phone[ci].info.ci.filler) {
            filler[i++] = ci;
        }
    }
    filler[i] = BAD_S3CIPID;


    /* Loop over cdphones only if ci_only is not set */
    for (pid = bin_mdef_n_ciphone(mdef); pid < nphone;
         pid++) {
        p = pid2phmm[pid];

        if (mdef->phone[mdef->phone[pid].info.cd.ctx[1]].info.ci.filler) {
            for (i = 0; IS_S3CIPID(filler[i]); i++)
                bitvec_set(p->lc, filler[i]);
        }
        else
            bitvec_set(p->lc, mdef->phone[pid].info.cd.ctx[1]);

        if (mdef->phone[mdef->phone[pid].info.cd.ctx[2]].info.ci.filler) {
            for (i = 0; IS_S3CIPID(filler[i]); i++)
                bitvec_set(p->rc, filler[i]);
        }
        else
            bitvec_set(p->rc, mdef->phone[pid].info.cd.ctx[2]);
    }
    ckd_free(pid2phmm);
    ckd_free(filler);

    /* Create links between PHMM nodes */
    n_link = phmm_link(allphs);

    E_INFO("%d nodes, %d links\n", n_phmm, n_link);
    return 0;
}