conftest.py 1.01 KB
Newer Older
1
2
3
4
5
6
import torch
from torchaudio_unittest.common_utils import get_asset_path
import pytest


class GreedyCTCDecoder(torch.nn.Module):
7
    def __init__(self, labels, blank: int = 0):
8
        super().__init__()
9
        self.blank = blank
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
        self.labels = labels

    def forward(self, logits: torch.Tensor) -> str:
        """Given a sequence logits over labels, get the best path string

        Args:
            logits (Tensor): Logit tensors. Shape `[num_seq, num_label]`.

        Returns:
            str: The resulting transcript
        """
        best_path = torch.argmax(logits, dim=-1)  # [num_seq,]
        best_path = torch.unique_consecutive(best_path, dim=-1)
        hypothesis = []
        for i in best_path:
25
26
            if i != self.blank:
                hypothesis.append(self.labels[i])
27
28
29
30
31
32
33
34
35
36
37
        return ''.join(hypothesis)


@pytest.fixture
def ctc_decoder():
    return GreedyCTCDecoder


@pytest.fixture
def sample_speech_16000_en():
    return get_asset_path('Lab41-SRI-VOiCES-src-sp0307-ch127535-sg0042.flac')