[ASR_with_SpeakerDiarization] Fine-Tuned TinaNet predicts only one speaker
State: closed | Pull request: no
Labels: name: bug

**Describe the bug**

I've fine-tuned TitaNet-Large model 10 Epoch for Korean with 1,000,000 datas and 60 speaker dataset because NGC TitaNet(for English) only predict one token. I've checked loss has decreased not only in training step, but also in validation step(min loss was 0.0079).

However, the fine-tuned model only predict one token, <speaker 0>:

```
{'sample': ['1.8 2.16 speaker_0',
  '2.32 2.52 speaker_0',
  '2.92 3.4 speaker_0',
  '4.16 5.0 speaker_0',
  '7.44 8.08 speaker_0',
  '8.88 9.16 speaker_0',
  '9.48 9.84 speaker_0',
  '12.76 13.08 speaker_0',
  '13.48 13.68 speaker_0',
  '14.76 15.24 speaker_0',
  '15.8 16.400000000000002 speaker_0',
  '16.88 17.04 speaker_0',
  '17.2 18.24 speaker_0',
  '20.4 20.72 speaker_0',
  '20.88 21.24 speaker_0',
  '25.2 25.68 speaker_0',
...]
```

Since label in the manifest file is integer, it seems it is a label-related issue. But there is a few reasons i can assume.

1. As I expected in the above, it is a label-related issue.
2. 1,000,000 data and 60 speakers are too small for fine-tuning TitaNet
3. There are problems in the Configuration yaml file. 
4. The model is overfitted to training data
5. pre-trained Model weight is not initialized

** + Additional Information **

Diarization score is None in the below code(in the ASR_with_SpeakerDiarization.ipynb)

```python
diar_hyp, diar_score = asr_diar_offline.run_diarization(cfg, word_ts_hyp)
```

**Steps/Code to reproduce bug**

### Example of manifest file for training: 
`{"audio_filepath":"\/home\/me\/datas\/speaker_tasks\/datas\/1.Training\/original\/call\/2021-12-16\/3544\/A0210-3544M2010-11020010-06897809.wav","label":3544,"offset":0,"duration":1.38}
`

### YAML Configuration for training
```python
cfg:
  train_ds:
    manifest_filepath: train_speakers.json
    sample_rate: 16000
    labels:
    - 8
    - 191
    - 236
    - 366
    - 423
    - 432
    - 575
    - 624
    - 776
    - 922
    - 1053
    - 1057
    - 1123
    - 1136
    - 1221
    - 1330
    - 1347
    - 1379
    - 1462
    - 1553
    - 1625
    - 1667
    - 1718
    - 1964
    - 1989
    - 2062
    - 2104
    - 2122
    - 2131
    - 2177
    - 2254
    - 2357
    - 2556
    - 2615
    - 2662
    - 2674
    - 2704
    - 2758
    - 2805
    - 2987
    - 3007
    - 3070
    - 3093
    - 3159
    - 3499
    - 3544
    - 3548
    - 3562
    - 3573
    - 3580
    - 3604
    - 3664
    - 3715
    - 3740
    - 3775
    - 3791
    - 3858
    - 3988
    - 4055
    - 9036
    batch_size: 32
    shuffle: true
    is_tarred: false
    tarred_audio_filepaths: null
    tarred_shard_strategy: scatter
    augmentor:
      speed:
        prob: 0.3
        sr: 16000
        resample_type: kaiser_fast
        min_speed_rate: 0.95
        max_speed_rate: 1.05
  validation_ds:
    manifest_filepath: eval_speakers.json
    sample_rate: 16000
    labels: null
    batch_size: 32
    shuffle: false
  model_defaults:
    filters: 1024
    repeat: 3
    dropout: 0.1
    separable: true
    se: true
    se_context_size: -1
    kernel_size_factor: 1.0
  preprocessor:
    _target_: nemo.collections.asr.modules.AudioToMelSpectrogramPreprocessor
    normalize: per_feature
    window_size: 0.025
    sample_rate: 16000
    window_stride: 0.01
    window: hann
    features: 80
    n_fft: 512
    frame_splicing: 1
    dither: 1.0e-05
  spec_augment:
    _target_: nemo.collections.asr.modules.SpectrogramAugmentation
    freq_masks: 0
    freq_width: 4
    time_masks: 0
    time_width: 0.03
  encoder:
    _target_: nemo.collections.asr.modules.ConvASREncoder
    feat_in: 80
    activation: relu
    conv_mask: true
    jasper:
    - filters: 1024
      repeat: 1
      kernel:
      - 3
      stride:
      - 1
      dilation:
      - 1
      dropout: 0.0
      residual: false
      separable: true
      se: true
      se_context_size: -1
    - filters: 1024
      repeat: 3
      kernel:
      - 7
      stride:
      - 1
      dilation:
      - 1
      dropout: 0.1
      residual: true
      separable: true
      se: true
      se_context_size: -1
    - filters: 1024
      repeat: 3
      kernel:
      - 11
      stride:
      - 1
      dilation:
      - 1
      dropout: 0.1
      residual: true
      separable: true
      se: true
      se_context_size: -1
    - filters: 1024
      repeat: 3
      kernel:
      - 15
      stride:
      - 1
      dilation:
      - 1
      dropout: 0.1
      residual: true
      separable: true
      se: true
      se_context_size: -1
    - filters: 3072
      repeat: 1
      kernel:
      - 1
      stride:
      - 1
      dilation:
      - 1
      dropout: 0.0
      residual: false
      separable: true
      se: true
      se_context_size: -1
  decoder:
    _target_: nemo.collections.asr.modules.SpeakerDecoder
    feat_in: 3072
    num_classes: 60
    pool_mode: attention
    emb_sizes: 192
    angular: false
  loss:
    scale: 30
    margin: 0.2
  optim:
    name: sgd
    lr: 0.006
    weight_decay: 0.001
    sched:
      name: CosineAnnealing
      warmup_ratio: 0.1
      min_lr: 0.0
  target: nemo.collections.asr.models.label_models.EncDecSpeakerLabelModel
  nemo_version: 1.12.0
```

