| |
| |
| |
|
|
| |
|
|
| |
|
|
| |
| |
|
|
| import argparse |
| import logging |
| import multiprocessing |
| from tqdm import tqdm |
| from typing import List, Callable |
| from os.path import exists, basename |
| from os import makedirs, mkdir |
| import random |
|
|
| import numpy as np |
| import pandas as pd |
| import torch |
| import torch.utils.data |
|
|
| 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 DATASET_DIR_NAME, CHUNK_SIZE |
| from wrangling.full import OUTPUT_DIR as DATASET_OUTPUT_DIR |
| from wrangling.deduplicate import FACETS |
| from reading.music import MusicRender |
| from reading.read_musescore import read_musescore |
| from representation import Indexer, get_encoding, extract_notes, encode_notes, save_csv_notes, MAX_BEAT, RESOLUTION |
| import utils |
|
|
| |
|
|
|
|
| |
| |
|
|
| |
| OUTPUT_DIR = "/data1/pnlong/musescore/experiments" |
|
|
| |
| RANDOM_FACET = "random" |
| FINE_TUNING_FACET = "fine_tuning" |
| NOT_RATED_FACET = "not_rated_deduplicated" |
| FACETS_PPL_PREFIX = FACETS[-1] |
| FACETS_PPL = list(map(lambda quartile: f"{FACETS_PPL_PREFIX}-{quartile}" if (quartile > 0) else NOT_RATED_FACET, range(5))) |
| HELD_OUT_FRACTION = 0.1 |
|
|
| |
| PARTITIONS = {"train": 0.9, "valid": 0.1, "test": 0.0} |
|
|
| |
| PAD_VALUE = 0 |
|
|
| |
| HIGH_QUALITY_RATING_THRESHOLD = 4.0 |
|
|
| |
|
|
|
|
| |
| |
|
|
| |
| def pad(data: np.array, maxlen: int = None) -> np.array: |
| """Pad a sequence.""" |
|
|
| |
| if maxlen is None: |
| max_len = max(len(seq) for seq in data) |
| else: |
| for seq in data: |
| assert len(seq) <= max_len |
|
|
| |
| if data[0].ndim == 1: |
| padded = [np.pad(array = seq, pad_width = (0, max_len - len(seq)), mode = "constant", constant_values = PAD_VALUE) for seq in data] |
| elif data[0].ndim == 2: |
| padded = [np.pad(array = seq, pad_width = ((0, max_len - len(seq)), (0, 0)), mode = "constant", constant_values = PAD_VALUE) for seq in data] |
| else: |
| raise ValueError("Got 3D data.") |
|
|
| |
| return np.stack(arrays = padded, axis = 0) |
|
|
| |
| def get_mask(data: np.array) -> torch.tensor: |
| """Get a boolean mask to cover part of data.""" |
| max_seq_len = max(len(seq) for seq in data) |
| mask = torch.zeros(size = (len(data), max_seq_len), dtype = torch.bool) |
| for i, seq in enumerate(data): |
| mask[i, :len(seq)] = True |
| mask = mask[:, :(max_seq_len - 1)] |
| return mask |
|
|
| |
|
|
|
|
| |
| |
|
|
| class MusicDataset(torch.utils.data.Dataset): |
|
|
| |
| def __init__( |
| self, |
| paths: str, |
| encoding: dict = get_encoding(), |
| indexer: Indexer = Indexer(), |
| encode_fn: Callable = encode_notes, |
| max_seq_len: int = None, |
| max_beat: int = MAX_BEAT, |
| use_augmentation: bool = False, |
| ): |
| super().__init__() |
| with open(paths, "r") as file: |
| self.paths = [line.strip() for line in file if line] |
| self.encoding = encoding |
| self.indexer = indexer |
| self.encode_fn = encode_fn |
| self.max_seq_len = max_seq_len |
| self.max_beat = max_beat |
| self.use_csv = self.paths[0].endswith("csv") |
| self.use_augmentation = use_augmentation |
|
|
| |
| def __len__(self) -> int: |
| return len(self.paths) |
|
|
| |
| def __getitem__(self, index: int) -> dict: |
|
|
| |
| path = self.paths[index] |
|
|
| |
| if self.use_csv: |
| notes = utils.load_csv(filepath = path) |
| else: |
| notes = np.load(file = path) |
|
|
| |
| assert notes.shape[1] == 5 |
|
|
| |
| notes[:, 2] = np.clip(a = notes[:, 2], a_min = 0, a_max = 127) |
|
|
| |
| if self.use_augmentation: |
|
|
| |
| pitch_shift = np.random.randint(low = -5, high = 7) |
| notes[:, 2] = np.clip(a = notes[:, 2] + pitch_shift, a_min = 0, a_max = 127) |
|
|
| |
| n_beats = notes[-1, 0] + 1 |
| if n_beats > self.max_beat: |
| trial = 0 |
| while trial < 10: |
| start_beat = np.random.randint(low = 0, high = n_beats - self.max_beat) |
| end_beat = start_beat + self.max_beat |
| sliced_notes = notes[(notes[:, 0] >= start_beat) & (notes[:, 0] < end_beat)] |
| if len(sliced_notes) > 10: |
| break |
| trial += 1 |
| sliced_notes[:, 0] = sliced_notes[:, 0] - start_beat |
| notes = sliced_notes |
|
|
| |
| elif self.max_beat is not None: |
| n_beats = notes[-1, 0] + 1 |
| if n_beats > self.max_beat: |
| notes = notes[notes[:, 0] < self.max_beat] |
|
|
| |
| seq = self.encode_fn(notes = notes, encoding = self.encoding, indexer = self.indexer) |
|
|
| |
| if (self.max_seq_len is not None) and (len(seq) > self.max_seq_len): |
| seq = np.concatenate((seq[:(self.max_seq_len - 2)], seq[(-2):])) |
|
|
| return {"name": path, "seq": seq} |
|
|
| |
| @classmethod |
| def collate(cls, data: List[dict]) -> dict: |
| seq = [sample["seq"] for sample in data] |
| return { |
| "name": [sample["name"] for sample in data], |
| "seq": torch.tensor(pad(data = seq), dtype = torch.long), |
| "seq_len": torch.tensor([len(s) for s in seq], dtype = torch.long), |
| "mask": get_mask(data = seq), |
| } |
|
|
| |
|
|
|
|
| |
| |
|
|
| def parse_args(args = None, namespace = None): |
| """Parse command-line arguments.""" |
| parser = argparse.ArgumentParser(prog = "Dataset", description = "Create and test PyTorch Dataset for MuseScore data.") |
| parser.add_argument("-df", "--dataset_filepath", default = f"{DATASET_OUTPUT_DIR}/{DATASET_DIR_NAME}.csv", type = str, help = "Filepath to full dataset") |
| parser.add_argument("-o", "--output_dir", default = OUTPUT_DIR, type = str, help = "Output directory for any relevant files") |
| parser.add_argument("-u", "--use_csv", action = "store_true", help = "Whether to save outputs in CSV format (default to NPY format)") |
| parser.add_argument("-rv", "--ratio_valid", default = PARTITIONS["valid"], type = float, help = "Ratio of validation files") |
| parser.add_argument("-rt", "--ratio_test", default = PARTITIONS["test"], type = float, help = "Ratio of test files") |
| parser.add_argument("-r", "--reset", action = "store_true", help = "Whether or not to recreate data files") |
| parser.add_argument("-j", "--jobs", default = int(multiprocessing.cpu_count() / 4), type = int, help = "Number of Jobs") |
| return parser.parse_args(args = args, namespace = namespace) |
|
|
| |
|
|
|
|
| |
| |
|
|
| if __name__ == "__main__": |
| |
| |
| |
|
|
| |
| args = parse_args() |
|
|
| |
| if not exists(args.output_dir): |
| makedirs(args.output_dir) |
| DATA_DIR = f"{args.output_dir}/data" |
| if not exists(DATA_DIR): |
| mkdir(DATA_DIR) |
|
|
| |
| logging.basicConfig(level = logging.INFO, format = "%(message)s") |
|
|
| |
|
|
|
|
| |
| |
|
|
| |
| logging.info("Loading in Dataset.") |
| dataset = pd.read_csv(filepath_or_buffer = args.dataset_filepath, sep = ",", header = 0, index_col = False) |
|
|
| |
|
|
|
|
| |
| |
|
|
| |
| def extract_notes(path: str) -> str: |
| """ |
| Helper function for extracting notes from MuseScore of music object files. |
| Given a path, extract notes and return the output path. |
| """ |
|
|
| |
| output_path = f"{DATA_DIR}/{'.'.join(basename(path).split('.')[:-1])}.{'csv' if args.use_csv else 'npy'}" |
| if exists(output_path) and (not args.reset): |
| return output_path |
|
|
| |
| if path.endswith("mscz"): |
| music = read_musescore(path = path, resolution = RESOLUTION) |
| elif path.endswith("json"): |
| music = MusicRender().load_json(path = path) |
| else: |
| raise ValueError(f"Unknown filetype `{path.split('.')[-1]}`.") |
|
|
| |
| notes = extract_notes(music = music, resolution = RESOLUTION) |
|
|
| |
| if args.use_csv: |
| save_csv_notes(filepath = output_path, data = notes) |
| else: |
| np.save(file = output_path, arr = notes) |
|
|
| |
| return output_path |
|
|
| |
| with multiprocessing.Pool(processes = args.jobs) as pool: |
| dataset["output_path"] = list(pool.map(func = extract_notes, iterable = tqdm(iterable = dataset["path"], desc = "Extracting Notes", total = len(dataset)), chunksize = CHUNK_SIZE)) |
|
|
| |
|
|
|
|
| |
| |
|
|
| |
| random.seed(0) |
| np.random.seed(0) |
| torch.manual_seed(0) |
| |
| |
| def save_paths_to_file(paths: List[str], output_filepath: str) -> None: |
| """Given a list of paths, save to a file.""" |
|
|
| |
| if not exists(dirname(output_filepath)): |
| mkdir(dirname(output_filepath)) |
|
|
| |
| with open(output_filepath, "w") as output_file: |
| output_file.write("\n".join(paths)) |
|
|
| |
| partitions = dict(zip(PARTITIONS.keys(), (1 - args.ratio_valid - args.ratio_test, args.ratio_valid, args.ratio_test))) |
|
|
| |
| quartiles = list(range(0, 101, 25)) |
| rating_quartiles = np.percentile(a = dataset.loc[dataset[f"facet:{FACETS[-1]}"], "rating"], q = quartiles) |
| logging.info(f"Rating Quartiles:") |
| for quartile, rating_quartile in zip(quartiles, rating_quartiles): |
| logging.info(f" - {quartile}th: {rating_quartile:.2f}") |
| rating_quartiles[0] -= 0.01 |
|
|
| |
| off_limit_paths_valid = set() |
| for facet in FACETS_PPL: |
| if facet == NOT_RATED_FACET: |
| paths_mask = (dataset[f"facet:{FACETS[2]}"] & (dataset["rating"] == 0)) |
| else: |
| i = int(facet[len(f"{FACETS_PPL_PREFIX}-"):]) |
| paths_mask = (dataset[f"facet:{FACETS[-1]}"] & (dataset["rating"] > rating_quartiles[i - 1]) & (dataset["rating"] <= rating_quartiles[i])) |
| paths = dataset.loc[paths_mask, "output_path"].to_list() |
| paths_valid = random.sample(population = paths, k = int(len(paths) * HELD_OUT_FRACTION)) |
| save_paths_to_file(paths = paths_valid, output_filepath = f"{args.output_dir}/{facet}/valid.txt") |
| off_limit_paths_valid.update(paths_valid) |
|
|
| |
| dataset[f"facet:{RANDOM_FACET}"] = np.zeros(shape = len(dataset), dtype = np.bool_) |
| dataset.loc[random.sample(population = range(len(dataset)), k = sum(dataset[f"facet:{FACETS[-1]}"])), f"facet:{RANDOM_FACET}"] = True |
| dataset[f"facet:{FINE_TUNING_FACET}"] = (dataset[f"facet:{FACETS[-1]}"] & (dataset["rating"] > rating_quartiles[len(rating_quartiles) // 2])) |
|
|
| |
| for facet in FACETS + [RANDOM_FACET, FINE_TUNING_FACET]: |
|
|
| |
| paths = dataset.loc[dataset[f"facet:{facet}"], "output_path"].to_list() |
| paths = random.sample(population = paths, k = len(paths)) |
| n_valid, n_test = int(partitions["valid"] * len(paths)), int(partitions["test"] * len(paths)) |
| n_train = len(paths) - n_valid - n_test |
| |
| |
| paths_train, paths_valid = paths[:n_train], paths[n_train:(n_train + n_valid)] |
| leaks = list(filter(lambda path: path in off_limit_paths_valid, paths_train)) |
| paths_train = list(filter(lambda path: path not in off_limit_paths_valid, paths_train)) |
| paths_valid += leaks |
|
|
| |
| output_dir = f"{args.output_dir}/{facet}" |
| save_paths_to_file(paths = paths_train, output_filepath = f"{output_dir}/train.txt") |
| save_paths_to_file(paths = paths_valid, output_filepath = f"{output_dir}/valid.txt") |
| if n_test > 0: |
| save_paths_to_file(paths = paths[(n_train + n_valid):], output_filepath = f"{output_dir}/test.txt") |
|
|
| |
| logging.info("Partitioned data.") |
|
|
| |
|
|
|
|
| |
| |
|
|
| |
| encoding = get_encoding() |
|
|
| |
| indexer = Indexer(data = encoding["event_code_map"]) |
|
|
| |
| dataset = MusicDataset( |
| paths = f"{args.output_dir}/{FACETS[0]}/valid.txt", |
| encoding = encoding, |
| indexer = indexer, |
| encode_fn = encode_notes, |
| ) |
| data_loader = torch.utils.data.DataLoader( |
| dataset = dataset, |
| batch_size = 8, |
| shuffle = True, |
| collate_fn = MusicDataset.collate, |
| ) |
|
|
| |
| n_batches = 0 |
| n_samples = 0 |
| seq_lens = [] |
| for i, batch in enumerate(data_loader): |
|
|
| |
| n_batches += 1 |
| n_samples += len(batch["name"]) |
| seq_lens.extend(int(l) for l in batch["seq_len"]) |
|
|
| |
| if i == 0: |
| logging.info("Example:") |
| for key, value in batch.items(): |
| if key == "name": |
| continue |
| logging.info(f"Shape of {key}: {value.shape}") |
| logging.info(f"Name: {batch['name'][0]}") |
|
|
| |
| logging.info(f"Successfully loaded {n_batches} batches ({n_samples} samples).") |
|
|
| |
|
|
|
|
| |
| |
|
|
| |
| logging.info(f"Average sequence length: {np.mean(seq_lens):2f}") |
| logging.info(f"Minimum sequence length: {min(seq_lens)}") |
| logging.info(f"Maximum sequence length: {max(seq_lens)}") |
|
|
| |
|
|
| |
|
|