{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "from matplotlib import pyplot as plt\n", "\n", "from anticipation.config import *\n", "from anticipation.vocab import *\n", "\n", "import alignment as am\n", "\n", "from anticipation.sample import generate, generate2\n", "from anticipation.convert import midi_to_events, midi_to_compound, events_to_midi\n", "from train.midi_preprocess import convert_midi\n", "\n", "from anticipation.tokenize import tokenize2, maybe_tokenize, tokenize3\n", "\n", "import anticipation.ops as ops\n", "\n", "from tqdm import tqdm" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv('asap-dataset-master/metadata.csv')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
composertitlefolderxml_scoremidi_scoremidi_performanceperformance_annotationsmidi_score_annotationsmaestro_midi_performancemaestro_audio_performancestartendaudio_performance
0BachFugue_bwv_846Bach/Fugue/bwv_846Bach/Fugue/bwv_846/xml_score.musicxmlBach/Fugue/bwv_846/midi_score.midBach/Fugue/bwv_846/Shi05M.midBach/Fugue/bwv_846/Shi05M_annotations.txtBach/Fugue/bwv_846/midi_score_annotations.txt{maestro}/2006/MIDI-Unprocessed_19_R1_2006_01-...{maestro}/2006/MIDI-Unprocessed_19_R1_2006_01-...144.567708NaNBach/Fugue/bwv_846/Shi05M.wav
1BachFugue_bwv_848Bach/Fugue/bwv_848Bach/Fugue/bwv_848/xml_score.musicxmlBach/Fugue/bwv_848/midi_score.midBach/Fugue/bwv_848/Denisova06M.midBach/Fugue/bwv_848/Denisova06M_annotations.txtBach/Fugue/bwv_848/midi_score_annotations.txt{maestro}/2008/MIDI-Unprocessed_02_R1_2008_01-...{maestro}/2008/MIDI-Unprocessed_02_R1_2008_01-...69.317708NaNBach/Fugue/bwv_848/Denisova06M.wav
2BachFugue_bwv_848Bach/Fugue/bwv_848Bach/Fugue/bwv_848/xml_score.musicxmlBach/Fugue/bwv_848/midi_score.midBach/Fugue/bwv_848/Lee01M.midBach/Fugue/bwv_848/Lee01M_annotations.txtBach/Fugue/bwv_848/midi_score_annotations.txt{maestro}/2011/MIDI-Unprocessed_08_R1_2011_MID...{maestro}/2011/MIDI-Unprocessed_08_R1_2011_MID...74.795573NaNBach/Fugue/bwv_848/Lee01M.wav
3BachFugue_bwv_848Bach/Fugue/bwv_848Bach/Fugue/bwv_848/xml_score.musicxmlBach/Fugue/bwv_848/midi_score.midBach/Fugue/bwv_848/LeeSH01M.midBach/Fugue/bwv_848/LeeSH01M_annotations.txtBach/Fugue/bwv_848/midi_score_annotations.txt{maestro}/2017/MIDI-Unprocessed_049_PIANO049_M...{maestro}/2017/MIDI-Unprocessed_049_PIANO049_M...83.644792NaNBach/Fugue/bwv_848/LeeSH01M.wav
4BachFugue_bwv_848Bach/Fugue/bwv_848Bach/Fugue/bwv_848/xml_score.musicxmlBach/Fugue/bwv_848/midi_score.midBach/Fugue/bwv_848/Lin04M.midBach/Fugue/bwv_848/Lin04M_annotations.txtBach/Fugue/bwv_848/midi_score_annotations.txt{maestro}/2008/MIDI-Unprocessed_10_R1_2008_01-...{maestro}/2008/MIDI-Unprocessed_10_R1_2008_01-...75.373698NaNBach/Fugue/bwv_848/Lin04M.wav
\n", "
" ], "text/plain": [ " composer title folder \\\n", "0 Bach Fugue_bwv_846 Bach/Fugue/bwv_846 \n", "1 Bach Fugue_bwv_848 Bach/Fugue/bwv_848 \n", "2 Bach Fugue_bwv_848 Bach/Fugue/bwv_848 \n", "3 Bach Fugue_bwv_848 Bach/Fugue/bwv_848 \n", "4 Bach Fugue_bwv_848 Bach/Fugue/bwv_848 \n", "\n", " xml_score midi_score \\\n", "0 Bach/Fugue/bwv_846/xml_score.musicxml Bach/Fugue/bwv_846/midi_score.mid \n", "1 Bach/Fugue/bwv_848/xml_score.musicxml Bach/Fugue/bwv_848/midi_score.mid \n", "2 Bach/Fugue/bwv_848/xml_score.musicxml Bach/Fugue/bwv_848/midi_score.mid \n", "3 Bach/Fugue/bwv_848/xml_score.musicxml Bach/Fugue/bwv_848/midi_score.mid \n", "4 Bach/Fugue/bwv_848/xml_score.musicxml Bach/Fugue/bwv_848/midi_score.mid \n", "\n", " midi_performance \\\n", "0 Bach/Fugue/bwv_846/Shi05M.mid \n", "1 Bach/Fugue/bwv_848/Denisova06M.mid \n", "2 Bach/Fugue/bwv_848/Lee01M.mid \n", "3 Bach/Fugue/bwv_848/LeeSH01M.mid \n", "4 Bach/Fugue/bwv_848/Lin04M.mid \n", "\n", " performance_annotations \\\n", "0 Bach/Fugue/bwv_846/Shi05M_annotations.txt \n", "1 Bach/Fugue/bwv_848/Denisova06M_annotations.txt \n", "2 Bach/Fugue/bwv_848/Lee01M_annotations.txt \n", "3 Bach/Fugue/bwv_848/LeeSH01M_annotations.txt \n", "4 Bach/Fugue/bwv_848/Lin04M_annotations.txt \n", "\n", " midi_score_annotations \\\n", "0 Bach/Fugue/bwv_846/midi_score_annotations.txt \n", "1 Bach/Fugue/bwv_848/midi_score_annotations.txt \n", "2 Bach/Fugue/bwv_848/midi_score_annotations.txt \n", "3 Bach/Fugue/bwv_848/midi_score_annotations.txt \n", "4 Bach/Fugue/bwv_848/midi_score_annotations.txt \n", "\n", " maestro_midi_performance \\\n", "0 {maestro}/2006/MIDI-Unprocessed_19_R1_2006_01-... \n", "1 {maestro}/2008/MIDI-Unprocessed_02_R1_2008_01-... \n", "2 {maestro}/2011/MIDI-Unprocessed_08_R1_2011_MID... \n", "3 {maestro}/2017/MIDI-Unprocessed_049_PIANO049_M... \n", "4 {maestro}/2008/MIDI-Unprocessed_10_R1_2008_01-... \n", "\n", " maestro_audio_performance start end \\\n", "0 {maestro}/2006/MIDI-Unprocessed_19_R1_2006_01-... 144.567708 NaN \n", "1 {maestro}/2008/MIDI-Unprocessed_02_R1_2008_01-... 69.317708 NaN \n", "2 {maestro}/2011/MIDI-Unprocessed_08_R1_2011_MID... 74.795573 NaN \n", "3 {maestro}/2017/MIDI-Unprocessed_049_PIANO049_M... 83.644792 NaN \n", "4 {maestro}/2008/MIDI-Unprocessed_10_R1_2008_01-... 75.373698 NaN \n", "\n", " audio_performance \n", "0 Bach/Fugue/bwv_846/Shi05M.wav \n", "1 Bach/Fugue/bwv_848/Denisova06M.wav \n", "2 Bach/Fugue/bwv_848/Lee01M.wav \n", "3 Bach/Fugue/bwv_848/LeeSH01M.wav \n", "4 Bach/Fugue/bwv_848/Lin04M.wav " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "BASE = \"./asap-dataset-master/\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "with open('./data/output.txt', 'r') as f:\n", " lines = f.read().split('\\n')" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████| 19388/19388 [00:03<00:00, 5029.93it/s]\n" ] } ], "source": [ "EVENTS=[]\n", "CONTROLS=[]\n", "\n", "for line in tqdm(lines):\n", " seq = [int(i) for i in line.split()]\n", "\n", " events, controls = ops.split(seq)\n", " EVENTS.extend(events)\n", " CONTROLS.extend(controls)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10124397 9708504 19832901\n" ] } ], "source": [ "print(len(EVENTS), len(CONTROLS), len(EVENTS)+len(CONTROLS))" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "21337" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(set(EVENTS).union(set(CONTROLS)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Loading in the model trained from Colab:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from transformers import GPT2Config, GPT2LMHeadModel" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "model = GPT2LMHeadModel.from_pretrained(\"./results/3-5 model/model-final\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Trying to perform inference on an unseen MIDI file... without the map" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "controls = midi_to_events('./examples/Bach prelude.MID')" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "events = midi_to_events('./asap-dataset-master/Bach/Prelude/bwv_846/midi_score.mid')\n", "history = ops.clip(events, 0, 5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " 94%|█████████▍| 5657/6000 [00:42<00:02, 133.45it/s]\n" ] } ], "source": [ "score = generate(model,start_time=0,end_time=60,inputs=history,controls=controls)\n", "mid = events_to_midi(score)\n", "mid.save('./examples/Bach prelude generated3.mid')" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " 0%| | 0/6000 [00:03