PRIYANSHUDHAKED commited on
Commit
ad1a612
·
verified ·
1 Parent(s): 1c2f0d1

Delete src/miocodec/module/ssl_extractor.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. src/miocodec/module/ssl_extractor.py +0 -106
src/miocodec/module/ssl_extractor.py DELETED
@@ -1,106 +0,0 @@
1
- import torch
2
- import torch.nn as nn
3
- import torchaudio
4
- import torchaudio.pipelines as pipelines
5
- from torchaudio.models.wav2vec2 import Wav2Vec2Model
6
- from torchaudio.models.wav2vec2.components import ConvLayerBlock
7
-
8
- from ..util import get_logger
9
-
10
- logger = get_logger()
11
-
12
-
13
- # Map of friendly names to torchaudio pipeline bundles
14
- MODEL_REGISTRY = {
15
- "wav2vec2_base": pipelines.WAV2VEC2_BASE,
16
- "wav2vec2_large": pipelines.WAV2VEC2_LARGE,
17
- "wav2vec2_large_lv60k": pipelines.WAV2VEC2_LARGE_LV60K,
18
- "hubert_base": pipelines.HUBERT_BASE,
19
- "hubert_large": pipelines.HUBERT_LARGE,
20
- "hubert_xlarge": pipelines.HUBERT_XLARGE,
21
- "wavlm_base": pipelines.WAVLM_BASE,
22
- "wavlm_base_plus": pipelines.WAVLM_BASE_PLUS,
23
- "wavlm_large": pipelines.WAVLM_LARGE,
24
- }
25
-
26
-
27
- class SSLFeatureExtractor(nn.Module):
28
- def __init__(self, model_name: str = "wavlm_base_plus", output_layer: int | None = None, sample_rate: int = 16000):
29
- """
30
- Args:
31
- model_name: Name of the SSL model to use
32
- output_layer: Which layer's features to extract (None for last layer), 1-based indexing
33
- sample_rate: Sample rate of input audio
34
- """
35
- super().__init__()
36
- self.output_layer = output_layer if output_layer is not None else -1
37
-
38
- if model_name not in MODEL_REGISTRY:
39
- raise ValueError(f"Unknown model: {model_name}. Available models: {list(MODEL_REGISTRY.keys())}")
40
- bundle = MODEL_REGISTRY[model_name]
41
- self.model: Wav2Vec2Model = bundle.get_model()
42
- self.model.eval()
43
- self.feature_dim: int = bundle._params["encoder_embed_dim"]
44
-
45
- self.ssl_sample_rate = bundle.sample_rate
46
- # Create resampler if needed
47
- if sample_rate != self.ssl_sample_rate:
48
- logger.debug(f"Resampling from {sample_rate} to {self.ssl_sample_rate} required by {model_name}.")
49
- self.resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=self.ssl_sample_rate)
50
- else:
51
- self.resampler = None
52
-
53
- @property
54
- def hop_size(self) -> int:
55
- """Get the hop size of the model's convolutional layers."""
56
- hop_size = 1
57
- for _, stride in self.conv_config:
58
- hop_size *= stride
59
- return hop_size
60
-
61
- @property
62
- def conv_config(self) -> list[tuple[int, int]]:
63
- """Get the configuration of the convolutional layers in the model."""
64
- conv_layers = []
65
- for layer in self.model.feature_extractor.conv_layers:
66
- layer: ConvLayerBlock
67
- conv_layers.append((layer.kernel_size, layer.stride))
68
- return conv_layers
69
-
70
- def get_minimum_input_length(self, desired_output_length: int) -> int:
71
- """Calculate the minimum input length required to produce a given output length."""
72
- length = desired_output_length
73
- for kernel_size, stride in reversed(self.conv_config):
74
- length = (length - 1) * stride + kernel_size
75
- return length
76
-
77
- @torch.no_grad()
78
- def forward(
79
- self,
80
- waveform: torch.Tensor,
81
- lengths: torch.Tensor | None = None,
82
- num_layers: int | None = None,
83
- return_lengths: bool = False,
84
- ) -> list[torch.Tensor]:
85
- """
86
- Args:
87
- waveform: (batch_size, num_samples)
88
- lengths: Optional tensor of sequence lengths for each batch item (used for attention masking)
89
-
90
- Returns:
91
- features: List of feature tensors for each layer (batch_size, frame, dim)
92
- lengths: Sequence lengths for each batch item
93
- """
94
- if waveform.dim() == 1:
95
- waveform = waveform.unsqueeze(0)
96
- # Resample if needed
97
- if self.resampler is not None:
98
- waveform = self.resampler(waveform)
99
-
100
- features, feature_lengths = self.model.extract_features(
101
- waveform, lengths, num_layers=num_layers or self.output_layer
102
- )
103
-
104
- if return_lengths:
105
- return features, feature_lengths
106
- return features