A lightweight yet powerful audio-to-MIDI converter with pitch bend detection
Python audiolightweightmachine-learningmidimusicpitch-detectionpolyphonicpythontranscriptiontypescript
small formatting fixes
Saltine51913a9879ba
diff --git a/basic_pitch/callbacks.py b/basic_pitch/callbacks.pyindex 141c0fa..dd6f305 100644--- a/basic_pitch/callbacks.py+++ b/basic_pitch/callbacks.py@@ -17,7 +17,7 @@import os-from typing import Any, Dict+from typing import Any, Dict, Callableimport tensorflow as tf@@ -25,6 +25,7 @@ from basic_pitch import visualizeTENSORBOARD_LOGS_SUBDIR = "tensorboard_logs"+class VisualizeCallback(tf.keras.callbacks.Callback):"""Callback to run during training to create tensorboard visualizations per epoch.@@ -49,10 +50,10 @@ class VisualizeCallback(tf.keras.callbacks.Callback):use_tf_function: bool = True,):super().__init__()- self.train_ds = train_ds.take(max_batches).prefatch(prefetch_batches)+ self.train_ds = train_ds.take(max_batches).prefetch(prefetch_batches)self.validation_ds = validation_ds.take(max_batches).prefetch(prefetch_batches)self.tensorboard_dir = os.path.join(tensorboard_dir, TENSORBOARD_LOGS_SUBDIR)- self.file_writer = tf.summary.create_file_writer(tensorboard_dir)+ self.file_writer = tf.summary.create_file_writer(self.tensorboard_dir)self.sonify = sonifyself.contours = contoursself.use_tf_function = use_tf_function@@ -62,20 +63,18 @@ class VisualizeCallback(tf.keras.callbacks.Callback):self._predict_fn = None- def set_module(self, model):+ def set_model(self, model: tf.keras.Model) -> None:super().set_model(model)if self.use_tf_function:- @tf_function- def fast_predict(inputs):+ def fast_predict(inputs: tf.Tensor) -> Any:return model(inputs, training=False)- self._predict_fn = fast_predict+ self._predict_fn = tf.function(fast_predict)else:self._predict_fn = model.predict- def _predict(self, inputs):+ def _predict(self, inputs: tf.Tensor) -> Any:if self._predict_fn is not None:outputs = self._predict_fn(inputs)- # tf.functions might output as a dict of tensors, convert to numpy:if isinstance(outputs, dict):outputs = {k: v.numpy() if hasattr(v, "numpy") else v for k, v in outputs.items()}return outputs@@ -90,15 +89,16 @@ class VisualizeCallback(tf.keras.callbacks.Callback):for batch in ds:inputs, targets = batch[:2]outputs = self._predict(inputs)+ loss_val = logs.get(loss_key)visualize.visualize_transcription(self.file_writer,stage,inputs,targets,outputs,- logs.get(loss_key),+ float(loss_val) if loss_val is not None else 0.0,epoch,sonify=self.sonify,- contours=self.contours+ contours=self.contours,)- break+ break\ No newline at end of filediff --git a/basic_pitch/commandline_printing.py b/basic_pitch/commandline_printing.pyindex 4e8c73a..1106ef7 100644--- a/basic_pitch/commandline_printing.py+++ b/basic_pitch/commandline_printing.py@@ -23,6 +23,8 @@ from typing import Iterator, UnionTF_LOG_LEVEL_KEY = "TF_CPP_MIN_LOG_LEVEL"TF_LOG_LEVEL_NO_WARNINGS_VALUE = "3"+DEFAULT_PRINT_INDENT = " "+s_print_lock = threading.Lock()OUTPUT_EMOJIS = {"MIDI": "💅",diff --git a/basic_pitch/constants.py b/basic_pitch/constants.pyindex e17e79b..2668704 100644--- a/basic_pitch/constants.py+++ b/basic_pitch/constants.py@@ -19,6 +19,7 @@ import numpy as npfrom enum import Enum+SEMITONES_PER_OCTAVE = 12 # for frequency bin calculationsFFT_HOP = 256diff --git a/basic_pitch/layers/nnaudio.py b/basic_pitch/layers/nnaudio.pyindex 3bc9bde..f011ac2 100644--- a/basic_pitch/layers/nnaudio.py+++ b/basic_pitch/layers/nnaudio.py@@ -41,6 +41,7 @@ DEFAULT_CQT_PAD_MODE = "reflect"DEFAULT_CQT_OUTPUT_FORMAT = "Magnitude"DEFAULT_LOW_PASS_TRANSITION_BANDWIDTH = 0.001+def create_lowpass_filter(band_center: float = DEFAULT_BAND_CENTER,kernel_length: int = DEFAULT_KERNEL_LENGTH,diff --git a/basic_pitch/models.py b/basic_pitch/models.pyindex 44b3b6c..47631b6 100644--- a/basic_pitch/models.py+++ b/basic_pitch/models.py@@ -51,6 +51,7 @@ ONSET_KERNEL_SIZE_1 = (5, 5)ONSET_STRIDES_1 = (1, 3)ONSET_KERNEL_SIZE_2 = (3, 3)+def transcription_loss(y_true: tf.Tensor, y_pred: tf.Tensor, label_smoothing: float) -> tf.Tensor:"""Really a binary cross entropy loss. Used to calculate the loss between the predictedposteriorgrams and the ground truth matrices.diff --git a/basic_pitch/nn.py b/basic_pitch/nn.pyindex b9acb0a..23db0a3 100644--- a/basic_pitch/nn.py+++ b/basic_pitch/nn.py@@ -24,6 +24,7 @@ from basic_pitch.layers.math import log_base_bSEMITONES_PER_OCTAVE = 12+class HarmonicStacking(tf.keras.layers.Layer):"""Harmonic stacking layerdiff --git a/basic_pitch/note_creation.py b/basic_pitch/note_creation.pyindex 822613d..f365eb4 100644--- a/basic_pitch/note_creation.py+++ b/basic_pitch/note_creation.py@@ -48,6 +48,7 @@ MAGIC_ALIGNMENT_OFFSET = 0.0018MIDI_VELOCITY_SCALE = 127PITCH_BEND_SCALE = 4096+def model_output_to_notes(output: Dict[str, np.array],onset_thresh: float,