Skip to content

Commit 870aedc

Browse files
committed
fix dependencies with librosa
1 parent ee6f0b5 commit 870aedc

File tree

6 files changed

+43
-36
lines changed

6 files changed

+43
-36
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [1.1.5] - 2022-10-17
2+
### Changed
3+
- Fixed dependencies with `librosa` library
4+
15
## [1.1.4] - 2022-09-29
26
### Changed
37
- Improoved `mltu.torch.dataProvider.DataProvider` to hangle `multiprocessing` when it doesn't work to switch to `multithreading`

mltu/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "1.1.4"
1+
__version__ = "1.1.5"
22

33
from .annotations.images import Image
44
from .annotations.images import CVImage

mltu/augmentors.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,6 @@ class RandomAudioPitchShift(Augmentor):
581581
augment_annotation (bool): Whether to augment the annotation. Defaults to False.
582582
max_n_steps (int): Maximum number of steps to shift audio. Defaults to 5.
583583
"""
584-
try:
585-
import librosa
586-
# samplerate
587-
except ImportError:
588-
raise ImportError("librosa is required to augment Audio. Please install it with `pip install librosa`.")
589584
def __init__(
590585
self,
591586
random_chance: float = 0.5,
@@ -596,6 +591,12 @@ def __init__(
596591
super(RandomAudioPitchShift, self).__init__(random_chance, log_level, augment_annotation)
597592
self.max_n_steps = max_n_steps
598593

594+
try:
595+
import librosa
596+
# samplerate
597+
except ImportError:
598+
raise ImportError("librosa is required to augment Audio. Please install it with `pip install librosa`.")
599+
599600
def augment(self, audio: Audio) -> Audio:
600601
random_n_steps = np.random.randint(-self.max_n_steps, self.max_n_steps)
601602
# changing default res_type "kaiser_best" to "linear" for speed and memory efficiency
@@ -617,10 +618,6 @@ class RandomAudioTimeStretch(Augmentor):
617618
min_rate (float): Minimum rate to stretch audio. Defaults to 0.8.
618619
max_rate (float): Maximum rate to stretch audio. Defaults to 1.2.
619620
"""
620-
try:
621-
import librosa
622-
except ImportError:
623-
raise ImportError("librosa is required to augment Audio. Please install it with `pip install librosa`.")
624621
def __init__(
625622
self,
626623
random_chance: float = 0.5,
@@ -633,6 +630,11 @@ def __init__(
633630
self.min_rate = min_rate
634631
self.max_rate = max_rate
635632

633+
try:
634+
import librosa
635+
except ImportError:
636+
raise ImportError("librosa is required to augment Audio. Please install it with `pip install librosa`.")
637+
636638
def augment(self, audio: Audio) -> Audio:
637639
random_rate = np.random.uniform(self.min_rate, self.max_rate)
638640
stretch_audio = self.librosa.effects.time_stretch(audio.numpy(), rate=random_rate)

mltu/preprocessors.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,6 @@ class AudioReader:
5757
sample_rate (int): Sample rate. Defaults to None.
5858
log_level (int): Log level. Defaults to logging.INFO.
5959
"""
60-
try:
61-
import librosa
62-
librosa.__version__
63-
except AttributeError:
64-
raise ImportError("librosa is required to read WAV files. Please install it with `pip install librosa`.")
65-
6660
def __init__(
6761
self,
6862
sample_rate = None,
@@ -72,6 +66,12 @@ def __init__(
7266
self.logger = logging.getLogger(self.__class__.__name__)
7367
self.logger.setLevel(log_level)
7468

69+
try:
70+
import librosa
71+
librosa.__version__
72+
except AttributeError:
73+
raise ImportError("librosa is required to read WAV files. Please install it with `pip install librosa`.")
74+
7575
def __call__(self, audio_path: str, label: typing.Any) -> typing.Tuple[np.ndarray, typing.Any]:
7676
""" Read audio from path and return audio and label
7777
@@ -105,12 +105,6 @@ class WavReader:
105105
frame_step (int): Step size between frames in samples.
106106
fft_length (int): Number of FFT components.
107107
"""
108-
# Check if librosa is installed
109-
try:
110-
import librosa
111-
librosa.__version__
112-
except AttributeError:
113-
raise ImportError("librosa is required to read WAV files. Please install it with `pip install librosa`.")
114108

115109
def __init__(
116110
self,
@@ -124,6 +118,12 @@ def __init__(
124118
self.fft_length = fft_length
125119

126120
matplotlib.interactive(False)
121+
# Check if librosa is installed
122+
try:
123+
import librosa
124+
librosa.__version__
125+
except AttributeError:
126+
raise ImportError("librosa is required to read WAV files. Please install it with `pip install librosa`.")
127127

128128
@staticmethod
129129
def get_spectrogram(wav_path: str, frame_length: int, frame_step: int, fft_length: int) -> np.ndarray:

mltu/tensorflow/callbacks.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@
55
import logging
66

77
class Model2onnx(Callback):
8-
try:
9-
import tf2onnx
10-
except:
11-
raise ImportError("tf2onnx is not installed. Please install it using 'pip install tf2onnx'")
12-
13-
try:
14-
import onnx
15-
except:
16-
raise ImportError("onnx is not installed. Please install it using 'pip install onnx'")
178
""" Converts the model to onnx format after training is finished. """
189
def __init__(
1910
self,
@@ -32,6 +23,16 @@ def __init__(
3223
self.metadata = metadata
3324
self.save_on_epoch_end = save_on_epoch_end
3425

26+
try:
27+
import tf2onnx
28+
except:
29+
raise ImportError("tf2onnx is not installed. Please install it using 'pip install tf2onnx'")
30+
31+
try:
32+
import onnx
33+
except:
34+
raise ImportError("onnx is not installed. Please install it using 'pip install onnx'")
35+
3536
@staticmethod
3637
def model2onnx(model: tf.keras.Model, onnx_model_path: str):
3738
try:

mltu/transformers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,6 @@ class AudioToSpectrogram(Transformer):
252252
fft_length (int): Number of FFT components.
253253
log_level (int): Logging level (default: logging.INFO)
254254
"""
255-
try:
256-
import librosa
257-
except ImportError:
258-
raise ImportError("librosa is required to transform Audio. Please install it with `pip install librosa`.")
259-
260255
def __init__(
261256
self,
262257
frame_length: int = 256,
@@ -269,6 +264,11 @@ def __init__(
269264
self.frame_step = frame_step
270265
self.fft_length = fft_length
271266

267+
try:
268+
import librosa
269+
except ImportError:
270+
raise ImportError("librosa is required to transform Audio. Please install it with `pip install librosa`.")
271+
272272
def __call__(self, audio: Audio, label: typing.Any):
273273
"""Compute the spectrogram of a WAV file
274274

0 commit comments

Comments
 (0)