def test_with_mel_lstm(self): """ Test that a very short training runs without exceptions """ model = mel_lstm.get_model() train_generator = RandomMidiDataGenerator( real_midis[::2], mel_lstm.preprocess, mapper, 2, 100 ) test_generator = RandomMidiDataGenerator( real_midis[1::2], mel_lstm.preprocess, mapper, 2, 100 ) self.train(model, train_generator, test_generator) plt.title("Mel LSTM, precomputed - Accuracy") plt.savefig("test_mel_lstm_training_precomputed.png")
def test_with_seq_lstm(self): """ Test that a very short training runs without exceptions """ model = seq_lstm.get_model(mapper.dims) train_generator = RandomMidiDataGenerator( real_midis[::2], seq_lstm.make_preprocessor(mapper), mapper, 2, 100 ) test_generator = RandomMidiDataGenerator( real_midis[1::2], seq_lstm.make_preprocessor(mapper), mapper, 2, 100 ) self.train(model, train_generator, test_generator) plt.title("Seq LSTM, on generator - Accuracy") plt.savefig("test_seq_lstm_training_generator.png")
def test_with_mel_lstm(self): generator = RandomMidiDataGenerator(real_midis, mel_lstm.preprocess, mapper, 2) got_a_batch = False for x, y in generator(): got_a_batch = True self.assertFalse(np.any(np.isnan(x))) self.assertFalse(np.any(np.isnan(y))) if generator.epochs >= 1: break self.assertTrue(got_a_batch)
import pretty_midi from glob import glob from tensorflow.keras.models import Sequential from tensorflow.keras.layers import LSTM, Dense from tensorflow.keras.layers import TimeDistributed from musicrl.midi2vec import MidiVectorMapper from musicrl.render import * from musicrl.random_generator import generate_random_midi, resemble_midi from musicrl.data import RandomMidiDataGenerator from musicrl import mel_lstm filepaths = list(glob("maestro-v2.0.0/2008/**.midi")) real_midis = [pretty_midi.PrettyMIDI(i) for i in filepaths] mapper = MidiVectorMapper(real_midis) model = mel_lstm.get_model() train_generator = RandomMidiDataGenerator(real_midis[::2], mel_lstm.preprocess, mapper, 10, 8000) test_generator = RandomMidiDataGenerator(real_midis[1::2], mel_lstm.preprocess, mapper, 10, 1000) test_data = test_generator.compute_batch() x_train, y_train = train_generator.compute_batch() history = model.fit(x_train, y_train, 10, validation_data=test_data, epochs=30) mel_lstm.plot_history(history) plt.savefig("test_mel_lstm_training.png")
from musicrl.midi2vec import MidiVectorMapper from musicrl.render import * from musicrl.random_generator import generate_random_midi, resemble_midi from musicrl.data import RandomMidiDataGenerator from musicrl import mel_lstm filepaths = list(glob("maestro-v2.0.0/2008/**.midi")) real_midis = [pretty_midi.PrettyMIDI(i) for i in filepaths] mapper = MidiVectorMapper(real_midis) model = mel_lstm.get_model() train_generator = RandomMidiDataGenerator( real_midis[:50], mel_lstm.preprocess, mapper, 4 ) test_generator = RandomMidiDataGenerator( real_midis[50:60], mel_lstm.preprocess, mapper, 10, max_num_timeframes=1000 ) history = model.fit_generator( train_generator(), validation_data=test_generator(), steps_per_epoch=train_generator.steps_per_epoch, epochs=2, validation_steps=test_generator.steps_per_epoch, validation_freq=1, verbose=1, ) mel_lstm.plot_history(history)