File size: 65,116 Bytes
7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 f5d2dd3 7965430 | 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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 | # Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
#
# 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.
import copy
import json
import os
from abc import abstractmethod
from dataclasses import dataclass, field
from math import ceil, floor
from typing import Any, Dict, List, Optional, Union
import torch
from lightning.pytorch import Trainer
from omegaconf import DictConfig, ListConfig, OmegaConf
from torch.utils.data import DataLoader
from torchmetrics import Accuracy
from torchmetrics.regression import MeanAbsoluteError, MeanSquaredError
from nemo.collections.asr.data import audio_to_label_dataset, feature_to_label_dataset
from nemo.collections.asr.models.asr_model import ASRModel, ExportableEncDecModel
from nemo.collections.asr.models.label_models import EncDecSpeakerLabelModel
from nemo.collections.asr.parts.mixins import TranscriptionMixin, TranscriptionReturnType
from nemo.collections.asr.parts.mixins.transcription import InternalTranscribeConfig
from nemo.collections.asr.parts.preprocessing.features import WaveformFeaturizer
from nemo.collections.asr.parts.preprocessing.perturb import process_augmentations
from nemo.collections.common.losses import CrossEntropyLoss, MSELoss
from nemo.collections.common.metrics import TopKClassificationAccuracy
from nemo.core.classes.common import PretrainedModelInfo, typecheck
from nemo.core.neural_types import *
from nemo.utils import logging, model_utils
from nemo.utils.cast_utils import cast_all
__all__ = ['EncDecClassificationModel', 'EncDecRegressionModel']
@dataclass
class ClassificationInferConfig:
batch_size: int = 4
logprobs: bool = False
_internal: InternalTranscribeConfig = field(default_factory=lambda: InternalTranscribeConfig())
@dataclass
class RegressionInferConfig:
batch_size: int = 4
logprobs: bool = True
_internal: InternalTranscribeConfig = field(default_factory=lambda: InternalTranscribeConfig())
class _EncDecBaseModel(ASRModel, ExportableEncDecModel, TranscriptionMixin):
"""Encoder decoder Classification models."""
def __init__(self, cfg: DictConfig, trainer: Trainer = None):
# Get global rank and total number of GPU workers for IterableDataset partitioning, if applicable
# Global_rank and local_rank is set by LightningModule in Lightning 1.2.0
self.world_size = 1
if trainer is not None:
self.world_size = trainer.num_nodes * trainer.num_devices
# Convert config to a DictConfig
cfg = model_utils.convert_model_config_to_dict_config(cfg)
# Convert config to support Hydra 1.0+ instantiation
cfg = model_utils.maybe_update_config_version(cfg)
self.is_regression_task = cfg.get('is_regression_task', False)
# Change labels if needed
self._update_decoder_config(cfg.labels, cfg.decoder)
super().__init__(cfg=cfg, trainer=trainer)
if hasattr(self._cfg, 'spec_augment') and self._cfg.spec_augment is not None:
self.spec_augmentation = ASRModel.from_config_dict(self._cfg.spec_augment)
else:
self.spec_augmentation = None
if hasattr(self._cfg, 'crop_or_pad_augment') and self._cfg.crop_or_pad_augment is not None:
self.crop_or_pad = ASRModel.from_config_dict(self._cfg.crop_or_pad_augment)
else:
self.crop_or_pad = None
self.preprocessor = self._setup_preprocessor()
self.encoder = self._setup_encoder()
self.decoder = self._setup_decoder()
self.loss = self._setup_loss()
self._setup_metrics()
@abstractmethod
def _setup_preprocessor(self):
"""
Setup preprocessor for audio data
Returns: Preprocessor
"""
pass
@abstractmethod
def _setup_encoder(self):
"""
Setup encoder for the Encoder-Decoder network
Returns: Encoder
"""
pass
@abstractmethod
def _setup_decoder(self):
"""
Setup decoder for the Encoder-Decoder network
Returns: Decoder
"""
pass
@abstractmethod
def _setup_loss(self):
"""
Setup loss function for training
Returns: Loss function
"""
pass
@abstractmethod
def _setup_metrics(self):
"""
Setup metrics to be tracked in addition to loss
Returns: void
"""
pass
@property
def input_types(self) -> Optional[Dict[str, NeuralType]]:
if hasattr(self.preprocessor, '_sample_rate'):
audio_eltype = AudioSignal(freq=self.preprocessor._sample_rate)
else:
audio_eltype = AudioSignal()
return {
"input_signal": NeuralType(('B', 'T'), audio_eltype, optional=True),
"input_signal_length": NeuralType(tuple('B'), LengthsType(), optional=True),
"processed_signal": NeuralType(('B', 'D', 'T'), SpectrogramType(), optional=True),
"processed_signal_length": NeuralType(tuple('B'), LengthsType(), optional=True),
}
@property
@abstractmethod
def output_types(self) -> Optional[Dict[str, NeuralType]]:
pass
def forward(
self, input_signal=None, input_signal_length=None, processed_signal=None, processed_signal_length=None
):
has_input_signal = input_signal is not None and input_signal_length is not None
has_processed_signal = processed_signal is not None and processed_signal_length is not None
if (has_input_signal ^ has_processed_signal) == False:
raise ValueError(
f"{self} Arguments ``input_signal`` and ``input_signal_length`` are mutually exclusive "
" with ``processed_signal`` and ``processed_signal_length`` arguments."
)
if not has_processed_signal:
processed_signal, processed_signal_length = self.preprocessor(
input_signal=input_signal,
length=input_signal_length,
)
# Crop or pad is always applied
if self.crop_or_pad is not None:
processed_signal, processed_signal_length = self.crop_or_pad(
input_signal=processed_signal, length=processed_signal_length
)
# Spec augment is not applied during evaluation/testing
if self.spec_augmentation is not None and self.training:
processed_signal = self.spec_augmentation(input_spec=processed_signal, length=processed_signal_length)
encoded, encoded_len = self.encoder(audio_signal=processed_signal, length=processed_signal_length)
logits = self.decoder(encoder_output=encoded)
return logits
def setup_training_data(self, train_data_config: Optional[Union[DictConfig, Dict]]):
if 'shuffle' not in train_data_config:
train_data_config['shuffle'] = True
# preserve config
self._update_dataset_config(dataset_name='train', config=train_data_config)
self._train_dl = self._setup_dataloader_from_config(config=DictConfig(train_data_config))
# Need to set this because if using an IterableDataset, the length of the dataloader is the total number
# of samples rather than the number of batches, and this messes up the tqdm progress bar.
# So we set the number of steps manually (to the correct number) to fix this.
if (
self._train_dl is not None
and hasattr(self._train_dl, 'dataset')
and isinstance(self._train_dl.dataset, torch.utils.data.IterableDataset)
):
# We also need to check if limit_train_batches is already set.
# If it's an int, we assume that the user has set it to something sane, i.e. <= # training batches,
# and don't change it. Otherwise, adjust batches accordingly if it's a float (including 1.0).
if isinstance(self._trainer.limit_train_batches, float):
self._trainer.limit_train_batches = int(
self._trainer.limit_train_batches
* ceil((len(self._train_dl.dataset) / self.world_size) / train_data_config['batch_size'])
)
def setup_validation_data(self, val_data_config: Optional[Union[DictConfig, Dict]]):
if 'shuffle' not in val_data_config:
val_data_config['shuffle'] = False
# preserve config
self._update_dataset_config(dataset_name='validation', config=val_data_config)
self._validation_dl = self._setup_dataloader_from_config(config=DictConfig(val_data_config))
def setup_test_data(self, test_data_config: Optional[Union[DictConfig, Dict]], use_feat: bool = False):
if 'shuffle' not in test_data_config:
test_data_config['shuffle'] = False
# preserve config
self._update_dataset_config(dataset_name='test', config=test_data_config)
if use_feat and hasattr(self, '_setup_feature_label_dataloader'):
self._test_dl = self._setup_feature_label_dataloader(config=DictConfig(test_data_config))
else:
self._test_dl = self._setup_dataloader_from_config(config=DictConfig(test_data_config))
def test_dataloader(self):
if self._test_dl is not None:
return self._test_dl
def _setup_dataloader_from_config(self, config: DictConfig):
OmegaConf.set_struct(config, False)
config.is_regression_task = self.is_regression_task
OmegaConf.set_struct(config, True)
if 'augmentor' in config:
augmentor = process_augmentations(config['augmentor'])
else:
augmentor = None
featurizer = WaveformFeaturizer(
sample_rate=config['sample_rate'], int_values=config.get('int_values', False), augmentor=augmentor
)
shuffle = config['shuffle']
# Instantiate tarred dataset loader or normal dataset loader
if config.get('is_tarred', False):
if ('tarred_audio_filepaths' in config and config['tarred_audio_filepaths'] is None) or (
'manifest_filepath' in config and config['manifest_filepath'] is None
):
logging.warning(
"Could not load dataset as `manifest_filepath` is None or "
f"`tarred_audio_filepaths` is None. Provided config : {config}"
)
return None
if 'vad_stream' in config and config['vad_stream']:
logging.warning("VAD inference does not support tarred dataset now")
return None
shuffle_n = config.get('shuffle_n', 4 * config['batch_size']) if shuffle else 0
dataset = audio_to_label_dataset.get_tarred_classification_label_dataset(
featurizer=featurizer,
config=config,
shuffle_n=shuffle_n,
global_rank=self.global_rank,
world_size=self.world_size,
)
shuffle = False
batch_size = config['batch_size']
if hasattr(dataset, 'collate_fn'):
collate_fn = dataset.collate_fn
elif hasattr(dataset.datasets[0], 'collate_fn'):
# support datasets that are lists of entries
collate_fn = dataset.datasets[0].collate_fn
else:
# support datasets that are lists of lists
collate_fn = dataset.datasets[0].datasets[0].collate_fn
else:
if 'manifest_filepath' in config and config['manifest_filepath'] is None:
logging.warning(f"Could not load dataset as `manifest_filepath` is None. Provided config : {config}")
return None
if 'vad_stream' in config and config['vad_stream']:
logging.info("Perform streaming frame-level VAD")
dataset = audio_to_label_dataset.get_speech_label_dataset(featurizer=featurizer, config=config)
batch_size = 1
collate_fn = dataset.vad_frame_seq_collate_fn
else:
dataset = audio_to_label_dataset.get_classification_label_dataset(featurizer=featurizer, config=config)
batch_size = config['batch_size']
if hasattr(dataset, 'collate_fn'):
collate_fn = dataset.collate_fn
elif hasattr(dataset.datasets[0], 'collate_fn'):
# support datasets that are lists of entries
collate_fn = dataset.datasets[0].collate_fn
else:
# support datasets that are lists of lists
collate_fn = dataset.datasets[0].datasets[0].collate_fn
return torch.utils.data.DataLoader(
dataset=dataset,
batch_size=batch_size,
collate_fn=collate_fn,
drop_last=config.get('drop_last', False),
shuffle=shuffle,
num_workers=config.get('num_workers', 0),
pin_memory=config.get('pin_memory', False),
)
def _setup_feature_label_dataloader(self, config: DictConfig) -> torch.utils.data.DataLoader:
"""
setup dataloader for VAD inference with audio features as input
"""
OmegaConf.set_struct(config, False)
config.is_regression_task = self.is_regression_task
OmegaConf.set_struct(config, True)
if 'augmentor' in config:
augmentor = process_augmentations(config['augmentor'])
else:
augmentor = None
if 'manifest_filepath' in config and config['manifest_filepath'] is None:
logging.warning(f"Could not load dataset as `manifest_filepath` is None. Provided config : {config}")
return None
dataset = feature_to_label_dataset.get_feature_label_dataset(config=config, augmentor=augmentor)
if 'vad_stream' in config and config['vad_stream']:
collate_func = dataset._vad_segment_collate_fn
batch_size = 1
shuffle = False
else:
collate_func = dataset._collate_fn
batch_size = config['batch_size']
shuffle = config['shuffle']
return torch.utils.data.DataLoader(
dataset=dataset,
batch_size=batch_size,
collate_fn=collate_func,
drop_last=config.get('drop_last', False),
shuffle=shuffle,
num_workers=config.get('num_workers', 0),
pin_memory=config.get('pin_memory', False),
)
@torch.no_grad()
def transcribe(
self,
audio: Union[List[str], DataLoader],
batch_size: int = 4,
logprobs=None,
override_config: Optional[ClassificationInferConfig] | Optional[RegressionInferConfig] = None,
) -> TranscriptionReturnType:
"""
Generate class labels for provided audio files. Use this method for debugging and prototyping.
Args:
audio: (a single or list) of paths to audio files or a np.ndarray audio array.
Can also be a dataloader object that provides values that can be consumed by the model.
Recommended length per file is approximately 1 second.
batch_size: (int) batch size to use during inference. \
Bigger will result in better throughput performance but would use more memory.
logprobs: (bool) pass True to get log probabilities instead of class labels.
override_config: (Optional) ClassificationInferConfig to use for this inference call.
If None, will use the default config.
Returns:
A list of transcriptions (or raw log probabilities if logprobs is True) in the same order as paths2audio_files
"""
if logprobs is None:
logprobs = self.is_regression_task
if override_config is None:
if not self.is_regression_task:
trcfg = ClassificationInferConfig(batch_size=batch_size, logprobs=logprobs)
else:
trcfg = RegressionInferConfig(batch_size=batch_size, logprobs=logprobs)
else:
if not isinstance(override_config, ClassificationInferConfig) and not isinstance(
override_config, RegressionInferConfig
):
raise ValueError(
f"override_config must be of type {ClassificationInferConfig}, " f"but got {type(override_config)}"
)
trcfg = override_config
return super().transcribe(audio=audio, override_config=trcfg)
""" Transcription related methods """
def _transcribe_input_manifest_processing(
self, audio_files: List[str], temp_dir: str, trcfg: ClassificationInferConfig
):
with open(os.path.join(temp_dir, 'manifest.json'), 'w', encoding='utf-8') as fp:
for audio_file in audio_files:
label = 0.0 if self.is_regression_task else self.cfg.labels[0]
entry = {'audio_filepath': audio_file, 'duration': 100000.0, 'label': label}
fp.write(json.dumps(entry) + '\n')
config = {'paths2audio_files': audio_files, 'batch_size': trcfg.batch_size, 'temp_dir': temp_dir}
return config
def _transcribe_forward(self, batch: Any, trcfg: ClassificationInferConfig):
logits = self.forward(input_signal=batch[0], input_signal_length=batch[1])
output = dict(logits=logits)
return output
def _transcribe_output_processing(
self, outputs, trcfg: ClassificationInferConfig
) -> Union[List[str], List[torch.Tensor]]:
logits = outputs.pop('logits')
labels = []
if trcfg.logprobs:
# dump log probs per file
for idx in range(logits.shape[0]):
lg = logits[idx]
labels.append(lg.cpu().numpy())
else:
labels_k = []
top_ks = self._accuracy.top_k
for top_k_i in top_ks:
# replace top k value with current top k
self._accuracy.top_k = top_k_i
labels_k_i = self._accuracy.top_k_predicted_labels(logits)
labels_k_i = labels_k_i.cpu()
labels_k.append(labels_k_i)
# convenience: if only one top_k, pop out the nested list
if len(top_ks) == 1:
labels_k = labels_k[0]
labels += labels_k
# reset top k to orignal value
self._accuracy.top_k = top_ks
return labels
def _setup_transcribe_dataloader(self, config: Dict) -> 'torch.utils.data.DataLoader':
"""
Setup function for a temporary data loader which wraps the provided audio file.
Args:
config: A python dictionary which contains the following keys:
Returns:
A pytorch DataLoader for the given audio file(s).
"""
dl_config = {
'manifest_filepath': os.path.join(config['temp_dir'], 'manifest.json'),
'sample_rate': self.preprocessor._sample_rate,
'labels': self.cfg.labels,
'batch_size': min(config['batch_size'], len(config['paths2audio_files'])),
'trim_silence': False,
'shuffle': False,
}
temporary_datalayer = self._setup_dataloader_from_config(config=DictConfig(dl_config))
return temporary_datalayer
@abstractmethod
def _update_decoder_config(self, labels, cfg):
pass
@classmethod
def get_transcribe_config(cls) -> ClassificationInferConfig:
"""
Utility method that returns the default config for transcribe() function.
Returns:
A dataclass
"""
return ClassificationInferConfig()
class EncDecClassificationModel(EncDecSpeakerLabelModel, TranscriptionMixin):
def setup_test_data(self, test_data_config: Optional[Union[DictConfig, Dict]], use_feat: bool = False):
if 'shuffle' not in test_data_config:
test_data_config['shuffle'] = False
# preserve config
self._update_dataset_config(dataset_name='test', config=test_data_config)
if use_feat and hasattr(self, '_setup_feature_label_dataloader'):
self._test_dl = self._setup_feature_label_dataloader(config=DictConfig(test_data_config))
else:
self._test_dl = self._setup_dataloader_from_config(config=DictConfig(test_data_config))
def _setup_feature_label_dataloader(self, config: DictConfig) -> torch.utils.data.DataLoader:
"""
setup dataloader for VAD inference with audio features as input
"""
OmegaConf.set_struct(config, False)
config.is_regression_task = self.is_regression_task
OmegaConf.set_struct(config, True)
if 'augmentor' in config:
augmentor = process_augmentations(config['augmentor'])
else:
augmentor = None
if 'manifest_filepath' in config and config['manifest_filepath'] is None:
logging.warning(f"Could not load dataset as `manifest_filepath` is None. Provided config : {config}")
return None
dataset = feature_to_label_dataset.get_feature_label_dataset(config=config, augmentor=augmentor)
if 'vad_stream' in config and config['vad_stream']:
collate_func = dataset._vad_segment_collate_fn
batch_size = 1
shuffle = False
else:
collate_func = dataset._collate_fn
batch_size = config['batch_size']
shuffle = config['shuffle']
return torch.utils.data.DataLoader(
dataset=dataset,
batch_size=batch_size,
collate_fn=collate_func,
drop_last=config.get('drop_last', False),
shuffle=shuffle,
num_workers=config.get('num_workers', 0),
pin_memory=config.get('pin_memory', False),
)
def _setup_dataloader_from_config(self, config: DictConfig):
OmegaConf.set_struct(config, False)
config.is_regression_task = self.is_regression_task
OmegaConf.set_struct(config, True)
if 'augmentor' in config:
augmentor = process_augmentations(config['augmentor'])
else:
augmentor = None
featurizer = WaveformFeaturizer(
sample_rate=config['sample_rate'], int_values=config.get('int_values', False), augmentor=augmentor
)
shuffle = config['shuffle']
# Instantiate tarred dataset loader or normal dataset loader
if config.get('is_tarred', False):
if ('tarred_audio_filepaths' in config and config['tarred_audio_filepaths'] is None) or (
'manifest_filepath' in config and config['manifest_filepath'] is None
):
logging.warning(
"Could not load dataset as `manifest_filepath` is None or "
f"`tarred_audio_filepaths` is None. Provided config : {config}"
)
return None
if 'vad_stream' in config and config['vad_stream']:
logging.warning("VAD inference does not support tarred dataset now")
return None
shuffle_n = config.get('shuffle_n', 4 * config['batch_size']) if shuffle else 0
dataset = audio_to_label_dataset.get_tarred_classification_label_dataset(
featurizer=featurizer,
config=config,
shuffle_n=shuffle_n,
global_rank=self.global_rank,
world_size=self.world_size,
)
shuffle = False
batch_size = config['batch_size']
if hasattr(dataset, 'collate_fn'):
collate_fn = dataset.collate_fn
elif hasattr(dataset.datasets[0], 'collate_fn'):
# support datasets that are lists of entries
collate_fn = dataset.datasets[0].collate_fn
else:
# support datasets that are lists of lists
collate_fn = dataset.datasets[0].datasets[0].collate_fn
else:
if 'manifest_filepath' in config and config['manifest_filepath'] is None:
logging.warning(f"Could not load dataset as `manifest_filepath` is None. Provided config : {config}")
return None
if 'vad_stream' in config and config['vad_stream']:
logging.info("Perform streaming frame-level VAD")
dataset = audio_to_label_dataset.get_speech_label_dataset(featurizer=featurizer, config=config)
batch_size = 1
collate_fn = dataset.vad_frame_seq_collate_fn
else:
dataset = audio_to_label_dataset.get_classification_label_dataset(featurizer=featurizer, config=config)
batch_size = config['batch_size']
if hasattr(dataset, 'collate_fn'):
collate_fn = dataset.collate_fn
elif hasattr(dataset.datasets[0], 'collate_fn'):
# support datasets that are lists of entries
collate_fn = dataset.datasets[0].collate_fn
else:
# support datasets that are lists of lists
collate_fn = dataset.datasets[0].datasets[0].collate_fn
return torch.utils.data.DataLoader(
dataset=dataset,
batch_size=batch_size,
collate_fn=collate_fn,
drop_last=config.get('drop_last', False),
shuffle=shuffle,
num_workers=config.get('num_workers', 0),
pin_memory=config.get('pin_memory', False),
)
def forward_for_export(self, audio_signal, length):
encoded, length = self.encoder(audio_signal=audio_signal, length=length)
logits = self.decoder(encoder_output=encoded, length=length)
return logits
def _update_decoder_config(self, labels, cfg):
"""
Update the number of classes in the decoder based on labels provided.
Args:
labels: The current labels of the model
cfg: The config of the decoder which will be updated.
"""
OmegaConf.set_struct(cfg, False)
if 'params' in cfg:
cfg.params.num_classes = len(labels)
cfg.num_classes = len(labels)
OmegaConf.set_struct(cfg, True)
def __init__(self, cfg: DictConfig, trainer: Trainer = None):
logging.warning(
"Please use the EncDecSpeakerLabelModel instead of this model. EncDecClassificationModel model is kept for backward compatibility with older models."
)
self._update_decoder_config(cfg.labels, cfg.decoder)
if hasattr(cfg, 'is_regression_task') and cfg.is_regression_task is not None:
self.is_regression_task = cfg.is_regression_task
else:
self.is_regression_task = False
super().__init__(cfg, trainer)
if hasattr(cfg, 'crop_or_pad_augment') and cfg.crop_or_pad_augment is not None:
self.crop_or_pad = ASRModel.from_config_dict(cfg.crop_or_pad_augment)
else:
self.crop_or_pad = None
def change_labels(self, new_labels: List[str]):
"""
Changes labels used by the decoder model. Use this method when fine-tuning on from pre-trained model.
This method changes only decoder and leaves encoder and pre-processing modules unchanged. For example, you would
use it if you want to use pretrained encoder when fine-tuning on a data in another dataset.
If new_labels == self.decoder.vocabulary then nothing will be changed.
Args:
new_labels: list with new labels. Must contain at least 2 elements. Typically, \
this is set of labels for the dataset.
Returns: None
"""
if new_labels is not None and not isinstance(new_labels, ListConfig):
new_labels = ListConfig(new_labels)
if self._cfg.labels == new_labels:
logging.warning(
f"Old labels ({self._cfg.labels}) and new labels ({new_labels}) match. Not changing anything"
)
else:
if new_labels is None or len(new_labels) == 0:
raise ValueError(f'New labels must be non-empty list of labels. But I got: {new_labels}')
# Update config
self._cfg.labels = new_labels
decoder_config = self.decoder.to_config_dict()
new_decoder_config = copy.deepcopy(decoder_config)
self._update_decoder_config(new_labels, new_decoder_config)
del self.decoder
self.decoder = EncDecClassificationModel.from_config_dict(new_decoder_config)
OmegaConf.set_struct(self._cfg.decoder, False)
self._cfg.decoder = new_decoder_config
OmegaConf.set_struct(self._cfg.decoder, True)
if 'train_ds' in self._cfg and self._cfg.train_ds is not None:
self._cfg.train_ds.labels = new_labels
if 'validation_ds' in self._cfg and self._cfg.validation_ds is not None:
self._cfg.validation_ds.labels = new_labels
if 'test_ds' in self._cfg and self._cfg.test_ds is not None:
self._cfg.test_ds.labels = new_labels
self._macro_accuracy = Accuracy(
num_classes=self.decoder.num_classes, top_k=1, average='macro', task='multiclass'
)
logging.info(f"Changed decoder output to {self.decoder.num_classes} labels.")
@classmethod
def list_available_models(cls) -> Optional[List[PretrainedModelInfo]]:
"""
This method returns a list of pre-trained model which can be instantiated directly from NVIDIA's NGC cloud.
Returns:
List of available pre-trained models.
"""
results = []
model = PretrainedModelInfo(
pretrained_model_name="vad_multilingual_marblenet",
description="For details about this model, please visit https://catalog.ngc.nvidia.com/orgs/nvidia/teams/nemo/models/vad_multilingual_marblenet",
location="https://api.ngc.nvidia.com/v2/models/nvidia/nemo/vad_multilingual_marblenet/versions/1.10.0/files/vad_multilingual_marblenet.nemo",
)
results.append(model)
model = PretrainedModelInfo(
pretrained_model_name="vad_telephony_marblenet",
description="For details about this model, please visit https://ngc.nvidia.com/catalog/models/nvidia:nemo:vad_telephony_marblenet",
location="https://api.ngc.nvidia.com/v2/models/nvidia/nemo/vad_telephony_marblenet/versions/1.0.0rc1/files/vad_telephony_marblenet.nemo",
)
results.append(model)
model = PretrainedModelInfo(
pretrained_model_name="vad_marblenet",
description="For details about this model, please visit https://ngc.nvidia.com/catalog/models/nvidia:nemo:vad_marblenet",
location="https://api.ngc.nvidia.com/v2/models/nvidia/nemo/vad_marblenet/versions/1.0.0rc1/files/vad_marblenet.nemo",
)
results.append(model)
model = PretrainedModelInfo(
pretrained_model_name="commandrecognition_en_matchboxnet3x1x64_v1",
description="For details about this model, please visit https://ngc.nvidia.com/catalog/models/nvidia:nemo:commandrecognition_en_matchboxnet3x1x64_v1",
location="https://api.ngc.nvidia.com/v2/models/nvidia/nemo/commandrecognition_en_matchboxnet3x1x64_v1/versions/1.0.0rc1/files/commandrecognition_en_matchboxnet3x1x64_v1.nemo",
)
results.append(model)
model = PretrainedModelInfo(
pretrained_model_name="commandrecognition_en_matchboxnet3x2x64_v1",
description="For details about this model, please visit https://ngc.nvidia.com/catalog/models/nvidia:nemo:commandrecognition_en_matchboxnet3x2x64_v1",
location="https://api.ngc.nvidia.com/v2/models/nvidia/nemo/commandrecognition_en_matchboxnet3x2x64_v1/versions/1.0.0rc1/files/commandrecognition_en_matchboxnet3x2x64_v1.nemo",
)
results.append(model)
model = PretrainedModelInfo(
pretrained_model_name="commandrecognition_en_matchboxnet3x1x64_v2",
description="For details about this model, please visit https://ngc.nvidia.com/catalog/models/nvidia:nemo:commandrecognition_en_matchboxnet3x1x64_v2",
location="https://api.ngc.nvidia.com/v2/models/nvidia/nemo/commandrecognition_en_matchboxnet3x1x64_v2/versions/1.0.0rc1/files/commandrecognition_en_matchboxnet3x1x64_v2.nemo",
)
results.append(model)
model = PretrainedModelInfo(
pretrained_model_name="commandrecognition_en_matchboxnet3x2x64_v2",
description="For details about this model, please visit https://ngc.nvidia.com/catalog/models/nvidia:nemo:commandrecognition_en_matchboxnet3x2x64_v2",
location="https://api.ngc.nvidia.com/v2/models/nvidia/nemo/commandrecognition_en_matchboxnet3x2x64_v2/versions/1.0.0rc1/files/commandrecognition_en_matchboxnet3x2x64_v2.nemo",
)
results.append(model)
model = PretrainedModelInfo(
pretrained_model_name="commandrecognition_en_matchboxnet3x1x64_v2_subset_task",
description="For details about this model, please visit https://ngc.nvidia.com/catalog/models/nvidia:nemo:commandrecognition_en_matchboxnet3x1x64_v2_subset_task",
location="https://api.ngc.nvidia.com/v2/models/nvidia/nemo/commandrecognition_en_matchboxnet3x1x64_v2_subset_task/versions/1.0.0rc1/files/commandrecognition_en_matchboxnet3x1x64_v2_subset_task.nemo",
)
results.append(model)
model = PretrainedModelInfo(
pretrained_model_name="commandrecognition_en_matchboxnet3x2x64_v2_subset_task",
description="For details about this model, please visit https://ngc.nvidia.com/catalog/models/nvidia:nemo:commandrecognition_en_matchboxnet3x2x64_v2_subset_task",
location="https://api.ngc.nvidia.com/v2/models/nvidia/nemo/commandrecognition_en_matchboxnet3x2x64_v2_subset_task/versions/1.0.0rc1/files/commandrecognition_en_matchboxnet3x2x64_v2_subset_task.nemo",
)
results.append(model)
return results
def _setup_transcribe_dataloader(self, config: Dict) -> 'torch.utils.data.DataLoader':
"""
Setup function for a temporary data loader which wraps the provided audio file.
Args:
config: A python dictionary which contains the following keys:
Returns:
A pytorch DataLoader for the given audio file(s).
"""
dl_config = {
'manifest_filepath': os.path.join(config['temp_dir'], 'manifest.json'),
'sample_rate': self.preprocessor._sample_rate,
'labels': self.cfg.labels,
'batch_size': min(config['batch_size'], len(config['paths2audio_files'])),
'trim_silence': False,
'shuffle': False,
}
temporary_datalayer = self._setup_dataloader_from_config(config=DictConfig(dl_config))
return temporary_datalayer
@torch.no_grad()
def transcribe(
self,
audio: Union[List[str], DataLoader],
batch_size: int = 4,
logprobs=None,
override_config: Optional[ClassificationInferConfig] | Optional[RegressionInferConfig] = None,
) -> TranscriptionReturnType:
"""
Generate class labels for provided audio files. Use this method for debugging and prototyping.
Args:
audio: (a single or list) of paths to audio files or a np.ndarray audio array.
Can also be a dataloader object that provides values that can be consumed by the model.
Recommended length per file is approximately 1 second.
batch_size: (int) batch size to use during inference. \
Bigger will result in better throughput performance but would use more memory.
logprobs: (bool) pass True to get log probabilities instead of class labels.
override_config: (Optional) ClassificationInferConfig to use for this inference call.
If None, will use the default config.
Returns:
A list of transcriptions (or raw log probabilities if logprobs is True) in the same order as paths2audio_files
"""
if logprobs is None:
logprobs = self.is_regression_task
if override_config is None:
if not self.is_regression_task:
trcfg = ClassificationInferConfig(batch_size=batch_size, logprobs=logprobs)
else:
trcfg = RegressionInferConfig(batch_size=batch_size, logprobs=logprobs)
else:
if not isinstance(override_config, ClassificationInferConfig) and not isinstance(
override_config, RegressionInferConfig
):
raise ValueError(
f"override_config must be of type {ClassificationInferConfig}, " f"but got {type(override_config)}"
)
trcfg = override_config
return super().transcribe(audio=audio, override_config=trcfg)
""" Transcription related methods """
def _transcribe_input_manifest_processing(
self, audio_files: List[str], temp_dir: str, trcfg: ClassificationInferConfig
):
with open(os.path.join(temp_dir, 'manifest.json'), 'w', encoding='utf-8') as fp:
for audio_file in audio_files:
label = 0.0 if self.is_regression_task else self.cfg.labels[0]
entry = {'audio_filepath': audio_file, 'duration': 100000.0, 'label': label}
fp.write(json.dumps(entry) + '\n')
config = {'paths2audio_files': audio_files, 'batch_size': trcfg.batch_size, 'temp_dir': temp_dir}
return config
def _transcribe_forward(self, batch: Any, trcfg: ClassificationInferConfig):
logits = self.forward(input_signal=batch[0], input_signal_length=batch[1])
output = dict(logits=logits)
return output
def _transcribe_output_processing(
self, outputs, trcfg: ClassificationInferConfig
) -> Union[List[str], List[torch.Tensor]]:
logits = outputs.pop('logits')
labels = []
if trcfg.logprobs:
# dump log probs per file
for idx in range(logits.shape[0]):
lg = logits[idx]
labels.append(lg.cpu().numpy())
else:
labels_k = []
top_ks = self._accuracy.top_k
for top_k_i in top_ks:
# replace top k value with current top k
self._accuracy.top_k = top_k_i
labels_k_i = self._accuracy.top_k_predicted_labels(logits)
labels_k_i = labels_k_i.cpu()
labels_k.append(labels_k_i)
# convenience: if only one top_k, pop out the nested list
if len(top_ks) == 1:
labels_k = labels_k[0]
labels += labels_k
# reset top k to orignal value
self._accuracy.top_k = top_ks
return labels
def forward(self, input_signal, input_signal_length):
logits, _ = super().forward(input_signal, input_signal_length)
return logits
class EncDecRegressionModel(_EncDecBaseModel):
"""Encoder decoder class for speech regression models.
Model class creates training, validation methods for setting up data
performing model forward pass.
"""
@classmethod
def list_available_models(cls) -> List[PretrainedModelInfo]:
"""
This method returns a list of pre-trained model which can be instantiated directly from NVIDIA's NGC cloud.
Returns:
List of available pre-trained models.
"""
result = []
return result
def __init__(self, cfg: DictConfig, trainer: Trainer = None):
if not cfg.get('is_regression_task', False):
raise ValueError("EndDecRegressionModel requires the flag is_regression_task to be set as true")
super().__init__(cfg=cfg, trainer=trainer)
def _setup_preprocessor(self):
return EncDecRegressionModel.from_config_dict(self._cfg.preprocessor)
def _setup_encoder(self):
return EncDecRegressionModel.from_config_dict(self._cfg.encoder)
def _setup_decoder(self):
return EncDecRegressionModel.from_config_dict(self._cfg.decoder)
def _setup_loss(self):
return MSELoss()
def _setup_metrics(self):
self._mse = MeanSquaredError()
self._mae = MeanAbsoluteError()
@property
def output_types(self) -> Optional[Dict[str, NeuralType]]:
return {"preds": NeuralType(tuple('B'), RegressionValuesType())}
@typecheck()
def forward(self, input_signal, input_signal_length):
logits = super().forward(input_signal=input_signal, input_signal_length=input_signal_length)
return logits.view(-1)
# PTL-specific methods
def training_step(self, batch, batch_idx):
audio_signal, audio_signal_len, targets, targets_len = batch
logits = self.forward(input_signal=audio_signal, input_signal_length=audio_signal_len)
loss = self.loss(preds=logits, labels=targets)
train_mse = self._mse(preds=logits, target=targets)
train_mae = self._mae(preds=logits, target=targets)
self.log_dict(
{
'train_loss': loss,
'train_mse': train_mse,
'train_mae': train_mae,
'learning_rate': self._optimizer.param_groups[0]['lr'],
},
)
return {'loss': loss}
def validation_step(self, batch, batch_idx, dataloader_idx: int = 0):
audio_signal, audio_signal_len, targets, targets_len = batch
logits = self.forward(input_signal=audio_signal, input_signal_length=audio_signal_len)
loss_value = self.loss(preds=logits, labels=targets)
val_mse = self._mse(preds=logits, target=targets)
val_mae = self._mae(preds=logits, target=targets)
return {'val_loss': loss_value, 'val_mse': val_mse, 'val_mae': val_mae}
def test_step(self, batch, batch_idx, dataloader_idx: int = 0):
logs = self.validation_step(batch, batch_idx, dataloader_idx)
return {'test_loss': logs['val_loss'], 'test_mse': logs['test_mse'], 'test_mae': logs['val_mae']}
def multi_validation_epoch_end(self, outputs, dataloader_idx: int = 0):
val_loss_mean = torch.stack([x['val_loss'] for x in outputs]).mean()
val_mse = self._mse.compute()
self._mse.reset()
val_mae = self._mae.compute()
self._mae.reset()
tensorboard_logs = {'val_loss': val_loss_mean, 'val_mse': val_mse, 'val_mae': val_mae}
return {'val_loss': val_loss_mean, 'val_mse': val_mse, 'val_mae': val_mae, 'log': tensorboard_logs}
def multi_test_epoch_end(self, outputs, dataloader_idx: int = 0):
test_loss_mean = torch.stack([x['test_loss'] for x in outputs]).mean()
test_mse = self._mse.compute()
self._mse.reset()
test_mae = self._mae.compute()
self._mae.reset()
tensorboard_logs = {'test_loss': test_loss_mean, 'test_mse': test_mse, 'test_mae': test_mae}
return {'test_loss': test_loss_mean, 'test_mse': test_mse, 'test_mae': test_mae, 'log': tensorboard_logs}
@torch.no_grad()
def transcribe(
self, audio: List[str], batch_size: int = 4, override_config: Optional[RegressionInferConfig] = None
) -> List[float]:
"""
Generate class labels for provided audio files. Use this method for debugging and prototyping.
Args:
paths2audio_files: (a list) of paths to audio files. \
Recommended length per file is approximately 1 second.
batch_size: (int) batch size to use during inference. \
Bigger will result in better throughput performance but would use more memory.
Returns:
A list of predictions in the same order as paths2audio_files
"""
if override_config is None:
trcfg = RegressionInferConfig(batch_size=batch_size, logprobs=True)
else:
if not isinstance(override_config, RegressionInferConfig):
raise ValueError(
f"override_config must be of type {RegressionInferConfig}, " f"but got {type(override_config)}"
)
trcfg = override_config
predictions = super().transcribe(audio, override_config=trcfg)
return [float(pred) for pred in predictions]
def _update_decoder_config(self, labels, cfg):
OmegaConf.set_struct(cfg, False)
if 'params' in cfg:
cfg.params.num_classes = 1
else:
cfg.num_classes = 1
OmegaConf.set_struct(cfg, True)
class EncDecFrameClassificationModel(_EncDecBaseModel):
"""
EncDecFrameClassificationModel is a model that performs classification on each frame of the input audio.
The default config (i.e., marblenet_3x2x64_20ms.yaml) outputs 20ms frames.
"""
def __init__(self, cfg: DictConfig, trainer: Trainer = None):
self.num_classes = len(cfg.labels)
self.eval_loop_cnt = 0
self.ratio_threshold = cfg.get('ratio_threshold', 0.2)
if cfg.get("is_regression_task", False):
raise ValueError("EndDecClassificationModel requires the flag is_regression_task to be set as false")
super().__init__(cfg=cfg, trainer=trainer)
self.decoder.output_types = self.output_types
self.decoder.output_types_for_export = self.output_types
@property
def output_types(self) -> Optional[Dict[str, NeuralType]]:
return {"outputs": NeuralType(('B', 'T', 'C'), LogitsType())}
@classmethod
def list_available_models(cls) -> Optional[List[PretrainedModelInfo]]:
results = []
model = PretrainedModelInfo(
pretrained_model_name="vad_multilingual_frame_marblenet",
description="For details about this model, please visit https://catalog.ngc.nvidia.com/orgs/nvidia/teams/nemo/models/vad_multilingual_frame_marblenet",
location="https://api.ngc.nvidia.com/v2/models/nvidia/nemo/vad_multilingual_frame_marblenet/versions/1.20.0/files/vad_multilingual_frame_marblenet.nemo",
)
results.append(model)
return results
def _setup_preprocessor(self):
return EncDecClassificationModel.from_config_dict(self._cfg.preprocessor)
def _setup_encoder(self):
return EncDecClassificationModel.from_config_dict(self._cfg.encoder)
def _setup_decoder(self):
return EncDecClassificationModel.from_config_dict(self._cfg.decoder)
def _update_decoder_config(self, labels, cfg):
"""
Update the number of classes in the decoder based on labels provided.
Args:
labels: The current labels of the model
cfg: The config of the decoder which will be updated.
"""
OmegaConf.set_struct(cfg, False)
if 'params' in cfg:
cfg.params.num_classes = len(labels)
else:
cfg.num_classes = len(labels)
OmegaConf.set_struct(cfg, True)
def _setup_metrics(self):
self._accuracy = TopKClassificationAccuracy(dist_sync_on_step=True)
self._macro_accuracy = Accuracy(num_classes=self.num_classes, average='macro', task="multiclass")
def _setup_loss(self):
if "loss" in self.cfg:
weight = self.cfg.loss.get("weight", None)
if weight in [None, "none", "None"]:
weight = [1.0] * self.num_classes
logging.info(f"Using cross-entropy with weights: {weight}")
else:
weight = [1.0] * self.num_classes
return CrossEntropyLoss(logits_ndim=3, weight=weight)
def _setup_dataloader_from_config(self, config: DictConfig):
OmegaConf.set_struct(config, False)
config.is_regression_task = self.is_regression_task
OmegaConf.set_struct(config, True)
shuffle = config.get('shuffle', False)
if config.get('is_tarred', False):
if ('tarred_audio_filepaths' in config and config['tarred_audio_filepaths'] is None) or (
'manifest_filepath' in config and config['manifest_filepath'] is None
):
raise ValueError(
"Could not load dataset as `manifest_filepath` is None or "
f"`tarred_audio_filepaths` is None. Provided cfg : {config}"
)
shuffle_n = config.get('shuffle_n', 4 * config['batch_size']) if shuffle else 0
dataset = audio_to_label_dataset.get_tarred_audio_multi_label_dataset(
cfg=config,
shuffle_n=shuffle_n,
global_rank=self.global_rank,
world_size=self.world_size,
)
shuffle = False
if hasattr(dataset, 'collate_fn'):
collate_func = dataset.collate_fn
else:
collate_func = dataset.datasets[0].collate_fn
else:
if 'manifest_filepath' in config and config['manifest_filepath'] is None:
raise ValueError(f"Could not load dataset as `manifest_filepath` is None. Provided cfg : {config}")
dataset = audio_to_label_dataset.get_audio_multi_label_dataset(config)
collate_func = dataset.collate_fn
return torch.utils.data.DataLoader(
dataset=dataset,
batch_size=config.get("batch_size", 1),
collate_fn=collate_func,
drop_last=config.get('drop_last', False),
shuffle=shuffle,
num_workers=config.get('num_workers', 0),
pin_memory=config.get('pin_memory', False),
)
def _setup_feature_label_dataloader(self, config: DictConfig) -> torch.utils.data.DataLoader:
"""
setup dataloader for VAD inference with audio features as input
"""
OmegaConf.set_struct(config, False)
config.is_regression_task = self.is_regression_task
OmegaConf.set_struct(config, True)
if 'augmentor' in config:
augmentor = process_augmentations(config['augmentor'])
else:
augmentor = None
if 'manifest_filepath' in config and config['manifest_filepath'] is None:
logging.warning(f"Could not load dataset as `manifest_filepath` is None. Provided config : {config}")
return None
dataset = feature_to_label_dataset.get_feature_multi_label_dataset(config=config, augmentor=augmentor)
return torch.utils.data.DataLoader(
dataset=dataset,
batch_size=config.get("batch_size", 1),
collate_fn=dataset.collate_fn,
drop_last=config.get('drop_last', False),
shuffle=config.get('shuffle', False),
num_workers=config.get('num_workers', 0),
pin_memory=config.get('pin_memory', False),
)
def get_label_masks(self, labels, labels_len):
mask = torch.arange(labels.size(1))[None, :].to(labels.device) < labels_len[:, None]
return mask.to(labels.device, dtype=bool)
@typecheck()
def forward(
self, input_signal=None, input_signal_length=None, processed_signal=None, processed_signal_length=None
):
has_input_signal = input_signal is not None and input_signal_length is not None
has_processed_signal = processed_signal is not None and processed_signal_length is not None
if (has_input_signal ^ has_processed_signal) == False:
raise ValueError(
f"{self} Arguments ``input_signal`` and ``input_signal_length`` are mutually exclusive "
" with ``processed_signal`` and ``processed_signal_length`` arguments."
)
if not has_processed_signal:
processed_signal, processed_signal_length = self.preprocessor(
input_signal=input_signal,
length=input_signal_length,
)
# Crop or pad is always applied
if self.crop_or_pad is not None:
processed_signal, processed_signal_length = self.crop_or_pad(
input_signal=processed_signal, length=processed_signal_length
)
# Spec augment is not applied during evaluation/testing
if self.spec_augmentation is not None and self.training:
processed_signal = self.spec_augmentation(input_spec=processed_signal, length=processed_signal_length)
encoded, encoded_len = self.encoder(audio_signal=processed_signal, length=processed_signal_length)
logits = self.decoder(encoded.transpose(1, 2))
return logits
# PTL-specific methods
def training_step(self, batch, batch_idx):
audio_signal, audio_signal_len, labels, labels_len = batch
logits = self.forward(input_signal=audio_signal, input_signal_length=audio_signal_len)
labels, labels_len = self.reshape_labels(logits, labels, audio_signal_len, labels_len)
masks = self.get_label_masks(labels, labels_len)
loss_value = self.loss(logits=logits, labels=labels, loss_mask=masks)
tensorboard_logs = {
'train_loss': loss_value,
'learning_rate': self._optimizer.param_groups[0]['lr'],
'global_step': torch.tensor(self.trainer.global_step, dtype=torch.float32),
}
metric_logits, metric_labels = self.get_metric_logits_labels(logits, labels, masks)
self._accuracy(logits=metric_logits, labels=metric_labels)
topk_scores = self._accuracy.compute()
self._accuracy.reset()
for top_k, score in zip(self._accuracy.top_k, topk_scores):
tensorboard_logs[f'training_batch_accuracy_top@{top_k}'] = score
return {'loss': loss_value, 'log': tensorboard_logs}
def validation_step(self, batch, batch_idx, dataloader_idx: int = 0, tag: str = 'val'):
audio_signal, audio_signal_len, labels, labels_len = batch
logits = self.forward(input_signal=audio_signal, input_signal_length=audio_signal_len)
labels, labels_len = self.reshape_labels(logits, labels, audio_signal_len, labels_len)
masks = self.get_label_masks(labels, labels_len)
loss_value = self.loss(logits=logits, labels=labels, loss_mask=masks)
metric_logits, metric_labels = self.get_metric_logits_labels(logits, labels, masks)
acc = self._accuracy(logits=metric_logits, labels=metric_labels)
correct_counts, total_counts = self._accuracy.correct_counts_k, self._accuracy.total_counts_k
self._macro_accuracy.update(preds=metric_logits, target=metric_labels)
stats = self._macro_accuracy._final_state()
output = {
f'{tag}_loss': loss_value,
f'{tag}_correct_counts': correct_counts,
f'{tag}_total_counts': total_counts,
f'{tag}_acc_micro': acc,
f'{tag}_acc_stats': stats,
}
if tag == 'val':
if isinstance(self.trainer.val_dataloaders, (list, tuple)) and len(self.trainer.val_dataloaders) > 1:
self.validation_step_outputs[dataloader_idx].append(output)
else:
self.validation_step_outputs.append(output)
else:
if isinstance(self.trainer.test_dataloaders, (list, tuple)) and len(self.trainer.test_dataloaders) > 1:
self.test_step_outputs[dataloader_idx].append(output)
else:
self.test_step_outputs.append(output)
return output
def multi_validation_epoch_end(self, outputs, dataloader_idx: int = 0, tag: str = 'val'):
val_loss_mean = torch.stack([x[f'{tag}_loss'] for x in outputs]).mean()
correct_counts = torch.stack([x[f'{tag}_correct_counts'] for x in outputs]).sum(axis=0)
total_counts = torch.stack([x[f'{tag}_total_counts'] for x in outputs]).sum(axis=0)
self._accuracy.correct_counts_k = correct_counts
self._accuracy.total_counts_k = total_counts
topk_scores = self._accuracy.compute()
self._macro_accuracy.tp = torch.stack([x[f'{tag}_acc_stats'][0] for x in outputs]).sum(axis=0)
self._macro_accuracy.fp = torch.stack([x[f'{tag}_acc_stats'][1] for x in outputs]).sum(axis=0)
self._macro_accuracy.tn = torch.stack([x[f'{tag}_acc_stats'][2] for x in outputs]).sum(axis=0)
self._macro_accuracy.fn = torch.stack([x[f'{tag}_acc_stats'][3] for x in outputs]).sum(axis=0)
macro_accuracy_score = self._macro_accuracy.compute()
self._accuracy.reset()
self._macro_accuracy.reset()
tensorboard_log = {
f'{tag}_loss': val_loss_mean,
f'{tag}_acc_macro': macro_accuracy_score,
}
for top_k, score in zip(self._accuracy.top_k, topk_scores):
tensorboard_log[f'{tag}_acc_micro_top@{top_k}'] = score
self.log_dict(tensorboard_log, sync_dist=True)
return tensorboard_log
def test_step(self, batch, batch_idx, dataloader_idx=0):
return self.validation_step(batch, batch_idx, dataloader_idx, tag='test')
def multi_test_epoch_end(self, outputs, dataloader_idx: int = 0):
return self.multi_validation_epoch_end(outputs, dataloader_idx, tag='test')
def reshape_labels(self, logits, labels, logits_len, labels_len):
"""
Reshape labels to match logits shape. For example, each label is expected to cover a 40ms frame, while each frme prediction from the
model covers 20ms. If labels are shorter than logits, labels are repeated, otherwise labels are folded and argmax is applied to obtain
the label of each frame. When lengths of labels and logits are not factors of each other, labels are truncated or padded with zeros.
The ratio_threshold=0.2 is used to determine whether to pad or truncate labels, where the value 0.2 is not important as in real cases the ratio
is very close to either ceil(ratio) or floor(ratio). We use 0.2 here for easier unit-testing. This implementation does not allow frame length
and label length that are not multiples of each other.
Args:
logits: logits tensor with shape [B, T1, C]
labels: labels tensor with shape [B, T2]
logits_len: logits length tensor with shape [B]
labels_len: labels length tensor with shape [B]
Returns:
labels: labels tensor with shape [B, T1]
labels_len: labels length tensor with shape [B]
"""
logits_max_len = logits.size(1)
labels_max_len = labels.size(1)
batch_size = logits.size(0)
if logits_max_len < labels_max_len:
ratio = labels_max_len // logits_max_len
res = labels_max_len % logits_max_len
if ceil(ratio) - ratio < self.ratio_threshold: # e.g., ratio is 1.99
# pad labels with zeros until labels_max_len is a multiple of logits_max_len
labels = labels.cpu().tolist()
if len(labels) % ceil(ratio) != 0:
labels += [0] * (ceil(ratio) - len(labels) % ceil(ratio))
labels = torch.tensor(labels).long().to(logits.device)
labels = labels.view(-1, ceil(ratio)).amax(1)
return self.reshape_labels(logits, labels, logits_len, labels_len)
else:
# truncate additional labels until labels_max_len is a multiple of logits_max_len
if res > 0:
labels = labels[:, :-res]
mask = labels_len > (labels_max_len - res)
labels_len = labels_len - mask * (labels_len - (labels_max_len - res))
labels = labels.view(batch_size, ratio, -1).amax(1)
labels_len = torch.div(labels_len, ratio, rounding_mode="floor")
labels_len = torch.min(torch.cat([logits_len[:, None], labels_len[:, None]], dim=1), dim=1)[0]
return labels.contiguous(), labels_len.contiguous()
elif logits_max_len > labels_max_len:
ratio = logits_max_len / labels_max_len
res = logits_max_len % labels_max_len
if ceil(ratio) - ratio < self.ratio_threshold: # e.g., ratio is 1.99
# repeat labels for ceil(ratio) times, and DROP additional labels based on logits_max_len
labels = labels.repeat_interleave(ceil(ratio), dim=1).long()
labels = labels[:, :logits_max_len]
labels_len = labels_len * ceil(ratio)
mask = labels_len > logits_max_len
labels_len = labels_len - mask * (labels_len - logits_max_len)
else: # e.g., ratio is 2.01
# repeat labels for floor(ratio) times, and ADD padding labels based on logits_max_len
labels = labels.repeat_interleave(floor(ratio), dim=1).long()
labels_len = labels_len * floor(ratio)
if res > 0:
labels = torch.cat([labels, labels[:, -res:]], dim=1)
# no need to update `labels_len` since we ignore additional "res" padded labels
labels_len = torch.min(torch.cat([logits_len[:, None], labels_len[:, None]], dim=1), dim=1)[0]
return labels.contiguous(), labels_len.contiguous()
else:
labels_len = torch.min(torch.cat([logits_len[:, None], labels_len[:, None]], dim=1), dim=1)[0]
return labels, labels_len
def get_metric_logits_labels(self, logits, labels, masks):
"""
Computes valid logits and labels for metric computation.
Args:
logits: tensor of shape [B, T, C]
labels: tensor of shape [B, T]
masks: tensor of shape [B, T]
Returns:
logits of shape [N, C]
labels of shape [N,]
"""
C = logits.size(2)
logits = logits.view(-1, C) # [BxT, C]
labels = labels.view(-1).contiguous() # [BxT,]
masks = masks.view(-1) # [BxT,]
idx = masks.nonzero() # [BxT, 1]
logits = logits.gather(dim=0, index=idx.repeat(1, 2))
labels = labels.gather(dim=0, index=idx.view(-1))
return logits, labels
def forward_for_export(
self, input, length=None, cache_last_channel=None, cache_last_time=None, cache_last_channel_len=None
):
"""
This forward is used when we need to export the model to ONNX format.
Inputs cache_last_channel and cache_last_time are needed to be passed for exporting streaming models.
Args:
input: Tensor that represents a batch of raw audio signals,
of shape [B, T]. T here represents timesteps.
length: Vector of length B, that contains the individual lengths of the audio sequences.
cache_last_channel: Tensor of shape [N, B, T, H] which contains the cache for last channel layers
cache_last_time: Tensor of shape [N, B, H, T] which contains the cache for last time layers
N is the number of such layers which need caching, B is batch size, H is the hidden size of activations,
and T is the length of the cache
Returns:
the output of the model
"""
enc_fun = getattr(self.input_module, 'forward_for_export', self.input_module.forward)
if cache_last_channel is None:
encoder_output = enc_fun(audio_signal=input, length=length)
if isinstance(encoder_output, tuple):
encoder_output = encoder_output[0]
else:
encoder_output, length, cache_last_channel, cache_last_time, cache_last_channel_len = enc_fun(
audio_signal=input,
length=length,
cache_last_channel=cache_last_channel,
cache_last_time=cache_last_time,
cache_last_channel_len=cache_last_channel_len,
)
dec_fun = getattr(self.output_module, 'forward_for_export', self.output_module.forward)
ret = dec_fun(hidden_states=encoder_output.transpose(1, 2))
if isinstance(ret, tuple):
ret = ret[0]
if cache_last_channel is not None:
ret = (ret, length, cache_last_channel, cache_last_time, cache_last_channel_len)
return cast_all(ret, from_dtype=torch.float16, to_dtype=torch.float32)
|