Skip to content

Commit

Permalink
fix dependencies with librosa
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonlessons committed Oct 17, 2023
1 parent ee6f0b5 commit 870aedc
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [1.1.5] - 2022-10-17
### Changed
- Fixed dependencies with `librosa` library

## [1.1.4] - 2022-09-29
### Changed
- Improoved `mltu.torch.dataProvider.DataProvider` to hangle `multiprocessing` when it doesn't work to switch to `multithreading`
Expand Down
2 changes: 1 addition & 1 deletion mltu/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.1.4"
__version__ = "1.1.5"

from .annotations.images import Image
from .annotations.images import CVImage
Expand Down
20 changes: 11 additions & 9 deletions mltu/augmentors.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,11 +581,6 @@ class RandomAudioPitchShift(Augmentor):
augment_annotation (bool): Whether to augment the annotation. Defaults to False.
max_n_steps (int): Maximum number of steps to shift audio. Defaults to 5.
"""
try:
import librosa
# samplerate
except ImportError:
raise ImportError("librosa is required to augment Audio. Please install it with `pip install librosa`.")
def __init__(
self,
random_chance: float = 0.5,
Expand All @@ -596,6 +591,12 @@ def __init__(
super(RandomAudioPitchShift, self).__init__(random_chance, log_level, augment_annotation)
self.max_n_steps = max_n_steps

try:
import librosa
# samplerate
except ImportError:
raise ImportError("librosa is required to augment Audio. Please install it with `pip install librosa`.")

def augment(self, audio: Audio) -> Audio:
random_n_steps = np.random.randint(-self.max_n_steps, self.max_n_steps)
# changing default res_type "kaiser_best" to "linear" for speed and memory efficiency
Expand All @@ -617,10 +618,6 @@ class RandomAudioTimeStretch(Augmentor):
min_rate (float): Minimum rate to stretch audio. Defaults to 0.8.
max_rate (float): Maximum rate to stretch audio. Defaults to 1.2.
"""
try:
import librosa
except ImportError:
raise ImportError("librosa is required to augment Audio. Please install it with `pip install librosa`.")
def __init__(
self,
random_chance: float = 0.5,
Expand All @@ -633,6 +630,11 @@ def __init__(
self.min_rate = min_rate
self.max_rate = max_rate

try:
import librosa
except ImportError:
raise ImportError("librosa is required to augment Audio. Please install it with `pip install librosa`.")

def augment(self, audio: Audio) -> Audio:
random_rate = np.random.uniform(self.min_rate, self.max_rate)
stretch_audio = self.librosa.effects.time_stretch(audio.numpy(), rate=random_rate)
Expand Down
24 changes: 12 additions & 12 deletions mltu/preprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ class AudioReader:
sample_rate (int): Sample rate. Defaults to None.
log_level (int): Log level. Defaults to logging.INFO.
"""
try:
import librosa
librosa.__version__
except AttributeError:
raise ImportError("librosa is required to read WAV files. Please install it with `pip install librosa`.")

def __init__(
self,
sample_rate = None,
Expand All @@ -72,6 +66,12 @@ def __init__(
self.logger = logging.getLogger(self.__class__.__name__)
self.logger.setLevel(log_level)

try:
import librosa
librosa.__version__
except AttributeError:
raise ImportError("librosa is required to read WAV files. Please install it with `pip install librosa`.")

def __call__(self, audio_path: str, label: typing.Any) -> typing.Tuple[np.ndarray, typing.Any]:
""" Read audio from path and return audio and label
Expand Down Expand Up @@ -105,12 +105,6 @@ class WavReader:
frame_step (int): Step size between frames in samples.
fft_length (int): Number of FFT components.
"""
# Check if librosa is installed
try:
import librosa
librosa.__version__
except AttributeError:
raise ImportError("librosa is required to read WAV files. Please install it with `pip install librosa`.")

def __init__(
self,
Expand All @@ -124,6 +118,12 @@ def __init__(
self.fft_length = fft_length

matplotlib.interactive(False)
# Check if librosa is installed
try:
import librosa
librosa.__version__
except AttributeError:
raise ImportError("librosa is required to read WAV files. Please install it with `pip install librosa`.")

@staticmethod
def get_spectrogram(wav_path: str, frame_length: int, frame_step: int, fft_length: int) -> np.ndarray:
Expand Down
19 changes: 10 additions & 9 deletions mltu/tensorflow/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@
import logging

class Model2onnx(Callback):
try:
import tf2onnx
except:
raise ImportError("tf2onnx is not installed. Please install it using 'pip install tf2onnx'")

try:
import onnx
except:
raise ImportError("onnx is not installed. Please install it using 'pip install onnx'")
""" Converts the model to onnx format after training is finished. """
def __init__(
self,
Expand All @@ -32,6 +23,16 @@ def __init__(
self.metadata = metadata
self.save_on_epoch_end = save_on_epoch_end

try:
import tf2onnx
except:
raise ImportError("tf2onnx is not installed. Please install it using 'pip install tf2onnx'")

try:
import onnx
except:
raise ImportError("onnx is not installed. Please install it using 'pip install onnx'")

@staticmethod
def model2onnx(model: tf.keras.Model, onnx_model_path: str):
try:
Expand Down
10 changes: 5 additions & 5 deletions mltu/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,6 @@ class AudioToSpectrogram(Transformer):
fft_length (int): Number of FFT components.
log_level (int): Logging level (default: logging.INFO)
"""
try:
import librosa
except ImportError:
raise ImportError("librosa is required to transform Audio. Please install it with `pip install librosa`.")

def __init__(
self,
frame_length: int = 256,
Expand All @@ -269,6 +264,11 @@ def __init__(
self.frame_step = frame_step
self.fft_length = fft_length

try:
import librosa
except ImportError:
raise ImportError("librosa is required to transform Audio. Please install it with `pip install librosa`.")

def __call__(self, audio: Audio, label: typing.Any):
"""Compute the spectrogram of a WAV file
Expand Down

0 comments on commit 870aedc

Please sign in to comment.