示例#1
0
    def __getitem__(self, index):
        if not self.io_status:
            return 0., 0.

        chunk = [
            int(self.data_frame[index][2]),
            int(self.data_frame[index][3])
        ]

        egs = kaldi_io.read_mat(self.data_frame[index][1], chunk=chunk)
        target = self.data_frame[index][4]
        alignment = kaldi_io.read_ali_ark(self.data_frame[index][5],
                                          chunk=chunk)  #Zheng Li 2020-10

        chunk_size = int(chunk[1] - chunk[0] + 1)

        alignment = np.ascontiguousarray(alignment)
        alignment = np.reshape(alignment, (chunk_size, 1))

        # Note, egs read from kaldi_io is read-only and
        # use egs = np.require(egs, requirements=['O', 'W']) to make it writeable.
        # It avoids the problem "ValueError: assignment destination is read-only".
        # Note that, do not use inputs.flags.writeable = True when the version of numpy >= 1.17.
        egs = np.require(egs, requirements=['O', 'W'])

        if self.aug is not None:
            return self.aug(egs.T), target, alignment
        else:
            return egs.T, target, alignment
示例#2
0
    def __getitem__(self, index):
        if not self.io_status:
            return 0., 0.

        chunk = [
            int(self.data_frame[index][2]),
            int(self.data_frame[index][3])
        ]

        egs = kaldi_io.read_mat(self.data_frame[index][1], chunk=chunk)

        if self.num_target_types == 1:
            target = self.data_frame[index][4]
        else:
            target = self.data_frame[index][4:]

        # Note, egs read from kaldi_io is read-only and
        # use egs = np.require(egs, requirements=['O', 'W']) to make it writeable.
        # It avoids the problem "ValueError: assignment destination is read-only".
        # Note that, do not use inputs.flags.writeable = True when the version of numpy >= 1.17.
        egs = np.require(egs, requirements=['O', 'W'])

        if self.aug is not None:
            return self.aug(egs.T), target
        else:
            return egs.T, target
示例#3
0
    def __getitem__(self, index):
        if not self.io_status:
            return 0., 0.

        # Decode string from bytes after using astype(np.string_).
        egs_path = str(self.ark_path[index][0], encoding='utf-8')

        if self.chunk_position is not None:
            chunk = [
                self.chunk_position[index][0], self.chunk_position[index][1]
            ]
        else:
            chunk = None

        if self.egs_type is "chunk":
            egs = kaldi_io.read_mat(egs_path, chunk=chunk)
        else:
            egs = kaldi_io.read_vec_flt(egs_path)

        target = self.label[index][0]

        # Note, egs which is read from kaldi_io is read-only and
        # use egs = np.require(egs, requirements=['O', 'W']) to make it writeable.
        # It avoids the problem "ValueError: assignment destination is read-only".
        # Note that, do not use inputs.flags.writeable = True when the version of numpy >= 1.17.
        egs = np.require(egs, requirements=['O', 'W'])

        if self.aug is not None:
            return self.aug(egs.T), target
        else:
            return egs.T, target
示例#4
0
    def get_base_attribute_(self):
        ## Base attribute
        # Total utts
        self.num_utts = len(self.utt2spk) if "utt2spk" in self.loaded_attr else None

        # Total spks
        self.num_spks = len(self.spk2utt) if "spk2utt" in self.loaded_attr else None

        # Total frames
        if "utt2num_frames" in self.loaded_attr:
            self.num_frames = 0
            for utt, num_frames in self.utt2num_frames.items():
                self.num_frames += num_frames
        else:
            self.num_frames = None

        # Feature dim
        self.feat_dim = kaldi_io.read_mat(
            self.feats_scp[list(self.feats_scp.keys())[0]]).shape[1] if "feats_scp" in self.loaded_attr else None
示例#5
0
    # 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:
        #         break
        #     print("Process utterance for key {0}".format(key))
        #     feats = kaldi_io.read_mat(r)
        #     embedding = model.extract_embedding(feats)
        #     kaldi_io.write_vec_flt(w, embedding.numpy(), key=key)

except BaseException as e:
    if not isinstance(e, KeyboardInterrupt):
        traceback.print_exc()
        )

    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:

        while (True):
            key = kaldi_io.read_key(r)

            if not key:
                break

            print("Process utterance for key {0}".format(key))

            feats = kaldi_io.read_mat(r)
            embedding = model.extract_embedding(feats)
            kaldi_io.write_vec_flt(w, embedding.numpy(), key=key)

except BaseException as e:
    if not isinstance(e, KeyboardInterrupt):
        traceback.print_exc()
    sys.exit(1)