Spaces:
Build error
Build error
File size: 14,610 Bytes
b100e1c |
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# Copyright 2022 The MT3 Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Transcription task definitions."""
import functools
from typing import Optional, Sequence
from mt3 import datasets
from mt3 import event_codec
from mt3 import metrics
from mt3 import mixing
from mt3 import preprocessors
from mt3 import run_length_encoding
from mt3 import spectrograms
from mt3 import vocabularies
import note_seq
import numpy as np
import seqio
import t5
import tensorflow as tf
# Split audio frame sequences into this length before the cache placeholder.
MAX_NUM_CACHED_FRAMES = 2000
seqio.add_global_cache_dirs(['gs://mt3/data/cache_tasks/'])
def construct_task_name(
task_prefix: str,
spectrogram_config=spectrograms.SpectrogramConfig(),
vocab_config=vocabularies.VocabularyConfig(),
task_suffix: Optional[str] = None
) -> str:
"""Construct task name from prefix, config, and optional suffix."""
fields = [task_prefix]
if spectrogram_config.abbrev_str:
fields.append(spectrogram_config.abbrev_str)
if vocab_config.abbrev_str:
fields.append(vocab_config.abbrev_str)
if task_suffix:
fields.append(task_suffix)
return '_'.join(fields)
def trim_eos(tokens: Sequence[int]) -> np.ndarray:
"""If EOS is present, remove it and everything after."""
tokens = np.array(tokens, np.int32)
if vocabularies.DECODED_EOS_ID in tokens:
tokens = tokens[:np.argmax(tokens == vocabularies.DECODED_EOS_ID)]
return tokens
def postprocess(tokens, example, is_target, codec):
"""Transcription postprocessing function."""
tokens = trim_eos(tokens)
if is_target:
return {
'unique_id': example['unique_id'][0],
'ref_ns': (note_seq.NoteSequence.FromString(example['sequence'][0])
if example['sequence'][0] else None),
'ref_tokens': tokens,
}
start_time = example['input_times'][0]
# Round down to nearest symbolic token step.
start_time -= start_time % (1 / codec.steps_per_second)
return {
'unique_id': example['unique_id'][0],
'raw_inputs': example['raw_inputs'],
'est_tokens': tokens,
'start_time': start_time
}
def add_transcription_task_to_registry(
dataset_config: datasets.DatasetConfig,
spectrogram_config: spectrograms.SpectrogramConfig,
vocab_config: vocabularies.VocabularyConfig,
tokenize_fn, # TODO(iansimon): add type signature
onsets_only: bool,
include_ties: bool,
skip_too_long: bool = False
) -> None:
"""Add note transcription task to seqio.TaskRegistry."""
codec = vocabularies.build_codec(vocab_config)
vocabulary = vocabularies.vocabulary_from_codec(codec)
output_features = {
'targets': seqio.Feature(vocabulary=vocabulary),
'inputs': seqio.ContinuousFeature(dtype=tf.float32, rank=2)
}
task_name = 'onsets' if onsets_only else 'notes'
if include_ties:
task_name += '_ties'
task_prefix = f'{dataset_config.name}_{task_name}'
train_task_name = construct_task_name(
task_prefix=task_prefix,
spectrogram_config=spectrogram_config,
vocab_config=vocab_config,
task_suffix='train')
mixture_task_names = []
tie_token = codec.encode_event(event_codec.Event('tie', 0))
track_specs = (dataset_config.track_specs
if dataset_config.track_specs else None)
# Add transcription training task.
seqio.TaskRegistry.add(
train_task_name,
source=seqio.TFExampleDataSource(
split_to_filepattern={
'train': dataset_config.paths[dataset_config.train_split],
'eval': dataset_config.paths[dataset_config.train_eval_split]
},
feature_description=dataset_config.features),
output_features=output_features,
preprocessors=[
functools.partial(
tokenize_fn,
spectrogram_config=spectrogram_config, codec=codec,
is_training_data=True, onsets_only=onsets_only,
include_ties=include_ties),
functools.partial(
t5.data.preprocessors.split_tokens,
max_tokens_per_segment=MAX_NUM_CACHED_FRAMES,
feature_key='inputs',
additional_feature_keys=[
'input_event_start_indices', 'input_event_end_indices',
'input_state_event_indices'
],
passthrough_feature_keys=['targets', 'state_events']),
seqio.CacheDatasetPlaceholder(),
functools.partial(
t5.data.preprocessors.select_random_chunk,
feature_key='inputs',
additional_feature_keys=[
'input_event_start_indices', 'input_event_end_indices',
'input_state_event_indices'
],
passthrough_feature_keys=['targets', 'state_events'],
uniform_random_start=True),
functools.partial(
run_length_encoding.extract_target_sequence_with_indices,
state_events_end_token=tie_token if include_ties else None),
functools.partial(preprocessors.map_midi_programs, codec=codec),
run_length_encoding.run_length_encode_shifts_fn(
codec,
feature_key='targets'),
functools.partial(
mixing.mix_transcription_examples,
codec=codec,
targets_feature_keys=['targets']),
run_length_encoding.remove_redundant_state_changes_fn(
feature_key='targets', codec=codec,
state_change_event_types=['velocity', 'program']),
functools.partial(
preprocessors.compute_spectrograms,
spectrogram_config=spectrogram_config),
functools.partial(preprocessors.handle_too_long, skip=skip_too_long),
functools.partial(
seqio.preprocessors.tokenize_and_append_eos,
copy_pretokenized=False)
],
postprocess_fn=None,
metric_fns=[],
)
# Add transcription eval tasks.
for split in dataset_config.infer_eval_splits:
eval_task_name = construct_task_name(
task_prefix=task_prefix,
spectrogram_config=spectrogram_config,
vocab_config=vocab_config,
task_suffix=split.suffix)
if split.include_in_mixture:
mixture_task_names.append(eval_task_name)
seqio.TaskRegistry.add(
eval_task_name,
source=seqio.TFExampleDataSource(
split_to_filepattern={'eval': dataset_config.paths[split.name]},
feature_description=dataset_config.features),
output_features=output_features,
preprocessors=[
functools.partial(
tokenize_fn,
spectrogram_config=spectrogram_config, codec=codec,
is_training_data='train' in split.name, onsets_only=onsets_only,
include_ties=include_ties),
seqio.CacheDatasetPlaceholder(),
preprocessors.add_unique_id,
preprocessors.pad_notesequence_array,
functools.partial(
t5.data.preprocessors.split_tokens_to_inputs_length,
feature_key='inputs',
additional_feature_keys=['input_times', 'sequence'],
passthrough_feature_keys=['unique_id']),
# Add dummy targets as they are dropped during the above split to
# avoid memory blowups, but expected to be present by seqio; the
# evaluation metrics currently only use the target NoteSequence.
preprocessors.add_dummy_targets,
functools.partial(
preprocessors.compute_spectrograms,
spectrogram_config=spectrogram_config),
functools.partial(preprocessors.handle_too_long, skip=False),
functools.partial(
seqio.preprocessors.tokenize_and_append_eos,
copy_pretokenized=False)
],
postprocess_fn=functools.partial(postprocess, codec=codec),
metric_fns=[
functools.partial(
metrics.transcription_metrics,
codec=codec,
spectrogram_config=spectrogram_config,
onsets_only=onsets_only,
use_ties=include_ties,
track_specs=track_specs)
],
)
seqio.MixtureRegistry.add(
construct_task_name(
task_prefix=task_prefix, spectrogram_config=spectrogram_config,
vocab_config=vocab_config, task_suffix='eval'),
mixture_task_names,
default_rate=1)
# Just use default spectrogram config.
SPECTROGRAM_CONFIG = spectrograms.SpectrogramConfig()
# Create two vocabulary configs, one default and one with only on-off velocity.
VOCAB_CONFIG_FULL = vocabularies.VocabularyConfig()
VOCAB_CONFIG_NOVELOCITY = vocabularies.VocabularyConfig(num_velocity_bins=1)
# Transcribe MAESTRO v1.
add_transcription_task_to_registry(
dataset_config=datasets.MAESTROV1_CONFIG,
spectrogram_config=SPECTROGRAM_CONFIG,
vocab_config=VOCAB_CONFIG_FULL,
tokenize_fn=functools.partial(
preprocessors.tokenize_transcription_example,
audio_is_samples=False,
id_feature_key='id'),
onsets_only=False,
include_ties=False)
# Transcribe MAESTRO v3.
add_transcription_task_to_registry(
dataset_config=datasets.MAESTROV3_CONFIG,
spectrogram_config=SPECTROGRAM_CONFIG,
vocab_config=VOCAB_CONFIG_FULL,
tokenize_fn=functools.partial(
preprocessors.tokenize_transcription_example,
audio_is_samples=False,
id_feature_key='id'),
onsets_only=False,
include_ties=False)
# Transcribe MAESTRO v3 without velocities, with ties.
add_transcription_task_to_registry(
dataset_config=datasets.MAESTROV3_CONFIG,
spectrogram_config=SPECTROGRAM_CONFIG,
vocab_config=VOCAB_CONFIG_NOVELOCITY,
tokenize_fn=functools.partial(
preprocessors.tokenize_transcription_example,
audio_is_samples=False,
id_feature_key='id'),
onsets_only=False,
include_ties=True)
# Transcribe GuitarSet, with ties.
add_transcription_task_to_registry(
dataset_config=datasets.GUITARSET_CONFIG,
spectrogram_config=SPECTROGRAM_CONFIG,
vocab_config=VOCAB_CONFIG_NOVELOCITY,
tokenize_fn=preprocessors.tokenize_guitarset_example,
onsets_only=False,
include_ties=True)
# Transcribe URMP mixes, with ties.
add_transcription_task_to_registry(
dataset_config=datasets.URMP_CONFIG,
spectrogram_config=SPECTROGRAM_CONFIG,
vocab_config=VOCAB_CONFIG_NOVELOCITY,
tokenize_fn=functools.partial(
preprocessors.tokenize_example_with_program_lookup,
inst_name_to_program_fn=preprocessors.urmp_instrument_to_program,
id_feature_key='id'),
onsets_only=False,
include_ties=True)
# Transcribe MusicNet, with ties.
add_transcription_task_to_registry(
dataset_config=datasets.MUSICNET_CONFIG,
spectrogram_config=SPECTROGRAM_CONFIG,
vocab_config=VOCAB_CONFIG_NOVELOCITY,
tokenize_fn=functools.partial(
preprocessors.tokenize_transcription_example,
audio_is_samples=True,
id_feature_key='id'),
onsets_only=False,
include_ties=True)
# Transcribe MusicNetEM, with ties.
add_transcription_task_to_registry(
dataset_config=datasets.MUSICNET_EM_CONFIG,
spectrogram_config=SPECTROGRAM_CONFIG,
vocab_config=VOCAB_CONFIG_NOVELOCITY,
tokenize_fn=functools.partial(
preprocessors.tokenize_transcription_example,
audio_is_samples=True,
id_feature_key='id'),
onsets_only=False,
include_ties=True)
# Transcribe Cerberus4 (piano-guitar-bass-drums quartets), with ties.
add_transcription_task_to_registry(
dataset_config=datasets.CERBERUS4_CONFIG,
spectrogram_config=SPECTROGRAM_CONFIG,
vocab_config=VOCAB_CONFIG_NOVELOCITY,
tokenize_fn=functools.partial(
preprocessors.tokenize_slakh_example,
track_specs=datasets.CERBERUS4_CONFIG.track_specs,
ignore_pitch_bends=True),
onsets_only=False,
include_ties=True)
# Transcribe 10 random sub-mixes of each song from Slakh, with ties.
add_transcription_task_to_registry(
dataset_config=datasets.SLAKH_CONFIG,
spectrogram_config=SPECTROGRAM_CONFIG,
vocab_config=VOCAB_CONFIG_NOVELOCITY,
tokenize_fn=functools.partial(
preprocessors.tokenize_slakh_example,
track_specs=None,
ignore_pitch_bends=True),
onsets_only=False,
include_ties=True)
# Construct task names to include in transcription mixture.
MIXTURE_DATASET_NAMES = [
'maestrov3', 'guitarset', 'urmp', 'musicnet_em', 'cerberus4', 'slakh'
]
MIXTURE_TRAIN_TASK_NAMES = []
MIXTURE_EVAL_TASK_NAMES = []
MIXTURE_TEST_TASK_NAMES = []
for dataset_name in MIXTURE_DATASET_NAMES:
MIXTURE_TRAIN_TASK_NAMES.append(
construct_task_name(task_prefix=f'{dataset_name}_notes_ties',
spectrogram_config=SPECTROGRAM_CONFIG,
vocab_config=VOCAB_CONFIG_NOVELOCITY,
task_suffix='train'))
MIXTURE_EVAL_TASK_NAMES.append(
construct_task_name(task_prefix=f'{dataset_name}_notes_ties',
spectrogram_config=SPECTROGRAM_CONFIG,
vocab_config=VOCAB_CONFIG_NOVELOCITY,
task_suffix='validation'))
MIXING_TEMPERATURE = 10 / 3
# Add the mixture of all transcription tasks, with ties.
seqio.MixtureRegistry.add(
construct_task_name(
task_prefix='mega_notes_ties',
spectrogram_config=SPECTROGRAM_CONFIG,
vocab_config=VOCAB_CONFIG_NOVELOCITY,
task_suffix='train'),
MIXTURE_TRAIN_TASK_NAMES,
default_rate=functools.partial(
seqio.mixing_rate_num_examples,
temperature=MIXING_TEMPERATURE))
seqio.MixtureRegistry.add(
construct_task_name(
task_prefix='mega_notes_ties',
spectrogram_config=SPECTROGRAM_CONFIG,
vocab_config=VOCAB_CONFIG_NOVELOCITY,
task_suffix='eval'),
MIXTURE_EVAL_TASK_NAMES,
default_rate=functools.partial(
seqio.mixing_rate_num_examples,
temperature=MIXING_TEMPERATURE))
|