D4ve-R commited on
Commit
258e1da
1 Parent(s): 326b458

Upload model

Browse files
Files changed (6) hide show
  1. README.md +203 -0
  2. config.json +27 -0
  3. config.py +40 -0
  4. model.py +166 -0
  5. model.safetensors +3 -0
  6. model_hf.py +27 -0
README.md ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ tags:
4
+ - feature-extraction
5
+ - audio
6
+ ---
7
+
8
+ # Model Card for Model ID
9
+
10
+ <!-- Provide a quick summary of what the model is/does. -->
11
+
12
+
13
+
14
+ ## Model Details
15
+
16
+ ### Model Description
17
+
18
+ <!-- Provide a longer summary of what this model is. -->
19
+
20
+ This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
21
+
22
+ - **Developed by:** [More Information Needed]
23
+ - **Funded by [optional]:** [More Information Needed]
24
+ - **Shared by [optional]:** [More Information Needed]
25
+ - **Model type:** [More Information Needed]
26
+ - **Language(s) (NLP):** [More Information Needed]
27
+ - **License:** [More Information Needed]
28
+ - **Finetuned from model [optional]:** [More Information Needed]
29
+
30
+ ### Model Sources [optional]
31
+
32
+ <!-- Provide the basic links for the model. -->
33
+
34
+ - **Repository:** [More Information Needed]
35
+ - **Paper [optional]:** [More Information Needed]
36
+ - **Demo [optional]:** [More Information Needed]
37
+
38
+ ## Uses
39
+
40
+ <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
41
+
42
+ ### Direct Use
43
+
44
+ <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
45
+
46
+ [More Information Needed]
47
+
48
+ ### Downstream Use [optional]
49
+
50
+ <!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
51
+
52
+ [More Information Needed]
53
+
54
+ ### Out-of-Scope Use
55
+
56
+ <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
57
+
58
+ [More Information Needed]
59
+
60
+ ## Bias, Risks, and Limitations
61
+
62
+ <!-- This section is meant to convey both technical and sociotechnical limitations. -->
63
+
64
+ [More Information Needed]
65
+
66
+ ### Recommendations
67
+
68
+ <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
69
+
70
+ Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
71
+
72
+ ## How to Get Started with the Model
73
+
74
+ Use the code below to get started with the model.
75
+
76
+ [More Information Needed]
77
+
78
+ ## Training Details
79
+
80
+ ### Training Data
81
+
82
+ <!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
83
+
84
+ [More Information Needed]
85
+
86
+ ### Training Procedure
87
+
88
+ <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
89
+
90
+ #### Preprocessing [optional]
91
+
92
+ [More Information Needed]
93
+
94
+
95
+ #### Training Hyperparameters
96
+
97
+ - **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
98
+
99
+ #### Speeds, Sizes, Times [optional]
100
+
101
+ <!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
102
+
103
+ [More Information Needed]
104
+
105
+ ## Evaluation
106
+
107
+ <!-- This section describes the evaluation protocols and provides the results. -->
108
+
109
+ ### Testing Data, Factors & Metrics
110
+
111
+ #### Testing Data
112
+
113
+ <!-- This should link to a Dataset Card if possible. -->
114
+
115
+ [More Information Needed]
116
+
117
+ #### Factors
118
+
119
+ <!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
120
+
121
+ [More Information Needed]
122
+
123
+ #### Metrics
124
+
125
+ <!-- These are the evaluation metrics being used, ideally with a description of why. -->
126
+
127
+ [More Information Needed]
128
+
129
+ ### Results
130
+
131
+ [More Information Needed]
132
+
133
+ #### Summary
134
+
135
+
136
+
137
+ ## Model Examination [optional]
138
+
139
+ <!-- Relevant interpretability work for the model goes here -->
140
+
141
+ [More Information Needed]
142
+
143
+ ## Environmental Impact
144
+
145
+ <!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
146
+
147
+ Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
148
+
149
+ - **Hardware Type:** [More Information Needed]
150
+ - **Hours used:** [More Information Needed]
151
+ - **Cloud Provider:** [More Information Needed]
152
+ - **Compute Region:** [More Information Needed]
153
+ - **Carbon Emitted:** [More Information Needed]
154
+
155
+ ## Technical Specifications [optional]
156
+
157
+ ### Model Architecture and Objective
158
+
159
+ [More Information Needed]
160
+
161
+ ### Compute Infrastructure
162
+
163
+ [More Information Needed]
164
+
165
+ #### Hardware
166
+
167
+ [More Information Needed]
168
+
169
+ #### Software
170
+
171
+ [More Information Needed]
172
+
173
+ ## Citation [optional]
174
+
175
+ <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
176
+
177
+ **BibTeX:**
178
+
179
+ [More Information Needed]
180
+
181
+ **APA:**
182
+
183
+ [More Information Needed]
184
+
185
+ ## Glossary [optional]
186
+
187
+ <!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
188
+
189
+ [More Information Needed]
190
+
191
+ ## More Information [optional]
192
+
193
+ [More Information Needed]
194
+
195
+ ## Model Card Authors [optional]
196
+
197
+ [More Information Needed]
198
+
199
+ ## Model Card Contact
200
+
201
+ [More Information Needed]
202
+
203
+
config.json ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "SincNetModel"
4
+ ],
5
+ "auto_map": {
6
+ "AutoConfig": "config.SincNetConfig",
7
+ "AutoModel": "model_hf.SincNetModel"
8
+ },
9
+ "conv_filter_length": 5,
10
+ "min_band_hz": 50,
11
+ "min_low_hz": 50,
12
+ "model_type": "sincnet",
13
+ "num_conv_filters": 60,
14
+ "num_sinc_filters": 80,
15
+ "num_wavform_channels": 1,
16
+ "pool_kernel_size": 3,
17
+ "pool_stride": 3,
18
+ "sample_rate": 16000,
19
+ "sinc_filter_dilation": 1,
20
+ "sinc_filter_in_channels": 1,
21
+ "sinc_filter_length": 251,
22
+ "sinc_filter_padding": 0,
23
+ "sinc_filter_stride": 10,
24
+ "stride": 10,
25
+ "torch_dtype": "float32",
26
+ "transformers_version": "4.37.2"
27
+ }
config.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PretrainedConfig
2
+
3
+ class SincNetConfig(PretrainedConfig):
4
+ model_type = "sincnet"
5
+
6
+ def __init__(
7
+ self,
8
+ stride: int = 10,
9
+ num_sinc_filters: int = 80,
10
+ sinc_filter_length: int = 251,
11
+ num_conv_filters: int = 60,
12
+ conv_filter_length: int = 5,
13
+ pool_kernel_size: int = 3,
14
+ pool_stride: int = 3,
15
+ sample_rate: int = 16000,
16
+ sinc_filter_stride: int = 10,
17
+ sinc_filter_padding: int = 0,
18
+ sinc_filter_dilation: int = 1,
19
+ min_low_hz: int = 50,
20
+ min_band_hz: int = 50,
21
+ sinc_filter_in_channels: int = 1,
22
+ num_wavform_channels: int = 1,
23
+ **kwargs
24
+ ):
25
+ self.sample_rate = sample_rate
26
+ self.stride = stride
27
+ self.num_sinc_filters = num_sinc_filters
28
+ self.sinc_filter_length = sinc_filter_length
29
+ self.num_conv_filters = num_conv_filters
30
+ self.conv_filter_length = conv_filter_length
31
+ self.pool_kernel_size = pool_kernel_size
32
+ self.pool_stride = pool_stride
33
+ self.sinc_filter_stride = sinc_filter_stride
34
+ self.sinc_filter_padding = sinc_filter_padding
35
+ self.sinc_filter_dilation = sinc_filter_dilation
36
+ self.min_low_hz = min_low_hz
37
+ self.min_band_hz = min_band_hz
38
+ self.sinc_filter_in_channels = sinc_filter_in_channels
39
+ self.num_wavform_channels = num_wavform_channels
40
+ super().__init__(**kwargs)
model.py ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ """ SincNet model """
3
+ from functools import lru_cache
4
+ import numpy as np
5
+ import logging
6
+
7
+ import torch
8
+ import torch.nn as nn
9
+ import torch.nn.functional as F
10
+
11
+ logger = logging.getLogger(__name__)
12
+
13
+ class SincNetFilterConvLayer(nn.Module):
14
+ """SincNet fast convolution filter layer"""
15
+
16
+ def __init__(self, out_channels: int, kernel_size: int, sample_rate=16000,
17
+ stride=1, padding=0, dilation=1, min_low_hz=50, min_band_hz=50,
18
+ in_channels=1, requires_grad=False):
19
+ """
20
+ Args:
21
+ out_channels : `int` number of filters.
22
+ kernel_size : `int` filter length.
23
+ sample_rate : `int`, optional sample rate. Defaults to 16000.
24
+ """
25
+ super(SincNetFilterConvLayer, self).__init__()
26
+
27
+ if in_channels != 1:
28
+ raise ValueError(f"SincNetFilterConvLayer only support in_channels = 1, was in_channels = {in_channels}")
29
+
30
+ self._out_channels = out_channels
31
+ self._kernel_size = kernel_size
32
+
33
+ if kernel_size % 2 == 0:
34
+ self._kernel_size += 1 # Forcing the filters to be odd
35
+
36
+ self._stride = stride
37
+ self._padding = padding
38
+ self._dilation = dilation
39
+ self._sample_rate = sample_rate
40
+ self._min_low_hz = min_low_hz
41
+ self._min_band_hz = min_band_hz
42
+
43
+ # initialize filterbanks such that they are equally spaced in Mel scale
44
+ low_hz = 30
45
+ high_hz = self._sample_rate / 2 - (self._min_low_hz + self._min_band_hz)
46
+ mel = np.linspace(
47
+ 2595 * np.log10(1 + low_hz / 700), # Convert Hz to Mel
48
+ 2595 * np.log10(1 + high_hz / 700), # Convert Hz to Mel
49
+ self._out_channels // 2 + 1
50
+ )
51
+ hz = 700 * (10 ** (mel / 2595) - 1) # Convert Mel to Hz
52
+
53
+ self._low_hz = nn.Parameter(
54
+ torch.Tensor(hz[:-1]).view(-1, 1),
55
+ requires_grad=requires_grad
56
+ )
57
+ self._band_hz = nn.Parameter(
58
+ torch.Tensor(np.diff(hz)).view(-1, 1),
59
+ requires_grad=requires_grad
60
+ )
61
+ self.register_buffer(
62
+ "_window",
63
+ torch.from_numpy(np.hamming(self._kernel_size)[: self._kernel_size // 2]).float()
64
+ )
65
+ self.register_buffer(
66
+ "_n",
67
+ (2* np.pi * torch.arange(-(self._kernel_size // 2), 0.0).view(1, -1) / self._sample_rate)
68
+ )
69
+
70
+ @property
71
+ @lru_cache(maxsize=1)
72
+ def filters(self) -> torch.Tensor:
73
+ low = self._min_low_hz + torch.abs(self._low_hz)
74
+ high = torch.clamp(low + self._min_band_hz + torch.abs(self._band_hz), self._min_low_hz, self._sample_rate/2)
75
+ band = (high-low)[:,0]
76
+
77
+ f_times_t_low = torch.matmul(low, self._n)
78
+ f_times_t_high = torch.matmul(high, self._n)
79
+
80
+ band_pass_left = ((torch.sin(f_times_t_high)-torch.sin(f_times_t_low))/(self._n/2))*self._window
81
+ band_pass_center = 2 * band.view(-1, 1)
82
+ band_pass_right = torch.flip(band_pass_left, dims=[1])
83
+
84
+ band_pass = torch.cat([band_pass_left,band_pass_center,band_pass_right],dim=1)
85
+ band_pass = band_pass / (2*band[:,None])
86
+ return band_pass.view(self._out_channels, 1, self._kernel_size)
87
+
88
+ def forward(self, waveforms: torch.Tensor) -> torch.Tensor:
89
+ """
90
+ Args:
91
+ waveforms : (batch_size, 1, n_samples) batch of waveforms.
92
+
93
+ Returns:
94
+ features : (batch_size, out_channels, n_samples_out) batch of sinc filters activations.
95
+ """
96
+ return F.conv1d(waveforms, self.filters, stride=self._stride,
97
+ padding=self._padding, dilation=self._dilation,
98
+ ).abs_() # https://github.com/mravanelli/SincNet/issues/4
99
+
100
+ class SincNet(nn.Module):
101
+ """SincNet"""
102
+
103
+ def __init__(
104
+ self,
105
+ num_sinc_filters: int = 80,
106
+ sinc_filter_length: int = 251,
107
+ num_conv_filters: int = 60,
108
+ conv_filter_length: int = 5,
109
+ pool_kernel_size: int = 3,
110
+ pool_stride: int = 3,
111
+ sample_rate: int = 16000,
112
+ sinc_filter_stride: int = 10,
113
+ sinc_filter_padding: int = 0,
114
+ sinc_filter_dilation: int = 1,
115
+ min_low_hz: int = 50,
116
+ min_band_hz: int = 50,
117
+ sinc_filter_in_channels: int = 1,
118
+ num_wavform_channels: int = 1,
119
+ ):
120
+ super().__init__()
121
+
122
+ if sample_rate != 16000:
123
+ raise NotImplementedError(f"SincNet only supports 16kHz audio (sample_rate = 16000), was sample_rate = {sample_rate}")
124
+
125
+ self.wav_norm1d = nn.InstanceNorm1d(num_wavform_channels, affine=True)
126
+
127
+ self.conv1d = nn.ModuleList([
128
+ SincNetFilterConvLayer(
129
+ num_sinc_filters,
130
+ sinc_filter_length,
131
+ sample_rate=sample_rate,
132
+ stride=sinc_filter_stride,
133
+ padding=sinc_filter_padding,
134
+ dilation=sinc_filter_dilation,
135
+ min_low_hz=min_low_hz,
136
+ min_band_hz=min_band_hz,
137
+ in_channels=sinc_filter_in_channels,
138
+ ),
139
+ nn.Conv1d(num_sinc_filters, num_conv_filters, conv_filter_length),
140
+ nn.Conv1d(num_conv_filters, num_conv_filters, conv_filter_length),
141
+ ])
142
+ self.pool1d = nn.ModuleList([
143
+ nn.MaxPool1d(pool_kernel_size, stride=pool_stride),
144
+ nn.MaxPool1d(pool_kernel_size, stride=pool_stride),
145
+ nn.MaxPool1d(pool_kernel_size, stride=pool_stride),
146
+ ])
147
+ self.norm1d = nn.ModuleList([
148
+ nn.InstanceNorm1d(num_sinc_filters, affine=True),
149
+ nn.InstanceNorm1d(num_conv_filters, affine=True),
150
+ nn.InstanceNorm1d(num_conv_filters, affine=True),
151
+ ])
152
+
153
+ def forward(self, waveforms: torch.Tensor) -> torch.Tensor:
154
+ """
155
+ Args:
156
+ waveforms : (batch, channel, sample)
157
+ """
158
+ outputs = self.wav_norm1d(waveforms)
159
+
160
+ for _, (conv1d, pool1d, norm1d) in enumerate(
161
+ zip(self.conv1d, self.pool1d, self.norm1d)
162
+ ):
163
+ outputs = conv1d(outputs)
164
+ outputs = F.leaky_relu(norm1d(pool1d(outputs)))
165
+
166
+ return outputs
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d1a33ae4bb439f0732b21ba7a5239c20e918b04573eec7233d07da310011dd17
3
+ size 172768
model_hf.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PreTrainedModel, AutoConfig, AutoModel
2
+ from .model import SincNet
3
+ from .config import SincNetConfig
4
+
5
+ class SincNetModel(PreTrainedModel):
6
+ config_class = SincNetConfig
7
+ base_model_prefix = "sincnet"
8
+
9
+ def __init__(self, config: SincNetConfig):
10
+ super().__init__(config)
11
+
12
+ self.model = SincNet(
13
+ sinc_filter_stride=config.stride,
14
+ num_sinc_filters=config.num_sinc_filters,
15
+ sinc_filter_length=config.sinc_filter_length,
16
+ num_conv_filters=config.num_conv_filters,
17
+ conv_filter_length=config.conv_filter_length,
18
+ pool_kernel_size=config.pool_kernel_size,
19
+ pool_stride=config.pool_stride,
20
+ sample_rate=config.sample_rate,
21
+ )
22
+
23
+ def forward(self, waveforms):
24
+ return self.model(waveforms)
25
+
26
+ AutoConfig.register('sincnet', SincNetConfig)
27
+ AutoModel.register(SincNetConfig, SincNetModel)