def train(feat_file, model_dir, M, ivector_dim=None, num_gselect=None):
    """
    This function will call the Bash script to train an i-vector extractor (and its corresponding UBM)
    Inputs:
        feat_file: Path to the Kaldi script (.spc) file with the features to use for i-vector training
        model_dir: Path where the model will be stored. It will create a sub-folder according to the number of Gaussians.
        M: Number of Gaussians in the UBM
        ivector_dim: dimension of the i-vectors
        num_gselect: Number of gaussians for the gaussian selection process

    Returns:
        num_gselect: Number of gaussians for the gaussian selection process so it can be used during the i-vectors extraction
    """
    if num_gselect == None or ivector_dim == None:
        k = int(np.log2(M))
    if num_gselect == None:
        num_gselect = k + 1
    if ivector_dim == None:
        # Read to obtain the dimension of the feature vector
        for key, mat in kaldi_io.read_mat_scp(feat_file):
            feat_dim = mat.shape[1]
            break
        ivector_dim = k * feat_dim
    os.system("kaldi_ivector/train_ivector_models.sh " + str(M) + " " +
              str(ivector_dim) + " " + str(num_gselect) + " " + feat_file +
              " " + model_dir)
    return num_gselect
Ejemplo n.º 2
0
 def _get_from_loader(self, filepath, filetype):
     if filetype in ["mat", "vec"]:
         if not self.keep_all_data_on_mem:
             return read_mat(filepath)
         if filepath not in self._loaders:
             self._loaders[filepath] = read_mat(filepath)
         return self._loaders[filepath]
     elif filetype == "scp":
         filepath, key = filepath.split(":", 1)
         loader = self._loaders.get(filepath)
         if loader is None:
             loader = read_mat_scp(filepath)
             self._loaders[filepath] = loader
         return loader[key]
     else:
         raise NotImplementedError(f"Not supported: loader_type={filetype}")
Ejemplo n.º 3
0
    def read_ali_and_compute_prior(self, path, initial_prior):
        """

        :param path: path to read alignments. Alignments must be in Kaldi feature format with one hot encoding. TODO: Check sparse options
        :param initial_prior: initial priors in same shape. if None, priors are not calculated
        :return: alignment keys, alignements, prior
        """
        tr_ali = []
        key2 = []
        print 'reading train alignments'
        prior = initial_prior
        for _key, _ali in kaldi_io.read_mat_scp(path):
            tr_ali.extend(_ali)
            key2.append(_key)
            if not initial_prior is None:
                prior = np.add(prior, np.asarray(_ali).sum(axis=0))
        return key2, tr_ali, prior
Ejemplo n.º 4
0
    def read_feats(self, path, return_feat_len=False):
        """

        :param path: path to features in Kaldi format
        :param return_feat_len: If True return number of features for each utterance. Useful in decoding
        :return: return feature keys, feature matrix [if return_feat_lean is True, returns feat_len]
        """
        mat = []
        keys = []
        feat_len = []
        print 'reading feats:', path
        for _key, _mat in kaldi_io.read_mat_scp(path):
            mat.extend(_mat)
            keys.append(_key)
            if return_feat_len == True:
                feat_len.append(len(_mat))

        if return_feat_len:
            return keys, mat, feat_len
        else:
            return keys, mat
Ejemplo n.º 5
0
 def load_variance_scp(self, variance_scp):
     if len(self.variance_feats) == 0:
         data = read_mat_scp(variance_scp)
         for k, v in data:
             self.variance_feats[k] = v
Ejemplo n.º 6
0
 def load_mel_scp(self, mel_scp):
     if len(self.mel_feats) == 0:
         data = read_mat_scp(mel_scp)
         for k, v in data:
             self.mel_feats[k] = v