()
e.g. compute_cmvn_stats.py scp:data/train/feats.scp data/train/cmvn.ark # compute global cmvn
e.g. compute_cmvn_stats.py scp:data/train/feats.scp data/train/cmvn.ark # compute global cmvn
e . g . compute_cmvn_stats . py scp : data / train / feats . scp data / train / cmvn . ark # compute global cmvn
def compute_cmvn_stats():
  """
  e.g. compute_cmvn_stats.py scp:data/train/feats.scp data/train/cmvn.ark # compute global cmvn
  """
  args = get_parser().parse_args()

  is_wspecifier = ':' in args.wspecifier_or_wxfilename

  if is_wspecifier:
    if args.spk2utt is not None:
      utt2spk_dict = {}
      with open(args.spk2utt) as f:
        for line in f:
          spk, utts = line.rstrip().split(None, 1)
          for utt in utts.split():
            utt2spk_dict[utt] = spk

      def utt2spk(x):
        return utt2spk_dict[x]
    else:
      logging.info('Performing as utterance CMVN mode')

      def utt2spk(x):
        return x

  else:
    logging.info('Performing as gloabl CMVN model')
    if args.spk2utt is not None:
      logging.warning('spk2utt is not used for global CMVN mode')

    def utt2spk(x):
      return None

  # Calculate stats for each speaker
  counts = {}
  sum_feats = {}
  square_sum_feats = {}

  idx = 0
  for idx, (utt, matrix) in enumerate(KaldiReader(args.rspecifier), 1):
    spk = utt2spk(utt)

    if spk not in counts:
      counts[spk] = 0
      feat_shape = matrix.shape[1:]
      sum_feats[spk] = np.zeros(feat_shape, dtype=np.float)
      square_sum_feats[spk] = np.zeros(feat_shape, dtype=np.float)

    counts[spk] += matrix.shape[0]
    sum_feats[spk] += matrix.sum(axis=0)
    square_sum_feats[spk] += (matrix**2).sum(axis=0)

  assert idx > 0, idx

  cmvn_stats = {}
  for spk in counts:
    feat_shape = sum_feats[spk].shape
    cmvn_shape = (2, feat_shape[0] + 1) + feat_shape[1:]
    _cmvn_stats = np.empty(cmvn_shape, dtype=np.float64)
    _cmvn_stats[0, :-1] = sum_feats[spk]
    _cmvn_stats[1, :-1] = square_sum_feats[spk]

    _cmvn_stats[0, -1] = counts[spk]
    _cmvn_stats[1, -1] = 0.

    cmvn_stats[spk] = _cmvn_stats

  if is_wspecifier:
    with KaldiWriter(args.wspecifier_or_wxfilename) as writer:
      for spk, mat in cmvn_stats.items():
        writer[spk] = mat
  else:
    matrix = cmvn_stats[None]
    kaldiio.save_mat(args.wspecifier_or_wxfilename, matrix)
def compute_cmvn_stats ( ) : args = get_parser ( ) . parse_args ( ) is_wspecifier = ':' in args . wspecifier_or_wxfilename if is_wspecifier : if args . spk2utt is not None : utt2spk_dict = { } with open ( args . spk2utt ) as f : for line in f : spk , utts = line . rstrip ( ) . split ( None , 1 ) for utt in utts . split ( ) : utt2spk_dict [ utt ] = spk def utt2spk ( x ) : return utt2spk_dict [ x ] else : logging . info ( 'Performing as utterance CMVN mode' ) def utt2spk ( x ) : return x else : logging . info ( 'Performing as gloabl CMVN model' ) if args . spk2utt is not None : logging . warning ( 'spk2utt is not used for global CMVN mode' ) def utt2spk ( x ) : return None # Calculate stats for each speaker counts = { } sum_feats = { } square_sum_feats = { } idx = 0 for idx , ( utt , matrix ) in enumerate ( KaldiReader ( args . rspecifier ) , 1 ) : spk = utt2spk ( utt ) if spk not in counts : counts [ spk ] = 0 feat_shape = matrix . shape [ 1 : ] sum_feats [ spk ] = np . zeros ( feat_shape , dtype = np . float ) square_sum_feats [ spk ] = np . zeros ( feat_shape , dtype = np . float ) counts [ spk ] += matrix . shape [ 0 ] sum_feats [ spk ] += matrix . sum ( axis = 0 ) square_sum_feats [ spk ] += ( matrix ** 2 ) . sum ( axis = 0 ) assert idx > 0 , idx cmvn_stats = { } for spk in counts : feat_shape = sum_feats [ spk ] . shape cmvn_shape = ( 2 , feat_shape [ 0 ] + 1 ) + feat_shape [ 1 : ] _cmvn_stats = np . empty ( cmvn_shape , dtype = np . float64 ) _cmvn_stats [ 0 , : - 1 ] = sum_feats [ spk ] _cmvn_stats [ 1 , : - 1 ] = square_sum_feats [ spk ] _cmvn_stats [ 0 , - 1 ] = counts [ spk ] _cmvn_stats [ 1 , - 1 ] = 0. cmvn_stats [ spk ] = _cmvn_stats if is_wspecifier : with KaldiWriter ( args . wspecifier_or_wxfilename ) as writer : for spk , mat in cmvn_stats . items ( ) : writer [ spk ] = mat else : matrix = cmvn_stats [ None ] kaldiio . save_mat ( args . wspecifier_or_wxfilename , matrix )