def main(): parser = argparse.ArgumentParser() parser.add_argument('checkpoint') parser.add_argument('base_directory') args = parser.parse_args() device = 'cuda' if torch.cuda.is_available() else 'cpu' recurrent_model = nn.GRU( input_size=2, hidden_size=N_HIDDEN, num_layers=N_LAYERS, bias=True, batch_first=True, dropout=DROPOUT, bidirectional=BIDIRECTIONAL ) recurrent_model.to(device) output_model = nn.Sequential( nn.ReLU(), nn.Linear(BD_FACTOR * N_HIDDEN, 3, bias=True), ) output_model.to(device) model_state = torch.load(args.checkpoint) recurrent_model.load_state_dict(model_state['recurrent_model']) output_model.load_state_dict(model_state['output_model']) base_directory = args.base_directory test_sequences = get_dataset_individually(os.path.join(base_directory, 'test')) test_loaders = [] for sequence in test_sequences: loader = DataLoader( sequence, batch_size=1, sampler=SequentialSampler(sequence), drop_last=False ) test_loaders.append((sequence.midifilename, loader)) print('len(test_loaders)', len(test_loaders)) logged = evaluate( None, 'test', device, recurrent_model, output_model, test_loaders, 1 ) print('logged', logged)
def main(): parser = argparse.ArgumentParser() parser.add_argument('base_directory') parser.add_argument('--count-params-only', default=False, action='store_true') args = parser.parse_args() device = 'cuda' if torch.cuda.is_available() else 'cpu' recurrent_model = nn.GRU(input_size=2, hidden_size=N_HIDDEN, num_layers=1, bias=True, batch_first=True, dropout=0.0, bidirectional=False) recurrent_model.to(device) output_model = nn.Sequential( nn.ReLU(), nn.Linear(N_HIDDEN, 3, bias=True), ) output_model.to(device) parameters = [] for p in recurrent_model.parameters(): parameters.append(p) for p in output_model.parameters(): parameters.append(p) if args.count_params_only: n_total = 0 for p in parameters: n_total += np.prod(list(p.size())) print('n_total', n_total) exit() optimizer = torch.optim.Adam(parameters, lr=1e-3, betas=(0.9, 0.999)) base_directory = args.base_directory train_loader = get_data_loader(os.path.join(base_directory, 'train'), 'RandomSampler') # we have to decode the individual notes for individual pieces separately, of course ... valid_sequences = get_dataset_individually( os.path.join(base_directory, 'valid')) valid_loaders = [] for sequence in valid_sequences: loader = DataLoader(sequence, batch_size=1, sampler=SequentialSampler(sequence), drop_last=False) valid_loaders.append((sequence.midifilename, loader)) print('len(train_loader)', len(train_loader)) log_dir = 'runs/rnn_gru_maps_spec2labels_swd' logger = SummaryWriter(log_dir=log_dir) best_f = -np.inf global_step = 0 for i_epoch in range(100): print('i_epoch', i_epoch) global_step = train(logger, device, recurrent_model, output_model, optimizer, train_loader, global_step) to_log = evaluate(logger, 'valid', device, recurrent_model, output_model, valid_loaders, global_step) model_state = dict(recurrent_model=recurrent_model.state_dict(), output_model=output_model.state_dict()) torch.save(model_state, os.path.join(log_dir, 'model_state_{}.pkl'.format(i_epoch))) if best_f < to_log['valid_prf/f']: best_f = to_log['valid_prf/f'] torch.save( model_state, os.path.join(log_dir, 'model_state_best.pkl'.format(i_epoch)))