### YAML Configuration for prediction
```yaml
# This YAML file is created for all types of offline speaker diarization inference tasks in `<NeMo git root>/example/speaker_tasks/diarization` folder.
# The inference parameters for VAD, speaker embedding extractor, clustering module, MSDD module, ASR decoder are all included in this YAML file. 
# All the keys under `diarizer` key (`vad`, `speaker_embeddings`, `clustering`, `msdd_model`, `asr`) can be selectively used for its own purpose and also can be ignored if the module is not used.
# The configurations in this YAML file is suitable for telephone recordings involving 2~8 speakers in a session and may not show the best performance on the other types of acoustic conditions or dialogues.
# An example line in an input manifest file (`.json` format):
# {"audio_filepath": "/path/to/audio_file", "offset": 0, "duration": null, "label": "infer", "text": "-", "num_speakers": null, "rttm_filepath": "/path/to/rttm/file", "uem_filepath": "/path/to/uem/file"}
name: &name "ClusterDiarizer"

num_workers: 4
sample_rate: 16000
batch_size: 64

diarizer:
  manifest_filepath: '/home/me/codes/NeMo/input_manifest.json'
  out_dir: ???
  oracle_vad: False # If True, uses RTTM files provided in the manifest file to get speech activity (VAD) timestamps
  collar: 0.25 # Collar value for scoring
  ignore_overlap: True # Consider or ignore overlap segments while scoring

  vad:
    model_path: ??? # .nemo local model path or pretrained VAD model name 
    external_vad_manifest: null # This option is provided to use external vad and provide its speech activity labels for speaker embeddings extraction. Only one of model_path or external_vad_manifest should be set

    parameters: # Tuned parameters for CH109 (using the 11 multi-speaker sessions as dev set) 
      window_length_in_sec: 0.15  # Window length in sec for VAD context input 
      shift_length_in_sec: 0.01 # Shift length in sec for generate frame level VAD prediction
      smoothing: "median" # False or type of smoothing method (eg: median)
      overlap: 0.5 # Overlap ratio for overlapped mean/median smoothing filter
      onset: 0.1 # Onset threshold for detecting the beginning and end of a speech 
      offset: 0.1 # Offset threshold for detecting the end of a speech
      pad_onset: 0.1 # Adding durations before each speech segment 
      pad_offset: 0 # Adding durations after each speech segment 
      min_duration_on: 0 # Threshold for small non_speech deletion
      min_duration_off: 0.2 # Threshold for short speech segment deletion
      filter_speech_first: True 

  speaker_embeddings:
    model_path: /'home/insutil/codes/NeMo/nemo_experiments/TitaNet/2022-10-17_19-48-25/checkpoints/TitaNet.nemo' # .nemo local model path or pretrained model name (titanet_large, ecapa_tdnn or speakerverification_speakernet)
    parameters:
      window_length_in_sec: [1.5,1.25,1.0,0.75,0.5] # Window length(s) in sec (floating-point number). either a number or a list. ex) 1.5 or [1.5,1.0,0.5]
      shift_length_in_sec: [0.75,0.625,0.5,0.375,0.25] # Shift length(s) in sec (floating-point number). either a number or a list. ex) 0.75 or [0.75,0.5,0.25]
      multiscale_weights: [1,1,1,1,1] # Weight for each scale. should be null (for single scale) or a list matched with window/shift scale count. ex) [0.33,0.33,0.33]
      save_embeddings: True # If True, save speaker embeddings in pickle format. This should be True if clustering result is used for other models, such as `msdd_model`.
  
  clustering:
    parameters:
      oracle_num_speakers: False # If True, use num of speakers value provided in manifest file.
      max_num_speakers: 2 # Max number of speakers for each recording. If an oracle number of speakers is passed, this value is ignored.
      enhanced_count_thres: 80 # If the number of segments is lower than this number, enhanced speaker counting is activated.
      max_rp_threshold: 0.25 # Determines the range of p-value search: 0 < p <= max_rp_threshold. 
      sparse_search_volume: 30 # The higher the number, the more values will be examined with more time. 
      maj_vote_spk_count: False  # If True, take a majority vote on multiple p-values to estimate the number of speakers.
  
  msdd_model:
    model_path: ??? # .nemo local model path or pretrained model name for multiscale diarization decoder (MSDD)
    parameters:
      use_speaker_model_from_ckpt: True # If True, use speaker embedding model in checkpoint. If False, the provided speaker embedding model in config will be used.
      infer_batch_size: 25 # Batch size for MSDD inference. 
      sigmoid_threshold: [0.7] # Sigmoid threshold for generating binarized speaker labels. The smaller the more generous on detecting overlaps.
      seq_eval_mode: False # If True, use oracle number of speaker and evaluate F1 score for the given speaker sequences. Default is False.
      split_infer: True # If True, break the input audio clip to short sequences and calculate cluster average embeddings for inference.
      diar_window_length: 50 # The length of split short sequence when split_infer is True.
      overlap_infer_spk_limit: 5 # If the estimated number of speakers are larger than this number, overlap speech is not estimated.
  
  asr:
    model_path: /home/me/codes/NeMo/checkpoints/conformer/Conformer-CTC-BPE.nemo # Provide NGC cloud ASR model name. stt_en_conformer_ctc_* models are recommended for diarization purposes.
    parameters:
      asr_based_vad: False # if True, speech segmentation for diarization is based on word-timestamps from ASR inference.
      asr_based_vad_threshold: 0.05 # Threshold (in sec) that caps the gap between two words when generating VAD timestamps using ASR based VAD.
      asr_batch_size: null # Batch size can be dependent on each ASR model. Default batch sizes are applied if set to null.
      lenient_overlap_WDER: True # If true, when a word falls into  speaker-overlappedregions, consider the word as a correctly diarized word.
      decoder_delay_in_sec: null # Native decoder delay. null is recommended to use the default values for each ASR model.
      word_ts_anchor_offset: null # Offset to set a reference point from the start of the word. Recommended range of values is [-0.05  0.2]. 
      word_ts_anchor_pos: "start" # Select which part of the word timestamp we want to use. The options are: 'start', 'end', 'mid'.
      fix_word_ts_with_VAD: False # Fix the word timestamp using VAD output. You must provide a VAD model to use this feature.
      colored_text: False # If True, use colored text to distinguish speakers in the output transcript.
      print_time: True # If True, the start and end time of each speaker turn is printed in the output transcript.
      break_lines: False # If True, the output transcript breaks the line to fix the line width (default is 90 chars)
    
    ctc_decoder_parameters: # Optional beam search decoder (pyctcdecode)
      pretrained_language_model: null # KenLM model file: .arpa model file or .bin binary file.
      beam_width: 32
      alpha: 0.5
      beta: 2.5

    realigning_lm_parameters: # Experimental feature
      arpa_language_model: null # Provide a KenLM language model in .arpa format.
      min_number_of_words: 3 # Min number of words for the left context.
      max_number_of_words: 10 # Max number of words for the right context.
      logprob_diff_threshold: 1.2  # The threshold for the difference between two log probability values from two hypotheses.

### Training code
```python

