Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to not remove preceding silence in AbsTokenizer #110

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 15 additions & 8 deletions aria/tokenizer/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,20 @@ def tokenize(self, midi_dict: MidiDict, **kwargs):
required. For instance, in fine-tuning tokenizer you may want to insert
additional tokens. The default behavior is to call tokenize_midi_dict.
"""
return self._tokenize_midi_dict(midi_dict)
return self._tokenize_midi_dict(midi_dict, **kwargs)

def _detokenize_midi_dict(self, tokenized_seq: list):
"""Abstract method for de-tokenizing a sequence of tokens into a
MidiDict Object."""
raise NotImplementedError

def detokenize(self, tokenized_seq: list):
def detokenize(self, tokenized_seq: list, **kwargs):
"""Detokenizes a MidiDict object.

This function should be overridden if additional are required during
detokenization. The default behavior is to call detokenize_midi_dict.
"""
return self._detokenize_midi_dict(tokenized_seq)
return self._detokenize_midi_dict(tokenized_seq, **kwargs)

def export_data_aug(cls):
"""Abstract method for exporting a list of all data augmentation
Expand Down Expand Up @@ -411,7 +411,9 @@ def truncate_by_time(self, tokenized_seq: list, trunc_time_ms: int):

return tokenized_seq

def _tokenize_midi_dict(self, midi_dict: MidiDict):
def _tokenize_midi_dict(
self, midi_dict: MidiDict, remove_preceding_silence: bool = True
):
ticks_per_beat = midi_dict.ticks_per_beat
midi_dict.remove_instruments(self.config["ignore_instruments"])

Expand Down Expand Up @@ -450,9 +452,13 @@ def _tokenize_midi_dict(self, midi_dict: MidiDict):
prefix.insert(0, ("prefix", "genre", genre))
random.shuffle(prefix)

# NOTE: Any preceding silence is removed implicitly
tokenized_seq = []
initial_onset_tick = midi_dict.note_msgs[0]["data"]["start"]

if remove_preceding_silence is False:
initial_onset_tick = 0
else:
initial_onset_tick = midi_dict.note_msgs[0]["data"]["start"]

curr_time_since_onset = 0
for _, msg in enumerate(midi_dict.note_msgs):
# Extract msg data
Expand Down Expand Up @@ -543,12 +549,14 @@ def _detokenize_midi_dict(self, tokenized_seq: list):

# Add non-drum instrument_msgs, breaks at first note token
channel_idx = 0
curr_tick = 0
for idx, tok in enumerate(tokenized_seq):
if channel_idx == 9: # Skip channel reserved for drums
channel_idx += 1

if tok in self.special_tokens:
# Skip special tokens
if tok == self.time_tok:
curr_tick += self.abs_time_step
continue
elif (
tok[0] == "prefix"
Expand Down Expand Up @@ -590,7 +598,6 @@ def _detokenize_midi_dict(self, tokenized_seq: list):

# Note messages
note_msgs = []
curr_tick = 0
for tok_1, tok_2, tok_3 in zip(
tokenized_seq[start:],
tokenized_seq[start + 1 :],
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
torch >= 2.1
torch >= 2.0
accelerate
mido
jsonlines
Expand Down