code: import tensorflow as tf
  ############################################################
  #  Miscellenous Graph Functions
  ############################################################
  
  def trim_zeros_graph(boxes, name='trim_zeros'):
      """Often boxes are represented with matrices of shape [N, 4] and
      are padded with zeros. This removes zero boxes.
      boxes: [N, 4] matrix of boxes.
      non_zeros: [N] a 1D boolean mask identifying the rows to keep
      """
      non_zeros = tf.cast(tf.reduce_sum(tf.abs(boxes), axis=1), tf.bool)
      boxes = tf.boolean_mask(boxes, non_zeros, name=name)
      return boxes, non_zeros
  
  
  def batch_pack_graph(x, counts, num_rows):
      """Picks different number of values from each row
      in x depending on the values in counts.
      """
      outputs = []
      for i in range(num_rows):
          outputs.append(x[i, :counts[i]])
      return tf.concat(outputs, axis=0)
  
  
  def norm_boxes_graph(boxes, shape):
      """Converts boxes from pixel coordinates to normalized coordinates.
      boxes: [..., (y1, x1, y2, x2)] in pixel coordinates
      shape: [..., (height, width)] in pixels
      Note: In pixel coordinates (y2, x2) is outside the box. But in normalized
      coordinates it's inside the box.
      Returns:
          [..., (y1, x1, y2, x2)] in normalized coordinates
      """
      h, w = tf.split(tf.cast(shape, tf.float32), 2)
      scale = tf.concat([h, w, h, w], axis=-1) - tf.constant(1.0)
      shift = tf.constant([0., 0., 1., 1.])
      return tf.divide(boxes - shift, scale)
  
  
  def denorm_boxes_graph(boxes, shape):
      """Converts boxes from normalized coordinates to pixel coordinates.
      boxes: [..., (y1, x1, y2, x2)] in normalized coordinates
      shape: [..., (height, width)] in pixels
      Note: In pixel coordinates (y2, x2) is outside the box. But in normalized
      coordinates it's inside the box.
      Returns:
          [..., (y1, x1, y2, x2)] in pixel coordinates
      """
      h, w = tf.split(tf.cast(shape, tf.float32), 2)
      scale = tf.concat([h, w, h, w], axis=-1) - tf.constant(1.0)
      shift = tf.constant([0., 0., 1., 1.])
      return tf.cast(tf.round(tf.multiply(boxes, scale) + shift), tf.int32)
  
  def overlaps_graph(boxes1, boxes2):
      """Computes IoU overlaps between two sets of boxes.
      boxes1, boxes2: [N, (y1, x1, y2, x2)].
      """
      # 1. Tile boxes2 and repeat boxes1. This allows us to compare
      # every boxes1 against every boxes2 without loops.
      # TF doesn't have an equivalent to np.repeat() so simulate it
      # using tf.tile() and tf.reshape.
      b1 = tf.reshape(tf.tile(tf.expand_dims(boxes1, 1),
                              [1, 1, tf.shape(boxes2)[0]]), [-1, 4])
      b2 = tf.tile(boxes2, [tf.shape(boxes1)[0], 1])
      # 2. Compute intersections
      b1_y1, b1_x1, b1_y2, b1_x2 = tf.split(b1, 4, axis=1)
      b2_y1, b2_x1, b2_y2, b2_x2 = tf.split(b2, 4, axis=1)
      y1 = tf.maximum(b1_y1, b2_y1)
      x1 = tf.maximum(b1_x1, b2_x1)
      y2 = tf.minimum(b1_y2, b2_y2)
      x2 = tf.minimum(b1_x2, b2_x2)
      intersection = tf.maximum(x2 - x1, 0) * tf.maximum(y2 - y1, 0)
      # 3. Compute unions
      b1_area = (b1_y2 - b1_y1) * (b1_x2 - b1_x1)
      b2_area = (b2_y2 - b2_y1) * (b2_x2 - b2_x1)
      union = b1_area + b2_area - intersection
      # 4. Compute IoU and reshape to [boxes1, boxes2]
      iou = intersection / union
      overlaps = tf.reshape(iou, [tf.shape(boxes1)[0], tf.shape(boxes2)[0]])
      return overlaps
  
  def batch_slice(inputs, graph_fn, batch_size, names=None):
      """Splits inputs into slices and feeds each slice to a copy of the given
      computation graph and then combines the results. It allows you to run a
      graph on a batch of inputs even if the graph is written to support one
      instance only.
      inputs: list of tensors. All must have the same first dimension length
      graph_fn: A function that returns a TF tensor that's part of a graph.
      batch_size: number of slices to divide the data into.
      names: If provided, assigns names to the resulting tensors.
      """
      if not isinstance(inputs, list):
          inputs = [inputs]
  
      outputs = []
      for i in range(batch_size):
          inputs_slice = [x[i] for x in inputs]
          output_slice = graph_fn(*inputs_slice)
          if not isinstance(output_slice, (tuple, list)):
              output_slice = [output_slice]
          outputs.append(output_slice)
      # Change outputs from a list of slices where each is
      # a list of outputs to a list of outputs and each has
      # a list of slices
      outputs = list(zip(*outputs))
  
      if names is None:
          names = [None] * len(outputs)
  
      result = [tf.stack(o, axis=0, name=n)
                for o, n in zip(outputs, names)]
      if len(result) == 1:
          result = result[0]
  
      return result
  
  def norm_boxes_graph(boxes, shape):
      """Converts boxes from pixel coordinates to normalized coordinates.
      boxes: [..., (y1, x1, y2, x2)] in pixel coordinates
      shape: [..., (height, width)] in pixels
      Note: In pixel coordinates (y2, x2) is outside the box. But in normalized
      coordinates it's inside the box.
      Returns:
          [..., (y1, x1, y2, x2)] in normalized coordinates
      """
      h, w = tf.split(tf.cast(shape, tf.float32), 2)
      scale = tf.concat([h, w, h, w], axis=-1) - tf.constant(1.0)
      shift = tf.constant([0., 0., 1., 1.])
      return tf.divide(boxes - shift, scale)
  
  
  def denorm_boxes_graph(boxes, shape):
      """Converts boxes from normalized coordinates to pixel coordinates.
      boxes: [..., (y1, x1, y2, x2)] in normalized coordinates
      shape: [..., (height, width)] in pixels
      Note: In pixel coordinates (y2, x2) is outside the box. But in normalized
      coordinates it's inside the box.
      Returns:
          [..., (y1, x1, y2, x2)] in pixel coordinates
      """
      h, w = tf.split(tf.cast(shape, tf.float32), 2)
      scale = tf.concat([h, w, h, w], axis=-1) - tf.constant(1.0)
      shift = tf.constant([0., 0., 1., 1.])
      return tf.cast(tf.round(tf.multiply(boxes, scale) + shift), tf.int32)
apis: tensorflow.divide tensorflow.minimum tensorflow.stack tensorflow.shape tensorflow.multiply tensorflow.expand_dims tensorflow.cast tensorflow.abs tensorflow.concat tensorflow.boolean_mask tensorflow.constant tensorflow.split tensorflow.maximum