from omegaconf import OmegaConf

import pytorch_lightning as pl

try:
    from ruamel.yaml import YAML
except ModuleNotFoundError:
    from ruamel_yaml import YAML

from nemo.core.config import hydra_runner
from nemo.utils import logging
from nemo.utils.exp_manager import exp_manager
import nemo.collections.asr as nemo_asr


@hydra_runner(config_path="../../model_cards/", config_name='config')
def __main__(cfg):
    """
    Train a Speaker embedding model.
    Args:
        cfg: DictConfig contains model configurations.
    """

    logging.info(f"hydra config: {OmegaConf.to_yaml(cfg)}")

    trainer = pl.Trainer(**cfg.trainer)
    exp_manager(trainer, cfg.get('exp_manager', None))
    model = nemo_asr.models.label_models.EncDecSpeakerLabelModel(cfg=cfg)

    model.setup_training_data(cfg.model.train_ds)
    model.setup_validation_data(cfg.model.validation_ds)
    model.setup_test_data(cfg.model.test_ds)

    model.maybe_init_from_pretrained_checkpoint(cfg.init_from_pretrained_model)

    trainer.fit(model)

    if hasattr(cfg.model, 'test_ds') and cfg.model.test_ds.manifest_filepath is not None:
        if model.prepare_test(trainer):
            trainer.test(model)

```

prediction: Uses ASR_with_SpeakerDiarization.ipynb notebook in tutorials/speaker_tasks

**Expected behavior**

- Predict 2 speaker, <speaker 0> and <speaker 1>

**Environment overview (please complete the following information)**

- location: Bare-metal
- installation method: downloaded source code from repository. 
- model ckpt method: downloaded from NGC, fine-tuned in the machine.

**Environment details**

- OS: Ubuntu 20.24
- GPU: Nvidia A40 * 2EA for training, A40 1EA for prediction
- pytorch version: 1.11.0
- CUDA Toolkit version: 11.3