Skip to content

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

magic number undoer

Saltineb3e0f73e2b9a
diff --git a/basic_pitch/models.py b/basic_pitch/models.py
index 0e72651..44b3b6c 100644
--- a/basic_pitch/models.py
+++ b/basic_pitch/models.py
@@ -35,6 +35,21 @@ tfkl = tf.keras.layers
MAX_N_SEMITONES = int(np.floor(12.0 * np.log2(0.5 * AUDIO_SAMPLE_RATE / ANNOTATIONS_BASE_FREQUENCY)))
+DEFAULT_LABEL_SMOOTHING = 0.2
+DEFAULT_POSITIVE_WEIGHT = 0.5
+
+CONTOUR_KERNEL_SIZE_1 = (5, 5)
+CONTOUR_KERNEL_SIZE_2 = (3, 39) # 3*13
+CONTOUR_KERNEL_SIZE_3 = (5, 5)
+CONTOUR_FILTERS_2 = 8
+
+NOTES_KERNEL_SIZE_1 = (7, 7)
+NOTES_STRIDES_1 = (1, 3)
+NOTES_KERNEL_SIZE_2 = (7, 3)
+
+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
@@ -103,7 +118,7 @@ def onset_loss(
return lambda x, y: transcription_loss(x, y, label_smoothing=label_smoothing)
-def loss(label_smoothing: float = 0.2, weighted: bool = False, positive_weight: float = 0.5) -> Dict[str, Any]:
+def loss(label_smoothing: float = DEFAULT_LABEL_SMOOTHING, weighted: bool = False, positive_weight: float = DEFAULT_POSITIVE_WEIGHT) -> Dict[str, Any]:
"""Creates a keras-compatible dictionary of loss functions to calculate
the loss for the contour, note and onset posteriorgrams.
@@ -206,7 +221,7 @@ def model(
# contour layers - fully convolutional
x_contours = tfkl.Conv2D(
n_filters_contour,
- (5, 5),
+ CONTOUR_KERNEL_SIZE_1,
padding="same",
kernel_initializer=_initializer(),
kernel_constraint=_kernel_constraint(),
@@ -216,12 +231,12 @@ def model(
x_contours = tfkl.ReLU()(x_contours)
x_contours = tfkl.Conv2D(
- 8,
- (3, 3 * 13),
+ CONTOUR_FILTERS_2,
+ CONTOUR_KERNEL_SIZE_2,
padding="same",
kernel_initializer=_initializer(),
kernel_constraint=_kernel_constraint(),
- )(x)
+ )(x_contours)
x_contours = tfkl.BatchNormalization()(x_contours)
x_contours = tfkl.ReLU()(x_contours)
@@ -230,7 +245,7 @@ def model(
contour_name = "contour"
x_contours = tfkl.Conv2D(
1,
- (5, 5),
+ CONTOUR_KERNEL_SIZE_3,
padding="same",
activation="sigmoid",
kernel_initializer=_initializer(),
@@ -246,9 +261,9 @@ def model(
x_contours_reduced = tfkl.Conv2D(
n_filters_notes,
- (7, 7),
+ NOTES_KERNEL_SIZE_1,
padding="same",
- strides=(1, 3),
+ strides=NOTES_STRIDES_1,
kernel_initializer=_initializer(),
kernel_constraint=_kernel_constraint(),
)(x_contours_reduced)
@@ -258,7 +273,7 @@ def model(
note_name = "note"
x_notes_pre = tfkl.Conv2D(
1,
- (7, 3),
+ NOTES_KERNEL_SIZE_2,
padding="same",
kernel_initializer=_initializer(),
kernel_constraint=_kernel_constraint(),
@@ -271,9 +286,9 @@ def model(
# onsets - fully convolutional
x_onset = tfkl.Conv2D(
n_filters_onsets,
- (5, 5),
+ ONSET_KERNEL_SIZE_1,
padding="same",
- strides=(1, 3),
+ strides=ONSET_STRIDES_1,
kernel_initializer=_initializer(),
kernel_constraint=_kernel_constraint(),
)(x)
@@ -282,7 +297,7 @@ def model(
x_onset = tfkl.Concatenate(axis=3, name="concat")([x_notes_pre, x_onset])
x_onset = tfkl.Conv2D(
1,
- (3, 3),
+ ONSET_KERNEL_SIZE_2,
padding="same",
activation="sigmoid",
kernel_initializer=_initializer(),