diff --git a/CHANGELOG.md b/CHANGELOG.md index 13c525d..4c42c39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/mltu/__init__.py b/mltu/__init__.py index 0f9f200..5cb2f26 100644 --- a/mltu/__init__.py +++ b/mltu/__init__.py @@ -1,4 +1,4 @@ -__version__ = "1.1.4" +__version__ = "1.1.5" from .annotations.images import Image from .annotations.images import CVImage diff --git a/mltu/augmentors.py b/mltu/augmentors.py index c7f98db..bc7396b 100644 --- a/mltu/augmentors.py +++ b/mltu/augmentors.py @@ -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, @@ -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 @@ -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, @@ -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) diff --git a/mltu/preprocessors.py b/mltu/preprocessors.py index b86c2ee..ead6bb8 100644 --- a/mltu/preprocessors.py +++ b/mltu/preprocessors.py @@ -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, @@ -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 @@ -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, @@ -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: diff --git a/mltu/tensorflow/callbacks.py b/mltu/tensorflow/callbacks.py index 4a9d576..ea9479e 100644 --- a/mltu/tensorflow/callbacks.py +++ b/mltu/tensorflow/callbacks.py @@ -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, @@ -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: diff --git a/mltu/transformers.py b/mltu/transformers.py index e224fbe..48d597b 100644 --- a/mltu/transformers.py +++ b/mltu/transformers.py @@ -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, @@ -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