Skip to content

A lightweight yet powerful audio-to-MIDI converter with pitch bend detection

small formatting fixes

Saltine51913a9879ba
diff --git a/basic_pitch/callbacks.py b/basic_pitch/callbacks.py
index 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, Callable
import tensorflow as tf
@@ -25,6 +25,7 @@ from basic_pitch import visualize
TENSORBOARD_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 = sonify
self.contours = contours
self.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 file
diff --git a/basic_pitch/commandline_printing.py b/basic_pitch/commandline_printing.py
index 4e8c73a..1106ef7 100644
--- a/basic_pitch/commandline_printing.py
+++ b/basic_pitch/commandline_printing.py
@@ -23,6 +23,8 @@ from typing import Iterator, Union
TF_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.py
index e17e79b..2668704 100644
--- a/basic_pitch/constants.py
+++ b/basic_pitch/constants.py
@@ -19,6 +19,7 @@ import numpy as np
from enum import Enum
+
SEMITONES_PER_OCTAVE = 12 # for frequency bin calculations
FFT_HOP = 256
diff --git a/basic_pitch/layers/nnaudio.py b/basic_pitch/layers/nnaudio.py
index 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.py
index 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 predicted
posteriorgrams and the ground truth matrices.
diff --git a/basic_pitch/nn.py b/basic_pitch/nn.py
index 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_b
SEMITONES_PER_OCTAVE = 12
+
class HarmonicStacking(tf.keras.layers.Layer):
"""Harmonic stacking layer
diff --git a/basic_pitch/note_creation.py b/basic_pitch/note_creation.py
index 822613d..f365eb4 100644
--- a/basic_pitch/note_creation.py
+++ b/basic_pitch/note_creation.py
@@ -48,6 +48,7 @@ MAGIC_ALIGNMENT_OFFSET = 0.0018
MIDI_VELOCITY_SCALE = 127
PITCH_BEND_SCALE = 4096
+
def model_output_to_notes(
output: Dict[str, np.array],
onset_thresh: float,