-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_train_tokenizer.py
More file actions
36 lines (28 loc) · 925 Bytes
/
_train_tokenizer.py
File metadata and controls
36 lines (28 loc) · 925 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from miditok import REMI, TokenizerConfig
from pathlib import Path
# tokenizer config
from config import TOKENIZER_SAVE_PATH, VOCAB_SIZE
midi_paths = list(Path("data/maestro-v3.0.0").resolve().glob("*/*.mid?"))
BEAT_RES = {(0, 1): 12, (1, 2): 4, (2, 4): 2, (4, 8): 1}
TOKENIZER_PARAMS = {
"pitch_range": (21, 109),
"beat_res": BEAT_RES,
"num_velocities": 8,
"special_tokens": ["PAD", "BOS", "EOS"],
"use_chords": True,
"use_rests": True,
"use_tempos": True,
"use_time_signatures": True,
"use_programs": False, # no multitrack
"num_tempos": 16,
"tempo_range": (50, 200), # (min_tempo, max_tempo)
}
# create tokenizer
config = TokenizerConfig(**TOKENIZER_PARAMS)
tokenizer = REMI(config)
# train the tokenizer with Byte Pair Encoding (BPE) to build the vocabulary
tokenizer.train(
vocab_size=VOCAB_SIZE,
files_paths=midi_paths,
)
tokenizer.save(TOKENIZER_SAVE_PATH)