| |
| |
| |
|
|
| |
|
|
| |
|
|
| |
| |
|
|
| import argparse |
| import logging |
| import pprint |
| import sys |
| from os.path import exists, dirname, basename, isdir |
| from os import mkdir, listdir |
| from typing import Union, List |
| import multiprocessing |
| import math |
|
|
| import numpy as np |
| import pandas as pd |
| import torch |
| import torch.utils.data |
| from tqdm import tqdm |
| import warnings |
| warnings.simplefilter(action = "ignore", category = FutureWarning) |
| import x_transformers |
|
|
| from os.path import dirname, realpath |
| import sys |
| sys.path.insert(0, dirname(realpath(__file__))) |
| sys.path.insert(0, dirname(dirname(realpath(__file__)))) |
|
|
| from wrangling.full import MMT_STATISTIC_COLUMNS, CHUNK_SIZE, pitch_class_entropy, scale_consistency, groove_consistency, get_tracks_string |
| from wrangling.deduplicate import FACETS |
| from dataset import FACETS_PPL, MusicDataset, pad |
| from train import OUTPUT_DIR, FINE_TUNING_SUFFIX |
| from train import BATCH_SIZE as TRAIN_BATCH_SIZE |
| from representation import Indexer, get_encoding, encode_notes, decode |
| import utils |
|
|
| |
|
|
|
|
| |
| |
|
|
| |
| BATCH_SIZE = TRAIN_BATCH_SIZE |
| N_SAMPLES = 1200 |
| N_BATCHES = int((N_SAMPLES - 1) / BATCH_SIZE) + 1 |
|
|
| |
| SEQ_LEN = 1024 |
| TEMPERATURE = 1.0 |
| FILTER = "top_k" |
|
|
| |
| LOSS_FACETS = [FACETS[0]] + FACETS_PPL |
|
|
| |
| OUTPUT_COLUMNS = ["model", "path"] + MMT_STATISTIC_COLUMNS + ["tracks"] + list(map(lambda loss_facet: f"loss:{loss_facet}", LOSS_FACETS)) |
|
|
| |
|
|
|
|
| |
| |
|
|
| |
| basestem = lambda path: ".".join(basename(path).split(".")[:-1]) |
|
|
| |
| perplexity_function = lambda loss: math.exp(-math.log(loss)) |
|
|
| |
| def loss_to_perplexity(losses: List[float]) -> float: |
| return perplexity_function(loss = sum(losses) / N_BATCHES) |
|
|
| |
|
|
|
|
| |
| |
|
|
| def parse_args(args = None, namespace = None): |
| """Parse command-line arguments.""" |
| parser = argparse.ArgumentParser(prog = "Evaluate", description = "Evaluate a REMI-Style Model.") |
| parser.add_argument("-d", "--input_dir", default = OUTPUT_DIR, type = str, help = "Dataset facet directory containing the model(s) (as subdirectories) to evaluate") |
| |
| parser.add_argument("--seq_len", default = SEQ_LEN, type = int, help = "Sequence length to generate") |
| parser.add_argument("--temperature", nargs = "+", default = TEMPERATURE, type = float, help = f"Sampling temperature (default: {TEMPERATURE})") |
| parser.add_argument("--filter", nargs = "+", default = FILTER, type = str, help = f"Sampling filter (default: '{FILTER}')") |
| |
| parser.add_argument("-g", "--gpu", default = -1, type = int, help = "GPU number") |
| parser.add_argument("-j", "--jobs", default = int(multiprocessing.cpu_count() / 4), type = int, help = "Number of workers for data loading") |
| parser.add_argument("-r", "--reset", action = "store_true", help = "Whether or not to regenerate samples") |
| return parser.parse_args(args = args, namespace = namespace) |
|
|
| |
|
|
|
|
| |
| |
|
|
| if __name__ == "__main__": |
|
|
| |
| |
|
|
| |
| args = parse_args() |
|
|
| |
| model_dirs = list(filter(lambda path: isdir(path) and basename(path).split("_")[0].endswith("M"), map(lambda base: f"{args.input_dir}/{base}", listdir(args.input_dir)))) |
| model_dirs = sorted(model_dirs, key = lambda model_dir: int(basename(model_dir).split("_")[0][:-1]) + (0.5 if FINE_TUNING_SUFFIX in basename(model_dir) else 0)) |
| models = list(map(basename, model_dirs)) |
|
|
| |
| logging.basicConfig(level = logging.INFO, format = "%(message)s", handlers = [logging.FileHandler(filename = f"{args.input_dir}/evaluate.log", mode = "a"), logging.StreamHandler(stream = sys.stdout)]) |
|
|
| |
| logging.info(f"Running command: python {' '.join(sys.argv)}") |
| logging.info(f"Using arguments:\n{pprint.pformat(vars(args))}") |
| args_output_filepath = f"{args.input_dir}/evaluate_args.json" |
| logging.info(f"Saved arguments to {args_output_filepath}") |
| utils.save_args(filepath = args_output_filepath, args = args) |
| del args_output_filepath |
|
|
| |
| data_paths_dirs = list(map(lambda loss_facet: f"{dirname(args.input_dir)}/{loss_facet}", LOSS_FACETS)) |
| data_paths_filepaths = list(map(lambda data_paths_dir: data_paths_dir + "/" + ("test" if exists(f"{data_paths_dir}/test.txt") else "valid") + ".txt", data_paths_dirs)) |
|
|
| |
| device = torch.device(f"cuda:{abs(args.gpu)}" if (torch.cuda.is_available() and args.gpu != -1) else "cpu") |
| logging.info(f"Using device: {device}") |
|
|
| |
| encoding = get_encoding() |
|
|
| |
| indexer = Indexer(data = encoding["event_code_map"]) |
| vocabulary = utils.inverse_dict(indexer.get_dict()) |
|
|
| |
| sos = indexer["start-of-song"] |
| eos = indexer["end-of-song"] |
|
|
| |
| if args.filter == "top_k": |
| filter_logits_fn = x_transformers.autoregressive_wrapper.top_k |
| elif args.filter == "top_p": |
| filter_logits_fn = x_transformers.autoregressive_wrapper.top_p |
| elif args.filter == "top_a": |
| filter_logits_fn = x_transformers.autoregressive_wrapper.top_a |
| else: |
| raise ValueError("Unknown logits filter.") |
| |
| |
| |
| |
| |
| |
|
|
| |
| output_filepath = f"{args.input_dir}/evaluation.csv" |
| if (not exists(output_filepath)) or args.reset: |
| pd.DataFrame(columns = OUTPUT_COLUMNS).to_csv(path_or_buf = output_filepath, sep = ",", na_rep = utils.NA_STRING, header = True, index = False, mode = "w") |
|
|
| |
|
|
|
|
| |
| |
|
|
| if sum(1 for _ in open(output_filepath, "r")) < ((len(models) * N_SAMPLES) + 1): |
|
|
| |
| |
|
|
| |
| def evaluate(codes: Union[np.array, torch.tensor]) -> List[float]: |
| """Evaluate the results.""" |
|
|
| |
| music = decode(codes = codes, encoding = encoding, vocabulary = vocabulary) |
|
|
| |
| if len(music.tracks) == 0: |
| return utils.rep(x = np.nan, times = len(MMT_STATISTIC_COLUMNS)) + [""] |
| else: |
| return [ |
| pitch_class_entropy(music = music), |
| scale_consistency(music = music), |
| groove_consistency(music = music), |
| get_tracks_string(tracks = music.tracks), |
| ] |
|
|
| |
|
|
|
|
| |
| |
|
|
| for model_name, model_dir in zip(models, model_dirs): |
|
|
| |
| |
|
|
| |
| eval_dir = f"{model_dir}/eval" |
| if not exists(eval_dir): |
| mkdir(eval_dir) |
|
|
| |
| train_args_filepath = f"{model_dir}/train_args.json" |
| train_args = utils.load_json(filepath = train_args_filepath) |
| del train_args_filepath |
|
|
| |
| datasets = [MusicDataset( |
| paths = data_paths_filepath, |
| encoding = encoding, |
| indexer = indexer, |
| encode_fn = encode_notes, |
| max_seq_len = train_args["max_seq_len"], |
| max_beat = train_args["max_beat"], |
| use_augmentation = train_args["aug"], |
| ) for data_paths_filepath in data_paths_filepaths] |
| data_loaders = [torch.utils.data.DataLoader( |
| dataset = dataset, |
| num_workers = args.jobs, |
| collate_fn = dataset.collate, |
| batch_size = BATCH_SIZE, |
| shuffle = False |
| ) for dataset in datasets] |
| data_iters = [iter(data_loader) for data_loader in data_loaders] |
|
|
| |
| model = x_transformers.TransformerWrapper( |
| num_tokens = len(indexer), |
| max_seq_len = train_args["max_seq_len"], |
| attn_layers = x_transformers.Decoder( |
| dim = train_args["dim"], |
| depth = train_args["layers"], |
| heads = train_args["heads"], |
| rotary_pos_emb = train_args["rel_pos_emb"], |
| emb_dropout = train_args["dropout"], |
| attn_dropout = train_args["dropout"], |
| ff_dropout = train_args["dropout"], |
| ), |
| use_abs_pos_emb = train_args["abs_pos_emb"], |
| ).to(device) |
| model = x_transformers.AutoregressiveWrapper(net = model) |
|
|
| |
| checkpoint_filepath = f"{model_dir}/checkpoints/best_model.valid.pth" |
| model_state_dict = torch.load(f = checkpoint_filepath, map_location = device, weights_only = True) |
| model.load_state_dict(state_dict = model_state_dict) |
| model.eval() |
| del checkpoint_filepath, model_state_dict |
|
|
| |
|
|
|
|
| |
| |
|
|
| |
| with torch.no_grad(): |
| for i in tqdm(iterable = range(N_BATCHES), desc = f"Evaluating the {model_name} Model"): |
|
|
| |
| n_samples_in_batch = (((N_SAMPLES - 1) % BATCH_SIZE) + 1) if (i == (N_BATCHES - 1)) else BATCH_SIZE |
|
|
| |
| |
|
|
| |
| generated_output_filepaths = list(map(lambda j: f"{eval_dir}/{(i * BATCH_SIZE) + j}.npy", range(n_samples_in_batch))) |
|
|
| |
| if (not all(map(exists, generated_output_filepaths))) or args.reset: |
|
|
| |
| prefix = torch.ones(size = (n_samples_in_batch, 1), dtype = torch.long, device = device) * sos |
|
|
| |
| generated = model.generate( |
| prompts = prefix, |
| seq_len = args.seq_len, |
| eos_token = eos, |
| temperature = args.temperature, |
| filter_logits_fn = filter_logits_fn, |
| ) |
| generated = torch.cat(tensors = (prefix, generated), dim = 1).cpu().numpy() |
|
|
| |
| for j in range(len(generated)): |
| np.save(file = generated_output_filepaths[j], arr = generated[j]) |
|
|
| |
| else: |
|
|
| |
| generated = pad(data = list(map(np.load, generated_output_filepaths))) |
|
|
| |
| with multiprocessing.Pool(processes = args.jobs) as pool: |
| results = pool.map(func = evaluate, iterable = generated, chunksize = CHUNK_SIZE) |
|
|
| |
|
|
|
|
| |
| |
|
|
| |
| loss_batch = utils.rep(x = 0.0, times = len(LOSS_FACETS)) |
|
|
| |
| for j in range(len(LOSS_FACETS)): |
|
|
| |
| try: |
| batch = next(data_iters[j]) |
| except (StopIteration): |
| data_iters[j] = iter(data_loaders[j]) |
| batch = next(data_iters[j]) |
|
|
| |
| loss_batch_facet = model( |
| x = batch["seq"][:n_samples_in_batch].to(device), |
| return_outputs = False, |
| mask = batch["mask"][:n_samples_in_batch].to(device), |
| ) |
|
|
| |
| loss_batch[j] = float(loss_batch_facet) / n_samples_in_batch |
| del loss_batch_facet |
|
|
| |
| |
| |
|
|
| |
|
|
|
|
| |
| |
|
|
| |
| results = pd.DataFrame(data = map(lambda j: [model_name, generated_output_filepaths[j]] + results[j] + loss_batch, range(n_samples_in_batch)), columns = OUTPUT_COLUMNS) |
| results.to_csv(path_or_buf = output_filepath, sep = ",", na_rep = utils.NA_STRING, header = False, index = False, mode = "a") |
| |
| |
|
|
| |
|
|
| |
| del model, datasets, data_loaders, data_iters |
|
|
| |
|
|
| |
|
|
|
|
| |
| |
|
|
| |
| bar_width = 50 |
| results = pd.read_csv(filepath_or_buffer = output_filepath, sep = ",", na_values = utils.NA_STRING, header = 0, index_col = False) |
| for model in models: |
| results_model = results[results["model"] == model] |
| logging.info(f"\n{f' {model} ':=^{bar_width}}") |
| for mmt_statistic in MMT_STATISTIC_COLUMNS: |
| logging.info(f"{mmt_statistic.replace('_', ' ').title()}: mean = {np.nanmean(a = results_model[mmt_statistic], axis = 0):.4f}, std = {np.nanstd(a = results_model[mmt_statistic], axis = 0):.4f}") |
| logging.info(f"Perplexity (All): {loss_to_perplexity(losses = results_model[f'loss:all']):.4f}") |
| print("") |
|
|
| |
|
|
| |