Skip to content

Commit 024cc74

Browse files
committed
Diffusion: Clean-up
1 parent eb03c29 commit 024cc74

6 files changed

Lines changed: 84 additions & 93 deletions

File tree

recipes/AudioMNIST/diffusion/custom_transforms.py

Lines changed: 0 additions & 84 deletions
This file was deleted.

recipes/AudioMNIST/diffusion/hparams/train_conditioned.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,14 @@ compute_features: !new:speechbrain.nnet.containers.Sequential
132132
mel_scale: !ref <spec_mel_scale>
133133
amp2db: !new:torchaudio.transforms.AmplitudeToDB
134134

135-
min_level_norm: !new:custom_transforms.MinLevelNorm
135+
min_level_norm: !new:speechbrain.processing.features.MinLevelNorm
136136
min_level_db: !ref <min_level_db>
137137

138-
global_norm: !new:custom_transforms.GlobalNorm
138+
global_norm: !new:speechbrain.processing.features.GlobalNorm
139139
norm_mean: !ref <spec_norm_mean>
140140
norm_std: !ref <spec_norm_std>
141141

142-
dynamic_range_compression: !new:custom_transforms.DynamicRangeCompression
142+
dynamic_range_compression: !new:speechbrain.processing.features.DynamicRangeCompression
143143

144144
compute_cost: !new:speechbrain.nnet.schedulers.ScheduledLoss
145145
schedule:

recipes/AudioMNIST/diffusion/hparams/train_latent_ae.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,14 @@ compute_features: !new:speechbrain.nnet.containers.Sequential
151151
mel_scale: !ref <spec_mel_scale>
152152
amp2db: !new:torchaudio.transforms.AmplitudeToDB
153153

154-
min_level_norm: !new:custom_transforms.MinLevelNorm
154+
min_level_norm: !new:speechbrain.processing.features.MinLevelNorm
155155
min_level_db: !ref <min_level_db>
156156

157157
global_norm: !new:speechbrain.processing.features.GlobalNorm
158158
norm_mean: !ref <spec_norm_mean>
159159
norm_std: !ref <spec_norm_std>
160160

161-
dynamic_range_compression: !new:custom_transforms.DynamicRangeCompression
161+
dynamic_range_compression: !new:speechbrain.processing.features.DynamicRangeCompression
162162

163163
compute_cost_autoencoder_rec: !new:speechbrain.nnet.schedulers.ScheduledLoss
164164
schedule:

recipes/AudioMNIST/diffusion/hparams/train_latent_vae.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,14 @@ compute_features: !new:speechbrain.nnet.containers.Sequential
153153
mel_scale: !ref <spec_mel_scale>
154154
amp2db: !new:torchaudio.transforms.AmplitudeToDB
155155

156-
min_level_norm: !new:custom_transforms.MinLevelNorm
156+
min_level_norm: !new:speechbrain.processing.features.MinLevelNorm
157157
min_level_db: !ref <min_level_db>
158158

159159
global_norm: !new:speechbrain.processing.features.GlobalNorm
160160
norm_mean: !ref <spec_norm_mean>
161161
norm_std: !ref <spec_norm_std>
162162

163-
dynamic_range_compression: !new:custom_transforms.DynamicRangeCompression
163+
dynamic_range_compression: !new:speechbrain.processing.features.DynamicRangeCompression
164164

165165
compute_cost_autoencoder_rec: !new:speechbrain.nnet.schedulers.ScheduledLoss
166166
schedule:

recipes/AudioMNIST/diffusion/hparams/train_unconditioned.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,14 @@ compute_features: !new:speechbrain.nnet.containers.Sequential
133133
mel_scale: !ref <spec_mel_scale>
134134
amp2db: !new:torchaudio.transforms.AmplitudeToDB
135135

136-
min_level_norm: !new:custom_transforms.MinLevelNorm
136+
min_level_norm: !new:speechbrain.processing.features.MinLevelNorm
137137
min_level_db: !ref <min_level_db>
138138

139139
global_norm: !new:speechbrain.processing.features.GlobalNorm
140140
norm_mean: !ref <spec_norm_mean>
141141
norm_std: !ref <spec_norm_std>
142142

143-
dynamic_range_compression: !new:custom_transforms.DynamicRangeCompression
143+
dynamic_range_compression: !new:speechbrain.processing.features.DynamicRangeCompression
144144

145145
compute_cost: !new:speechbrain.nnet.schedulers.ScheduledLoss
146146
schedule:

speechbrain/processing/features.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,3 +1384,78 @@ def freeze(self):
13841384
def unfreeze(self):
13851385
"""Resumes updates to the running mean/std"""
13861386
self.frozen = False
1387+
1388+
class MinLevelNorm(torch.nn.Module):
1389+
"""A normalization for the decibel scale
1390+
1391+
The scheme is as follows
1392+
1393+
x_norm = (x - min_level_db)/-min_level_db * 2 - 1
1394+
1395+
Arguments
1396+
---------
1397+
min_level_db: float
1398+
the minimum level
1399+
"""
1400+
1401+
def __init__(self, min_level_db):
1402+
super().__init__()
1403+
self.min_level_db = min_level_db
1404+
1405+
def forward(self, x):
1406+
"""Normalizes audio features in decibels (usually spectrograms)
1407+
1408+
Arguments
1409+
---------
1410+
x: torch.Tensor
1411+
input features
1412+
Returns
1413+
-------
1414+
normalized_features: torch.Tensor
1415+
the normalized features
1416+
"""
1417+
1418+
x = (x - self.min_level_db) / -self.min_level_db
1419+
x *= 2.0
1420+
x = x - 1.0
1421+
x = torch.clip(x, -1, 1)
1422+
return x
1423+
1424+
def denormalize(self, x):
1425+
"""Reverses the min level normalization process
1426+
1427+
Arguments
1428+
---------
1429+
x: torch.Tensor
1430+
the normalized tensor
1431+
1432+
Returns
1433+
-------
1434+
result: torch.Tensor
1435+
the denormalized tensor
1436+
"""
1437+
x = torch.clip(x, -1, 1)
1438+
x = (x + 1.0) / 2.0
1439+
x *= -self.min_level_db
1440+
x += self.min_level_db
1441+
return x
1442+
1443+
1444+
class DynamicRangeCompression(torch.nn.Module):
1445+
"""Dynamic range compression for audio signals
1446+
Arguments
1447+
---------
1448+
multiplier: float
1449+
the multiplier constant
1450+
clip_val: float
1451+
the minimum accepted value (values below this
1452+
minimum will be clipped)
1453+
"""
1454+
1455+
def __init__(self, multiplier=1, clip_val=1e-5):
1456+
super().__init__()
1457+
self.multiplier = multiplier
1458+
self.clip_val = clip_val
1459+
1460+
def forward(self, x):
1461+
return torch.log(torch.clamp(x, min=self.clip_val) * self.multiplier)

0 commit comments

Comments
 (0)