(categorical, dim, dilation=1, colorize=True)
return piano_roll
Visualizes discrete sequences as a colorful piano roll.
Visualizes discrete sequences as a colorful piano roll.
Visualizes discrete sequences as a colorful piano roll .
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., 1., 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.
    colors_rgb = tf.reshape(colors_rgb, [1, 1, dim, 3])

    piano_roll = tf.multiply(piano_roll, colors_rgb)
  else:
    piano_roll *= 255.

  # 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., 255.), tf.uint8)

  return piano_roll
def discrete_to_piano_roll ( categorical , dim , dilation = 1 , colorize = True ) : # 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. , 1. , 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. colors_rgb = tf . reshape ( colors_rgb , [ 1 , 1 , dim , 3 ] ) piano_roll = tf . multiply ( piano_roll , colors_rgb ) else : piano_roll *= 255. # 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. , 255. ) , tf . uint8 ) return piano_roll