code optimize, model update, scripts
| | |
| | | from funasr import AutoModel |
| | | |
| | | model = AutoModel(model="damo/speech_paraformer-large-vad-punc_asr_nat-zh-cn-16k-common-vocab8404-pytorch", |
| | | model_revision="v2.0.0", |
| | | model_revision="v2.0.2", |
| | | vad_model="damo/speech_fsmn_vad_zh-cn-16k-common-pytorch", |
| | | vad_model_revision="v2.0.2", |
| | | punc_model="damo/punc_ct-transformer_zh-cn-common-vocab272727-pytorch", |
| | | punc_model_revision="v2.0.1", |
| | | spk_model="/Users/shixian/code/modelscope_models/speech_campplus_sv_zh-cn_16k-common", |
| | | punc_model_revision="v2.0.2", |
| | | spk_model="damo/speech_campplus_sv_zh-cn_16k-common", |
| | | spk_model_revision="v2.0.2", |
| | | ) |
| | | |
| | | res = model(input="https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_vad_punc_example.wav", batch_size_s=300, batch_size_threshold_s=60) |
| | | print(res) |
| | | |
| | | '''try asr with speaker label with |
| | | model = AutoModel(model="damo/speech_paraformer-large-vad-punc_asr_nat-zh-cn-16k-common-vocab8404-pytorch", |
| | | model_revision="v2.0.0", |
| | | vad_model="damo/speech_fsmn_vad_zh-cn-16k-common-pytorch", |
| | | vad_model_revision="v2.0.2", |
| | | punc_model="damo/punc_ct-transformer_zh-cn-common-vocab272727-pytorch", |
| | | punc_model_revision="v2.0.1", |
| | | spk_model="/Users/shixian/code/modelscope_models/speech_campplus_sv_zh-cn_16k-common", |
| | | spk_mode='punc_segment', |
| | | ) |
| | | |
| | | res = model(input="https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_speaker_demo.wav", batch_size_s=300, batch_size_threshold_s=60) |
| | | print(res) |
| | | ''' |
| | |
| | | |
| | | model="damo/speech_paraformer-large-vad-punc_asr_nat-zh-cn-16k-common-vocab8404-pytorch" |
| | | model_revision="v2.0.0" |
| | | model_revision="v2.0.2" |
| | | vad_model="damo/speech_fsmn_vad_zh-cn-16k-common-pytorch" |
| | | vad_model_revision="v2.0.0" |
| | | vad_model_revision="v2.0.2" |
| | | punc_model="damo/punc_ct-transformer_zh-cn-common-vocab272727-pytorch" |
| | | punc_model_revision="v2.0.1" |
| | | punc_model_revision="v2.0.2" |
| | | spk_model="damo/speech_campplus_sv_zh-cn_16k-common" |
| | | spk_model_revision="v2.0.2" |
| | | |
| | | python funasr/bin/inference.py \ |
| | | +model=${model} \ |
| | |
| | | +vad_model_revision=${vad_model_revision} \ |
| | | +punc_model=${punc_model} \ |
| | | +punc_model_revision=${punc_model_revision} \ |
| | | +spk_model=${spk_model} \ |
| | | +spk_model_revision=${spk_model_revision} \ |
| | | +input="https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_vad_punc_example.wav" \ |
| | | +output_dir="./outputs/debug" \ |
| | | +device="cpu" \ |
| | |
| | | |
| | | from funasr import AutoModel |
| | | |
| | | model = AutoModel(model="/Users/shixian/code/modelscope_models/speech_campplus_sv_zh-cn_16k-common") |
| | | model = AutoModel(model="damo/speech_campplus_sv_zh-cn_16k-common", |
| | | model_revision="v2.0.2", |
| | | ) |
| | | |
| | | res = model(input="https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_example_zh.wav") |
| | | print(res) |
| | |
| | | |
| | | from funasr import AutoModel |
| | | |
| | | model = AutoModel(model="damo/speech_paraformer-large-contextual_asr_nat-zh-cn-16k-common-vocab8404", model_revision="v2.0.0") |
| | | model = AutoModel(model="damo/speech_paraformer-large-contextual_asr_nat-zh-cn-16k-common-vocab8404", model_revision="v2.0.2") |
| | | |
| | | res = model(input="https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_example_zh.wav", |
| | | hotword='达摩院 魔搭') |
| | |
| | | |
| | | model="damo/speech_paraformer-large-contextual_asr_nat-zh-cn-16k-common-vocab8404" |
| | | model_revision="v2.0.0" |
| | | model_revision="v2.0.2" |
| | | |
| | | python funasr/bin/inference.py \ |
| | | +model=${model} \ |
| | |
| | | |
| | | from funasr import AutoModel |
| | | |
| | | model = AutoModel(model="damo/punc_ct-transformer_zh-cn-common-vocab272727-pytorch", model_revision="v2.0.1") |
| | | model = AutoModel(model="damo/punc_ct-transformer_zh-cn-common-vocab272727-pytorch", model_revision="v2.0.2") |
| | | |
| | | res = model(input="https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_text/punc_example.txt") |
| | | print(res) |
| | |
| | | |
| | | from funasr import AutoModel |
| | | |
| | | model = AutoModel(model="damo/punc_ct-transformer_cn-en-common-vocab471067-large", model_revision="v2.0.1") |
| | | model = AutoModel(model="damo/punc_ct-transformer_cn-en-common-vocab471067-large", model_revision="v2.0.2") |
| | | |
| | | res = model(input="https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_text/punc_example.txt") |
| | | print(res) |
| | |
| | | |
| | | model="damo/punc_ct-transformer_zh-cn-common-vocab272727-pytorch" |
| | | model_revision="v2.0.1" |
| | | model_revision="v2.0.2" |
| | | |
| | | model="damo/punc_ct-transformer_cn-en-common-vocab471067-large" |
| | | model_revision="v2.0.1" |
| | | model_revision="v2.0.2" |
| | | |
| | | python funasr/bin/inference.py \ |
| | | +model=${model} \ |
| | |
| | | |
| | | from funasr import AutoModel |
| | | |
| | | model = AutoModel(model="damo/speech_timestamp_prediction-v1-16k-offline", model_revision="v2.0.0") |
| | | model = AutoModel(model="damo/speech_timestamp_prediction-v1-16k-offline", model_revision="v2.0.2") |
| | | |
| | | res = model(input=("https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_example_zh.wav", |
| | | "欢迎大家来到魔搭社区进行体验"), |
| | |
| | | |
| | | model="damo/speech_timestamp_prediction-v1-16k-offline" |
| | | model_revision="v2.0.0" |
| | | model_revision="v2.0.2" |
| | | |
| | | python funasr/bin/inference.py \ |
| | | +model=${model} \ |
| | |
| | | from funasr import AutoModel |
| | | |
| | | model = AutoModel(model="damo/speech_seaco_paraformer_large_asr_nat-zh-cn-16k-common-vocab8404-pytorch", |
| | | model_revision="v2.0.0", |
| | | model_revision="v2.0.2", |
| | | vad_model="damo/speech_fsmn_vad_zh-cn-16k-common-pytorch", |
| | | vad_model_revision="v2.0.2", |
| | | punc_model="damo/punc_ct-transformer_zh-cn-common-vocab272727-pytorch", |
| | | punc_model_revision="v2.0.1", |
| | | punc_model_revision="v2.0.2", |
| | | spk_model="damo/speech_campplus_sv_zh-cn_16k-common", |
| | | spk_model_revision="v2.0.0" |
| | | spk_model_revision="v2.0.2" |
| | | ) |
| | | |
| | | res = model(input="https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_example_zh.wav", |
| | |
| | | |
| | | model="damo/speech_seaco_paraformer_large_asr_nat-zh-cn-16k-common-vocab8404-pytorch" |
| | | model_revision="v2.0.0" |
| | | model_revision="v2.0.2" |
| | | vad_model="damo/speech_fsmn_vad_zh-cn-16k-common-pytorch" |
| | | vad_model_revision="v2.0.2" |
| | | punc_model="damo/punc_ct-transformer_zh-cn-common-vocab272727-pytorch" |
| | | punc_model_revision="v2.0.1" |
| | | punc_model_revision="v2.0.2" |
| | | spk_model="damo/speech_campplus_sv_zh-cn_16k-common" |
| | | spk_model_revision="v2.0.0" |
| | | spk_model_revision="v2.0.2" |
| | | |
| | | python funasr/bin/inference.py \ |
| | | +model=${model} \ |
| | |
| | | |
| | | from funasr import AutoModel |
| | | |
| | | model = AutoModel(model="damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch", model_revision="v2.0.0") |
| | | model = AutoModel(model="damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch", model_revision="v2.0.2") |
| | | |
| | | res = model(input="https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_example_zh.wav") |
| | | print(res) |
| | |
| | | |
| | | from funasr import AutoFrontend |
| | | |
| | | frontend = AutoFrontend(model="damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch", model_revision="v2.0.0") |
| | | frontend = AutoFrontend(model="damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch", model_revision="v2.0.2") |
| | | |
| | | fbanks = frontend(input="https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_example_zh.wav", batch_size=2) |
| | | |
| | |
| | | |
| | | model="damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch" |
| | | model_revision="v2.0.0" |
| | | model_revision="v2.0.2" |
| | | |
| | | python funasr/bin/inference.py \ |
| | | +model=${model} \ |
| | |
| | | encoder_chunk_look_back = 4 #number of chunks to lookback for encoder self-attention |
| | | decoder_chunk_look_back = 1 #number of encoder chunks to lookback for decoder cross-attention |
| | | |
| | | model = AutoModel(model="damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-online", model_revision="v2.0.0") |
| | | model = AutoModel(model="damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-online", model_revision="v2.0.2") |
| | | cache = {} |
| | | res = model(input="https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_example_zh.wav", |
| | | chunk_size=chunk_size, |
| | |
| | | |
| | | model="damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-online" |
| | | model_revision="v2.0.0" |
| | | model_revision="v2.0.2" |
| | | |
| | | python funasr/bin/inference.py \ |
| | | +model=${model} \ |
| | |
| | | from funasr import AutoModel |
| | | |
| | | model = AutoModel(model="damo/speech_seaco_paraformer_large_asr_nat-zh-cn-16k-common-vocab8404-pytorch", |
| | | model_revision="v2.0.0", |
| | | model_revision="v2.0.2", |
| | | vad_model="damo/speech_fsmn_vad_zh-cn-16k-common-pytorch", |
| | | vad_model_revision="v2.0.2", |
| | | punc_model="damo/punc_ct-transformer_zh-cn-common-vocab272727-pytorch", |
| | | punc_model_revision="v2.0.1", |
| | | punc_model_revision="v2.0.2", |
| | | ) |
| | | |
| | | res = model(input="https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_example_zh.wav", |
| | |
| | | |
| | | model="damo/speech_seaco_paraformer_large_asr_nat-zh-cn-16k-common-vocab8404-pytorch" |
| | | model_revision="v2.0.0" |
| | | model_revision="v2.0.2" |
| | | vad_model="damo/speech_fsmn_vad_zh-cn-16k-common-pytorch" |
| | | vad_model_revision="v2.0.2" |
| | | punc_model="damo/punc_ct-transformer_zh-cn-common-vocab272727-pytorch" |
| | | punc_model_revision="v2.0.1" |
| | | punc_model_revision="v2.0.2" |
| | | |
| | | python funasr/bin/inference.py \ |
| | | +model=${model} \ |
| | |
| | | |
| | | time1 = time.perf_counter() |
| | | with torch.no_grad(): |
| | | results, meta_data = model.generate(**batch, **kwargs) |
| | | results, meta_data = model.inference(**batch, **kwargs) |
| | | time2 = time.perf_counter() |
| | | |
| | | asr_result_list.extend(results) |
| | |
| | | """Boundary Aware Transducer (BAT) model.""" |
| | | #!/usr/bin/env python3 |
| | | # -*- encoding: utf-8 -*- |
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | |
| | | import logging |
| | | from contextlib import contextmanager |
| | | from typing import Dict, List, Optional, Tuple, Union |
| | | |
| | | import torch |
| | | import logging |
| | | import torch.nn as nn |
| | | from packaging.version import parse as V |
| | | |
| | | from typing import Dict, List, Optional, Tuple, Union |
| | | |
| | | |
| | | from torch.cuda.amp import autocast |
| | | from funasr.losses.label_smoothing_loss import ( |
| | | LabelSmoothingLoss, # noqa: H301 |
| | | ) |
| | | |
| | | from funasr.models.transformer.utils.nets_utils import get_transducer_task_io |
| | | from funasr.metrics.compute_acc import th_accuracy |
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask |
| | | from funasr.models.transformer.utils.add_sos_eos import add_sos_eos |
| | | from funasr.train_utils.device_funcs import force_gatherable |
| | | |
| | | from torch.cuda.amp import autocast |
| | | |
| | | |
| | | |
| | | |
| | |
| | | #!/usr/bin/env python3
|
| | | # -*- encoding: utf-8 -*-
|
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
|
| | | # MIT License (https://opensource.org/licenses/MIT)
|
| | |
|
| | | import torch
|
| | | from torch import nn
|
| | | from torch import Tensor
|
| | | import logging
|
| | | import numpy as np
|
| | | from funasr.train_utils.device_funcs import to_device
|
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask
|
| | | from funasr.models.scama.utils import sequence_mask
|
| | | from typing import Optional, Tuple
|
| | |
|
| | | from funasr.register import tables
|
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask
|
| | |
|
| | |
|
| | | class mae_loss(nn.Module):
|
| | | class mae_loss(torch.nn.Module):
|
| | |
|
| | | def __init__(self, normalize_length=False):
|
| | | super(mae_loss, self).__init__()
|
| | |
| | | return fires
|
| | |
|
| | | @tables.register("predictor_classes", "CifPredictorV3")
|
| | | class CifPredictorV3(nn.Module):
|
| | | class CifPredictorV3(torch.nn.Module):
|
| | | def __init__(self,
|
| | | idim,
|
| | | l_order,
|
| | |
| | | ):
|
| | | super(CifPredictorV3, self).__init__()
|
| | |
|
| | | self.pad = nn.ConstantPad1d((l_order, r_order), 0)
|
| | | self.cif_conv1d = nn.Conv1d(idim, idim, l_order + r_order + 1)
|
| | | self.cif_output = nn.Linear(idim, 1)
|
| | | self.pad = torch.nn.ConstantPad1d((l_order, r_order), 0)
|
| | | self.cif_conv1d = torch.nn.Conv1d(idim, idim, l_order + r_order + 1)
|
| | | self.cif_output = torch.nn.Linear(idim, 1)
|
| | | self.dropout = torch.nn.Dropout(p=dropout)
|
| | | self.threshold = threshold
|
| | | self.smooth_factor = smooth_factor
|
| | |
| | | self.upsample_type = upsample_type
|
| | | self.use_cif1_cnn = use_cif1_cnn
|
| | | if self.upsample_type == 'cnn':
|
| | | self.upsample_cnn = nn.ConvTranspose1d(idim, idim, self.upsample_times, self.upsample_times)
|
| | | self.cif_output2 = nn.Linear(idim, 1)
|
| | | self.upsample_cnn = torch.nn.ConvTranspose1d(idim, idim, self.upsample_times, self.upsample_times)
|
| | | self.cif_output2 = torch.nn.Linear(idim, 1)
|
| | | elif self.upsample_type == 'cnn_blstm':
|
| | | self.upsample_cnn = nn.ConvTranspose1d(idim, idim, self.upsample_times, self.upsample_times)
|
| | | self.blstm = nn.LSTM(idim, idim, 1, bias=True, batch_first=True, dropout=0.0, bidirectional=True)
|
| | | self.cif_output2 = nn.Linear(idim*2, 1)
|
| | | self.upsample_cnn = torch.nn.ConvTranspose1d(idim, idim, self.upsample_times, self.upsample_times)
|
| | | self.blstm = torch.nn.LSTM(idim, idim, 1, bias=True, batch_first=True, dropout=0.0, bidirectional=True)
|
| | | self.cif_output2 = torch.nn.Linear(idim*2, 1)
|
| | | elif self.upsample_type == 'cnn_attn':
|
| | | self.upsample_cnn = nn.ConvTranspose1d(idim, idim, self.upsample_times, self.upsample_times)
|
| | | self.upsample_cnn = torch.nn.ConvTranspose1d(idim, idim, self.upsample_times, self.upsample_times)
|
| | | from funasr.models.transformer.encoder import EncoderLayer as TransformerEncoderLayer
|
| | | from funasr.models.transformer.attention import MultiHeadedAttention
|
| | | from funasr.models.transformer.positionwise_feed_forward import PositionwiseFeedForward
|
| | |
| | | True, #normalize_before,
|
| | | False, #concat_after,
|
| | | )
|
| | | self.cif_output2 = nn.Linear(idim, 1)
|
| | | self.cif_output2 = torch.nn.Linear(idim, 1)
|
| | | self.smooth_factor2 = smooth_factor2
|
| | | self.noise_threshold2 = noise_threshold2
|
| | |
|
| | |
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | |
| | | import logging |
| | | from typing import Dict |
| | | from typing import List |
| | | from typing import Optional |
| | | from typing import Tuple |
| | | import copy |
| | | import torch |
| | | import torch.nn as nn |
| | | import random |
| | | import numpy as np |
| | | import time |
| | | import torch |
| | | import logging |
| | | from contextlib import contextmanager |
| | | from distutils.version import LooseVersion |
| | | from typing import Dict, List, Optional, Tuple |
| | | |
| | | from funasr.models.transformer.utils.add_sos_eos import add_sos_eos |
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask, pad_list |
| | | from funasr.metrics.compute_acc import th_accuracy |
| | | from funasr.train_utils.device_funcs import force_gatherable |
| | | |
| | | from funasr.models.paraformer.search import Hypothesis |
| | | |
| | | from funasr.utils.load_utils import load_audio_text_image_video, extract_fbank |
| | | from funasr.utils import postprocess_utils |
| | | from funasr.utils.datadir_writer import DatadirWriter |
| | | from funasr.utils.timestamp_tools import ts_prediction_lfr6_standard |
| | | from funasr.register import tables |
| | | from funasr.models.ctc.ctc import CTC |
| | | |
| | | |
| | | from funasr.utils import postprocess_utils |
| | | from funasr.metrics.compute_acc import th_accuracy |
| | | from funasr.utils.datadir_writer import DatadirWriter |
| | | from funasr.models.paraformer.model import Paraformer |
| | | from funasr.models.paraformer.search import Hypothesis |
| | | from funasr.train_utils.device_funcs import force_gatherable |
| | | from funasr.models.transformer.utils.add_sos_eos import add_sos_eos |
| | | from funasr.utils.timestamp_tools import ts_prediction_lfr6_standard |
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask, pad_list |
| | | from funasr.utils.load_utils import load_audio_text_image_video, extract_fbank |
| | | |
| | | |
| | | if LooseVersion(torch.__version__) >= LooseVersion("1.6.0"): |
| | | from torch.cuda.amp import autocast |
| | | else: |
| | | # Nothing to do if torch<1.6.0 |
| | | @contextmanager |
| | | def autocast(enabled=True): |
| | | yield |
| | | |
| | | |
| | | @tables.register("model_classes", "BiCifParaformer") |
| | | class BiCifParaformer(Paraformer): |
| | |
| | | return loss, stats, weight |
| | | |
| | | |
| | | def generate(self, |
| | | def inference(self, |
| | | data_in, |
| | | data_lengths=None, |
| | | key: list = None, |
| | |
| | | # Copyright (c) Alibaba, Inc. and its affiliates. |
| | | #!/usr/bin/env python3 |
| | | # -*- encoding: utf-8 -*- |
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | # Modified from 3D-Speaker (https://github.com/alibaba-damo-academy/3D-Speaker) |
| | | |
| | | from typing import Any, Dict, Union |
| | | |
| | | import umap |
| | | import scipy |
| | | import torch |
| | | import sklearn |
| | | import hdbscan |
| | | import numpy as np |
| | | import scipy |
| | | import sklearn |
| | | import umap |
| | | |
| | | from sklearn.cluster._kmeans import k_means |
| | | from torch import nn |
| | | |
| | | |
| | | class SpectralCluster: |
| | |
| | | return labels |
| | | |
| | | |
| | | class ClusterBackend(nn.Module): |
| | | class ClusterBackend(torch.nn.Module): |
| | | r"""Perfom clustering for input embeddings and output the labels. |
| | | Args: |
| | | model_dir: A model dir. |
| | |
| | | # Copyright 3D-Speaker (https://github.com/alibaba-damo-academy/3D-Speaker). All Rights Reserved. |
| | | # Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) |
| | | #!/usr/bin/env python3 |
| | | # -*- encoding: utf-8 -*- |
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | # Modified from 3D-Speaker (https://github.com/alibaba-damo-academy/3D-Speaker) |
| | | |
| | | import torch |
| | | import torch.nn.functional as F |
| | | import torch.utils.checkpoint as cp |
| | | from torch import nn |
| | | |
| | | |
| | | class BasicResBlock(nn.Module): |
| | | class BasicResBlock(torch.nn.Module): |
| | | expansion = 1 |
| | | |
| | | def __init__(self, in_planes, planes, stride=1): |
| | | super(BasicResBlock, self).__init__() |
| | | self.conv1 = nn.Conv2d(in_planes, |
| | | self.conv1 = torch.nn.Conv2d(in_planes, |
| | | planes, |
| | | kernel_size=3, |
| | | stride=(stride, 1), |
| | | padding=1, |
| | | bias=False) |
| | | self.bn1 = nn.BatchNorm2d(planes) |
| | | self.conv2 = nn.Conv2d(planes, |
| | | self.bn1 = torch.nn.BatchNorm2d(planes) |
| | | self.conv2 = torch.nn.Conv2d(planes, |
| | | planes, |
| | | kernel_size=3, |
| | | stride=1, |
| | | padding=1, |
| | | bias=False) |
| | | self.bn2 = nn.BatchNorm2d(planes) |
| | | self.bn2 = torch.nn.BatchNorm2d(planes) |
| | | |
| | | self.shortcut = nn.Sequential() |
| | | self.shortcut = torch.nn.Sequential() |
| | | if stride != 1 or in_planes != self.expansion * planes: |
| | | self.shortcut = nn.Sequential( |
| | | nn.Conv2d(in_planes, |
| | | self.shortcut = torch.nn.Sequential( |
| | | torch.nn.Conv2d(in_planes, |
| | | self.expansion * planes, |
| | | kernel_size=1, |
| | | stride=(stride, 1), |
| | | bias=False), |
| | | nn.BatchNorm2d(self.expansion * planes)) |
| | | torch.nn.BatchNorm2d(self.expansion * planes)) |
| | | |
| | | def forward(self, x): |
| | | out = F.relu(self.bn1(self.conv1(x))) |
| | |
| | | return out |
| | | |
| | | |
| | | class FCM(nn.Module): |
| | | class FCM(torch.nn.Module): |
| | | def __init__(self, |
| | | block=BasicResBlock, |
| | | num_blocks=[2, 2], |
| | |
| | | feat_dim=80): |
| | | super(FCM, self).__init__() |
| | | self.in_planes = m_channels |
| | | self.conv1 = nn.Conv2d(1, m_channels, kernel_size=3, stride=1, padding=1, bias=False) |
| | | self.bn1 = nn.BatchNorm2d(m_channels) |
| | | self.conv1 = torch.nn.Conv2d(1, m_channels, kernel_size=3, stride=1, padding=1, bias=False) |
| | | self.bn1 = torch.nn.BatchNorm2d(m_channels) |
| | | |
| | | self.layer1 = self._make_layer(block, m_channels, num_blocks[0], stride=2) |
| | | self.layer2 = self._make_layer(block, m_channels, num_blocks[0], stride=2) |
| | | |
| | | self.conv2 = nn.Conv2d(m_channels, m_channels, kernel_size=3, stride=(2, 1), padding=1, bias=False) |
| | | self.bn2 = nn.BatchNorm2d(m_channels) |
| | | self.conv2 = torch.nn.Conv2d(m_channels, m_channels, kernel_size=3, stride=(2, 1), padding=1, bias=False) |
| | | self.bn2 = torch.nn.BatchNorm2d(m_channels) |
| | | self.out_channels = m_channels * (feat_dim // 8) |
| | | |
| | | def _make_layer(self, block, planes, num_blocks, stride): |
| | |
| | | for stride in strides: |
| | | layers.append(block(self.in_planes, planes, stride)) |
| | | self.in_planes = planes * block.expansion |
| | | return nn.Sequential(*layers) |
| | | return torch.nn.Sequential(*layers) |
| | | |
| | | def forward(self, x): |
| | | x = x.unsqueeze(1) |
| | |
| | | |
| | | |
| | | def get_nonlinear(config_str, channels): |
| | | nonlinear = nn.Sequential() |
| | | nonlinear = torch.nn.Sequential() |
| | | for name in config_str.split('-'): |
| | | if name == 'relu': |
| | | nonlinear.add_module('relu', nn.ReLU(inplace=True)) |
| | | nonlinear.add_module('relu', torch.nn.ReLU(inplace=True)) |
| | | elif name == 'prelu': |
| | | nonlinear.add_module('prelu', nn.PReLU(channels)) |
| | | nonlinear.add_module('prelu', torch.nn.PReLU(channels)) |
| | | elif name == 'batchnorm': |
| | | nonlinear.add_module('batchnorm', nn.BatchNorm1d(channels)) |
| | | nonlinear.add_module('batchnorm', torch.nn.BatchNorm1d(channels)) |
| | | elif name == 'batchnorm_': |
| | | nonlinear.add_module('batchnorm', |
| | | nn.BatchNorm1d(channels, affine=False)) |
| | | torch.nn.BatchNorm1d(channels, affine=False)) |
| | | else: |
| | | raise ValueError('Unexpected module ({}).'.format(name)) |
| | | return nonlinear |
| | |
| | | return stats |
| | | |
| | | |
| | | class StatsPool(nn.Module): |
| | | class StatsPool(torch.nn.Module): |
| | | def forward(self, x): |
| | | return statistics_pooling(x) |
| | | |
| | | |
| | | class TDNNLayer(nn.Module): |
| | | class TDNNLayer(torch.nn.Module): |
| | | def __init__(self, |
| | | in_channels, |
| | | out_channels, |
| | |
| | | assert kernel_size % 2 == 1, 'Expect equal paddings, but got even kernel size ({})'.format( |
| | | kernel_size) |
| | | padding = (kernel_size - 1) // 2 * dilation |
| | | self.linear = nn.Conv1d(in_channels, |
| | | self.linear = torch.nn.Conv1d(in_channels, |
| | | out_channels, |
| | | kernel_size, |
| | | stride=stride, |
| | |
| | | return x |
| | | |
| | | |
| | | class CAMLayer(nn.Module): |
| | | class CAMLayer(torch.nn.Module): |
| | | def __init__(self, |
| | | bn_channels, |
| | | out_channels, |
| | |
| | | bias, |
| | | reduction=2): |
| | | super(CAMLayer, self).__init__() |
| | | self.linear_local = nn.Conv1d(bn_channels, |
| | | self.linear_local = torch.nn.Conv1d(bn_channels, |
| | | out_channels, |
| | | kernel_size, |
| | | stride=stride, |
| | | padding=padding, |
| | | dilation=dilation, |
| | | bias=bias) |
| | | self.linear1 = nn.Conv1d(bn_channels, bn_channels // reduction, 1) |
| | | self.relu = nn.ReLU(inplace=True) |
| | | self.linear2 = nn.Conv1d(bn_channels // reduction, out_channels, 1) |
| | | self.sigmoid = nn.Sigmoid() |
| | | self.linear1 = torch.nn.Conv1d(bn_channels, bn_channels // reduction, 1) |
| | | self.relu = torch.nn.ReLU(inplace=True) |
| | | self.linear2 = torch.nn.Conv1d(bn_channels // reduction, out_channels, 1) |
| | | self.sigmoid = torch.nn.Sigmoid() |
| | | |
| | | def forward(self, x): |
| | | y = self.linear_local(x) |
| | |
| | | return seg |
| | | |
| | | |
| | | class CAMDenseTDNNLayer(nn.Module): |
| | | class CAMDenseTDNNLayer(torch.nn.Module): |
| | | def __init__(self, |
| | | in_channels, |
| | | out_channels, |
| | |
| | | padding = (kernel_size - 1) // 2 * dilation |
| | | self.memory_efficient = memory_efficient |
| | | self.nonlinear1 = get_nonlinear(config_str, in_channels) |
| | | self.linear1 = nn.Conv1d(in_channels, bn_channels, 1, bias=False) |
| | | self.linear1 = torch.nn.Conv1d(in_channels, bn_channels, 1, bias=False) |
| | | self.nonlinear2 = get_nonlinear(config_str, bn_channels) |
| | | self.cam_layer = CAMLayer(bn_channels, |
| | | out_channels, |
| | |
| | | return x |
| | | |
| | | |
| | | class CAMDenseTDNNBlock(nn.ModuleList): |
| | | class CAMDenseTDNNBlock(torch.nn.ModuleList): |
| | | def __init__(self, |
| | | num_layers, |
| | | in_channels, |
| | |
| | | return x |
| | | |
| | | |
| | | class TransitLayer(nn.Module): |
| | | class TransitLayer(torch.nn.Module): |
| | | def __init__(self, |
| | | in_channels, |
| | | out_channels, |
| | |
| | | config_str='batchnorm-relu'): |
| | | super(TransitLayer, self).__init__() |
| | | self.nonlinear = get_nonlinear(config_str, in_channels) |
| | | self.linear = nn.Conv1d(in_channels, out_channels, 1, bias=bias) |
| | | self.linear = torch.nn.Conv1d(in_channels, out_channels, 1, bias=bias) |
| | | |
| | | def forward(self, x): |
| | | x = self.nonlinear(x) |
| | |
| | | return x |
| | | |
| | | |
| | | class DenseLayer(nn.Module): |
| | | class DenseLayer(torch.nn.Module): |
| | | def __init__(self, |
| | | in_channels, |
| | | out_channels, |
| | | bias=False, |
| | | config_str='batchnorm-relu'): |
| | | super(DenseLayer, self).__init__() |
| | | self.linear = nn.Conv1d(in_channels, out_channels, 1, bias=bias) |
| | | self.linear = torch.nn.Conv1d(in_channels, out_channels, 1, bias=bias) |
| | | self.nonlinear = get_nonlinear(config_str, out_channels) |
| | | |
| | | def forward(self, x): |
| | |
| | | # Copyright 3D-Speaker (https://github.com/alibaba-damo-academy/3D-Speaker). All Rights Reserved. |
| | | # Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) |
| | | #!/usr/bin/env python3 |
| | | # -*- encoding: utf-8 -*- |
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | # Modified from 3D-Speaker (https://github.com/alibaba-damo-academy/3D-Speaker) |
| | | |
| | | import os |
| | | import time |
| | | import torch |
| | | import logging |
| | | import numpy as np |
| | | import torch.nn as nn |
| | | from collections import OrderedDict |
| | | from typing import Union, Dict, List, Tuple, Optional |
| | | from contextlib import contextmanager |
| | | from distutils.version import LooseVersion |
| | | |
| | | from funasr.utils.load_utils import load_audio_text_image_video |
| | | from funasr.utils.datadir_writer import DatadirWriter |
| | | from funasr.register import tables |
| | | from funasr.models.campplus.components import DenseLayer, StatsPool, TDNNLayer, CAMDenseTDNNBlock, TransitLayer, \ |
| | | BasicResBlock, get_nonlinear, FCM |
| | | from funasr.models.campplus.utils import extract_feature |
| | | from funasr.utils.load_utils import load_audio_text_image_video |
| | | from funasr.models.campplus.components import DenseLayer, StatsPool, \ |
| | | TDNNLayer, CAMDenseTDNNBlock, TransitLayer, get_nonlinear, FCM |
| | | |
| | | |
| | | if LooseVersion(torch.__version__) >= LooseVersion("1.6.0"): |
| | | from torch.cuda.amp import autocast |
| | | else: |
| | | # Nothing to do if torch<1.6.0 |
| | | @contextmanager |
| | | def autocast(enabled=True): |
| | | yield |
| | | |
| | | |
| | | @tables.register("model_classes", "CAMPPlus") |
| | | class CAMPPlus(nn.Module): |
| | | class CAMPPlus(torch.nn.Module): |
| | | def __init__(self, |
| | | feat_dim=80, |
| | | embedding_size=192, |
| | |
| | | channels = self.head.out_channels |
| | | self.output_level = output_level |
| | | |
| | | self.xvector = nn.Sequential( |
| | | self.xvector = torch.nn.Sequential( |
| | | OrderedDict([ |
| | | |
| | | ('tdnn', |
| | |
| | | assert self.output_level == 'frame', '`output_level` should be set to \'segment\' or \'frame\'. ' |
| | | |
| | | for m in self.modules(): |
| | | if isinstance(m, (nn.Conv1d, nn.Linear)): |
| | | nn.init.kaiming_normal_(m.weight.data) |
| | | if isinstance(m, (torch.nn.Conv1d, torch.nn.Linear)): |
| | | torch.nn.init.kaiming_normal_(m.weight.data) |
| | | if m.bias is not None: |
| | | nn.init.zeros_(m.bias) |
| | | torch.nn.init.zeros_(m.bias) |
| | | |
| | | def forward(self, x): |
| | | x = x.permute(0, 2, 1) # (B,T,F) => (B,F,T) |
| | |
| | | x = x.transpose(1, 2) |
| | | return x |
| | | |
| | | def generate(self, |
| | | def inference(self, |
| | | data_in, |
| | | data_lengths=None, |
| | | key: list=None, |
| | |
| | | # Copyright 3D-Speaker (https://github.com/alibaba-damo-academy/3D-Speaker). All Rights Reserved. |
| | | # Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) |
| | | #!/usr/bin/env python3 |
| | | # -*- encoding: utf-8 -*- |
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | # Modified from 3D-Speaker (https://github.com/alibaba-damo-academy/3D-Speaker) |
| | | |
| | | import io |
| | | import os |
| | |
| | | from typing import Generator, Union |
| | | from abc import ABCMeta, abstractmethod |
| | | import torchaudio.compliance.kaldi as Kaldi |
| | | |
| | | from funasr.models.transformer.utils.nets_utils import pad_list |
| | | |
| | | |
| | |
| | | from typing import List |
| | | from typing import Tuple |
| | | import logging |
| | | #!/usr/bin/env python3 |
| | | # -*- encoding: utf-8 -*- |
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | |
| | | import torch |
| | | import torch.nn as nn |
| | | import logging |
| | | import numpy as np |
| | | |
| | | from funasr.models.scama import utils as myutils |
| | | |
| | | from funasr.models.sanm.attention import MultiHeadedAttentionSANMDecoder, MultiHeadedAttentionCrossAtt |
| | | from funasr.models.transformer.embedding import PositionalEncoding |
| | | from funasr.models.transformer.layer_norm import LayerNorm |
| | | from funasr.models.sanm.positionwise_feed_forward import PositionwiseFeedForwardDecoderSANM |
| | | from funasr.models.transformer.utils.repeat import repeat |
| | | from funasr.models.paraformer.decoder import DecoderLayerSANM, ParaformerSANMDecoder |
| | | from typing import Tuple |
| | | |
| | | from funasr.register import tables |
| | | from funasr.models.scama import utils as myutils |
| | | from funasr.models.transformer.utils.repeat import repeat |
| | | from funasr.models.transformer.layer_norm import LayerNorm |
| | | from funasr.models.transformer.embedding import PositionalEncoding |
| | | from funasr.models.paraformer.decoder import DecoderLayerSANM, ParaformerSANMDecoder |
| | | from funasr.models.sanm.positionwise_feed_forward import PositionwiseFeedForwardDecoderSANM |
| | | from funasr.models.sanm.attention import MultiHeadedAttentionSANMDecoder, MultiHeadedAttentionCrossAtt |
| | | |
| | | class ContextualDecoderLayer(nn.Module): |
| | | |
| | | class ContextualDecoderLayer(torch.nn.Module): |
| | | def __init__( |
| | | self, |
| | | size, |
| | |
| | | self.norm2 = LayerNorm(size) |
| | | if src_attn is not None: |
| | | self.norm3 = LayerNorm(size) |
| | | self.dropout = nn.Dropout(dropout_rate) |
| | | self.dropout = torch.nn.Dropout(dropout_rate) |
| | | self.normalize_before = normalize_before |
| | | self.concat_after = concat_after |
| | | if self.concat_after: |
| | | self.concat_linear1 = nn.Linear(size + size, size) |
| | | self.concat_linear2 = nn.Linear(size + size, size) |
| | | self.concat_linear1 = torch.nn.Linear(size + size, size) |
| | | self.concat_linear2 = torch.nn.Linear(size + size, size) |
| | | |
| | | def forward(self, tgt, tgt_mask, memory, memory_mask, cache=None,): |
| | | # tgt = self.dropout(tgt) |
| | |
| | | return x, tgt_mask, x_self_attn, x_src_attn |
| | | |
| | | |
| | | class ContextualBiasDecoder(nn.Module): |
| | | class ContextualBiasDecoder(torch.nn.Module): |
| | | def __init__( |
| | | self, |
| | | size, |
| | |
| | | self.src_attn = src_attn |
| | | if src_attn is not None: |
| | | self.norm3 = LayerNorm(size) |
| | | self.dropout = nn.Dropout(dropout_rate) |
| | | self.dropout = torch.nn.Dropout(dropout_rate) |
| | | self.normalize_before = normalize_before |
| | | |
| | | def forward(self, tgt, tgt_mask, memory, memory_mask=None, cache=None): |
| | |
| | | concat_after, |
| | | ), |
| | | ) |
| | | self.dropout = nn.Dropout(dropout_rate) |
| | | self.dropout = torch.nn.Dropout(dropout_rate) |
| | | self.bias_decoder = ContextualBiasDecoder( |
| | | size=attention_dim, |
| | | src_attn=MultiHeadedAttentionCrossAtt( |
| | |
| | | #!/usr/bin/env python3 |
| | | # -*- encoding: utf-8 -*- |
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | |
| | | import os |
| | | import re |
| | | import time |
| | | import torch |
| | | import codecs |
| | | import logging |
| | | import tempfile |
| | | import requests |
| | | import numpy as np |
| | | from typing import Dict, Tuple |
| | | from contextlib import contextmanager |
| | | from distutils.version import LooseVersion |
| | | from typing import Dict |
| | | from typing import List |
| | | from typing import Optional |
| | | from typing import Tuple |
| | | from typing import Union |
| | | import tempfile |
| | | import codecs |
| | | import requests |
| | | import re |
| | | import copy |
| | | import torch |
| | | import torch.nn as nn |
| | | import random |
| | | import numpy as np |
| | | import time |
| | | # from funasr.layers.abs_normalize import AbsNormalize |
| | | |
| | | from funasr.register import tables |
| | | from funasr.losses.label_smoothing_loss import ( |
| | | LabelSmoothingLoss, # noqa: H301 |
| | | ) |
| | | # from funasr.models.ctc import CTC |
| | | # from funasr.models.decoder.abs_decoder import AbsDecoder |
| | | # from funasr.models.e2e_asr_common import ErrorCalculator |
| | | # from funasr.models.encoder.abs_encoder import AbsEncoder |
| | | # from funasr.frontends.abs_frontend import AbsFrontend |
| | | # from funasr.models.postencoder.abs_postencoder import AbsPostEncoder |
| | | from funasr.models.paraformer.cif_predictor import mae_loss |
| | | # from funasr.models.preencoder.abs_preencoder import AbsPreEncoder |
| | | # from funasr.models.specaug.abs_specaug import AbsSpecAug |
| | | from funasr.utils import postprocess_utils |
| | | from funasr.metrics.compute_acc import th_accuracy |
| | | from funasr.models.paraformer.model import Paraformer |
| | | from funasr.utils.datadir_writer import DatadirWriter |
| | | from funasr.models.paraformer.search import Hypothesis |
| | | from funasr.train_utils.device_funcs import force_gatherable |
| | | from funasr.models.transformer.utils.add_sos_eos import add_sos_eos |
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask, pad_list |
| | | from funasr.metrics.compute_acc import th_accuracy |
| | | from funasr.train_utils.device_funcs import force_gatherable |
| | | # from funasr.models.base_model import FunASRModel |
| | | # from funasr.models.paraformer.cif_predictor import CifPredictorV3 |
| | | from funasr.models.paraformer.search import Hypothesis |
| | | from funasr.utils.load_utils import load_audio_text_image_video, extract_fbank |
| | | |
| | | |
| | | if LooseVersion(torch.__version__) >= LooseVersion("1.6.0"): |
| | |
| | | @contextmanager |
| | | def autocast(enabled=True): |
| | | yield |
| | | from funasr.utils.load_utils import load_audio_text_image_video, extract_fbank |
| | | from funasr.utils import postprocess_utils |
| | | from funasr.utils.datadir_writer import DatadirWriter |
| | | |
| | | |
| | | from funasr.models.paraformer.model import Paraformer |
| | | |
| | | from funasr.register import tables |
| | | |
| | | @tables.register("model_classes", "ContextualParaformer") |
| | | class ContextualParaformer(Paraformer): |
| | |
| | | decoder_out = torch.log_softmax(decoder_out, dim=-1) |
| | | return decoder_out, ys_pad_lens |
| | | |
| | | def generate(self, |
| | | def inference(self, |
| | | data_in, |
| | | data_lengths=None, |
| | | key: list = None, |
| | |
| | | frontend=None, |
| | | **kwargs, |
| | | ): |
| | | |
| | | # init beamsearch |
| | | is_use_ctc = kwargs.get("decoding_ctc_weight", 0.0) > 0.00001 and self.ctc != None |
| | | is_use_lm = kwargs.get("lm_weight", 0.0) > 0.00001 and kwargs.get("lm_file", None) is not None |
| | |
| | | from typing import Any |
| | | from typing import List |
| | | from typing import Tuple |
| | | from typing import Optional |
| | | #!/usr/bin/env python3 |
| | | # -*- encoding: utf-8 -*- |
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | |
| | | import torch |
| | | import numpy as np |
| | | import torch.nn.functional as F |
| | | |
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask |
| | | from funasr.train_utils.device_funcs import force_gatherable |
| | | from funasr.train_utils.device_funcs import to_device |
| | | import torch |
| | | import torch.nn as nn |
| | | from funasr.models.ct_transformer.utils import split_to_mini_sentence, split_words |
| | | from funasr.utils.load_utils import load_audio_text_image_video |
| | | from contextlib import contextmanager |
| | | from distutils.version import LooseVersion |
| | | from typing import Any, List, Tuple, Optional |
| | | |
| | | from funasr.register import tables |
| | | from funasr.train_utils.device_funcs import to_device |
| | | from funasr.train_utils.device_funcs import force_gatherable |
| | | from funasr.utils.load_utils import load_audio_text_image_video |
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask |
| | | from funasr.models.ct_transformer.utils import split_to_mini_sentence, split_words |
| | | |
| | | |
| | | if LooseVersion(torch.__version__) >= LooseVersion("1.6.0"): |
| | | from torch.cuda.amp import autocast |
| | | else: |
| | | # Nothing to do if torch<1.6.0 |
| | | @contextmanager |
| | | def autocast(enabled=True): |
| | | yield |
| | | |
| | | |
| | | @tables.register("model_classes", "CTTransformer") |
| | | class CTTransformer(nn.Module): |
| | | class CTTransformer(torch.nn.Module): |
| | | """ |
| | | Author: Speech Lab of DAMO Academy, Alibaba Group |
| | | CT-Transformer: Controllable time-delay transformer for real-time punctuation prediction and disfluency detection |
| | |
| | | punc_weight = [1] * punc_size |
| | | |
| | | |
| | | self.embed = nn.Embedding(vocab_size, embed_unit) |
| | | self.embed = torch.nn.Embedding(vocab_size, embed_unit) |
| | | encoder_class = tables.encoder_classes.get(encoder) |
| | | encoder = encoder_class(**encoder_conf) |
| | | |
| | | self.decoder = nn.Linear(att_unit, punc_size) |
| | | self.decoder = torch.nn.Linear(att_unit, punc_size) |
| | | self.encoder = encoder |
| | | self.punc_list = punc_list |
| | | self.punc_weight = punc_weight |
| | |
| | | loss, stats, weight = force_gatherable((loss, stats, ntokens), loss.device) |
| | | return loss, stats, weight |
| | | |
| | | def generate(self, |
| | | def inference(self, |
| | | data_in, |
| | | data_lengths=None, |
| | | key: list = None, |
| | |
| | | #!/usr/bin/env python3 |
| | | # -*- encoding: utf-8 -*- |
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | |
| | | import re |
| | | |
| | | |
| | | def split_to_mini_sentence(words: list, word_limit: int = 20): |
| | | assert word_limit > 1 |
| | |
| | | if length % word_limit > 0: |
| | | sentences.append(words[sentence_len * word_limit:]) |
| | | return sentences |
| | | |
| | | |
| | | |
| | | def split_words(text: str, jieba_usr_dict=None, **kwargs): |
| | | if jieba_usr_dict: |
| | |
| | | #!/usr/bin/env python3 |
| | | # -*- coding: utf-8 -*- |
| | | # -*- encoding: utf-8 -*- |
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | |
| | | # Copyright 2019 Shigeki Karita |
| | | # Apache 2.0 (http://www.apache.org/licenses/LICENSE-2.0) |
| | | |
| | | """Multi-Head Attention layer definition.""" |
| | | |
| | | import math |
| | | |
| | | import numpy |
| | | import torch |
| | | from torch import nn |
| | | import torch.nn.functional as F |
| | | from typing import Optional, Tuple |
| | | |
| | | from funasr.models.sanm.attention import MultiHeadedAttentionSANM |
| | | |
| | | |
| | | |
| | | |
| | | class MultiHeadedAttentionSANMwithMask(MultiHeadedAttentionSANM): |
| | |
| | | from typing import List |
| | | from typing import Optional |
| | | from typing import Sequence |
| | | from typing import Tuple |
| | | from typing import Union |
| | | import logging |
| | | import torch |
| | | import torch.nn as nn |
| | | import torch.nn.functional as F |
| | | from funasr.models.scama.chunk_utilis import overlap_chunk |
| | | import numpy as np |
| | | from funasr.train_utils.device_funcs import to_device |
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask |
| | | from funasr.models.sanm.attention import MultiHeadedAttention |
| | | from funasr.models.ct_transformer_streaming.attention import MultiHeadedAttentionSANMwithMask |
| | | from funasr.models.transformer.embedding import SinusoidalPositionEncoder, StreamSinusoidalPositionEncoder |
| | | from funasr.models.transformer.layer_norm import LayerNorm |
| | | from funasr.models.transformer.utils.multi_layer_conv import Conv1dLinear |
| | | from funasr.models.transformer.utils.multi_layer_conv import MultiLayeredConv1d |
| | | from funasr.models.transformer.positionwise_feed_forward import ( |
| | | PositionwiseFeedForward, # noqa: H301 |
| | | ) |
| | | from funasr.models.transformer.utils.repeat import repeat |
| | | from funasr.models.transformer.utils.subsampling import Conv2dSubsampling |
| | | from funasr.models.transformer.utils.subsampling import Conv2dSubsampling2 |
| | | from funasr.models.transformer.utils.subsampling import Conv2dSubsampling6 |
| | | from funasr.models.transformer.utils.subsampling import Conv2dSubsampling8 |
| | | from funasr.models.transformer.utils.subsampling import TooShortUttError |
| | | from funasr.models.transformer.utils.subsampling import check_short_utt |
| | | from funasr.models.transformer.utils.mask import subsequent_mask, vad_mask |
| | | #!/usr/bin/env python3 |
| | | # -*- encoding: utf-8 -*- |
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | |
| | | from funasr.models.ctc.ctc import CTC |
| | | import torch |
| | | from typing import List, Optional, Tuple |
| | | |
| | | from funasr.register import tables |
| | | from funasr.models.ctc.ctc import CTC |
| | | from funasr.models.transformer.utils.repeat import repeat |
| | | from funasr.models.transformer.layer_norm import LayerNorm |
| | | from funasr.models.sanm.attention import MultiHeadedAttention |
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask |
| | | from funasr.models.transformer.utils.subsampling import check_short_utt |
| | | from funasr.models.transformer.utils.subsampling import TooShortUttError |
| | | from funasr.models.transformer.embedding import SinusoidalPositionEncoder |
| | | from funasr.models.transformer.utils.multi_layer_conv import Conv1dLinear |
| | | from funasr.models.transformer.utils.mask import subsequent_mask, vad_mask |
| | | from funasr.models.transformer.utils.multi_layer_conv import MultiLayeredConv1d |
| | | from funasr.models.transformer.positionwise_feed_forward import PositionwiseFeedForward |
| | | from funasr.models.ct_transformer_streaming.attention import MultiHeadedAttentionSANMwithMask |
| | | from funasr.models.transformer.utils.subsampling import Conv2dSubsampling, Conv2dSubsampling2, Conv2dSubsampling6, Conv2dSubsampling8 |
| | | |
| | | class EncoderLayerSANM(nn.Module): |
| | | |
| | | class EncoderLayerSANM(torch.nn.Module): |
| | | def __init__( |
| | | self, |
| | | in_size, |
| | |
| | | self.feed_forward = feed_forward |
| | | self.norm1 = LayerNorm(in_size) |
| | | self.norm2 = LayerNorm(size) |
| | | self.dropout = nn.Dropout(dropout_rate) |
| | | self.dropout = torch.nn.Dropout(dropout_rate) |
| | | self.in_size = in_size |
| | | self.size = size |
| | | self.normalize_before = normalize_before |
| | | self.concat_after = concat_after |
| | | if self.concat_after: |
| | | self.concat_linear = nn.Linear(size + size, size) |
| | | self.concat_linear = torch.nn.Linear(size + size, size) |
| | | self.stochastic_depth_rate = stochastic_depth_rate |
| | | self.dropout_rate = dropout_rate |
| | | |
| | |
| | | |
| | | |
| | | @tables.register("encoder_classes", "SANMVadEncoder") |
| | | class SANMVadEncoder(nn.Module): |
| | | class SANMVadEncoder(torch.nn.Module): |
| | | """ |
| | | Author: Speech Lab of DAMO Academy, Alibaba Group |
| | | |
| | |
| | | assert 0 < min(interctc_layer_idx) and max(interctc_layer_idx) < num_blocks |
| | | self.interctc_use_conditioning = interctc_use_conditioning |
| | | self.conditioning_layer = None |
| | | self.dropout = nn.Dropout(dropout_rate) |
| | | self.dropout = torch.nn.Dropout(dropout_rate) |
| | | |
| | | def output_size(self) -> int: |
| | | return self._output_size |
| | |
| | | from typing import Any |
| | | from typing import List |
| | | from typing import Tuple |
| | | from typing import Optional |
| | | import numpy as np |
| | | import torch.nn.functional as F |
| | | #!/usr/bin/env python3 |
| | | # -*- encoding: utf-8 -*- |
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | |
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask |
| | | from funasr.train_utils.device_funcs import force_gatherable |
| | | from funasr.train_utils.device_funcs import to_device |
| | | import torch |
| | | import torch.nn as nn |
| | | from funasr.models.ct_transformer.utils import split_to_mini_sentence, split_words |
| | | from funasr.utils.load_utils import load_audio_text_image_video |
| | | from funasr.models.ct_transformer.model import CTTransformer |
| | | import numpy as np |
| | | from contextlib import contextmanager |
| | | from distutils.version import LooseVersion |
| | | |
| | | from funasr.register import tables |
| | | from funasr.train_utils.device_funcs import to_device |
| | | from funasr.models.ct_transformer.model import CTTransformer |
| | | from funasr.utils.load_utils import load_audio_text_image_video |
| | | from funasr.models.ct_transformer.utils import split_to_mini_sentence, split_words |
| | | |
| | | |
| | | if LooseVersion(torch.__version__) >= LooseVersion("1.6.0"): |
| | | from torch.cuda.amp import autocast |
| | | else: |
| | | # Nothing to do if torch<1.6.0 |
| | | @contextmanager |
| | | def autocast(enabled=True): |
| | | yield |
| | | |
| | | |
| | | @tables.register("model_classes", "CTTransformerStreaming") |
| | | class CTTransformerStreaming(CTTransformer): |
| | |
| | | |
| | | def with_vad(self): |
| | | return True |
| | | |
| | | |
| | | |
| | | def generate(self, |
| | | def inference(self, |
| | | data_in, |
| | | data_lengths=None, |
| | | key: list = None, |
| | |
| | | # This source code is licensed under the MIT license found in the |
| | | # LICENSE file in the root directory of this source tree. |
| | | |
| | | from typing import List, Tuple |
| | | from functools import partial |
| | | import torch |
| | | import torch.nn as nn |
| | | import torch.nn.functional as F |
| | | import numpy as np |
| | | import torch.nn as nn |
| | | from functools import partial |
| | | import torch.nn.functional as F |
| | | from typing import Callable, Dict |
| | | |
| | | from typing import Callable, Dict, Optional |
| | | from funasr.models.emotion2vec.fairseq_modules import ( |
| | | LayerNorm, |
| | | SamePad, |
| | | TransposeLast, |
| | | ConvFeatureExtractionModel, |
| | | ) |
| | | |
| | | from funasr.models.emotion2vec.base import ModalitySpecificEncoder, get_alibi_bias |
| | | from funasr.models.emotion2vec.modules import Modality, BlockEncoder, Decoder1d |
| | | |
| | | |
| | | from funasr.models.emotion2vec.base import ModalitySpecificEncoder, get_alibi_bias |
| | | |
| | | |
| | | class AudioEncoder(ModalitySpecificEncoder): |
| | |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | # Modified from https://github.com/ddlBoJack/emotion2vec/tree/main |
| | | |
| | | import logging |
| | | import os |
| | | from functools import partial |
| | | import numpy as np |
| | | |
| | | import torch |
| | | import torch.nn as nn |
| | | import torch.nn.functional as F |
| | | |
| | | |
| | | from funasr.models.emotion2vec.modules import AltBlock |
| | | from funasr.models.emotion2vec.audio import AudioEncoder |
| | | from funasr.utils.load_utils import load_audio_text_image_video, extract_fbank |
| | | |
| | | from omegaconf import OmegaConf |
| | | import time |
| | | |
| | | logger = logging.getLogger(__name__) |
| | | import torch |
| | | import logging |
| | | import numpy as np |
| | | from functools import partial |
| | | from omegaconf import OmegaConf |
| | | import torch.nn.functional as F |
| | | from contextlib import contextmanager |
| | | from distutils.version import LooseVersion |
| | | |
| | | from funasr.register import tables |
| | | from funasr.models.emotion2vec.modules import AltBlock |
| | | from funasr.models.emotion2vec.audio import AudioEncoder |
| | | from funasr.utils.load_utils import load_audio_text_image_video |
| | | |
| | | |
| | | logger = logging.getLogger(__name__) |
| | | if LooseVersion(torch.__version__) >= LooseVersion("1.6.0"): |
| | | from torch.cuda.amp import autocast |
| | | else: |
| | | # Nothing to do if torch<1.6.0 |
| | | @contextmanager |
| | | def autocast(enabled=True): |
| | | yield |
| | | |
| | | |
| | | @tables.register("model_classes", "Emotion2vec") |
| | | class Emotion2vec(nn.Module): |
| | | class Emotion2vec(torch.nn.Module): |
| | | """ |
| | | Author: Ziyang Ma, Zhisheng Zheng, Jiaxin Ye, Jinchao Li, Zhifu Gao, Shiliang Zhang, Xie Chen |
| | | emotion2vec: Self-Supervised Pre-Training for Speech Emotion Representation |
| | |
| | | self.cfg = cfg |
| | | |
| | | make_layer_norm = partial( |
| | | nn.LayerNorm, eps=cfg.get("norm_eps"), elementwise_affine=cfg.get("norm_affine") |
| | | torch.nn.LayerNorm, eps=cfg.get("norm_eps"), elementwise_affine=cfg.get("norm_affine") |
| | | ) |
| | | |
| | | def make_block(drop_path, dim=None, heads=None): |
| | |
| | | ) |
| | | |
| | | self.alibi_biases = {} |
| | | self.modality_encoders = nn.ModuleDict() |
| | | self.modality_encoders = torch.nn.ModuleDict() |
| | | |
| | | enc = AudioEncoder( |
| | | cfg.modalities.audio, |
| | |
| | | self.loss_beta = cfg.get("loss_beta") |
| | | self.loss_scale = cfg.get("loss_scale") |
| | | |
| | | self.dropout_input = nn.Dropout(cfg.get("dropout_input")) |
| | | self.dropout_input = torch.nn.Dropout(cfg.get("dropout_input")) |
| | | |
| | | dpr = np.linspace(cfg.get("start_drop_path_rate"), cfg.get("end_drop_path_rate"), cfg.get("depth")) |
| | | |
| | | self.blocks = nn.ModuleList([make_block(dpr[i]) for i in range(cfg.get("depth"))]) |
| | | self.blocks = torch.nn.ModuleList([make_block(dpr[i]) for i in range(cfg.get("depth"))]) |
| | | |
| | | self.norm = None |
| | | if cfg.get("layer_norm_first"): |
| | |
| | | ) |
| | | return res |
| | | |
| | | def generate(self, |
| | | def inference(self, |
| | | data_in, |
| | | data_lengths=None, |
| | | key: list = None, |
| | |
| | | # LICENSE file in the root directory of this source tree. |
| | | |
| | | import torch |
| | | import torch.nn as nn |
| | | import torch.nn.functional as F |
| | | import numpy as np |
| | | import torch.nn as nn |
| | | from enum import Enum, auto |
| | | import torch.nn.functional as F |
| | | from dataclasses import dataclass |
| | | from funasr.models.emotion2vec.fairseq_modules import ( |
| | | LayerNorm, |
| | |
| | | TransposeLast, |
| | | ) |
| | | |
| | | from enum import Enum, auto |
| | | |
| | | class Modality(Enum): |
| | | AUDIO = auto() |
| | | |
| | | |
| | | |
| | | @dataclass |
| | | class D2vDecoderConfig: |
| | | decoder_dim: int = 384 |
| | |
| | | from itertools import repeat |
| | | import collections.abc |
| | | from functools import partial |
| | | from typing import Optional, Tuple |
| | | import numpy as np |
| | | |
| | | import torch |
| | | import torch.nn as nn |
| | | import torch.nn.functional as F |
| | | import collections.abc |
| | | from itertools import repeat |
| | | from functools import partial |
| | | |
| | | |
| | | def drop_path(x, drop_prob: float = 0., training: bool = False, scale_by_keep: bool = True): |
| | | """Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks). |
| | |
| | | from enum import Enum |
| | | from typing import List, Tuple, Dict, Any |
| | | import logging |
| | | #!/usr/bin/env python3 |
| | | # -*- encoding: utf-8 -*- |
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | |
| | | import os |
| | | import json |
| | | import time |
| | | import math |
| | | import torch |
| | | from torch import nn |
| | | import math |
| | | from typing import Optional |
| | | import time |
| | | from funasr.register import tables |
| | | from funasr.utils.load_utils import load_audio_text_image_video,extract_fbank |
| | | from funasr.utils.datadir_writer import DatadirWriter |
| | | |
| | | from enum import Enum |
| | | from dataclasses import dataclass |
| | | from funasr.register import tables |
| | | from typing import List, Tuple, Dict, Any, Optional |
| | | |
| | | from funasr.utils.datadir_writer import DatadirWriter |
| | | from funasr.utils.load_utils import load_audio_text_image_video,extract_fbank |
| | | |
| | | |
| | | class VadStateMachine(Enum): |
| | | kVadInStateStartPointNotDetected = 1 |
| | | kVadInStateInSpeechSegment = 2 |
| | | kVadInStateEndPointDetected = 3 |
| | | |
| | | |
| | | class FrameState(Enum): |
| | | kFrameStateInvalid = -1 |
| | | kFrameStateSpeech = 1 |
| | | kFrameStateSil = 0 |
| | | |
| | | |
| | | # final voice/unvoice state per frame |
| | | class AudioChangeState(Enum): |
| | |
| | | kChangeStateSil2Speech = 3 |
| | | kChangeStateNoBegin = 4 |
| | | kChangeStateInvalid = 5 |
| | | |
| | | |
| | | class VadDetectMode(Enum): |
| | | kVadSingleUtteranceDetectMode = 0 |
| | |
| | | cache["stats"] = stats |
| | | return cache |
| | | |
| | | def generate(self, |
| | | def inference(self, |
| | | data_in, |
| | | data_lengths=None, |
| | | key: list = None, |
| | |
| | | #!/usr/bin/env python3 |
| | | # -*- encoding: utf-8 -*- |
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | |
| | | import time |
| | | import copy |
| | | import torch |
| | | from torch.cuda.amp import autocast |
| | | from typing import Union, Dict, List, Tuple, Optional |
| | | |
| | | from funasr.register import tables |
| | | from funasr.models.ctc.ctc import CTC |
| | | from funasr.utils import postprocess_utils |
| | | from funasr.utils.datadir_writer import DatadirWriter |
| | | from funasr.models.paraformer.cif_predictor import mae_loss |
| | | from funasr.train_utils.device_funcs import force_gatherable |
| | | from funasr.models.transformer.utils.add_sos_eos import add_sos_eos |
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask |
| | | from funasr.utils.timestamp_tools import ts_prediction_lfr6_standard |
| | | from funasr.utils import postprocess_utils |
| | | from funasr.utils.datadir_writer import DatadirWriter |
| | | from funasr.register import tables |
| | | from funasr.models.ctc.ctc import CTC |
| | | from funasr.utils.load_utils import load_audio_text_image_video, extract_fbank |
| | | |
| | | |
| | | @tables.register("model_classes", "monotonicaligner") |
| | | @tables.register("model_classes", "MonotonicAligner") |
| | | class MonotonicAligner(torch.nn.Module): |
| | | """ |
| | | Author: Speech Lab of DAMO Academy, Alibaba Group |
| | |
| | | |
| | | return encoder_out, encoder_out_lens |
| | | |
| | | def generate(self, |
| | | def inference(self, |
| | | data_in, |
| | | data_lengths=None, |
| | | key: list=None, |
| | |
| | | #!/usr/bin/env python3
|
| | | # -*- encoding: utf-8 -*-
|
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
|
| | | # MIT License (https://opensource.org/licenses/MIT)
|
| | |
|
| | | import torch
|
| | | from torch import nn
|
| | | from torch import Tensor
|
| | | import logging
|
| | | import numpy as np
|
| | | from funasr.train_utils.device_funcs import to_device
|
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask
|
| | | from funasr.models.scama.utils import sequence_mask
|
| | | from typing import Optional, Tuple
|
| | |
|
| | | from funasr.register import tables
|
| | | from funasr.train_utils.device_funcs import to_device
|
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask
|
| | |
|
| | |
|
| | | @tables.register("predictor_classes", "CifPredictor")
|
| | | class CifPredictor(nn.Module):
|
| | | class CifPredictor(torch.nn.Module):
|
| | | def __init__(self, idim, l_order, r_order, threshold=1.0, dropout=0.1, smooth_factor=1.0, noise_threshold=0, tail_threshold=0.45):
|
| | | super().__init__()
|
| | |
|
| | | self.pad = nn.ConstantPad1d((l_order, r_order), 0)
|
| | | self.cif_conv1d = nn.Conv1d(idim, idim, l_order + r_order + 1, groups=idim)
|
| | | self.cif_output = nn.Linear(idim, 1)
|
| | | self.pad = torch.nn.ConstantPad1d((l_order, r_order), 0)
|
| | | self.cif_conv1d = torch.nn.Conv1d(idim, idim, l_order + r_order + 1, groups=idim)
|
| | | self.cif_output = torch.nn.Linear(idim, 1)
|
| | | self.dropout = torch.nn.Dropout(p=dropout)
|
| | | self.threshold = threshold
|
| | | self.smooth_factor = smooth_factor
|
| | |
| | | return predictor_alignments.detach(), predictor_alignments_length.detach()
|
| | |
|
| | | @tables.register("predictor_classes", "CifPredictorV2")
|
| | | class CifPredictorV2(nn.Module):
|
| | | class CifPredictorV2(torch.nn.Module):
|
| | | def __init__(self,
|
| | | idim,
|
| | | l_order,
|
| | |
| | | ):
|
| | | super(CifPredictorV2, self).__init__()
|
| | |
|
| | | self.pad = nn.ConstantPad1d((l_order, r_order), 0)
|
| | | self.cif_conv1d = nn.Conv1d(idim, idim, l_order + r_order + 1)
|
| | | self.cif_output = nn.Linear(idim, 1)
|
| | | self.pad = torch.nn.ConstantPad1d((l_order, r_order), 0)
|
| | | self.cif_conv1d = torch.nn.Conv1d(idim, idim, l_order + r_order + 1)
|
| | | self.cif_output = torch.nn.Linear(idim, 1)
|
| | | self.dropout = torch.nn.Dropout(p=dropout)
|
| | | self.threshold = threshold
|
| | | self.smooth_factor = smooth_factor
|
| | |
| | | return var_dict_torch_update
|
| | |
|
| | |
|
| | | class mae_loss(nn.Module):
|
| | | class mae_loss(torch.nn.Module):
|
| | |
|
| | | def __init__(self, normalize_length=False):
|
| | | super(mae_loss, self).__init__()
|
| | |
| | | from typing import List |
| | | from typing import Tuple |
| | | import logging |
| | | #!/usr/bin/env python3 |
| | | # -*- encoding: utf-8 -*- |
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | |
| | | import torch |
| | | import torch.nn as nn |
| | | import numpy as np |
| | | from typing import List, Tuple |
| | | |
| | | from funasr.register import tables |
| | | from funasr.models.scama import utils as myutils |
| | | from funasr.models.transformer.decoder import BaseTransformerDecoder |
| | | |
| | | from funasr.models.sanm.attention import MultiHeadedAttentionSANMDecoder, MultiHeadedAttentionCrossAtt |
| | | from funasr.models.transformer.layer_norm import LayerNorm |
| | | from funasr.models.sanm.positionwise_feed_forward import PositionwiseFeedForwardDecoderSANM |
| | | from funasr.models.transformer.utils.repeat import repeat |
| | | from funasr.models.transformer.decoder import DecoderLayer |
| | | from funasr.models.transformer.attention import MultiHeadedAttention |
| | | from funasr.models.transformer.layer_norm import LayerNorm |
| | | from funasr.models.transformer.embedding import PositionalEncoding |
| | | from funasr.models.transformer.attention import MultiHeadedAttention |
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask |
| | | from funasr.models.transformer.decoder import BaseTransformerDecoder |
| | | from funasr.models.transformer.positionwise_feed_forward import PositionwiseFeedForward |
| | | from funasr.register import tables |
| | | from funasr.models.sanm.positionwise_feed_forward import PositionwiseFeedForwardDecoderSANM |
| | | from funasr.models.sanm.attention import MultiHeadedAttentionSANMDecoder, MultiHeadedAttentionCrossAtt |
| | | |
| | | class DecoderLayerSANM(nn.Module): |
| | | |
| | | class DecoderLayerSANM(torch.nn.Module): |
| | | """Single decoder layer module. |
| | | |
| | | Args: |
| | |
| | | self.norm2 = LayerNorm(size) |
| | | if src_attn is not None: |
| | | self.norm3 = LayerNorm(size) |
| | | self.dropout = nn.Dropout(dropout_rate) |
| | | self.dropout = torch.nn.Dropout(dropout_rate) |
| | | self.normalize_before = normalize_before |
| | | self.concat_after = concat_after |
| | | if self.concat_after: |
| | | self.concat_linear1 = nn.Linear(size + size, size) |
| | | self.concat_linear2 = nn.Linear(size + size, size) |
| | | self.concat_linear1 = torch.nn.Linear(size + size, size) |
| | | self.concat_linear2 = torch.nn.Linear(size + size, size) |
| | | self.reserve_attn=False |
| | | self.attn_mat = [] |
| | | |
| | |
| | | import os |
| | | import logging |
| | | from typing import Union, Dict, List, Tuple, Optional |
| | | |
| | | import torch |
| | | import torch.nn as nn |
| | | #!/usr/bin/env python3 |
| | | # -*- encoding: utf-8 -*- |
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | |
| | | import time |
| | | |
| | | from funasr.losses.label_smoothing_loss import ( |
| | | LabelSmoothingLoss, # noqa: H301 |
| | | ) |
| | | |
| | | from funasr.models.paraformer.cif_predictor import mae_loss |
| | | |
| | | from funasr.models.transformer.utils.add_sos_eos import add_sos_eos |
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask, pad_list |
| | | from funasr.metrics.compute_acc import th_accuracy |
| | | from funasr.train_utils.device_funcs import force_gatherable |
| | | |
| | | from funasr.models.paraformer.search import Hypothesis |
| | | |
| | | import torch |
| | | import logging |
| | | from torch.cuda.amp import autocast |
| | | from typing import Union, Dict, List, Tuple, Optional |
| | | |
| | | from funasr.utils.load_utils import load_audio_text_image_video, extract_fbank |
| | | from funasr.utils import postprocess_utils |
| | | from funasr.utils.datadir_writer import DatadirWriter |
| | | from funasr.register import tables |
| | | from funasr.models.ctc.ctc import CTC |
| | | from funasr.utils import postprocess_utils |
| | | from funasr.metrics.compute_acc import th_accuracy |
| | | from funasr.utils.datadir_writer import DatadirWriter |
| | | from funasr.models.paraformer.search import Hypothesis |
| | | from funasr.models.paraformer.cif_predictor import mae_loss |
| | | from funasr.train_utils.device_funcs import force_gatherable |
| | | from funasr.losses.label_smoothing_loss import LabelSmoothingLoss |
| | | from funasr.models.transformer.utils.add_sos_eos import add_sos_eos |
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask, pad_list |
| | | from funasr.utils.load_utils import load_audio_text_image_video, extract_fbank |
| | | |
| | | |
| | | @tables.register("model_classes", "Paraformer") |
| | | class Paraformer(nn.Module): |
| | | class Paraformer(torch.nn.Module): |
| | | """ |
| | | Author: Speech Lab of DAMO Academy, Alibaba Group |
| | | Paraformer: Fast and Accurate Parallel Transformer for Non-autoregressive End-to-End Speech Recognition |
| | |
| | | # scorer.to(device=kwargs.get("device", "cpu"), dtype=getattr(torch, kwargs.get("dtype", "float32"))).eval() |
| | | self.beam_search = beam_search |
| | | |
| | | def generate(self, |
| | | def inference(self, |
| | | data_in, |
| | | data_lengths=None, |
| | | key: list=None, |
| | |
| | | from itertools import chain |
| | | import logging |
| | | from typing import Any |
| | | from typing import Dict |
| | | from typing import List |
| | | from typing import NamedTuple |
| | | from typing import Tuple |
| | | from typing import Union |
| | | #!/usr/bin/env python3 |
| | | # -*- encoding: utf-8 -*- |
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | |
| | | import torch |
| | | import logging |
| | | from itertools import chain |
| | | from typing import Any, Dict, List, NamedTuple, Tuple, Union |
| | | |
| | | from funasr.metrics.common import end_detect |
| | | from funasr.models.transformer.scorers.scorer_interface import PartialScorerInterface |
| | | from funasr.models.transformer.scorers.scorer_interface import ScorerInterface |
| | | from funasr.models.transformer.scorers.scorer_interface import PartialScorerInterface, ScorerInterface |
| | | |
| | | |
| | | class Hypothesis(NamedTuple): |
| | | """Hypothesis data type.""" |
| | |
| | | import os |
| | | #!/usr/bin/env python3 |
| | | # -*- encoding: utf-8 -*- |
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | |
| | | import time |
| | | import torch |
| | | import logging |
| | | from typing import Dict, Tuple |
| | | from contextlib import contextmanager |
| | | from distutils.version import LooseVersion |
| | | from typing import Dict |
| | | from typing import List |
| | | from typing import Optional |
| | | from typing import Tuple |
| | | from typing import Union |
| | | import tempfile |
| | | import codecs |
| | | import requests |
| | | import re |
| | | import copy |
| | | import torch |
| | | import torch.nn as nn |
| | | import random |
| | | import numpy as np |
| | | import time |
| | | # from funasr.layers.abs_normalize import AbsNormalize |
| | | from funasr.losses.label_smoothing_loss import ( |
| | | LabelSmoothingLoss, # noqa: H301 |
| | | ) |
| | | |
| | | from funasr.register import tables |
| | | from funasr.models.ctc.ctc import CTC |
| | | from funasr.utils import postprocess_utils |
| | | from funasr.metrics.compute_acc import th_accuracy |
| | | from funasr.utils.datadir_writer import DatadirWriter |
| | | from funasr.models.paraformer.model import Paraformer |
| | | from funasr.models.paraformer.search import Hypothesis |
| | | from funasr.models.paraformer.cif_predictor import mae_loss |
| | | |
| | | from funasr.train_utils.device_funcs import force_gatherable |
| | | from funasr.losses.label_smoothing_loss import LabelSmoothingLoss |
| | | from funasr.models.transformer.utils.add_sos_eos import add_sos_eos |
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask, pad_list |
| | | from funasr.metrics.compute_acc import th_accuracy |
| | | from funasr.train_utils.device_funcs import force_gatherable |
| | | from funasr.utils.load_utils import load_audio_text_image_video, extract_fbank |
| | | |
| | | from funasr.models.paraformer.search import Hypothesis |
| | | |
| | | if LooseVersion(torch.__version__) >= LooseVersion("1.6.0"): |
| | | from torch.cuda.amp import autocast |
| | |
| | | @contextmanager |
| | | def autocast(enabled=True): |
| | | yield |
| | | from funasr.utils.load_utils import load_audio_text_image_video, extract_fbank |
| | | from funasr.utils import postprocess_utils |
| | | from funasr.utils.datadir_writer import DatadirWriter |
| | | from funasr.utils.timestamp_tools import ts_prediction_lfr6_standard |
| | | |
| | | from funasr.models.ctc.ctc import CTC |
| | | from funasr.models.paraformer.model import Paraformer |
| | | |
| | | from funasr.register import tables |
| | | |
| | | @tables.register("model_classes", "ParaformerStreaming") |
| | | class ParaformerStreaming(Paraformer): |
| | |
| | | |
| | | return results |
| | | |
| | | def generate(self, |
| | | def inference(self, |
| | | data_in, |
| | | data_lengths=None, |
| | | key: list = None, |
| | |
| | | #!/usr/bin/env python3 |
| | | # -*- encoding: utf-8 -*- |
| | | # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. |
| | | # MIT License (https://opensource.org/licenses/MIT) |
| | | |
| | | import os |
| | | import re |
| | | import time |
| | |
| | | import tempfile |
| | | import requests |
| | | import numpy as np |
| | | from typing import Dict |
| | | from typing import List |
| | | from typing import Tuple |
| | | from typing import Union |
| | | from typing import Optional |
| | | from typing import Dict, Tuple |
| | | from contextlib import contextmanager |
| | | from distutils.version import LooseVersion |
| | | |
| | | from funasr.losses.label_smoothing_loss import ( |
| | | LabelSmoothingLoss, # noqa: H301 |
| | | ) |
| | | from funasr.register import tables |
| | | from funasr.utils import postprocess_utils |
| | | from funasr.metrics.compute_acc import th_accuracy |
| | | from funasr.models.paraformer.model import Paraformer |
| | | from funasr.utils.datadir_writer import DatadirWriter |
| | | from funasr.models.paraformer.search import Hypothesis |
| | | from funasr.models.paraformer.cif_predictor import mae_loss |
| | | from funasr.train_utils.device_funcs import force_gatherable |
| | | from funasr.models.bicif_paraformer.model import BiCifParaformer |
| | | from funasr.losses.label_smoothing_loss import LabelSmoothingLoss |
| | | from funasr.utils.timestamp_tools import ts_prediction_lfr6_standard |
| | | from funasr.models.transformer.utils.add_sos_eos import add_sos_eos |
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask, pad_list |
| | | from funasr.utils.timestamp_tools import ts_prediction_lfr6_standard |
| | | from funasr.metrics.compute_acc import th_accuracy |
| | | from funasr.train_utils.device_funcs import force_gatherable |
| | | from funasr.models.paraformer.search import Hypothesis |
| | | from funasr.utils.load_utils import load_audio_text_image_video, extract_fbank |
| | | |
| | | |
| | | if LooseVersion(torch.__version__) >= LooseVersion("1.6.0"): |
| | |
| | | @contextmanager |
| | | def autocast(enabled=True): |
| | | yield |
| | | from funasr.utils.load_utils import load_audio_text_image_video, extract_fbank |
| | | from funasr.utils import postprocess_utils |
| | | from funasr.utils.datadir_writer import DatadirWriter |
| | | |
| | | from funasr.models.paraformer.model import Paraformer |
| | | from funasr.models.bicif_paraformer.model import BiCifParaformer |
| | | from funasr.register import tables |
| | | |
| | | |
| | | @tables.register("model_classes", "SeacoParaformer") |
| | |
| | | return ds_alphas, ds_cif_peak, us_alphas, us_peaks |
| | | ''' |
| | | |
| | | def generate(self, |
| | | def inference(self, |
| | | data_in, |
| | | data_lengths=None, |
| | | key: list = None, |