def main(note_embedding_dim, metadata_embedding_dim, num_encoder_layers, encoder_hidden_size, encoder_dropout_prob, latent_space_dim, num_decoder_layers, decoder_hidden_size, decoder_dropout_prob, has_metadata, batch_size, num_epochs, train, plot, log, lr): dataset_manager = DatasetManager() metadatas = [ BeatMarkerMetadata(subdivision=6), TickMetadata(subdivision=6) ] mvae_train_kwargs = { 'metadatas': metadatas, 'sequences_size': 32, 'num_bars': 16, 'train': True } mvae_test_kwargs = { 'metadatas': metadatas, 'sequences_size': 32, 'num_bars': 16, 'train': False } folk_dataset: FolkDataset = dataset_manager.get_dataset( name='folk_4by4nbars_train', **mvae_train_kwargs) folk_dataset_test: FolkDataset = dataset_manager.get_dataset( name='folk_4by4nbars_train', **mvae_test_kwargs) model = MeasureVAE(dataset=folk_dataset, note_embedding_dim=note_embedding_dim, metadata_embedding_dim=metadata_embedding_dim, num_encoder_layers=num_encoder_layers, encoder_hidden_size=encoder_hidden_size, encoder_dropout_prob=encoder_dropout_prob, latent_space_dim=latent_space_dim, num_decoder_layers=num_decoder_layers, decoder_hidden_size=decoder_hidden_size, decoder_dropout_prob=decoder_dropout_prob, has_metadata=has_metadata) if train: if torch.cuda.is_available(): model.cuda() trainer = VAETrainer(dataset=folk_dataset, model=model, lr=lr) trainer.train_model(batch_size=batch_size, num_epochs=num_epochs, plot=plot, log=log) else: model.load() model.cuda() model.eval() tester = VAETester(dataset=folk_dataset_test, model=model) tester.test_model()
def build_folk(dataset_manager, batch_size, subdivision, sequences_size): metadatas = [ BeatMarkerMetadata(subdivision=subdivision), TickMetadata(subdivision=subdivision) ] folk_dataset_kwargs = { 'metadatas': metadatas, 'sequences_size': sequences_size } folk_dataset: FolkDataset = dataset_manager.get_dataset( name='folk_4by4nbars', **folk_dataset_kwargs ) (train_dataloader, val_dataloader, test_dataloader) = folk_dataset.data_loaders( batch_size=batch_size, split=(0.7, 0.2) ) print('Num Train Batches: ', len(train_dataloader)) print('Num Valid Batches: ', len(val_dataloader)) print('Num Test Batches: ', len(test_dataloader))
def main(note_embedding_dim, metadata_embedding_dim, num_encoder_layers, encoder_hidden_size, encoder_dropout_prob, latent_space_dim, num_decoder_layers, decoder_hidden_size, decoder_dropout_prob, has_metadata, num_latent_rnn_layers, latent_rnn_hidden_size, latent_rnn_dropout_prob, num_layers, lstm_hidden_size, dropout_lstm, input_dropout, linear_hidden_size, batch_size, num_target, num_models): # init dataset dataset_manager = DatasetManager() metadatas = [ BeatMarkerMetadata(subdivision=6), TickMetadata(subdivision=6) ] mvae_train_kwargs = { 'metadatas': metadatas, 'sequences_size': 32, 'num_bars': 16, 'train': True } folk_dataset_vae: FolkDataset = dataset_manager.get_dataset( name='folk_4by4nbars_train', **mvae_train_kwargs) # init vae model vae_model = MeasureVAE(dataset=folk_dataset_vae, note_embedding_dim=note_embedding_dim, metadata_embedding_dim=metadata_embedding_dim, num_encoder_layers=num_encoder_layers, encoder_hidden_size=encoder_hidden_size, encoder_dropout_prob=encoder_dropout_prob, latent_space_dim=latent_space_dim, num_decoder_layers=num_decoder_layers, decoder_hidden_size=decoder_hidden_size, decoder_dropout_prob=decoder_dropout_prob, has_metadata=has_metadata) vae_model.load() # VAE model must be pre-trained if torch.cuda.is_available(): vae_model.cuda() folk_train_kwargs = { 'metadatas': metadatas, 'sequences_size': 32, 'num_bars': 16, 'train': True } folk_test_kwargs = { 'metadatas': metadatas, 'sequences_size': 32, 'num_bars': 16, 'train': False } folk_dataset_train: FolkDataset = dataset_manager.get_dataset( name='folk_4by4nbars_train', **folk_train_kwargs) folk_dataset_test: FolkDataset = dataset_manager.get_dataset( name='folk_4by4nbars_train', **folk_test_kwargs) # init latent_rnn model and latent_rnn_tester latent_rnn_model = LatentRNN(dataset=folk_dataset_train, vae_model=vae_model, num_rnn_layers=num_latent_rnn_layers, rnn_hidden_size=latent_rnn_hidden_size, dropout=latent_rnn_dropout_prob, rnn_class=torch.nn.GRU, auto_reg=False, teacher_forcing=True) latent_rnn_model.load() # latent_rnn model must be pre-trained if torch.cuda.is_available(): latent_rnn_model.cuda() latent_rnn_tester = LatentRNNTester(dataset=folk_dataset_test, model=latent_rnn_model) # inti arnn model and arnn_testes arnn_model = ConstraintModelGaussianReg( dataset=folk_dataset_train, note_embedding_dim=note_embedding_dim, metadata_embedding_dim=metadata_embedding_dim, num_layers=num_layers, num_lstm_constraints_units=lstm_hidden_size, num_lstm_generation_units=lstm_hidden_size, linear_hidden_size=linear_hidden_size, dropout_prob=dropout_lstm, dropout_input_prob=input_dropout, unary_constraint=True, teacher_forcing=True) arnn_model.load() # ARNN model must be pre-trained if torch.cuda.is_available(): arnn_model.cuda() arnn_tester = AnticipationRNNTester(dataset=folk_dataset_test, model=arnn_model) arnn_baseline_model = AnticipationRNNBaseline( dataset=folk_dataset_train, note_embedding_dim=note_embedding_dim, metadata_embedding_dim=metadata_embedding_dim, num_layers=num_layers, num_lstm_constraints_units=lstm_hidden_size, num_lstm_generation_units=lstm_hidden_size, linear_hidden_size=linear_hidden_size, dropout_prob=dropout_lstm, dropout_input_prob=input_dropout, unary_constraint=True, teacher_forcing=True) arnn_baseline_model.load() # ARNN model must be pre-trained if torch.cuda.is_available(): arnn_baseline_model.cuda() arnn_baseline_tester = AnticipationRNNTester(dataset=folk_dataset_test, model=arnn_baseline_model) # create test dataloader (_, _, test_dataloader) = folk_dataset_test.data_loaders(batch_size=batch_size, split=(0.01, 0.01)) # test print('Num Test Batches: ', len(test_dataloader)) latent_rnn_mean_loss, latent_rnn_mean_accuracy, \ arnn_mean_loss, arnn_mean_accuracy, \ arnn_baseline_mean_loss, arnn_baseline_mean_accuracy = loss_and_acc_test( data_loader=test_dataloader, latent_rnn_tester=latent_rnn_tester, arnn_tester=arnn_tester, arnn_baseline_tester=arnn_baseline_tester, num_target_measures=num_target, num_models=num_models ) print('Test Epoch:') print('latent_rnn Test Loss: ', latent_rnn_mean_loss, '\n' 'latent_rnn Test Accuracy: ', latent_rnn_mean_accuracy * 100, '\n' 'ARNN Test Loss: ', arnn_mean_loss, '\n' 'ARNN Test Accuracy: ', arnn_mean_accuracy * 100, '\n' 'ARNN Baseline Test Loss: ', arnn_baseline_mean_loss, '\n' 'ARNN Baseline Test Accuracy: ', arnn_baseline_mean_accuracy * 100, '\n')
def main(note_embedding_dim, metadata_embedding_dim, num_encoder_layers, encoder_hidden_size, encoder_dropout_prob, latent_space_dim, num_decoder_layers, decoder_hidden_size, decoder_dropout_prob, has_metadata, num_latent_rnn_layers, latent_rnn_hidden_size, latent_rnn_dropout_prob, num_layers, lstm_hidden_size, dropout_lstm, input_dropout, linear_hidden_size, batch_size, num_target, num_models): random.seed(0) # init dataset dataset_manager = DatasetManager() metadatas = [ BeatMarkerMetadata(subdivision=6), TickMetadata(subdivision=6) ] mvae_train_kwargs = { 'metadatas': metadatas, 'sequences_size': 32, 'num_bars': 16, 'train': True } folk_dataset_vae: FolkDataset = dataset_manager.get_dataset( name='folk_4by4nbars_train', **mvae_train_kwargs) # init vae model vae_model = MeasureVAE(dataset=folk_dataset_vae, note_embedding_dim=note_embedding_dim, metadata_embedding_dim=metadata_embedding_dim, num_encoder_layers=num_encoder_layers, encoder_hidden_size=encoder_hidden_size, encoder_dropout_prob=encoder_dropout_prob, latent_space_dim=latent_space_dim, num_decoder_layers=num_decoder_layers, decoder_hidden_size=decoder_hidden_size, decoder_dropout_prob=decoder_dropout_prob, has_metadata=has_metadata) vae_model.load() # VAE model must be pre-trained if torch.cuda.is_available(): vae_model.cuda() folk_train_kwargs = { 'metadatas': metadatas, 'sequences_size': 32, 'num_bars': 16, 'train': True } folk_test_kwargs = { 'metadatas': metadatas, 'sequences_size': 32, 'num_bars': 16, 'train': False } folk_dataset_train: FolkDataset = dataset_manager.get_dataset( name='folk_4by4nbars_train', **folk_train_kwargs) folk_dataset_test: FolkDataset = dataset_manager.get_dataset( name='folk_4by4nbars_train', **folk_test_kwargs) # Initialize stuff test_filenames = folk_dataset_test.dataset_filenames num_melodies = 32 num_measures = 16 req_length = num_measures * 4 * 6 num_past = 6 num_future = 6 num_target = 4 cur_dir = os.path.dirname(os.path.realpath(__file__)) save_folder = 'saved_midi/' # First save original data for i in tqdm(range(num_melodies)): f = test_filenames[i] f_id = f[:-4] # save original scores save_filename = os.path.join(cur_dir, save_folder + f_id + '_original.mid') if os.path.isfile(save_filename): continue f = os.path.join(folk_dataset_test.corpus_it_gen.raw_dataset_dir, f) score = folk_dataset_test.corpus_it_gen.get_score_from_path( f, fix_and_expand=True) score_tensor = folk_dataset_test.get_score_tensor(score) metadata_tensor = folk_dataset_test.get_metadata_tensor(score) # ignore scores with less than 16 measures if score_tensor.size(1) < req_length: continue score_tensor = score_tensor[:, :req_length] metadata_tensor = metadata_tensor[:, :req_length, :] trunc_score = folk_dataset_test.tensor_to_score(score_tensor) trunc_score.write('midi', fp=save_filename) # Initialize models and testers latent_rnn_model = LatentRNN(dataset=folk_dataset_train, vae_model=vae_model, num_rnn_layers=num_latent_rnn_layers, rnn_hidden_size=latent_rnn_hidden_size, dropout=latent_rnn_dropout_prob, rnn_class=torch.nn.GRU, auto_reg=False, teacher_forcing=True) latent_rnn_model.load() # Latent RNN model must be pre-trained if torch.cuda.is_available(): latent_rnn_model.cuda() latent_rnn_tester = LatentRNNTester(dataset=folk_dataset_test, model=latent_rnn_model) def process_latent_rnn_batch(score_tensor, num_past=6, num_future=6, num_target=4): assert (num_past + num_future + num_target == 16) score_tensor = score_tensor.unsqueeze(0) score_tensor = LatentRNNTrainer.split_to_measures(score_tensor, 24) tensor_past, tensor_future, tensor_target = LatentRNNTrainer.split_score( score_tensor=score_tensor, num_past=num_past, num_future=num_future, num_target=num_target, measure_seq_len=24) return tensor_past, tensor_future, tensor_target # Second save latent_rnn generations for i in tqdm(range(num_melodies)): f = test_filenames[i] f_id = f[:-4] save_filename = os.path.join(cur_dir, save_folder + f_id + '_latent_rnn.mid') if os.path.isfile(save_filename): continue f = os.path.join(folk_dataset_test.corpus_it_gen.raw_dataset_dir, f) score = folk_dataset_test.corpus_it_gen.get_score_from_path( f, fix_and_expand=True) score_tensor = folk_dataset_test.get_score_tensor(score) # metadata_tensor = folk_dataset_test.get_metadata_tensor(score) # ignore scores with less than 16 measures if score_tensor.size(1) < req_length: continue score_tensor = score_tensor[:, :req_length] # metadata_tensor = metadata_tensor[:, :req_length, :] # save regeneration using latent_rnn tensor_past, tensor_future, tensor_target = process_latent_rnn_batch( score_tensor, num_past, num_future, num_target) # forward pass through latent_rnn weights, gen_target, _ = latent_rnn_tester.model( past_context=tensor_past, future_context=tensor_future, target=tensor_target, measures_to_generate=num_target, train=False, ) # convert to score batch_size, _, _ = gen_target.size() gen_target = gen_target.view(batch_size, num_target, 24) gen_score_tensor = torch.cat((tensor_past, gen_target, tensor_future), 1) latent_rnn_score = folk_dataset_test.tensor_to_score( gen_score_tensor.cpu()) latent_rnn_score.write('midi', fp=save_filename) # Intialize arnn model and arnn_tester arnn_model = ConstraintModelGaussianReg( dataset=folk_dataset_train, note_embedding_dim=note_embedding_dim, metadata_embedding_dim=metadata_embedding_dim, num_layers=num_layers, num_lstm_constraints_units=lstm_hidden_size, num_lstm_generation_units=lstm_hidden_size, linear_hidden_size=linear_hidden_size, dropout_prob=dropout_lstm, dropout_input_prob=input_dropout, unary_constraint=True, teacher_forcing=True) arnn_model.load() # ARNN model must be pre-trained if torch.cuda.is_available(): arnn_model.cuda() arnn_tester = AnticipationRNNTester(dataset=folk_dataset_test, model=arnn_model) def process_arnn_batch(score_tensor, metadata_tensor, arnn_tester, num_past=6, num_target=4): score_tensor = score_tensor.unsqueeze(0) metadata_tensor = metadata_tensor.unsqueeze(0) tensor_score = to_cuda_variable_long(score_tensor) tensor_metadata = to_cuda_variable_long(metadata_tensor) constraints_location, start_tick, end_tick = arnn_tester.get_constraints_location( tensor_score, is_stochastic=False, start_measure=num_past, num_measures=num_target) arnn_batch = (tensor_score, tensor_metadata, constraints_location, start_tick, end_tick) return arnn_batch # Third save ARNN-Reg generations for i in tqdm(range(num_melodies)): f = test_filenames[i] f_id = f[:-4] save_filename = os.path.join(cur_dir, save_folder + f_id + '_arnn_reg.mid') if os.path.isfile(save_filename): continue f = os.path.join(folk_dataset_test.corpus_it_gen.raw_dataset_dir, f) score = folk_dataset_test.corpus_it_gen.get_score_from_path( f, fix_and_expand=True) score_tensor = folk_dataset_test.get_score_tensor(score) metadata_tensor = folk_dataset_test.get_metadata_tensor(score) # ignore scores with less than 16 measures if score_tensor.size(1) < req_length: continue score_tensor = score_tensor[:, :req_length] metadata_tensor = metadata_tensor[:, :req_length, :] # save regeneration using latent_rnn tensor_score, tensor_metadata, constraints_location, start_tick, end_tick = \ process_arnn_batch(score_tensor, metadata_tensor, arnn_tester, num_past, num_target) # forward pass through latent_rnn _, gen_target = arnn_tester.model.forward_inpaint( score_tensor=tensor_score, metadata_tensor=tensor_metadata, constraints_loc=constraints_location, start_tick=start_tick, end_tick=end_tick, ) # convert to score arnn_score = folk_dataset_test.tensor_to_score(gen_target.cpu()) arnn_score.write('midi', fp=save_filename) # Intialize arnn-baseline model and arnn_tester arnn_baseline_model = AnticipationRNNBaseline( dataset=folk_dataset_train, note_embedding_dim=note_embedding_dim, metadata_embedding_dim=metadata_embedding_dim, num_layers=num_layers, num_lstm_constraints_units=lstm_hidden_size, num_lstm_generation_units=lstm_hidden_size, linear_hidden_size=linear_hidden_size, dropout_prob=dropout_lstm, dropout_input_prob=input_dropout, unary_constraint=True, teacher_forcing=True) arnn_baseline_model.load() # ARNN model must be pre-trained if torch.cuda.is_available(): arnn_baseline_model.cuda() arnn_baseline_tester = AnticipationRNNTester(dataset=folk_dataset_test, model=arnn_baseline_model) # Fourth save ARNN-Baseline generations for i in tqdm(range(num_melodies)): f = test_filenames[i] f_id = f[:-4] save_filename = os.path.join(cur_dir, save_folder + f_id + '_arnn_baseline.mid') if os.path.isfile(save_filename): continue f = os.path.join(folk_dataset_test.corpus_it_gen.raw_dataset_dir, f) score = folk_dataset_test.corpus_it_gen.get_score_from_path( f, fix_and_expand=True) score_tensor = folk_dataset_test.get_score_tensor(score) metadata_tensor = folk_dataset_test.get_metadata_tensor(score) # ignore scores with less than 16 measures if score_tensor.size(1) < req_length: continue score_tensor = score_tensor[:, :req_length] metadata_tensor = metadata_tensor[:, :req_length, :] # save regeneration using latent_rnn tensor_score, tensor_metadata, constraints_location, start_tick, end_tick = \ process_arnn_batch(score_tensor, metadata_tensor, arnn_baseline_tester, num_past, num_target) # forward pass through latent_rnn _, gen_target = arnn_baseline_tester.model.forward_inpaint( score_tensor=tensor_score, metadata_tensor=tensor_metadata, constraints_loc=constraints_location, start_tick=start_tick, end_tick=end_tick, ) # convert to score arnn_baseline_score = folk_dataset_test.tensor_to_score( gen_target.cpu()) arnn_baseline_score.write('midi', fp=save_filename)
def main(note_embedding_dim, metadata_embedding_dim, num_encoder_layers, encoder_hidden_size, encoder_dropout_prob, latent_space_dim, num_decoder_layers, decoder_hidden_size, decoder_dropout_prob, has_metadata, num_latent_rnn_layers, latent_rnn_hidden_size, latent_rnn_dropout_prob, batch_size, num_epochs, train, lr, plot, log, auto_reg, teacher_forcing, early_stop ): # init dataset dataset_manager = DatasetManager() metadatas = [ BeatMarkerMetadata(subdivision=6), TickMetadata(subdivision=6) ] mvae_train_kwargs = { 'metadatas': metadatas, 'sequences_size': 32, 'num_bars': 16, 'train': True } folk_dataset_vae: FolkDataset = dataset_manager.get_dataset( name='folk_4by4nbars_train', **mvae_train_kwargs ) # init vae model vae_model = MeasureVAE( dataset=folk_dataset_vae, note_embedding_dim=note_embedding_dim, metadata_embedding_dim=metadata_embedding_dim, num_encoder_layers=num_encoder_layers, encoder_hidden_size=encoder_hidden_size, encoder_dropout_prob=encoder_dropout_prob, latent_space_dim=latent_space_dim, num_decoder_layers=num_decoder_layers, decoder_hidden_size=decoder_hidden_size, decoder_dropout_prob=decoder_dropout_prob, has_metadata=has_metadata ) vae_model.load() # VAE model must be pre-trained folk_train_kwargs = { 'metadatas': metadatas, 'sequences_size': 32, 'num_bars': 16, 'train': True } folk_test_kwargs = { 'metadatas': metadatas, 'sequences_size': 32, 'num_bars': 16, 'train': False } folk_dataset_train: FolkDataset = dataset_manager.get_dataset( name='folk_4by4nbars_train', **folk_train_kwargs ) folk_dataset_test: FolkDataset = dataset_manager.get_dataset( name='folk_4by4nbars_train', **folk_test_kwargs ) # init latent_rnn model model = LatentRNN( dataset=folk_dataset_train, vae_model=vae_model, num_rnn_layers=num_latent_rnn_layers, rnn_hidden_size=latent_rnn_hidden_size, dropout=latent_rnn_dropout_prob, rnn_class=torch.nn.GRU, auto_reg=auto_reg, teacher_forcing=teacher_forcing ) if train: if torch.cuda.is_available(): model.cuda() trainer = LatentRNNTrainer( dataset=folk_dataset_train, model=model, lr=lr, early_stopping=early_stop ) trainer.train_model( batch_size=batch_size, num_epochs=num_epochs, plot=plot, log=log ) else: model.load() model.cuda() model.eval() tester = LatentRNNTester( dataset=folk_dataset_test, model=model ) tester.test_model( batch_size=batch_size ) gen_score, score, original_score = tester.generation_random( tensor_score=None, start_measure=8, num_measures_gen=2 ) print( " --- score --- " ) print( score ) gen_score.show() original_score.show() gen_score2, score, original_score2 = tester.generation_test() gen_score2.show() original_score2.show() print( " --- score --- " ) print( score )
metadata_tensor_dataset = metadata_tensor_dataset.view( num_datapoints, 1, length, num_metadata) dataset = TensorDataset(score_tensor_dataset, metadata_tensor_dataset) print(f'Sizes: {score_tensor_dataset.size()}') print(f'Sizes: {metadata_tensor_dataset.size()}') return dataset if __name__ == '__main__': from DatasetManager.dataset_manager import DatasetManager from DatasetManager.metadata import BeatMarkerMetadata, TickMetadata dataset_manager = DatasetManager() metadatas = [ BeatMarkerMetadata(subdivision=6), TickMetadata(subdivision=6) ] folk_dataset_kwargs = {'metadatas': metadatas, 'sequences_size': 32} folk_dataset: FolkDataset = dataset_manager.get_dataset( name='folk_4by4measures_test', **folk_dataset_kwargs) (train_dataloader, val_dataloader, test_dataloader) = folk_dataset.data_loaders(batch_size=100, split=(0.7, 0.2)) print('Num Train Batches: ', len(train_dataloader)) print('Num Valid Batches: ', len(val_dataloader)) print('Num Test Batches: ', len(test_dataloader)) for sample_id, (score, _) in tqdm(enumerate(train_dataloader)): score = score.long() if torch.cuda.is_available():
def main(note_embedding_dim, metadata_embedding_dim, num_encoder_layers, encoder_hidden_size, encoder_dropout_prob, latent_space_dim, num_decoder_layers, decoder_hidden_size, decoder_dropout_prob, has_metadata, num_latent_rnn_layers, latent_rnn_hidden_size, latent_rnn_dropout_prob, num_layers, lstm_hidden_size, dropout_lstm, input_dropout, linear_hidden_size, batch_size, num_target, num_models): random.seed(0) # init dataset dataset_manager = DatasetManager() metadatas = [ BeatMarkerMetadata(subdivision=6), TickMetadata(subdivision=6) ] mvae_train_kwargs = { 'metadatas': metadatas, 'sequences_size': 32, 'num_bars': 16, 'train': True } folk_dataset_vae: FolkDataset = dataset_manager.get_dataset( name='folk_4by4nbars_train', **mvae_train_kwargs) # init vae model vae_model = MeasureVAE(dataset=folk_dataset_vae, note_embedding_dim=note_embedding_dim, metadata_embedding_dim=metadata_embedding_dim, num_encoder_layers=num_encoder_layers, encoder_hidden_size=encoder_hidden_size, encoder_dropout_prob=encoder_dropout_prob, latent_space_dim=latent_space_dim, num_decoder_layers=num_decoder_layers, decoder_hidden_size=decoder_hidden_size, decoder_dropout_prob=decoder_dropout_prob, has_metadata=has_metadata) vae_model.load() # VAE model must be pre-trained if torch.cuda.is_available(): vae_model.cuda() folk_train_kwargs = { 'metadatas': metadatas, 'sequences_size': 32, 'num_bars': 16, 'train': True } folk_test_kwargs = { 'metadatas': metadatas, 'sequences_size': 32, 'num_bars': 16, 'train': False } folk_dataset_train: FolkDataset = dataset_manager.get_dataset( name='folk_4by4nbars_train', **folk_train_kwargs) folk_dataset_test: FolkDataset = dataset_manager.get_dataset( name='folk_4by4nbars_train', **folk_test_kwargs) # Initialize stuff test_filenames = folk_dataset_test.dataset_filenames num_melodies = 32 num_measures = 16 req_length = num_measures * 4 * 6 num_past = 6 num_future = 6 num_target = 4 cur_dir = os.path.dirname(os.path.realpath(__file__)) save_folder = 'saved_midi/' # Initialize models and testers latent_rnn_model = LatentRNN(dataset=folk_dataset_train, vae_model=vae_model, num_rnn_layers=num_latent_rnn_layers, rnn_hidden_size=latent_rnn_hidden_size, dropout=latent_rnn_dropout_prob, rnn_class=torch.nn.GRU, auto_reg=False, teacher_forcing=True) latent_rnn_model.load() # latent_rnn model must be pre-trained if torch.cuda.is_available(): latent_rnn_model.cuda() latent_rnn_tester = LatentRNNTester(dataset=folk_dataset_test, model=latent_rnn_model) def process_latent_rnn_batch(score_tensor, num_past=6, num_future=6, num_target=4): assert (num_past + num_future + num_target == 16) score_tensor = score_tensor.unsqueeze(0) score_tensor = LatentRNNTrainer.split_to_measures(score_tensor, 24) tensor_past, tensor_future, tensor_target = LatentRNNTrainer.split_score( score_tensor=score_tensor, num_past=num_past, num_future=num_future, num_target=num_target, measure_seq_len=24) return tensor_past, tensor_future, tensor_target # Second save latent_rnn generations for i in tqdm(range(num_melodies)): f = test_filenames[i] f_id = f[:-4] if f_id == 'tune_16154': for j in range(15): save_filename = os.path.join( cur_dir, save_folder + f_id + '_' + str(j) + '_latent_rnn.mid') f = os.path.join( folk_dataset_test.corpus_it_gen.raw_dataset_dir, f) score = folk_dataset_test.corpus_it_gen.get_score_from_path( f, fix_and_expand=True) score_tensor = folk_dataset_test.get_score_tensor(score) # ignore scores with less than 16 measures if score_tensor.size(1) < req_length: continue score_tensor = score_tensor[:, :req_length] # metadata_tensor = metadata_tensor[:, :req_length, :] # save regeneration using latent_rnn tensor_past, tensor_future, tensor_target = process_latent_rnn_batch( score_tensor, num_past, num_future, num_target) # forward pass through latent_rnn weights, gen_target, _ = latent_rnn_tester.model( past_context=tensor_past, future_context=tensor_future, target=tensor_target, measures_to_generate=num_target, train=False, ) # convert to score batch_size, _, _ = gen_target.size() gen_target = gen_target.view(batch_size, num_target, 24) gen_score_tensor = torch.cat( (tensor_past, gen_target, tensor_future), 1) latent_rnn_score = folk_dataset_test.tensor_to_score( gen_score_tensor.cpu()) latent_rnn_score.write('midi', fp=save_filename)