A lightweight yet powerful audio-to-MIDI converter with pitch bend detection
Python audiolightweightmachine-learningmidimusicpitch-detectionpolyphonicpythontranscriptiontypescript
black formatting, flake linting, mypy corrections across files. Added test for tf_example_serialization, added / corrected tests for other data / train files.
Benjie Genchel5f78d3aaebe2
diff --git a/basic_pitch/commandline_printing.py b/basic_pitch/commandline_printing.pyindex d8e0871..6cf1248 100644--- a/basic_pitch/commandline_printing.py+++ b/basic_pitch/commandline_printing.py@@ -42,9 +42,7 @@ def generating_file_message(output_type: str) -> None:print(f"\n\n Creating {output_type.replace('_', ' ').lower()}...")-def file_saved_confirmation(- output_type: str, save_path: Union[pathlib.Path, str]-) -> None:+def file_saved_confirmation(output_type: str, save_path: Union[pathlib.Path, str]) -> None:"""Print a confirmation that the file was saved succesfullyArgs:@@ -63,9 +61,7 @@ def failed_to_save(output_type: str, save_path: Union[pathlib.Path, str]) -> Nonsave_path: The path to output file."""- print(- f"\n🚨 Failed to save {output_type.replace('_', ' ').lower()} to {save_path} \n"- )+ print(f"\n🚨 Failed to save {output_type.replace('_', ' ').lower()} to {save_path} \n")@contextmanagerdiff --git a/basic_pitch/constants.py b/basic_pitch/constants.pyindex 809119b..a78a487 100644--- a/basic_pitch/constants.py+++ b/basic_pitch/constants.py@@ -51,17 +51,11 @@ DATASET_SAMPLING_FREQUENCY = {}-def _freq_bins(- bins_per_semitone: int, base_frequency: float, n_semitones: int-) -> np.array:+def _freq_bins(bins_per_semitone: int, base_frequency: float, n_semitones: int) -> np.array:d = 2.0 ** (1.0 / (12 * bins_per_semitone))bin_freqs = base_frequency * d ** np.arange(bins_per_semitone * n_semitones)return bin_freqs-FREQ_BINS_NOTES = _freq_bins(- NOTES_BINS_PER_SEMITONE, ANNOTATIONS_BASE_FREQUENCY, ANNOTATIONS_N_SEMITONES-)-FREQ_BINS_CONTOURS = _freq_bins(- CONTOURS_BINS_PER_SEMITONE, ANNOTATIONS_BASE_FREQUENCY, ANNOTATIONS_N_SEMITONES-)+FREQ_BINS_NOTES = _freq_bins(NOTES_BINS_PER_SEMITONE, ANNOTATIONS_BASE_FREQUENCY, ANNOTATIONS_N_SEMITONES)+FREQ_BINS_CONTOURS = _freq_bins(CONTOURS_BINS_PER_SEMITONE, ANNOTATIONS_BASE_FREQUENCY, ANNOTATIONS_N_SEMITONES)diff --git a/basic_pitch/data/commandline.py b/basic_pitch/data/commandline.pyindex 61bcde4..233ac34 100644--- a/basic_pitch/data/commandline.py+++ b/basic_pitch/data/commandline.py@@ -18,21 +18,24 @@import argparseimport os+from pathlib import Pathfrom typing import Optionaldef add_default(parser: argparse.ArgumentParser, dataset_name: str = "") -> None:+ default_source = Path.home() / "mir_datasets" / dataset_name+ default_destination = Path.home() / "data" / "basic_pitch" / dataset_nameparser.add_argument("--source",- default=os.path.join(os.path.expanduser("~"), "mir_datasets", dataset_name),- help="Source directory for mir data. Defaults to local mir_datasets folder.",+ default=default_source,+ type=Path,+ help=f"Source directory for mir data. Defaults to {default_source}",)parser.add_argument("--destination",- default=os.path.join(- os.path.expanduser("~"), "data", "basic_pitch", dataset_name- ),- help="Output directory to write results to. Defaults to local ~/data/basic_pitch/{dataset}/",+ default=default_destination,+ type=Path,+ help=f"Output directory to write results to. Defaults to {default_destination}",)parser.add_argument("--runner",@@ -46,9 +49,7 @@ def add_default(parser: argparse.ArgumentParser, dataset_name: str = "") -> Noneaction="store_true",help="If passed, the dataset will be put into a timestamp directory instead of 'splits'",)- parser.add_argument(- "--batch-size", default=5, type=int, help="Number of examples per tfrecord"- )+ parser.add_argument("--batch-size", default=5, type=int, help="Number of examples per tfrecord")parser.add_argument("--worker-harness-container-image",default="",@@ -58,9 +59,7 @@ def add_default(parser: argparse.ArgumentParser, dataset_name: str = "") -> Nonedef resolve_destination(namespace: argparse.Namespace, time_created: int) -> str:- return os.path.join(- namespace.destination, str(time_created) if namespace.timestamped else "splits"- )+ return os.path.join(namespace.destination, str(time_created) if namespace.timestamped else "splits")def add_split(@@ -68,7 +67,7 @@ def add_split(train_percent: float = 0.8,validation_percent: float = 0.1,split_seed: Optional[int] = None,-):+) -> None:parser.add_argument("--train-percent",type=float,diff --git a/basic_pitch/data/datasets/__init__.py b/basic_pitch/data/datasets/__init__.pyindex e69de29..7b57781 100644--- a/basic_pitch/data/datasets/__init__.py+++ b/basic_pitch/data/datasets/__init__.py@@ -0,0 +1 @@+DOWNLOAD = Truediff --git a/basic_pitch/data/datasets/guitarset.py b/basic_pitch/data/datasets/guitarset.pyindex 82c881c..6169771 100644--- a/basic_pitch/data/datasets/guitarset.py+++ b/basic_pitch/data/datasets/guitarset.py@@ -20,18 +20,18 @@ import loggingimport osimport randomimport time-import sys-from typing import List, Tuple, Optional+from typing import Any, List, Dict, Tuple, Optionalimport apache_beam as beamimport mirdatafrom basic_pitch.data import commandline, pipeline+from basic_pitch.data.datasets import DOWNLOADclass GuitarSetInvalidTracks(beam.DoFn):- def process(self, element: Tuple[str, str], *args, **kwargs):+ def process(self, element: Tuple[str, str], *args: Tuple[Any, Any], **kwargs: Dict[str, Any]) -> Any:track_id, split = elementyield beam.pvalue.TaggedOutput(split, track_id)@@ -39,22 +39,20 @@ class GuitarSetInvalidTracks(beam.DoFn):class GuitarSetToTfExample(beam.DoFn):DOWNLOAD_ATTRIBUTES = ["audio_mic_path", "jams_path"]- def __init__(self, source: str):+ def __init__(self, source: str, download: bool) -> None:self.source = source+ self.download = download- def setup(self):+ def setup(self) -> None:import apache_beam as beamimport mirdataself.guitarset_remote = mirdata.initialize("guitarset", data_home=self.source)- self.filesystem = beam.io.filesystems.FileSystems()- if (- type(self.filesystem.get_filesystem(self.source)) == beam.io.localfilesystem.LocalFileSystem- and "pytest" not in sys.modules- ):+ self.filesystem = beam.io.filesystems.FileSystems() # TODO: replace with fsspec+ if self.download:self.guitarset_remote.download()- def process(self, element: List[str], *args, **kwargs):+ def process(self, element: List[str], *args: Tuple[Any, Any], **kwargs: Dict[str, Any]) -> List[Any]:import tempfileimport mirdata@@ -85,9 +83,7 @@ class GuitarSetToTfExample(beam.DoFn):source = getattr(track_remote, attribute)destination = getattr(track_local, attribute)os.makedirs(os.path.dirname(destination), exist_ok=True)- with self.filesystem.open(source) as s, open(- destination, "wb"- ) as d:+ with self.filesystem.open(source) as s, open(destination, "wb") as d:d.write(s.read())local_wav_path = f"{track_local.audio_mic_path}_tmp.wav"@@ -150,17 +146,14 @@ def create_input_data(return "test"guitarset = mirdata.initialize("guitarset")- # guitarset.download()return [(track_id, determine_split()) for track_id in guitarset.track_ids]-def main(known_args, pipeline_args):+def main(known_args: argparse.Namespace, pipeline_args: List[str]) -> None:time_created = int(time.time())destination = commandline.resolve_destination(known_args, time_created)- input_data = create_input_data(- known_args.train_percent, known_args.validation_percent, known_args.split_seed- )+ input_data = create_input_data(known_args.train_percent, known_args.validation_percent, known_args.split_seed)pipeline_options = {"runner": known_args.runner,@@ -175,7 +168,7 @@ def main(known_args, pipeline_args):pipeline.run(pipeline_options,input_data,- GuitarSetToTfExample(known_args.source),+ GuitarSetToTfExample(known_args.source, DOWNLOAD),GuitarSetInvalidTracks(),destination,known_args.batch_size,diff --git a/basic_pitch/data/download.py b/basic_pitch/data/download.pyindex 8ed74b3..9ce1ba6 100644--- a/basic_pitch/data/download.py+++ b/basic_pitch/data/download.py@@ -17,7 +17,7 @@ DATASET_DICT = {}-def main():+def main() -> None:dataset_parser = argparse.ArgumentParser()dataset_parser.add_argument("dataset",diff --git a/basic_pitch/data/pipeline.py b/basic_pitch/data/pipeline.pyindex 23c0b37..8520075 100644--- a/basic_pitch/data/pipeline.py+++ b/basic_pitch/data/pipeline.py@@ -18,7 +18,7 @@import loggingimport osimport uuid-from typing import Dict, List, Tuple, Callable, Union+from typing import Any, Dict, List, Tuple, Callable, Unionimport apache_beam as beamimport tensorflow as tf@@ -27,27 +27,25 @@ from apache_beam.options.pipeline_options import PipelineOptions# Beacase beam.GroupIntoBatches isn't supported as of 2.29class Batch(beam.DoFn):- def __init__(self, batch_size):+ def __init__(self, batch_size: int) -> None:self.batch_size = batch_size- def process(self, element):+ def process(self, element: List[Any], *args: Tuple[Any, Any], **kwargs: Dict[str, Any]) -> Any:for i in range(0, len(element), self.batch_size):yield element[i : i + self.batch_size]class WriteBatchToTfRecord(beam.DoFn):- def __init__(self, destination):+ def __init__(self, destination: str) -> None:self.destination = destination- def process(self, element):+ def process(self, element: Any, *args: Tuple[Any, Any], **kwargs: Dict[str, Any]) -> None:if not isinstance(element, list):element = [element]logging.info(f"Writing to file batch of length {len(element)}")# hopefully uuids are unique enough- with tf.io.TFRecordWriter(- os.path.join(self.destination, f"{uuid.uuid4()}.tfrecord")- ) as writer:+ with tf.io.TFRecordWriter(os.path.join(self.destination, f"{uuid.uuid4()}.tfrecord")) as writer:for example in element:writer.write(example.SerializeToString())@@ -55,11 +53,11 @@ class WriteBatchToTfRecord(beam.DoFn):def transcription_dataset_writer(p: beam.Pipeline,input_data: List[Tuple[str, str]],- to_tf_example: Union[beam.DoFn, Callable],+ to_tf_example: Union[beam.DoFn, Callable[[List[Any]], Any]],filter_invalid_tracks: beam.PTransform,destination: str,batch_size: int,-):+) -> None:valid_track_ids = (p| "Create PCollection of track IDS" >> beam.Create(input_data)@@ -75,15 +73,13 @@ def transcription_dataset_writer((getattr(valid_track_ids, split)| f"Combine {split} into giant list" >> beam.transforms.combiners.ToList()- | f"Batch {split}" >> beam.ParDo(Batch(batch_size))+ # | f"Batch {split}" >> beam.ParDo(Batch(batch_size))+ | f"Batch {split}" >> beam.BatchElements(max_batch_size=batch_size)| f"Reshuffle {split}" >> beam.Reshuffle() # To prevent fuses| f"Create tf.Example {split} batch" >> beam.ParDo(to_tf_example)- | f"Write {split} batch to tfrecord"- >> beam.ParDo(WriteBatchToTfRecord(os.path.join(destination, split)))+ | f"Write {split} batch to tfrecord" >> beam.ParDo(WriteBatchToTfRecord(os.path.join(destination, split))))- getattr(- valid_track_ids, split- ) | f"Write {split} index file" >> beam.io.textio.WriteToText(+ getattr(valid_track_ids, split) | f"Write {split} index file" >> beam.io.textio.WriteToText(os.path.join(destination, split, "index.csv"),num_shards=1,header="track_id",@@ -98,8 +94,6 @@ def run(filter_invalid_tracks: beam.DoFn,destination: str,batch_size: int,-):+) -> None:with beam.Pipeline(options=PipelineOptions(**pipeline_options)) as p:- transcription_dataset_writer(- p, input_data, to_tf_example, filter_invalid_tracks, destination, batch_size- )+ transcription_dataset_writer(p, input_data, to_tf_example, filter_invalid_tracks, destination, batch_size)diff --git a/basic_pitch/data/tf_example_serialization.py b/basic_pitch/data/tf_example_serialization.pyindex a7e0432..8d4cb34 100644--- a/basic_pitch/data/tf_example_serialization.py+++ b/basic_pitch/data/tf_example_serialization.py@@ -49,9 +49,9 @@ def _to_transcription_tfex(encoded_wav: bytes,notes_indices: List[Tuple[int, int]],notes_values: List[float],- onsets_indices: List[float],+ onsets_indices: List[int],onsets_values: List[float],- contours_indices: List[float],+ contours_indices: List[int],contours_values: List[float],notes_onsets_shape: Tuple[int, int],contours_shape: Tuple[int, int],@@ -62,30 +62,14 @@ def _to_transcription_tfex("file_id": bytes_feature(bytes(file_id, "utf-8")),"source": bytes_feature(bytes(source, "utf-8")),"audio_wav": bytes_feature(encoded_wav),- "notes_indices": bytes_feature(- tf.io.serialize_tensor(np.array(notes_indices, np.int64))- ),- "notes_values": bytes_feature(- tf.io.serialize_tensor(np.array(notes_values, np.float32))- ),- "onsets_indices": bytes_feature(- tf.io.serialize_tensor(np.array(onsets_indices, np.int64))- ),- "onsets_values": bytes_feature(- tf.io.serialize_tensor(np.array(onsets_values, np.float32))- ),- "contours_indices": bytes_feature(- tf.io.serialize_tensor(np.array(contours_indices, np.int64))- ),- "contours_values": bytes_feature(- tf.io.serialize_tensor(np.array(contours_values, np.float32))- ),- "notes_onsets_shape": bytes_feature(- tf.io.serialize_tensor(np.array(notes_onsets_shape, np.int64))- ),- "contours_shape": bytes_feature(- tf.io.serialize_tensor(np.array(contours_shape, np.int64))- ),+ "notes_indices": bytes_feature(tf.io.serialize_tensor(np.array(notes_indices, np.int64))),+ "notes_values": bytes_feature(tf.io.serialize_tensor(np.array(notes_values, np.float32))),+ "onsets_indices": bytes_feature(tf.io.serialize_tensor(np.array(onsets_indices, np.int64))),+ "onsets_values": bytes_feature(tf.io.serialize_tensor(np.array(onsets_values, np.float32))),+ "contours_indices": bytes_feature(tf.io.serialize_tensor(np.array(contours_indices, np.int64))),+ "contours_values": bytes_feature(tf.io.serialize_tensor(np.array(contours_values, np.float32))),+ "notes_onsets_shape": bytes_feature(tf.io.serialize_tensor(np.array(notes_onsets_shape, np.int64))),+ "contours_shape": bytes_feature(tf.io.serialize_tensor(np.array(contours_shape, np.int64))),}))@@ -97,9 +81,9 @@ def to_transcription_tfexample(audio_wav_file_path: str,notes_indices: List[Tuple[int, int]],notes_values: List[float],- onsets_indices: List[float],+ onsets_indices: List[int],onsets_values: List[float],- contours_indices: List[float],+ contours_indices: List[int],contours_values: List[float],notes_onsets_shape: Tuple[int, int],contours_shape: Tuple[int, int],diff --git a/basic_pitch/inference.py b/basic_pitch/inference.pyindex b4a33e0..2544cd9 100644--- a/basic_pitch/inference.py+++ b/basic_pitch/inference.py@@ -96,7 +96,7 @@ class Model:present.append("CoreML")try:self.model_type = Model.MODEL_TYPES.COREML- self.model = ct.models.MLModel(str(model_path))+ self.model = ct.models.MLModel(str(model_path), compute_units=ct.ComputeUnit.CPU_ONLY)returnexcept Exception as e:if str(model_path).endswith(".mlpackage"):@@ -224,9 +224,7 @@ def get_audio_input(length of original audio file, in frames, BEFORE padding."""- assert overlap_len % 2 == 0, "overlap_length must be even, got {}".format(- overlap_len- )+ assert overlap_len % 2 == 0, f"overlap_length must be even, got {overlap_len}"audio_original, _ = librosa.load(str(audio_path), sr=AUDIO_SAMPLE_RATE, mono=True)@@ -307,9 +305,7 @@ def run_inference("audio_original_length": audio_original_length,"hop_size_samples": hop_size,"overlap_length_samples": overlap_len,- "unwrapped_output": {- k: v.tolist() for k, v in unwrapped_output.items()- },+ "unwrapped_output": {k: v.tolist() for k, v in unwrapped_output.items()},},f,)@@ -407,9 +403,7 @@ def save_note_events(with open(save_path, "w") as fhandle:writer = csv.writer(fhandle, delimiter=",")- writer.writerow(- ["start_time_s", "end_time_s", "pitch_midi", "velocity", "pitch_bend"]- )+ writer.writerow(["start_time_s", "end_time_s", "pitch_midi", "velocity", "pitch_bend"])for start_time, end_time, note_number, amplitude, pitch_bend in note_events:row = [start_time, end_time, note_number, int(np.round(127 * amplitude))]if pitch_bend:@@ -552,25 +546,17 @@ def predict_and_save()if save_model_outputs:- model_output_path = build_output_path(- audio_path, output_directory, OutputExtensions.MODEL_OUTPUT_NPZ- )+ model_output_path = build_output_path(audio_path, output_directory, OutputExtensions.MODEL_OUTPUT_NPZ)try:np.savez(model_output_path, basic_pitch_model_output=model_output)- file_saved_confirmation(- OutputExtensions.MODEL_OUTPUT_NPZ.name, model_output_path- )+ file_saved_confirmation(OutputExtensions.MODEL_OUTPUT_NPZ.name, model_output_path)except Exception as e:- failed_to_save(- OutputExtensions.MODEL_OUTPUT_NPZ.name, model_output_path- )+ failed_to_save(OutputExtensions.MODEL_OUTPUT_NPZ.name, model_output_path)raise eif save_midi:try:- midi_path = build_output_path(- audio_path, output_directory, OutputExtensions.MIDI- )+ midi_path = build_output_path(audio_path, output_directory, OutputExtensions.MIDI)except IOError as e:raise etry:@@ -581,31 +567,19 @@ def predict_and_save(raise eif sonify_midi:- midi_sonify_path = build_output_path(- audio_path, output_directory, OutputExtensions.MIDI_SONIFICATION- )+ midi_sonify_path = build_output_path(audio_path, output_directory, OutputExtensions.MIDI_SONIFICATION)try:- infer.sonify_midi(- midi_data, midi_sonify_path, sr=sonification_samplerate- )- file_saved_confirmation(- OutputExtensions.MIDI_SONIFICATION.name, midi_sonify_path- )+ infer.sonify_midi(midi_data, midi_sonify_path, sr=sonification_samplerate)+ file_saved_confirmation(OutputExtensions.MIDI_SONIFICATION.name, midi_sonify_path)except Exception as e:- failed_to_save(- OutputExtensions.MIDI_SONIFICATION.name, midi_sonify_path- )+ failed_to_save(OutputExtensions.MIDI_SONIFICATION.name, midi_sonify_path)raise eif save_notes:- note_events_path = build_output_path(- audio_path, output_directory, OutputExtensions.NOTE_EVENTS- )+ note_events_path = build_output_path(audio_path, output_directory, OutputExtensions.NOTE_EVENTS)try:save_note_events(note_events, note_events_path)- file_saved_confirmation(- OutputExtensions.NOTE_EVENTS.name, note_events_path- )+ file_saved_confirmation(OutputExtensions.NOTE_EVENTS.name, note_events_path)except Exception as e:failed_to_save(OutputExtensions.NOTE_EVENTS.name, note_events_path)raise ediff --git a/basic_pitch/layers/nnaudio.py b/basic_pitch/layers/nnaudio.pyindex 078b060..a63fbf7 100644--- a/basic_pitch/layers/nnaudio.py+++ b/basic_pitch/layers/nnaudio.py@@ -56,9 +56,7 @@ def create_lowpass_filter(gain_at_key_frequencies = [1.0, 1.0, 0.0, 0.0]# This command produces the filter kernel coefficients- filter_kernel = scipy.signal.firwin2(- kernel_length, key_frequencies, gain_at_key_frequencies- )+ filter_kernel = scipy.signal.firwin2(kernel_length, key_frequencies, gain_at_key_frequencies)return tf.constant(filter_kernel, dtype=dtype)@@ -76,9 +74,7 @@ def early_downsample(filter_cutoff_hz: float,) -> Tuple[Union[float, int], int, int]:"""Return new sampling rate and hop length after early downsampling"""- downsample_count = early_downsample_count(- nyquist_hz, filter_cutoff_hz, hop_length, n_octaves- )+ downsample_count = early_downsample_count(nyquist_hz, filter_cutoff_hz, hop_length, n_octaves)downsample_factor = 2 ** (downsample_count)hop_length //= downsample_factor # Getting new hop_length@@ -90,14 +86,10 @@ def early_downsample(# The following two downsampling count functions are obtained from librosa CQT# They are used to determine the number of pre resamplings if the starting and ending frequency# are both in low frequency regions.-def early_downsample_count(- nyquist_hz: float, filter_cutoff_hz: float, hop_length: int, n_octaves: int-) -> int:+def early_downsample_count(nyquist_hz: float, filter_cutoff_hz: float, hop_length: int, n_octaves: int) -> int:"""Compute the number of early downsampling operations"""- downsample_count1 = max(- 0, int(np.ceil(np.log2(0.85 * nyquist_hz / filter_cutoff_hz)) - 1) - 1- )+ downsample_count1 = max(0, int(np.ceil(np.log2(0.85 * nyquist_hz / filter_cutoff_hz)) - 1) - 1)num_twos = next_power_of_2(hop_length)downsample_count2 = max(0, num_twos - n_octaves + 1)@@ -116,9 +108,7 @@ def get_early_downsample_params(window_bandwidth = 1.5 # for hann windowfilter_cutoff = fmax_t * (1 + 0.5 * window_bandwidth / Q)- sr, hop_length, downsample_factor = early_downsample(- sr, hop_length, n_octaves, sr // 2, filter_cutoff- )+ sr, hop_length, downsample_factor = early_downsample(sr, hop_length, n_octaves, sr // 2, filter_cutoff)if downsample_factor != 1:earlydownsample = Trueearly_downsample_filter = create_lowpass_filter(@@ -134,9 +124,7 @@ def get_early_downsample_params(return sr, hop_length, downsample_factor, early_downsample_filter, earlydownsample-def get_window_dispatch(- window: Union[str, Tuple[str, float]], N: int, fftbins: bool = True-) -> np.array:+def get_window_dispatch(window: Union[str, Tuple[str, float]], N: int, fftbins: bool = True) -> np.array:if isinstance(window, str):return scipy.signal.get_window(window, N, fftbins=fftbins)elif isinstance(window, tuple):@@ -147,15 +135,9 @@ def get_window_dispatch(else:Warning("Tuple windows may have undesired behaviour regarding Q factor")elif isinstance(window, float):- Warning(- "You are using Kaiser window with beta factor "- + str(window)- + ". Correct behaviour not checked."- )+ Warning("You are using Kaiser window with beta factor " + str(window) + ". Correct behaviour not checked.")else:- raise Exception(- "The function get_window from scipy only supports strings, tuples and floats."- )+ raise Exception("The function get_window from scipy only supports strings, tuples and floats.")def create_cqt_kernels(@@ -176,9 +158,7 @@ def create_cqt_kernels(fftLen = 2 ** next_power_of_2(np.ceil(Q * fs / fmin))if (fmax is not None) and (n_bins is None):- n_bins = np.ceil(- bins_per_octave * np.log2(fmax / fmin)- ) # Calculate the number of bins+ n_bins = np.ceil(bins_per_octave * np.log2(fmax / fmin)) # Calculate the number of binsfreqs = fmin * 2.0 ** (np.r_[0:n_bins] / float(bins_per_octave))elif (fmax is None) and (n_bins is not None):@@ -186,16 +166,12 @@ def create_cqt_kernels(else:warnings.warn("If fmax is given, n_bins will be ignored", SyntaxWarning)- n_bins = np.ceil(- bins_per_octave * np.log2(fmax / fmin)- ) # Calculate the number of bins+ n_bins = np.ceil(bins_per_octave * np.log2(fmax / fmin)) # Calculate the number of binsfreqs = fmin * 2.0 ** (np.r_[0:n_bins] / float(bins_per_octave))if np.max(freqs) > fs / 2 and topbin_check is True:raise ValueError(- "The top bin {}Hz has exceeded the Nyquist frequency, please reduce the n_bins".format(- np.max(freqs)- )+ "The top bin {}Hz has exceeded the Nyquist frequency, please reduce the n_bins".format(np.max(freqs)))tempKernel = np.zeros((int(n_bins), int(fftLen)), dtype=np.complex64)@@ -235,18 +211,14 @@ def get_cqt_complex(a constant Q transform.” (1992)."""try:- x = padding(- x- ) # When center is True, we need padding at the beginning and ending+ x = padding(x) # When center is True, we need padding at the beginning and endingexcept Exception:warnings.warn(f"\ninput size = {x.shape}\tkernel size = {cqt_kernels_real.shape[-1]}\n""padding with reflection mode might not be the best choice, try using constant padding",UserWarning,)- x = tf.pad(- x, (cqt_kernels_real.shape[-1] // 2, cqt_kernels_real.shape[-1] // 2)- )+ x = tf.pad(x, (cqt_kernels_real.shape[-1] // 2, cqt_kernels_real.shape[-1] // 2))CQT_real = tf.transpose(tf.nn.conv1d(tf.transpose(x, [0, 2, 1]),@@ -269,9 +241,7 @@ def get_cqt_complex(return tf.stack((CQT_real, CQT_imag), axis=-1)-def downsampling_by_n(- x: tf.Tensor, filter_kernel: tf.Tensor, n: float, match_torch_exactly: bool = True-) -> tf.Tensor:+def downsampling_by_n(x: tf.Tensor, filter_kernel: tf.Tensor, n: float, match_torch_exactly: bool = True) -> tf.Tensor:"""Downsample the given tensor using the given filter kernel.The input tensor is expected to have shape `(n_batches, channels, width)`,@@ -291,14 +261,10 @@ def downsampling_by_n(# Store this tensor in the shape `(n_batches, width, channels)`padded_nwc = tf.transpose(padded, [0, 2, 1])- result_nwc = tf.nn.conv1d(- padded_nwc, filter_kernel[:, None, None], padding="VALID", stride=n- )+ result_nwc = tf.nn.conv1d(padded_nwc, filter_kernel[:, None, None], padding="VALID", stride=n)else:x_nwc = tf.transpose(x, [0, 2, 1])- result_nwc = tf.nn.conv1d(- x_nwc, filter_kernel[:, None, None], padding="SAME", stride=n- )+ result_nwc = tf.nn.conv1d(x_nwc, filter_kernel[:, None, None], padding="SAME", stride=n)result_ncw = tf.transpose(result_nwc, [0, 2, 1])return result_ncw@@ -317,11 +283,7 @@ class ReflectionPad1D(tf.keras.layers.Layer):return (s[0],s[1],- (- s[2] + 2 * self.padding- if isinstance(self.padding, int)- else self.padding[0]- ),+ (s[2] + 2 * self.padding if isinstance(self.padding, int) else self.padding[0]),)def call(self, x: tf.Tensor) -> tf.Tensor:@@ -333,9 +295,7 @@ class ConstantPad1D(tf.keras.layers.Layer):Replica of Torch's nn.ConstantPad1D in TF."""- def __init__(- self, padding: Union[int, Tuple[int]] = 1, value: int = 0, **kwargs: Any- ):+ def __init__(self, padding: Union[int, Tuple[int]] = 1, value: int = 0, **kwargs: Any):self.padding = paddingself.value = valueself.input_spec = [tf.keras.layers.InputSpec(ndim=3)]@@ -345,22 +305,14 @@ class ConstantPad1D(tf.keras.layers.Layer):return (s[0],s[1],- (- s[2] + 2 * self.padding- if isinstance(self.padding, int)- else self.padding[0]- ),+ (s[2] + 2 * self.padding if isinstance(self.padding, int) else self.padding[0]),)def call(self, x: tf.Tensor) -> tf.Tensor:- return tf.pad(- x, [[0, 0], [0, 0], [self.padding, self.padding]], "CONSTANT", self.value- )+ return tf.pad(x, [[0, 0], [0, 0], [self.padding, self.padding]], "CONSTANT", self.value)-def pad_center(- data: np.ndarray, size: int, axis: int = -1, **kwargs: Any-) -> np.ndarray:+def pad_center(data: np.ndarray, size: int, axis: int = -1, **kwargs: Any) -> np.ndarray:"""Wrapper for np.pad to automatically center an array prior to padding.This is analogous to `str.center()`@@ -423,9 +375,7 @@ def pad_center(lengths[axis] = (lpad, int(size - n - lpad))if lpad < 0:- raise ValueError(- ("Target size ({:d}) must be at least input size ({:d})").format(size, n)- )+ raise ValueError(("Target size ({:d}) must be at least input size ({:d})").format(size, n))return np.pad(data, lengths, **kwargs)@@ -574,9 +524,7 @@ class CQT2010v2(tf.keras.layers.Layer):# This will be used to calculate filter_cutoff and creating CQT kernelsQ = float(self.filter_scale) / (2 ** (1 / self.bins_per_octave) - 1)- self.lowpass_filter = create_lowpass_filter(- band_center=0.5, kernel_length=256, transition_bandwidth=0.001- )+ self.lowpass_filter = create_lowpass_filter(band_center=0.5, kernel_length=256, transition_bandwidth=0.001)# Calculate num of filter requires for the kernel# n_octaves determines how many resampling requires for the CQT@@ -589,35 +537,25 @@ class CQT2010v2(tf.keras.layers.Layer):if remainder == 0:# Calculate the top bin frequency- fmax_t = self.fmin_t * 2 ** (- (self.bins_per_octave - 1) / self.bins_per_octave- )+ fmax_t = self.fmin_t * 2 ** ((self.bins_per_octave - 1) / self.bins_per_octave)else:# Calculate the top bin frequencyfmax_t = self.fmin_t * 2 ** ((remainder - 1) / self.bins_per_octave)- self.fmin_t = fmax_t / 2 ** (- 1 - 1 / self.bins_per_octave- ) # Adjusting the top minium bins+ self.fmin_t = fmax_t / 2 ** (1 - 1 / self.bins_per_octave) # Adjusting the top minium binsif fmax_t > self.sample_rate / 2:raise ValueError(- "The top bin {}Hz has exceeded the Nyquist frequency, please reduce the n_bins".format(- fmax_t- )+ "The top bin {}Hz has exceeded the Nyquist frequency, please reduce the n_bins".format(fmax_t))- if (- self.earlydownsample is True- ): # Do early downsampling if this argument is True+ if self.earlydownsample is True: # Do early downsampling if this argument is True(self.sample_rate,self.hop_length,self.downsample_factor,early_downsample_filter,self.earlydownsample,- ) = get_early_downsample_params(- self.sample_rate, self.hop_length, fmax_t, Q, self.n_octaves, self.dtype- )+ ) = get_early_downsample_params(self.sample_rate, self.hop_length, fmax_t, Q, self.n_octaves, self.dtype)self.early_downsample_filter = early_downsample_filterelse:@@ -638,9 +576,7 @@ class CQT2010v2(tf.keras.layers.Layer):# The freqs returned by create_cqt_kernels cannot be used# Since that returns only the top octave bins# We need the information for all freq bin- freqs = self.fmin * 2.0 ** (- np.r_[0 : self.n_bins] / float(self.bins_per_octave)- )+ freqs = self.fmin * 2.0 ** (np.r_[0 : self.n_bins] / float(self.bins_per_octave))self.frequencies = freqsself.lengths = np.ceil(Q * self.sample_rate / freqs)@@ -653,12 +589,8 @@ class CQT2010v2(tf.keras.layers.Layer):self.cqt_kernels_imag = tf.expand_dims(basis.imag.astype(self.dtype), 1)if self.trainable:- self.cqt_kernels_real = tf.Variable(- initial_value=self.cqt_kernels_real, trainable=True- )- self.cqt_kernels_imag = tf.Variable(- initial_value=self.cqt_kernels_imag, trainable=True- )+ self.cqt_kernels_real = tf.Variable(initial_value=self.cqt_kernels_real, trainable=True)+ self.cqt_kernels_imag = tf.Variable(initial_value=self.cqt_kernels_imag, trainable=True)# If center==True, the STFT window will be put in the middle, and paddings at the beginning# and ending are required.@@ -675,9 +607,7 @@ class CQT2010v2(tf.keras.layers.Layer):elif rank == 3:self.reshape_input = lambda x: xelse:- raise ValueError(- f"Input shape must be rank <= 3, found shape {input_shape}"- )+ raise ValueError(f"Input shape must be rank <= 3, found shape {input_shape}")def call(self, x: tf.Tensor) -> tf.Tensor:x = self.reshape_input(x) # type: ignore@@ -693,20 +623,14 @@ class CQT2010v2(tf.keras.layers.Layer):hop = self.hop_length# Getting the top octave CQT- CQT = get_cqt_complex(- x, self.cqt_kernels_real, self.cqt_kernels_imag, hop, self.padding- )+ CQT = get_cqt_complex(x, self.cqt_kernels_real, self.cqt_kernels_imag, hop, self.padding)x_down = x # Preparing a new variable for downsamplingfor _ in range(self.n_octaves - 1):hop = hop // 2- x_down = downsampling_by_n(- x_down, self.lowpass_filter, 2, self.match_torch_exactly- )- CQT1 = get_cqt_complex(- x_down, self.cqt_kernels_real, self.cqt_kernels_imag, hop, self.padding- )+ x_down = downsampling_by_n(x_down, self.lowpass_filter, 2, self.match_torch_exactly)+ CQT1 = get_cqt_complex(x_down, self.cqt_kernels_real, self.cqt_kernels_imag, hop, self.padding)CQT = tf.concat((CQT1, CQT), axis=1)CQT = CQT[:, -self.n_bins :, :] # Removing unwanted bottom bins@@ -723,10 +647,7 @@ class CQT2010v2(tf.keras.layers.Layer):elif self.normalization_type == "wrap":CQT *= 2else:- raise ValueError(- "The normalization_type %r is not part of our current options."- % self.normalization_type- )+ raise ValueError("The normalization_type %r is not part of our current options." % self.normalization_type)# Transpose the output to match the output of the other spectrogram layers.if self.output_format.lower() == "magnitude":diff --git a/basic_pitch/layers/signal.py b/basic_pitch/layers/signal.pyindex dc69fae..f5f629f 100644--- a/basic_pitch/layers/signal.py+++ b/basic_pitch/layers/signal.py@@ -67,9 +67,7 @@ class Stft(tf.keras.layers.Layer):lpad = (self.fft_length - self.window_length) // 2rpad = self.fft_length - self.window_length - lpad- def padded_window(- window_length: int, dtype: tf.dtypes.DType = tf.float32- ) -> tf.Tensor:+ def padded_window(window_length: int, dtype: tf.dtypes.DType = tf.float32) -> tf.Tensor:# This is a trick to match librosa's way of handling window lengths < their fft_lengths# In that case the window is 0 padded such that the window is centered around 0s# In the Tensorflow case, the window is computed, multiplied against the frame and then@@ -82,8 +80,7 @@ class Stft(tf.keras.layers.Layer):self.spec = tf.keras.layers.Lambda(lambda x: tf.pad(x,- [[0, 0] for _ in range(input_shape.rank - 1)]- + [[self.fft_length // 2, self.fft_length // 2]],+ [[0, 0] for _ in range(input_shape.rank - 1)] + [[self.fft_length // 2, self.fft_length // 2]],mode=self.pad_mode,))@@ -166,14 +163,10 @@ class NormalizedLog(tf.keras.layers.Layer):self.squeeze_batch = lambda batch: batchrank = input_shape.rankif rank == 4:- assert (- input_shape[1] == 1- ), "If the rank is 4, the second dimension must be length 1"+ assert input_shape[1] == 1, "If the rank is 4, the second dimension must be length 1"self.squeeze_batch = lambda batch: tf.squeeze(batch, axis=1)else:- assert (- rank == 3- ), f"Only ranks 3 and 4 are supported!. Received rank {rank} for {input_shape}."+ assert rank == 3, f"Only ranks 3 and 4 are supported!. Received rank {rank} for {input_shape}."def call(self, inputs: tf.Tensor) -> tf.Tensor:inputs = self.squeeze_batch(inputs) # type: ignore@@ -181,16 +174,12 @@ class NormalizedLog(tf.keras.layers.Layer):power = tf.math.square(inputs)log_power = 10 * log_base_b(power + 1e-10, 10)- log_power_min = tf.reshape(- tf.math.reduce_min(log_power, axis=[1, 2]), [tf.shape(inputs)[0], 1, 1]- )+ log_power_min = tf.reshape(tf.math.reduce_min(log_power, axis=[1, 2]), [tf.shape(inputs)[0], 1, 1])log_power_offset = log_power - log_power_minlog_power_offset_max = tf.reshape(tf.math.reduce_max(log_power_offset, axis=[1, 2]),[tf.shape(inputs)[0], 1, 1],)- log_power_normalized = tf.math.divide_no_nan(- log_power_offset, log_power_offset_max- )+ log_power_normalized = tf.math.divide_no_nan(log_power_offset, log_power_offset_max)return tf.reshape(log_power_normalized, tf.shape(inputs))diff --git a/basic_pitch/models.py b/basic_pitch/models.pyindex b5d8e9a..b8ec993 100644--- a/basic_pitch/models.py+++ b/basic_pitch/models.py@@ -33,14 +33,10 @@ from basic_pitch.layers import signal, nnaudiotfkl = tf.keras.layers-MAX_N_SEMITONES = int(- np.floor(12.0 * np.log2(0.5 * AUDIO_SAMPLE_RATE / ANNOTATIONS_BASE_FREQUENCY))-)+MAX_N_SEMITONES = int(np.floor(12.0 * np.log2(0.5 * AUDIO_SAMPLE_RATE / ANNOTATIONS_BASE_FREQUENCY)))-def transcription_loss(- y_true: tf.Tensor, y_pred: tf.Tensor, label_smoothing: float-) -> tf.Tensor:+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.@@ -52,9 +48,7 @@ def transcription_loss(Returns:The transcription loss."""- bce = tf.keras.losses.binary_crossentropy(- y_true, y_pred, label_smoothing=label_smoothing- )+ bce = tf.keras.losses.binary_crossentropy(y_true, y_pred, label_smoothing=label_smoothing)return bce@@ -112,9 +106,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 = 0.2, weighted: bool = False, positive_weight: float = 0.5) -> Dict[str, Any]:"""Creates a keras-compatible dictionary of loss functions to calculatethe loss for the contour, note and onset posteriorgrams.@@ -138,9 +130,7 @@ def loss(def _initializer() -> tf.keras.initializers.VarianceScaling:- return tf.keras.initializers.VarianceScaling(- scale=2.0, mode="fan_avg", distribution="uniform", seed=None- )+ return tf.keras.initializers.VarianceScaling(scale=2.0, mode="fan_avg", distribution="uniform", seed=None)def _kernel_constraint() -> tf.keras.constraints.UnitNorm:diff --git a/basic_pitch/nn.py b/basic_pitch/nn.pyindex 4633f85..6437242 100644--- a/basic_pitch/nn.py+++ b/basic_pitch/nn.py@@ -51,8 +51,7 @@ class HarmonicStacking(tf.keras.layers.Layer):self.bins_per_semitone = bins_per_semitoneself.harmonics = harmonicsself.shifts = [- int(tf.math.round(12.0 * self.bins_per_semitone * log_base_b(float(h), 2)))- for h in self.harmonics+ int(tf.math.round(12.0 * self.bins_per_semitone * log_base_b(float(h), 2))) for h in self.harmonics]self.n_output_freqs = n_output_freqs@@ -86,9 +85,7 @@ class HarmonicStacking(tf.keras.layers.Layer):channels.append(padded)x = tf.concat(channels, axis=-1)- x = x[- :, :, : self.n_output_freqs, :- ] # return only the first n_output_freqs frequency channels+ x = x[:, :, : self.n_output_freqs, :] # return only the first n_output_freqs frequency channelsreturn x@@ -116,6 +113,4 @@ class FlattenFreqCh(tf.keras.layers.Layer):def call(self, x: tf.Tensor) -> tf.Tensor:shapes = K.int_shape(x)- return tf.keras.layers.Reshape([shapes[1], shapes[2] * shapes[3]])(- x- ) # ignore batch size+ return tf.keras.layers.Reshape([shapes[1], shapes[2] * shapes[3]])(x) # ignore batch sizediff --git a/basic_pitch/note_creation.py b/basic_pitch/note_creation.pyindex 875fabc..27b63d3 100644--- a/basic_pitch/note_creation.py+++ b/basic_pitch/note_creation.py@@ -55,9 +55,7 @@ def model_output_to_notes(multiple_pitch_bends: bool = False,melodia_trick: bool = True,midi_tempo: float = 120,-) -> Tuple[- pretty_midi.PrettyMIDI, List[Tuple[float, float, int, float, Optional[List[int]]]]-]:+) -> Tuple[pretty_midi.PrettyMIDI, List[Tuple[float, float, int, float, Optional[List[int]]]]]:"""Convert model output to MIDIArgs:@@ -99,20 +97,15 @@ def model_output_to_notes(if include_pitch_bends:estimated_notes_with_pitch_bend = get_pitch_bends(contours, estimated_notes)else:- estimated_notes_with_pitch_bend = [- (note[0], note[1], note[2], note[3], None) for note in estimated_notes- ]+ estimated_notes_with_pitch_bend = [(note[0], note[1], note[2], note[3], None) for note in estimated_notes]times_s = model_frames_to_time(contours.shape[0])estimated_notes_time_seconds = [- (times_s[note[0]], times_s[note[1]], note[2], note[3], note[4])- for note in estimated_notes_with_pitch_bend+ (times_s[note[0]], times_s[note[1]], note[2], note[3], note[4]) for note in estimated_notes_with_pitch_bend]return (- note_events_to_midi(- estimated_notes_time_seconds, multiple_pitch_bends, midi_tempo- ),+ note_events_to_midi(estimated_notes_time_seconds, multiple_pitch_bends, midi_tempo),estimated_notes_time_seconds,)@@ -165,9 +158,7 @@ def sonify_salience(hop_length=AUDIO_N_SAMPLES / ANNOT_N_FRAMES, # THIS IS THE CORRECT HOP!!)gram[gram < thresh] = 0- y = mir_eval.sonify.time_frequency(- gram[:max_freq_idx, :], freqs[:max_freq_idx], times, fs=SONIFY_FS- )+ y = mir_eval.sonify.time_frequency(gram[:max_freq_idx, :], freqs[:max_freq_idx], times, fs=SONIFY_FS)if save_path:y_resamp = resampy.resample(y, SONIFY_FS, 44100)wavfile.write(save_path, 44100, y_resamp)@@ -186,11 +177,7 @@ def midi_pitch_to_contour_bin(pitch_midi: int) -> np.array:"""pitch_hz = librosa.midi_to_hz(pitch_midi)- return (- 12.0- * CONTOURS_BINS_PER_SEMITONE- * np.log2(pitch_hz / ANNOTATIONS_BASE_FREQUENCY)- )+ return 12.0 * CONTOURS_BINS_PER_SEMITONE * np.log2(pitch_hz / ANNOTATIONS_BASE_FREQUENCY)def get_pitch_bends(@@ -231,16 +218,12 @@ def get_pitch_bends(bends: Optional[List[int]] = list(np.argmax(pitch_bend_submatrix, axis=1) - pb_shift) # this is in units of 1/3 semitones- note_events_with_pitch_bends.append(- (start_idx, end_idx, pitch_midi, amplitude, bends)- )+ note_events_with_pitch_bends.append((start_idx, end_idx, pitch_midi, amplitude, bends))return note_events_with_pitch_bendsdef note_events_to_midi(- note_events_with_pitch_bends: List[- Tuple[float, float, int, float, Optional[List[int]]]- ],+ note_events_with_pitch_bends: List[Tuple[float, float, int, float, Optional[List[int]]]],multiple_pitch_bends: bool = False,midi_tempo: float = 120,) -> pretty_midi.PrettyMIDI:@@ -259,9 +242,7 @@ def note_events_to_midi("""mid = pretty_midi.PrettyMIDI(initial_tempo=midi_tempo)if not multiple_pitch_bends:- note_events_with_pitch_bends = drop_overlapping_pitch_bends(- note_events_with_pitch_bends- )+ note_events_with_pitch_bends = drop_overlapping_pitch_bends(note_events_with_pitch_bends)piano_program = pretty_midi.instrument_name_to_program("Electric Piano 1")instruments: DefaultDict[int, pretty_midi.Instrument] = defaultdict(@@ -274,9 +255,7 @@ def note_events_to_midi(amplitude,pitch_bend,) in note_events_with_pitch_bends:- instrument = (- instruments[note_number] if multiple_pitch_bends else instruments[0]- )+ instrument = instruments[note_number] if multiple_pitch_bends else instruments[0]note = pretty_midi.Note(velocity=int(np.round(127 * amplitude)),pitch=note_number,@@ -287,17 +266,11 @@ def note_events_to_midi(if not pitch_bend:continuepitch_bend_times = np.linspace(start_time, end_time, len(pitch_bend))- pitch_bend_midi_ticks = np.round(- np.array(pitch_bend) * 4096 / CONTOURS_BINS_PER_SEMITONE- ).astype(int)+ pitch_bend_midi_ticks = np.round(np.array(pitch_bend) * 4096 / CONTOURS_BINS_PER_SEMITONE).astype(int)# This supports pitch bends up to 2 semitones# If we estimate pitch bends above/below 2 semitones, crop them here when adding them to the midi file- pitch_bend_midi_ticks[pitch_bend_midi_ticks > N_PITCH_BEND_TICKS - 1] = (- N_PITCH_BEND_TICKS - 1- )- pitch_bend_midi_ticks[pitch_bend_midi_ticks < -N_PITCH_BEND_TICKS] = (- -N_PITCH_BEND_TICKS- )+ pitch_bend_midi_ticks[pitch_bend_midi_ticks > N_PITCH_BEND_TICKS - 1] = N_PITCH_BEND_TICKS - 1+ pitch_bend_midi_ticks[pitch_bend_midi_ticks < -N_PITCH_BEND_TICKS] = -N_PITCH_BEND_TICKSfor pb_time, pb_midi in zip(pitch_bend_times, pitch_bend_midi_ticks):instrument.pitch_bends.append(pretty_midi.PitchBend(pb_midi, pb_time))mid.instruments.extend(instruments.values())@@ -306,9 +279,7 @@ def note_events_to_midi(def drop_overlapping_pitch_bends(- note_events_with_pitch_bends: List[- Tuple[float, float, int, float, Optional[List[int]]]- ]+ note_events_with_pitch_bends: List[Tuple[float, float, int, float, Optional[List[int]]]]) -> List[Tuple[float, float, int, float, Optional[List[int]]]]:"""Drop pitch bends from any notes that overlap in time with another note"""note_events = sorted(note_events_with_pitch_bends)@@ -340,13 +311,9 @@ def get_infered_onsets(onsets: np.array, frames: np.array, n_diff: int = 2) -> nframe_diff = np.min(diffs, axis=0)frame_diff[frame_diff < 0] = 0frame_diff[:n_diff, :] = 0- frame_diff = (- np.max(onsets) * frame_diff / np.max(frame_diff)- ) # rescale to have the same max as onsets+ frame_diff = np.max(onsets) * frame_diff / np.max(frame_diff) # rescale to have the same max as onsets- max_onsets_diff = np.max(- [onsets, frame_diff], axis=0- ) # use the max of the predicted onsets and the differences+ max_onsets_diff = np.max([onsets, frame_diff], axis=0) # use the max of the predicted onsets and the differencesreturn max_onsets_diff@@ -488,9 +455,7 @@ def output_to_notes_polyphonic(energy_shape = remaining_energy.shapewhile np.max(remaining_energy) > frame_thresh:- i_mid, freq_idx = np.unravel_index(- np.argmax(remaining_energy), energy_shape- )+ i_mid, freq_idx = np.unravel_index(np.argmax(remaining_energy), energy_shape)remaining_energy[i_mid, freq_idx] = 0# forward passdiff --git a/pyproject.toml b/pyproject.tomlindex f6957ed..e649880 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -53,19 +53,17 @@ basic-pitch = "basic_pitch.predict:main"[project.optional-dependencies]train = [- "basic_pitch[tf,test]",+ "basic_pitch[tf]","apache_beam","mirdata","smart_open","sox"]test = [- "apache_beam",+ "basic_pitch[train]","coverage>=5.0.2",- "mirdata","pytest>=6.1.1","pytest-mock",- "sox"]tf = ["tensorflow>=2.4.1,<2.15.1; platform_system != 'Darwin'",@@ -78,7 +76,6 @@ dev = ["basic_pitch[train,test,tf,coreml,onnx,docs]","mypy","tox",- "mirdata"][tool.distutils.bdist_wheel]diff --git a/tests/data/test_guitarset.py b/tests/data/test_guitarset.pyindex 7701ca6..9212cb5 100644--- a/tests/data/test_guitarset.py+++ b/tests/data/test_guitarset.py@@ -33,16 +33,15 @@ RESOURCES_PATH = pathlib.Path(__file__).parent.parent / "resources"TRACK_ID = "00_BN1-129-Eb_comp"-def test_guitar_set_to_tf_example(tmpdir: str):+def test_guitar_set_to_tf_example(tmpdir: str) -> None:+ DOWNLOAD = Falseinput_data: List[str] = [TRACK_ID]with TestPipeline() as p:(p| "Create PCollection of track IDs" >> beam.Create([input_data])| "Create tf.Example"- >> beam.ParDo(- GuitarSetToTfExample(str(RESOURCES_PATH / "data" / "guitarset"))- )+ >> beam.ParDo(GuitarSetToTfExample(str(RESOURCES_PATH / "data" / "guitarset"), DOWNLOAD))| "Write to tfrecord" >> beam.ParDo(WriteBatchToTfRecord(tmpdir)))@@ -53,24 +52,21 @@ def test_guitar_set_to_tf_example(tmpdir: str):assert len(data) != 0-def test_guitar_set_invalid_tracks(tmpdir):+def test_guitar_set_invalid_tracks(tmpdir: str) -> None:split_labels = ["train", "test", "validation"]input_data = [(str(i), split) for i, split in enumerate(split_labels)]with TestPipeline() as p:splits = (p| "Create PCollection" >> beam.Create(input_data)- | "Tag it"- >> beam.ParDo(GuitarSetInvalidTracks()).with_outputs(*split_labels)+ | "Tag it" >> beam.ParDo(GuitarSetInvalidTracks()).with_outputs(*split_labels))for split in split_labels:(getattr(splits, split)| f"Write {split} to text"- >> beam.io.WriteToText(- os.path.join(tmpdir, f"output_{split}.txt"), shard_name_template=""- )+ >> beam.io.WriteToText(os.path.join(tmpdir, f"output_{split}.txt"), shard_name_template=""))for i, split in enumerate(split_labels):@@ -78,21 +74,18 @@ def test_guitar_set_invalid_tracks(tmpdir):assert fp.read().strip() == str(i)-def test_create_input_data():+def test_create_input_data() -> None:data = create_input_data(train_percent=0.33, validation_percent=0.33)data.sort(key=lambda el: el[1]) # sort by splittolerance = 0.1for key, group in itertools.groupby(data, lambda el: el[1]):- assert (- (0.33 - tolerance) * len(data)- <= len(list(group))- <= (0.33 + tolerance) * len(data)- )- # print(f"total length: {len(data)}, label: {key}, len: {len(list(group))}")+ assert (0.33 - tolerance) * len(data) <= len(list(group)) <= (0.33 + tolerance) * len(data)-def test_create_input_data_overallocate():+def test_create_input_data_overallocate() -> None:try:create_input_data(train_percent=0.6, validation_percent=0.6)except AssertionError:- return True+ assert True+ else:+ assert Falsediff --git a/tests/data/test_pipeline.py b/tests/data/test_pipeline.pyindex 7a96750..fa918ee 100644--- a/tests/data/test_pipeline.py+++ b/tests/data/test_pipeline.py@@ -17,7 +17,6 @@import csvimport os-import unittestfrom itertools import islice@@ -25,7 +24,6 @@ import apache_beam as beamimport numpy as npimport tensorflow as tf-from apache_beam import Createfrom apache_beam.testing.test_pipeline import TestPipelinefrom basic_pitch.data.tf_example_serialization import bytes_feature, int64_feature@@ -51,20 +49,14 @@ def test_batch() -> None:def test_write_batch_to_tf_record(tmpdir: str) -> None:data = np.random.random([10, 10])- features = {- f"feat{i}": bytes_feature(tf.io.serialize_tensor(vec))- for i, vec in enumerate(list(data))- }+ features = {f"feat{i}": bytes_feature(tf.io.serialize_tensor(vec)) for i, vec in enumerate(list(data))}example = tf.train.Example(features=tf.train.Features(feature=features))writer = WriteBatchToTfRecord(tmpdir)writer.process([example])assert len(os.listdir(tmpdir)) == 1- dataset = tf.data.TFRecordDataset(- [os.path.join(tmpdir, f) for f in os.listdir(tmpdir)]- )+ dataset = tf.data.TFRecordDataset([os.path.join(tmpdir, f) for f in os.listdir(tmpdir)])- # assert dataset.cardinality() == 1for d in dataset:ex = tf.train.Example()ex.ParseFromString(d.numpy())@@ -73,20 +65,14 @@ def test_write_batch_to_tf_record(tmpdir: str) -> None:def test_transcription_dataset_writer(tmpdir: str) -> None:- input_data = [- (f"track{i}", ("train", "test", "validation")[i % 3]) for i in range(9)- ] + [("foo", "bar")]+ input_data = [(f"track{i}", ("train", "test", "validation")[i % 3]) for i in range(9)] + [("foo", "bar")]batch_size = 2with TestPipeline() as p:transcription_dataset_writer(p=p,input_data=input_data,- to_tf_example=lambda el: [- tf.train.Example(- features=tf.train.Features(feature={"foo": int64_feature(1)})- )- ],+ to_tf_example=lambda el: [tf.train.Example(features=tf.train.Features(feature={"foo": int64_feature(1)}))],filter_invalid_tracks=lambda el: [beam.pvalue.TaggedOutput(el[1], el[0])],destination=tmpdir,batch_size=batch_size,@@ -97,5 +83,4 @@ def test_transcription_dataset_writer(tmpdir: str) -> None:with open(os.path.join(tmpdir, split, "index.csv")) as fp:reader = csv.reader(fp, delimiter=",")for row in islice(reader, 1, None):- # print(row)assert int(row[0][-1]) % 3 == idiff --git a/tests/data/test_tf_example_serialization.py b/tests/data/test_tf_example_serialization.pyindex 61ccb3f..755742d 100644--- a/tests/data/test_tf_example_serialization.py+++ b/tests/data/test_tf_example_serialization.py@@ -15,8 +15,79 @@# See the License for the specific language governing permissions and# limitations under the License.+import pathlib+import sox+import tensorflow as tf++from basic_pitch.constants import AUDIO_N_CHANNELS, AUDIO_SAMPLE_RATEfrom basic_pitch.data.tf_example_serialization import to_transcription_tfexample+RESOURCES_PATH = pathlib.Path(__file__).parent.parent / "resources"+++def test_to_transcription_tfexample(tmpdir: str) -> None:+ file_id = "test"+ source = "maestro"+ tmpfile = str(pathlib.Path(tmpdir) / "test.wav")+ notes_indices = [(0, 1), (1, 2)]+ notes_values = [30.0, 31.0]+ onsets_indices = [1, 2]+ onsets_values = [0.1, 0.2]+ contours_indices = [1, 2]+ contours_values = [0.1, 0.2]+ notes_onsets_shape = (1, 1)+ contours_shape = (1, 1)-def test_to_transcription_tfexample(tmp_path: str) -> None:- pass+ tfm = sox.Transformer()+ tfm.rate(AUDIO_SAMPLE_RATE)+ tfm.channels(AUDIO_N_CHANNELS)+ tfm.build(str(RESOURCES_PATH / "vocadito_10.wav"), tmpfile)+ example = to_transcription_tfexample(+ file_id=file_id,+ source=source,+ audio_wav_file_path=tmpfile,+ notes_indices=notes_indices,+ notes_values=notes_values,+ onsets_indices=onsets_indices,+ onsets_values=onsets_values,+ contours_indices=contours_indices,+ contours_values=contours_values,+ notes_onsets_shape=notes_onsets_shape,+ contours_shape=contours_shape,+ )+ assert type(example) is tf.train.Example+ assert example.features.feature["file_id"].bytes_list.value[0].decode("utf-8") == file_id+ assert example.features.feature["source"].bytes_list.value[0].decode("utf-8") == source+ assert example.features.feature["audio_wav"].bytes_list.value[0] == open(tmpfile, "rb").read()+ assert tf.reduce_all(+ tf.io.parse_tensor(example.features.feature["notes_indices"].bytes_list.value[0], out_type=tf.int64)+ == notes_indices+ )+ assert tf.reduce_all(+ tf.io.parse_tensor(example.features.feature["notes_values"].bytes_list.value[0], out_type=tf.float32)+ == notes_values+ )+ assert tf.reduce_all(+ tf.io.parse_tensor(example.features.feature["onsets_indices"].bytes_list.value[0], out_type=tf.int64)+ == onsets_indices+ )+ assert tf.reduce_all(+ tf.io.parse_tensor(example.features.feature["onsets_values"].bytes_list.value[0], out_type=tf.float32)+ == onsets_values+ )+ assert tf.reduce_all(+ tf.io.parse_tensor(example.features.feature["contours_indices"].bytes_list.value[0], out_type=tf.int64)+ == contours_indices+ )+ assert tf.reduce_all(+ tf.io.parse_tensor(example.features.feature["contours_values"].bytes_list.value[0], out_type=tf.float32)+ == contours_values+ )+ assert tf.reduce_all(+ tf.io.parse_tensor(example.features.feature["notes_onsets_shape"].bytes_list.value[0], out_type=tf.int64)+ == notes_onsets_shape+ )+ assert tf.reduce_all(+ tf.io.parse_tensor(example.features.feature["contours_shape"].bytes_list.value[0], out_type=tf.int64)+ == contours_shape+ )diff --git a/tests/resources/data/guitarset/annotation/annotation.zip b/tests/resources/data/guitarset/annotation/annotation.zipnew file mode 100644index 0000000..4841626Binary files /dev/null and b/tests/resources/data/guitarset/annotation/annotation.zip differdiff --git a/tests/test_inference.py b/tests/test_inference.pyindex 8966585..4e099a8 100644--- a/tests/test_inference.py+++ b/tests/test_inference.py@@ -55,16 +55,17 @@ def test_predict() -> None:assert all(note_pitch_max)assert isinstance(note_events, list)- expected_model_output = np.load("tests/resources/vocadito_10/model_output.npz", allow_pickle=True)["arr_0"].item()+ expected_model_output = np.load(RESOURCES_PATH / "vocadito_10" / "model_output.npz", allow_pickle=True)[+ "arr_0"+ ].item()for k in expected_model_output.keys():- np.testing.assert_allclose(expected_model_output[k], model_output[k], atol=1e-4, rtol=0)-- expected_note_events = np.load("tests/resources/vocadito_10/note_events.npz", allow_pickle=True)["arr_0"]+ np.testing.assert_allclose(expected_model_output[k], model_output[k], atol=1e-4, rtol=0) # atol=1e-4, rtol=0+ expected_note_events = np.load(RESOURCES_PATH / "vocadito_10" / "note_events.npz", allow_pickle=True)["arr_0"]assert len(expected_note_events) == len(note_events)for expected, calculated in zip(expected_note_events, note_events):for i in range(len(expected)):- np.testing.assert_allclose(expected[i], calculated[i], atol=1e-4, rtol=0)+ np.testing.assert_allclose(expected[i], calculated[i], atol=1e-4, rtol=0) # atol=1e-4, rtol=0def test_predict_with_saves() -> None:diff --git a/tests/test_nn.py b/tests/test_nn.pyindex f9e0e18..7e2b1c0 100644--- a/tests/test_nn.py+++ b/tests/test_nn.py@@ -37,12 +37,10 @@ tfkl = tf.keras.layersclass TestHarmonicStacking(unittest.TestCase):def _audio_data_gen(self) -> Iterator[Tuple[np.array, np.array]]:while True:- audio = np.random.uniform(size=(BATCH_SIZE, AUDIO_N_SAMPLES, 1)).astype(+ audio = np.random.uniform(size=(BATCH_SIZE, AUDIO_N_SAMPLES, 1)).astype(np.float32)+ output = np.random.uniform(size=(BATCH_SIZE, ANNOT_N_FRAMES, ANNOTATIONS_N_SEMITONES * 3, 1)).astype(np.float32)- output = np.random.uniform(- size=(BATCH_SIZE, ANNOT_N_FRAMES, ANNOTATIONS_N_SEMITONES * 3, 1)- ).astype(np.float32)yield (audio, output)def _dummy_dataset(self) -> tf.data.Dataset:@@ -51,9 +49,7 @@ class TestHarmonicStacking(unittest.TestCase):(tf.float32, tf.float32),(tf.TensorShape([BATCH_SIZE, AUDIO_N_SAMPLES, 1]),- tf.TensorShape(- [BATCH_SIZE, ANNOT_N_FRAMES, ANNOTATIONS_N_SEMITONES * 3, 1]- ),+ tf.TensorShape([BATCH_SIZE, ANNOT_N_FRAMES, ANNOTATIONS_N_SEMITONES * 3, 1]),),)return dsdiff --git a/tox.ini b/tox.iniindex 41a7006..e094b0a 100644--- a/tox.ini+++ b/tox.ini@@ -31,10 +31,6 @@ setenv =deps = -e .[train]commands =pytest tests/data {posargs}-setenv =- SOURCE = {toxinidir}/basic_pitch- TEST_SOURCE = {toxinidir}/tests/data- LINE_LENGTH = "120"[testenv:manifest]deps = check-manifest