Convert index to character. :param torch.Tensor seqs_hat: prediction (batch, seqlen) :param torch.Tensor seqs_true: reference (batch, seqlen) :return: token list of prediction :rtype list :return: token list of reference :rtype list
(self, ys_hat, ys_pad)
| 182 | return cer_ctc |
| 183 | |
| 184 | def convert_to_char(self, ys_hat, ys_pad): |
| 185 | """Convert index to character. |
| 186 | |
| 187 | :param torch.Tensor seqs_hat: prediction (batch, seqlen) |
| 188 | :param torch.Tensor seqs_true: reference (batch, seqlen) |
| 189 | :return: token list of prediction |
| 190 | :rtype list |
| 191 | :return: token list of reference |
| 192 | :rtype list |
| 193 | """ |
| 194 | seqs_hat, seqs_true = [], [] |
| 195 | for i, y_hat in enumerate(ys_hat): |
| 196 | y_true = ys_pad[i] |
| 197 | eos_true = np.where(y_true == -1)[0] |
| 198 | ymax = eos_true[0] if len(eos_true) > 0 else len(y_true) |
| 199 | # NOTE: padding index (-1) in y_true is used to pad y_hat |
| 200 | seq_hat = [self.char_list[int(idx)] for idx in y_hat[:ymax]] |
| 201 | seq_true = [self.char_list[int(idx)] for idx in y_true if int(idx) != -1] |
| 202 | seq_hat_text = "".join(seq_hat).replace(self.space, " ") |
| 203 | seq_hat_text = seq_hat_text.replace(self.blank, "") |
| 204 | seq_true_text = "".join(seq_true).replace(self.space, " ") |
| 205 | seqs_hat.append(seq_hat_text) |
| 206 | seqs_true.append(seq_true_text) |
| 207 | return seqs_hat, seqs_true |
| 208 | |
| 209 | def calculate_cer(self, seqs_hat, seqs_true): |
| 210 | """Calculate sentence-level CER score. |