def main(plda, enroll_xvector, test_xvector, trials, scores): with kaldi_io.open_or_fd(plda, 'rb') as f: for key, vec in kaldi_io.read_vec_flt_ark(f): if key == 'mean': mean = vec.reshape(-1, 1) dim = vec.shape[0] elif key == 'within_var': within_var = vec.reshape(dim, dim) else: between_var = vec.reshape(dim, dim) within_var = within_var + 5e-5 * np.eye(within_var.shape[0]) Gamma, Lambda, c, k = CalculateVar(between_var, within_var, mean) f_writer = open(scores, 'w') enrollutt2vector = {} for key, vector in kaldi_io.read_vec(enroll_xvector): enrollutt2vector[key] = vector testutt2vector = {} for key, vector in kaldi_io.read_vec(test_xvector): testutt2vector[key] = vector with open(trials, 'r') as f: for line in f: enroll, test, _ = line.strip().split() score = PLDAScoring(enrollutt2vector[enroll].reshape(-1,1),testutt2vector[test].reshape(-1,1),\ Gamma,Lambda,c,k) f_writer.write(enroll + ' ' + test + ' ' + str(score) + '\n') f_writer.close()
def plda_write(self, plda): with kaldi_io.open_or_fd(plda, 'wb') as f: kaldi_io.write_vec_flt(f, self.mean, key='mean') kaldi_io.write_vec_flt(f, self.within_var.reshape(-1, 1), key='within_var') kaldi_io.write_vec_flt(f, self.between_var.reshape(-1, 1), key='between_var')
def plda_read(self, plda): with kaldi_io.open_or_fd(plda, 'rb') as f: for key, vec in kaldi_io.read_vec_flt_ark(f): if key == 'mean': self.mean = vec.reshape(-1, 1) self.dim = self.mean.shape[0] elif key == 'within_var': self.within_var = vec.reshape(self.dim, self.dim) else: self.between_var = vec.reshape(self.dim, self.dim)
def plda_read(self,plda): with kaldi_io.open_or_fd(plda,'rb') as f: for key,vec in kaldi_io.read_vec_flt_ark(f): if key == 'mean': mean = vec.reshape(-1,1) dim = mean.shape[0] elif key == 'within_var': within_var = vec.reshape(dim, dim) else: between_var = vec.reshape(dim, dim) return mean,between_var,within_var
model_blueprint, model_creation = utils.read_nnet_config(args.nnet_config) elif args.model_blueprint is not None and args.model_creation is not None: model_blueprint = args.model_blueprint model_creation = args.model_creation else: raise ValueError("Expected nnet_config or (model_blueprint, model_creation) to exist.") model = utils.create_model_from_py(model_blueprint, model_creation) model.load_state_dict(torch.load(args.model_path, map_location='cpu'), strict=False) # Select device model = utils.select_model_device(model, args.use_gpu, gpu_id=args.gpu_id) model.eval() with kaldi_io.open_or_fd(args.feats_rspecifier, "rb") as r, \ kaldi_io.open_or_fd(args.vectors_wspecifier, 'wb') as w: for line in r: # (key, rxfile, chunk_start, chunk_end) = line.decode().split(' ') # chunk=[chunk_start, chunk_end] # print("Process utterance for key {0}".format(key)) # feats = kaldi_io.read_mat(rxfile, chunk=chunk) (key, rxfile) = line.decode().split(' ') print("Process utterance for key {0}".format(key)) feats = kaldi_io.read_mat(rxfile) embedding = model.extract_embedding(feats) kaldi_io.write_vec_flt(w, embedding.numpy(), key=key) # while(True): # key = kaldi_io.read_key(r) # if not key: