Ejemplo n.º 1
0
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()
Ejemplo n.º 2
0
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')
Ejemplo n.º 3
0
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  )