code: def build_model(spec, length, hparams, is_training):
      """Builds a raw, API-independent onsets & frames."""
  
      if (
          hparams.stop_activation_gradient
          and not hparams.activation_loss
      ):
          raise ValueError(
              "If stop_activation_gradient is true, activation_loss must be true."
          )
  
      with slim.arg_scope(
          [slim.batch_norm, slim.dropout], is_training=is_training
      ):
          with tf.variable_scope("onsets"):
              onset_outputs = acoustic_model(
                  spec,
                  hparams,
                  lstm_units=hparams.onset_lstm_units,
                  lengths=length,
                  is_training=is_training,
              )
              onset_logits = slim.fully_connected(
                  onset_outputs,
                  constants.MIDI_PITCHES,
                  activation_fn=None,
                  scope="onset_logits",
              )
  
          offset_logits = []
          if hparams.offset_network:
              with tf.variable_scope("offsets"):
                  offset_outputs = acoustic_model(
                      spec,
                      hparams,
                      lstm_units=hparams.offset_lstm_units,
                      lengths=length,
                      is_training=is_training,
                  )
                  offset_logits = slim.fully_connected(
                      offset_outputs,
                      constants.MIDI_PITCHES,
                      activation_fn=None,
                      scope="offset_logits",
                  )
  
          with tf.variable_scope("velocity"):
              velocity_outputs = acoustic_model(
                  spec,
                  hparams,
                  lstm_units=hparams.velocity_lstm_units,
                  lengths=length,
                  is_training=is_training,
              )
              velocity_values = slim.fully_connected(
                  velocity_outputs,
                  constants.MIDI_PITCHES,
                  activation_fn=None,
                  scope="onset_velocities",
              )
  
          with tf.variable_scope("frame"):
              if not hparams.share_conv_features:
                  # TODO(eriche): this is broken when hparams.frame_lstm_units > 0
                  activation_outputs = acoustic_model(
                      spec,
                      hparams,
                      lstm_units=hparams.frame_lstm_units,
                      lengths=length,
                      is_training=is_training,
                  )
                  activation_logits = slim.fully_connected(
                      activation_outputs,
                      constants.MIDI_PITCHES,
                      activation_fn=None,
                      scope="activation_logits",
                  )
              else:
                  activation_logits = slim.fully_connected(
                      onset_outputs,
                      constants.MIDI_PITCHES,
                      activation_fn=None,
                      scope="activation_logits",
                  )
  
              logits = []
              if hparams.stop_onset_gradient:
                  logits.append(tf.stop_gradient(onset_logits))
              else:
                  logits.append(onset_logits)
  
              if hparams.stop_activation_gradient:
                  logits.append(tf.stop_gradient(activation_logits))
              else:
                  logits.append(activation_logits)
  
              if hparams.offset_network:
                  if hparams.stop_offset_gradient:
                      logits.append(tf.stop_gradient(offset_logits))
                  else:
                      logits.append(offset_logits)
  
              combined_logits = tf.concat(logits, 2)
  
              if hparams.combined_lstm_units > 0:
                  if hparams.use_tflite_compatible:
                      lstm_layer_builder = lstm_layer_static_for_tflite
                  else:
                      lstm_layer_builder = lstm_layer
  
                  outputs = lstm_layer_builder(
                      tf.sigmoid(combined_logits),
                      hparams.combined_lstm_units,
                      hparams.bidirectional,
                      is_training=is_training,
                      lengths=length if hparams.use_lengths else None,
                      stack_size=hparams.combined_rnn_stack_size,
                      dropout_keep_prob=hparams.combined_rnn_dropout_keep_prob,
                  )
              else:
                  outputs = combined_logits
  
              frame_logits = slim.fully_connected(
                  outputs,
                  constants.MIDI_PITCHES,
                  activation_fn=None,
                  scope="frame_logits",
              )
  
      return frame_logits, onset_logits, offset_logits, velocity_values
  
file_path: magenta/magenta/models/onsets_frames_transcription/model_tpu.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from magenta.contrib import rnn
  from magenta.contrib import training
  from magenta.models.onsets_frames_transcription import constants
  from magenta.models.onsets_frames_transcription import estimator_spec_util
  import tensorflow.compat.v1
  from tensorflow.compat.v1 import estimator
  import tf_slim
label_desc: Use image node_id 5 for calling a global function with example usage: build_model(spec, length, hparams, is_training) and returns: frame_logits,  onset_logits,  offset_logits,  velocity_values