code: def probs_to_pianoroll_viterbi(frame_probs, onset_probs, alpha=0.5):
      """Viterbi decoding of frame & onset probabilities to pianoroll.
  
      Args:
        frame_probs: A numpy array (num-frames-by-num-pitches) of frame
          probabilities.
        onset_probs: A numpy array (num-frames-by-num-pitches) of onset
          probabilities.
        alpha: Relative weight of onset and frame loss, a float between 0 and 1.
          With alpha = 0, onset probabilities will be ignored. With alpha = 1, frame
          probabilities will be ignored.
  
      Returns:
        A numpy array (num-frames-by-num-pitches) representing the boolean-valued
        pianoroll.
      """
      n, d = onset_probs.shape
  
      loss_matrix = np.zeros([n, d, 2], dtype=float)
      path_matrix = np.zeros([n, d, 2], dtype=bool)
  
      frame_losses = (1 - alpha) * -np.log(
          np.stack([1 - frame_probs, frame_probs], axis=-1)
      )
      onset_losses = alpha * -np.log(
          np.stack([1 - onset_probs, onset_probs], axis=-1)
      )
  
      loss_matrix[0, :, :] = (
          frame_losses[0, :, :] + onset_losses[0, :, :]
      )
  
      for i in range(1, n):
          transition_loss = np.tile(
              loss_matrix[i - 1, :, :][:, :, np.newaxis], [1, 1, 2]
          )
  
          transition_loss[:, 0, 0] += onset_losses[i, :, 0]
          transition_loss[:, 0, 1] += onset_losses[i, :, 1]
          transition_loss[:, 1, 0] += onset_losses[i, :, 0]
          transition_loss[:, 1, 1] += onset_losses[i, :, 0]
  
          path_matrix[i, :, :] = np.argmin(transition_loss, axis=1)
  
          loss_matrix[i, :, 0] = transition_loss[
              np.arange(d), path_matrix[i, :, 0].astype(int), 0
          ]
          loss_matrix[i, :, 1] = transition_loss[
              np.arange(d), path_matrix[i, :, 1].astype(int), 1
          ]
  
          loss_matrix[i, :, :] += frame_losses[i, :, :]
  
      pianoroll = np.zeros([n, d], dtype=bool)
      pianoroll[n - 1, :] = np.argmin(loss_matrix[n - 1, :, :], axis=-1)
      for i in range(n - 2, -1, -1):
          pianoroll[i, :] = path_matrix[
              i + 1, np.arange(d), pianoroll[i + 1, :].astype(int)
          ]
  
      return pianoroll
  
file_path: magenta/magenta/models/onsets_frames_transcription/infer_util.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import collections
  from magenta.models.onsets_frames_transcription import data
  from note_seq import sequences_lib
  import numpy
label_desc: Use image node_id 1 for calling a global function with example usage: probs_to_pianoroll_viterbi(frame_probs, onset_probs, alpha) and returns: pianoroll