# /// script # requires-python = "<3.10" # dependencies = [ # "mir-eval", # "numpy", # "pandas", # "pretty-midi", # "setuptools", # ] # /// import mir_eval from pathlib import Path import pretty_midi as pm import numpy as np import pandas as pd import json pm.pretty_midi.MAX_TICK = 1e10 def ref_pitch_lambda(n): return n.pitch def est_pitch_lambda(n): return n.pitch def evaluate(midi_path='*.crepe_notes.mid', midi_replace_str='.crepe_notes.mid', midi_replace_with='.mid', output_label='crepe_notes', base_dir='./data/audio'): results = [] merge_tracks = True # gastro_test_scorehashes = ['Zpswc', 'rc1wc', 'bV1wc', 'XD1wc', '1M1wc', 'pM1wc', '5N1wc', 'bc1wc', 'Vpswc', 'hf1wc', 'vpswc', 'mf1wc', 'BSswc', 'y41wc', 'VD1wc', 'Jw1wc', 'Lf1wc', 'GD1wc', 'c41wc', 'gk1wc', 'Bc1wc', '-k1wc', 'sy1wc', '441wc', 'W41wc', 'xf1wc', 'Ny1wc', '3Sswc', '4w1wc', '1w1wc', 'cw1wc'] # hashes_to_skip = ['3HH2c', 'xFC2c'] # these were present in the FiloBass training data hashes_to_skip = ['jlj4c'] # this track didn't align properly paths = sorted(Path(base_dir).rglob(midi_path)) for path in paths: scorehash = path.stem.replace('-fine-aligned', '') if scorehash in hashes_to_skip: continue # print(f"{output_label}: {path}") est_path = str(path) ref_path = str(path).replace(midi_replace_str, midi_replace_with) if ref_path == est_path: continue try: ref = pm.PrettyMIDI(ref_path) except: # print(f"Couldn't load {ref_path}") continue try: est = pm.PrettyMIDI(est_path) except: # print(f"Couldn't load {est_path}") continue # delete instruments that aren't bass #for i in est.instruments: # if i.program != 33: # del(i) print(len(est.instruments)) ref_times = np.array([[n.start, n.end] for n in ref.instruments[0].notes]) ref_pitches = np.array( [pm.note_number_to_hz(ref_pitch_lambda(n)) for n in ref.instruments[0].notes]) if merge_tracks: est_times = np.array( [[n.start, n.end] for inst_notes in map(lambda i: i.notes, est.instruments) for n in inst_notes]) est_pitches = np.array([ pm.note_number_to_hz(est_pitch_lambda(n)) for inst_notes in map(lambda i: i.notes, est.instruments) for n in inst_notes ]) else: est_times = np.array([[n.start, n.end] for n in est.instruments[0].notes]) est_pitches = np.array([ pm.note_number_to_hz(est_pitch_lambda(n)) for n in est.instruments[0].notes ]) # downbeats_json = json.load(open(Path("../syncpoints") / Path(ref_path).name.replace('-transcribed.mid', '-syncpoints.json'))) # downbeats = [x[1] for x in downbeats_json if len(x) == 2 or x[2] == 0] # first_ref_note = downbeats[2] # last_ref_note = downbeats[-2] first_ref_note = np.min(ref_times) last_ref_note = np.max(ref_times) ref_times_valid_idxs = np.unique(np.where((ref_times > first_ref_note) * (ref_times < last_ref_note))[0]) ref_times = ref_times[ref_times_valid_idxs] ref_pitches = ref_pitches[ref_times_valid_idxs] est_times_valid_idxs = np.unique( np.where((est_times > first_ref_note) & (est_times < last_ref_note))[0]) est_times = est_times[est_times_valid_idxs] est_pitches = est_pitches[est_times_valid_idxs] eval_result = mir_eval.transcription.evaluate(ref_times, ref_pitches, est_times, est_pitches, onset_tolerance=0.05) # print(str(path)) # print(eval_result['F-measure_no_offset']) # breakpoint() eval_result['file'] = str(path) results.append(eval_result) df = pd.DataFrame(results) df.to_pickle(f'{output_label}_eval.pkl') pd.set_option('display.max_colwidth', None) print(df.describe()[['Precision_no_offset', 'Recall_no_offset', 'F-measure_no_offset', 'Average_Overlap_Ratio_no_offset']]) print(df['F-measure_no_offset'].to_list()) # evaluate('Sax.mt3.mid', '.mt3.mid', 'mt3') # evaluate('Sax.crepe_notes_amp_trimming.mid', '.crepe_notes_amp_trimming.mid', 'crepe_notes_amp_trimming') # evaluate('Sax_vamp_pyin_pyin_notes.mid', '_vamp_pyin_pyin_notes.mid', 'pyin_notes') # evaluate('Sax.crepe_notes_with_onsets.mid', '.crepe_notes_with_onsets.mid', 'crepe_notes_with_onsets') # evaluate('*_basic_pitch.mid', '_basic_pitch.mid', 'basic_pitch') # evaluate('*.mt3.mid', '.mt3.mid', 'mt3') # evaluate('*.cn_25ms_min.mid', '.cn_25ms_min.mid', 'crepe_notes-min-dur-25ms') # evaluate('*.crepe_notes-min-dur-11ms.mid', '.crepe_notes-min-dur-11ms.mid', 'crepe_notes-min-dur-11ms') # evaluate('*.crepe_notes-min-dur-11ms-no-tuning.mid', # evaluate('*.crepe_notes-min-dur-25ms-no-tuning.mid', # '.crepe_notes-min-dur-25ms-no-tuning.mid', # 'crepe_notes-min-dur-25ms-no-tuning') # evaluate('*_vamp_pyin_pyin_notes.mid', '_vamp_pyin_pyin_notes.mid', 'pyin_notes') # evaluate('*.cn_transition_a.mid', '.cn_transition_a.mid', 'crepe_notes-transitions-a') # evaluate('bass.transcription.mid', '.transcription.mid', '-fine-aligned.mid', 'filobass_cn_sens_2_min_50ms') # evaluate('md-bass.mid', 'md-bass.mid', 'bass-fine-aligned.mid', 'melodyne_melodic') # evaluate('bass_basic_pitch.mid', '_basic_pitch.mid', '-fine-aligned.mid', 'filobass_bp') # evaluate('bass_basic_pitch.mid', '_basic_pitch.mid', 'filobass_cn') # evaluate('*-fine-aligned.mid', '-fine-aligned.mid', '-transcribed.mid', 'gastro') # evaluate('*-fine-aligned.mid', '-fine-aligned.mid', '-transcribed.mid', 'BassBooks') import tempfile import shutil with tempfile.TemporaryDirectory() as tmpdirname: for f in Path("./ground_truth_midi").rglob("*.mid"): shutil.copy(f, tmpdirname) for f in Path("./model_output_ymt3").rglob("*.mid"): shutil.copy(f, tmpdirname) evaluate('*.mid', '.mp3.mid', '.mid', 'YMT3', base_dir=tmpdirname) with tempfile.TemporaryDirectory() as tmpdirname: for f in Path("./ground_truth_midi").rglob("*.mid"): shutil.copy(f, tmpdirname) for f in Path("./model_output_ymt3_structured").rglob("*.mid"): shutil.copy(f, tmpdirname) evaluate('*.mid', '.mp3.mid', '.mid', 'YMT3_Structured', base_dir=tmpdirname)