code: def discrete_to_piano_roll(
      categorical, dim, dilation=1, colorize=True
  ):
      """Visualizes discrete sequences as a colorful piano roll."""
      # Create piano roll
      if categorical.dtype == tf.int32:
          piano_roll = tf.one_hot(categorical, dim)
      elif categorical.dtype == tf.float32:
          assert int(categorical.get_shape()[-1]) == dim
          piano_roll = categorical
      else:
          raise NotImplementedError()
      piano_roll = tf.stack([piano_roll] * 3, axis=3)
  
      # Colorize
      if colorize:
          # Create color palette
          hues = np.linspace(0.0, 1.0, num=dim, endpoint=False)
          colors_hsv = np.ones([dim, 3], dtype=np.float32)
          colors_hsv[:, 0] = hues
          colors_hsv[:, 1] = 0.85
          colors_hsv[:, 2] = 0.85
          colors_rgb = tf.image.hsv_to_rgb(colors_hsv) * 255.0
          colors_rgb = tf.reshape(colors_rgb, [1, 1, dim, 3])
  
          piano_roll = tf.multiply(piano_roll, colors_rgb)
      else:
          piano_roll *= 255.0
  
      # Rotate and flip for visual ease
      piano_roll = tf.image.rot90(piano_roll)
  
      # Increase vertical dilation for visual ease
      if dilation > 1:
          old_height = tf.shape(piano_roll)[1]
          old_width = tf.shape(piano_roll)[2]
  
          piano_roll = tf.image.resize_nearest_neighbor(
              piano_roll, [old_height * dilation, old_width]
          )
  
      # Cast to tf.uint8
      piano_roll = tf.cast(
          tf.clip_by_value(piano_roll, 0.0, 255.0), tf.uint8
      )
  
      return piano_roll
  
file_path: magenta/magenta/models/piano_genie/util.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import numpy
  import tensorflow.compat.v1
label_desc: Use image node_id 3 for calling a global function with example usage: discrete_to_piano_roll(categorical, dim, dilation, colorize) and returns: piano_roll