jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def denormalize_boxes(boxes, image_shape):
    """Converts boxes normalized by [height, width] to pixel coordinates.

    Args:
      boxes: a tensor whose last dimension is 4 representing the coordinates
        of boxes in ymin, xmin, ymax, xmax order.
      image_shape: a list of two integers, a two-element vector or a tensor such
        that all but the last dimensions are `broadcastable` to `boxes`. The last
        dimension is 2, which represents [height, width].

    Returns:
      denormalized_boxes: a tensor whose shape is the same as `boxes` representing
        the denormalized boxes.

    Raises:
      ValueError: If the last dimension of boxes is not 4.
    """
    with tf.name_scope('denormalize_boxes'):
        if isinstance(image_shape, (list, tuple)):
            height, width = image_shape
        else:
            image_shape = tf.to_float(image_shape)
            height, width = tf.split(image_shape, 2, axis=-1)

        ymin, xmin, ymax, xmax = tf.split(boxes, 4, axis=-1)
        ymin = ymin * height
        xmin = xmin * width
        ymax = ymax * height
        xmax = xmax * width

        denormalized_boxes = tf.concat([ymin, xmin, ymax, xmax], axis=-1)
        return denormalized_boxes
//  [python]
def denormalize_boxes(boxes, image_shape):
    """Converts boxes normalized by [height, width] to pixel coordinates.

    Args:
      boxes: a tensor whose last dimension is 4 representing the coordinates
        of boxes in ymin, xmin, ymax, xmax order.
      image_shape: a list of two integers, a two-element vector or a tensor such
        that all but the last dimensions are `broadcastable` to `boxes`. The last
        dimension is 2, which represents [height, width].

    Returns:
      denormalized_boxes: a tensor whose shape is the same as `boxes` representing
        the denormalized boxes.

    Raises:
      ValueError: If the last dimension of boxes is not 4.
    """
    with tf.name_scope('denormalize_boxes'):
        if isinstance(image_shape, (list, tuple)):
            height, width = image_shape
        else:
            image_shape = tf.cast(image_shape, dtype=boxes.dtype)
            height, width = tf.split(image_shape, 2, axis=-1)

        ymin, xmin, ymax, xmax = tf.split(boxes, 4, axis=-1)
        ymin = ymin * height
        xmin = xmin * width
        ymax = ymax * height
        xmax = xmax * width

        denormalized_boxes = tf.concat([ymin, xmin, ymax, xmax], axis=-1)
        return denormalized_boxes
//  [python]
def normalize_boxes(boxes, image_shape):
    """Converts boxes to the normalized coordinates.

    Args:
      boxes: a tensor whose last dimension is 4 representing the coordinates
        of boxes in ymin, xmin, ymax, xmax order.
      image_shape: a list of two integers, a two-element vector or a tensor such
        that all but the last dimensions are `broadcastable` to `boxes`. The last
        dimension is 2, which represents [height, width].

    Returns:
      normalized_boxes: a tensor whose shape is the same as `boxes` representing
        the normalized boxes.

    Raises:
      ValueError: If the last dimension of boxes is not 4.
    """
    if boxes.shape[-1] != 4:
        raise ValueError(
            'boxes.shape[-1] is {:d}, but must be 4.'.format(boxes.shape[-1]))

    with tf.name_scope('normalize_boxes'):
        if isinstance(image_shape, (list, tuple)):
            height, width = image_shape
        else:
            image_shape = tf.to_float(image_shape)
            height = image_shape[..., 0:1]
            width = image_shape[..., 1:2]

        ymin = boxes[..., 0:1] / height
        xmin = boxes[..., 1:2] / width
        ymax = boxes[..., 2:3] / height
        xmax = boxes[..., 3:4] / width

        normalized_boxes = tf.concat([ymin, xmin, ymax, xmax], axis=-1)
        return normalized_boxes
//  [python]
def normalize_boxes(boxes, image_shape):
    """Converts boxes to the normalized coordinates.

    Args:
      boxes: a tensor whose last dimension is 4 representing the coordinates
        of boxes in ymin, xmin, ymax, xmax order.
      image_shape: a list of two integers, a two-element vector or a tensor such
        that all but the last dimensions are `broadcastable` to `boxes`. The last
        dimension is 2, which represents [height, width].

    Returns:
      normalized_boxes: a tensor whose shape is the same as `boxes` representing
        the normalized boxes.

    Raises:
      ValueError: If the last dimension of boxes is not 4.
    """
    if boxes.shape[-1] != 4:
        raise ValueError(
            'boxes.shape[-1] is {:d}, but must be 4.'.format(boxes.shape[-1]))

    with tf.name_scope('normalize_boxes'):
        if isinstance(image_shape, (list, tuple)):
            height, width = image_shape
        else:
            image_shape = tf.cast(image_shape, dtype=boxes.dtype)
            height = image_shape[..., 0:1]
            width = image_shape[..., 1:2]

        ymin = boxes[..., 0:1] / height
        xmin = boxes[..., 1:2] / width
        ymax = boxes[..., 2:3] / height
        xmax = boxes[..., 3:4] / width

        normalized_boxes = tf.concat([ymin, xmin, ymax, xmax], axis=-1)
        return normalized_boxes
//  [python]
def dot_product_attention(q, k, v, bias, dropout_rate=0.0):
  """Dot-product attention.

  Args:
    q: Tensor with shape [..., length_q, depth_k].
    k: Tensor with shape [..., length_kv, depth_k]. Leading dimensions must
      match with q.
    v: Tensor with shape [..., length_kv, depth_v] Leading dimensions must
      match with q.
    bias: bias Tensor (see attention_bias())
    dropout_rate: a float.

  Returns:
    Tensor with shape [..., length_q, depth_v].
  """
  logits = tf.matmul(q, k, transpose_b=True)  # [..., length_q, length_kv]
  logits = tf.multiply(logits, 1.0 / math.sqrt(float(get_shape_list(q)[-1])))
  if bias is not None:
    # `attention_mask` = [B, T]
    from_shape = get_shape_list(q)
    if len(from_shape) == 4:
      broadcast_ones = tf.ones([from_shape[0], 1, from_shape[2], 1], tf.float32)
    elif len(from_shape) == 5:
      # from_shape = [B, N, Block_num, block_size, depth]#
      broadcast_ones = tf.ones([from_shape[0], 1, from_shape[2], from_shape[3],
                                1], tf.float32)

    bias = tf.matmul(broadcast_ones,
                     tf.to_float(bias), transpose_b=True)

    # Since attention_mask is 1.0 for positions we want to attend and 0.0 for
    # masked positions, this operation will create a tensor which is 0.0 for
    # positions we want to attend and -10000.0 for masked positions.
    adder = (1.0 - bias) * -10000.0

    # Since we are adding it to the raw scores before the softmax, this is
    # effectively the same as removing these entirely.
    logits += adder
  else:
    adder = 0.0

  attention_probs = tf.nn.softmax(logits, name="attention_probs")
  attention_probs = dropout(attention_probs, dropout_rate)
  return tf.matmul(attention_probs, v)
//  [python]
def dot_product_attention(q, k, v, bias, dropout_rate=0.0):
  """Dot-product attention.

  Args:
    q: Tensor with shape [..., length_q, depth_k].
    k: Tensor with shape [..., length_kv, depth_k]. Leading dimensions must
      match with q.
    v: Tensor with shape [..., length_kv, depth_v] Leading dimensions must
      match with q.
    bias: bias Tensor (see attention_bias())
    dropout_rate: a float.

  Returns:
    Tensor with shape [..., length_q, depth_v].
  """
  logits = tf.matmul(q, k, transpose_b=True)  # [..., length_q, length_kv]
  logits = tf.multiply(logits, 1.0 / math.sqrt(float(get_shape_list(q)[-1])))
  if bias is not None:
    # `attention_mask` = [B, T]
    from_shape = get_shape_list(q)
    if len(from_shape) == 4:
      broadcast_ones = tf.ones([from_shape[0], 1, from_shape[2], 1], tf.float32)
    elif len(from_shape) == 5:
      # from_shape = [B, N, Block_num, block_size, depth]#
      broadcast_ones = tf.ones([from_shape[0], 1, from_shape[2], from_shape[3],
                                1], tf.float32)

    bias = tf.matmul(broadcast_ones,
                     tf.cast(bias, tf.float32), transpose_b=True)

    # Since attention_mask is 1.0 for positions we want to attend and 0.0 for
    # masked positions, this operation will create a tensor which is 0.0 for
    # positions we want to attend and -10000.0 for masked positions.
    adder = (1.0 - bias) * -10000.0

    # Since we are adding it to the raw scores before the softmax, this is
    # effectively the same as removing these entirely.
    logits += adder
  else:
    adder = 0.0

  attention_probs = tf.nn.softmax(logits, name="attention_probs")
  attention_probs = dropout(attention_probs, dropout_rate)
  return tf.matmul(attention_probs, v)
//  [python]
def clip_boxes(boxes, image_shape):
    """Clips boxes to image boundaries.

    Args:
      boxes: a tensor whose last dimension is 4 representing the coordinates
        of boxes in ymin, xmin, ymax, xmax order.
      image_shape: a list of two integers, a two-element vector or a tensor such
        that all but the last dimensions are `broadcastable` to `boxes`. The last
        dimension is 2, which represents [height, width].

    Returns:
      clipped_boxes: a tensor whose shape is the same as `boxes` representing the
        clipped boxes.

    Raises:
      ValueError: If the last dimension of boxes is not 4.
    """
    if boxes.shape[-1] != 4:
        raise ValueError(
            'boxes.shape[-1] is {:d}, but must be 4.'.format(boxes.shape[-1]))

    with tf.name_scope('clip_boxes'):
        if isinstance(image_shape, (list, tuple)):
            height, width = image_shape
        else:
            image_shape = tf.to_float(image_shape)
            height = image_shape[..., 0:1]
            width = image_shape[..., 1:2]

        ymin = boxes[..., 0:1]
        xmin = boxes[..., 1:2]
        ymax = boxes[..., 2:3]
        xmax = boxes[..., 3:4]

        clipped_ymin = tf.maximum(tf.minimum(ymin, height - 1.0), 0.0)
        clipped_ymax = tf.maximum(tf.minimum(ymax, height - 1.0), 0.0)
        clipped_xmin = tf.maximum(tf.minimum(xmin, width - 1.0), 0.0)
        clipped_xmax = tf.maximum(tf.minimum(xmax, width - 1.0), 0.0)

        clipped_boxes = tf.concat(
            [clipped_ymin, clipped_xmin, clipped_ymax, clipped_xmax],
            axis=-1)
        return clipped_boxes
//  [python]
def clip_boxes(boxes, image_shape):
    """Clips boxes to image boundaries.

    Args:
      boxes: a tensor whose last dimension is 4 representing the coordinates
        of boxes in ymin, xmin, ymax, xmax order.
      image_shape: a list of two integers, a two-element vector or a tensor such
        that all but the last dimensions are `broadcastable` to `boxes`. The last
        dimension is 2, which represents [height, width].

    Returns:
      clipped_boxes: a tensor whose shape is the same as `boxes` representing the
        clipped boxes.

    Raises:
      ValueError: If the last dimension of boxes is not 4.
    """
    if boxes.shape[-1] != 4:
        raise ValueError(
            'boxes.shape[-1] is {:d}, but must be 4.'.format(boxes.shape[-1]))

    with tf.name_scope('clip_boxes'):
        if isinstance(image_shape, (list, tuple)):
            height, width = image_shape
        else:
            image_shape = tf.cast(image_shape, dtype=boxes.dtype)
            height = image_shape[..., 0:1]
            width = image_shape[..., 1:2]

        ymin = boxes[..., 0:1]
        xmin = boxes[..., 1:2]
        ymax = boxes[..., 2:3]
        xmax = boxes[..., 3:4]

        clipped_ymin = tf.maximum(tf.minimum(ymin, height - 1.0), 0.0)
        clipped_ymax = tf.maximum(tf.minimum(ymax, height - 1.0), 0.0)
        clipped_xmin = tf.maximum(tf.minimum(xmin, width - 1.0), 0.0)
        clipped_xmax = tf.maximum(tf.minimum(xmax, width - 1.0), 0.0)

        clipped_boxes = tf.concat(
            [clipped_ymin, clipped_xmin, clipped_ymax, clipped_xmax],
            axis=-1)
        return clipped_boxes
//  [python]
def encode_boxes(boxes, anchors, weights=None):
    """Encode boxes to targets.

    Args:
      boxes: a tensor whose last dimension is 4 representing the coordinates
        of boxes in ymin, xmin, ymax, xmax order.
      anchors: a tensor whose shape is the same as, or `broadcastable` to `boxes`,
        representing the coordinates of anchors in ymin, xmin, ymax, xmax order.
      weights: None or a list of four float numbers used to scale coordinates.

    Returns:
      encoded_boxes: a tensor whose shape is the same as `boxes` representing the
        encoded box targets.

    Raises:
      ValueError: If the last dimension of boxes is not 4.
    """
    if boxes.shape[-1] != 4:
        raise ValueError(
            'boxes.shape[-1] is {:d}, but must be 4.'.format(boxes.shape[-1]))

    with tf.name_scope('encode_boxes'):
        boxes = tf.to_float(boxes, dtype=anchors.dtype)
        ymin = boxes[..., 0:1]
        xmin = boxes[..., 1:2]
        ymax = boxes[..., 2:3]
        xmax = boxes[..., 3:4]
        box_h = ymax - ymin + 1.0
        box_w = xmax - xmin + 1.0
        box_yc = ymin + 0.5 * box_h
        box_xc = xmin + 0.5 * box_w

        anchor_ymin = anchors[..., 0:1]
        anchor_xmin = anchors[..., 1:2]
        anchor_ymax = anchors[..., 2:3]
        anchor_xmax = anchors[..., 3:4]
        anchor_h = anchor_ymax - anchor_ymin + 1.0
        anchor_w = anchor_xmax - anchor_xmin + 1.0
        anchor_yc = anchor_ymin + 0.5 * anchor_h
        anchor_xc = anchor_xmin + 0.5 * anchor_w

        encoded_dy = (box_yc - anchor_yc) / anchor_h
        encoded_dx = (box_xc - anchor_xc) / anchor_w
        encoded_dh = tf.math.log(box_h / anchor_h)
        encoded_dw = tf.math.log(box_w / anchor_w)
        if weights:
            encoded_dy *= weights[0]
            encoded_dx *= weights[1]
            encoded_dh *= weights[2]
            encoded_dw *= weights[3]

        encoded_boxes = tf.concat(
            [encoded_dy, encoded_dx, encoded_dh, encoded_dw],
            axis=-1)
        return encoded_boxes
//  [python]
def encode_boxes(boxes, anchors, weights=None):
    """Encode boxes to targets.

    Args:
      boxes: a tensor whose last dimension is 4 representing the coordinates
        of boxes in ymin, xmin, ymax, xmax order.
      anchors: a tensor whose shape is the same as, or `broadcastable` to `boxes`,
        representing the coordinates of anchors in ymin, xmin, ymax, xmax order.
      weights: None or a list of four float numbers used to scale coordinates.

    Returns:
      encoded_boxes: a tensor whose shape is the same as `boxes` representing the
        encoded box targets.

    Raises:
      ValueError: If the last dimension of boxes is not 4.
    """
    if boxes.shape[-1] != 4:
        raise ValueError(
            'boxes.shape[-1] is {:d}, but must be 4.'.format(boxes.shape[-1]))

    with tf.name_scope('encode_boxes'):
        boxes = tf.cast(boxes, dtype=anchors.dtype)
        ymin = boxes[..., 0:1]
        xmin = boxes[..., 1:2]
        ymax = boxes[..., 2:3]
        xmax = boxes[..., 3:4]
        box_h = ymax - ymin + 1.0
        box_w = xmax - xmin + 1.0
        box_yc = ymin + 0.5 * box_h
        box_xc = xmin + 0.5 * box_w

        anchor_ymin = anchors[..., 0:1]
        anchor_xmin = anchors[..., 1:2]
        anchor_ymax = anchors[..., 2:3]
        anchor_xmax = anchors[..., 3:4]
        anchor_h = anchor_ymax - anchor_ymin + 1.0
        anchor_w = anchor_xmax - anchor_xmin + 1.0
        anchor_yc = anchor_ymin + 0.5 * anchor_h
        anchor_xc = anchor_xmin + 0.5 * anchor_w

        encoded_dy = (box_yc - anchor_yc) / anchor_h
        encoded_dx = (box_xc - anchor_xc) / anchor_w
        encoded_dh = tf.math.log(box_h / anchor_h)
        encoded_dw = tf.math.log(box_w / anchor_w)
        if weights:
            encoded_dy *= weights[0]
            encoded_dx *= weights[1]
            encoded_dh *= weights[2]
            encoded_dw *= weights[3]

        encoded_boxes = tf.concat(
            [encoded_dy, encoded_dx, encoded_dh, encoded_dw],
            axis=-1)
        return encoded_boxes
//  [python]
def filter_boxes_by_scores(boxes, scores, min_score_threshold):
    """Filter and remove boxes whose scores are smaller than the threshold.

    Args:
      boxes: a tensor whose last dimension is 4 representing the
        coordinates of boxes in ymin, xmin, ymax, xmax order.
      scores: a tensor whose shape is the same as tf.shape(boxes)[:-1]
        representing the original scores of the boxes.
      min_score_threshold: a float representing the minimal box score threshold.
        Boxes whose score are smaller than it will be filtered out.

    Returns:
      filtered_boxes: a tensor whose shape is the same as `boxes` but with
        the position of the filtered boxes are filled with 0.
      filtered_scores: a tensor whose shape is the same as 'scores' but with
        the
    """
    if boxes.shape[-1] != 4:
        raise ValueError(
            'boxes.shape[1] is {:d}, but must be 4.'.format(boxes.shape[-1]))

    with tf.name_scope('filter_boxes_by_scores'):
        filtered_mask = tf.greater(scores, min_score_threshold)
        filtered_scores = tf.where(filtered_mask, scores, tf.zeros_like(scores))
        filtered_boxes = tf.to_float(
            tf.expand_dims(filtered_mask, axis=-1)) * boxes

        return filtered_boxes, filtered_scores
//  [python]
def filter_boxes_by_scores(boxes, scores, min_score_threshold):
    """Filter and remove boxes whose scores are smaller than the threshold.

    Args:
      boxes: a tensor whose last dimension is 4 representing the
        coordinates of boxes in ymin, xmin, ymax, xmax order.
      scores: a tensor whose shape is the same as tf.shape(boxes)[:-1]
        representing the original scores of the boxes.
      min_score_threshold: a float representing the minimal box score threshold.
        Boxes whose score are smaller than it will be filtered out.

    Returns:
      filtered_boxes: a tensor whose shape is the same as `boxes` but with
        the position of the filtered boxes are filled with 0.
      filtered_scores: a tensor whose shape is the same as 'scores' but with
        the
    """
    if boxes.shape[-1] != 4:
        raise ValueError(
            'boxes.shape[1] is {:d}, but must be 4.'.format(boxes.shape[-1]))

    with tf.name_scope('filter_boxes_by_scores'):
        filtered_mask = tf.greater(scores, min_score_threshold)
        filtered_scores = tf.where(filtered_mask, scores, tf.zeros_like(scores))
        filtered_boxes = tf.cast(
            tf.expand_dims(filtered_mask, axis=-1), dtype=boxes.dtype) * boxes

        return filtered_boxes, filtered_scores
//  [python]
def __call__(self,x):
        return tf.cast(tf.equal(x,0),x.dtype)
//  [python]
def tf_imread(file_path):
    # tf.print('Reading file:', file_path)
    img = tf.io.read_file(file_path)
    # img = tf.image.decode_jpeg(img, channels=3)  # [0, 255]
    img = tf.image.decode_image(img, channels=3, expand_animations=False)  # [0, 255]
    img = t.cast(img, "float32")  # [0, 255]
    return img
//  [python]
def tf_imread(file_path):
    # tf.print('Reading file:', file_path)
    img = tf.io.read_file(file_path)
    # img = tf.image.decode_jpeg(img, channels=3)  # [0, 255]
    img = tf.image.decode_image(img, channels=3, expand_animations=False)  # [0, 255]
    img = tf.cast(img, "float32")  # [0, 255]
    return img
//  [python]
def decode_boxes(encoded_boxes, anchors, weights=None):
    """Decode boxes.

    Args:
      encoded_boxes: a tensor whose last dimension is 4 representing the
        coordinates of encoded boxes in ymin, xmin, ymax, xmax order.
      anchors: a tensor whose shape is the same as, or `broadcastable` to `boxes`,
        representing the coordinates of anchors in ymin, xmin, ymax, xmax order.
      weights: None or a list of four float numbers used to scale coordinates.

    Returns:
      encoded_boxes: a tensor whose shape is the same as `boxes` representing the
        decoded box targets.
    """
    if encoded_boxes.shape[-1] != 4:
        raise ValueError(
            'encoded_boxes.shape[-1] is {:d}, but must be 4.'
                .format(encoded_boxes.shape[-1]))

    with tf.name_scope('decode_boxes'):
        encoded_boxes = tf.to_float(encoded_boxes)
        dy = encoded_boxes[..., 0:1]
        dx = encoded_boxes[..., 1:2]
        dh = encoded_boxes[..., 2:3]
        dw = encoded_boxes[..., 3:4]
        if weights:
            dy /= weights[0]
            dx /= weights[1]
            dh /= weights[2]
            dw /= weights[3]
        dh = tf.minimum(dh, BBOX_XFORM_CLIP)
        dw = tf.minimum(dw, BBOX_XFORM_CLIP)

        anchor_ymin = anchors[..., 0:1]
        anchor_xmin = anchors[..., 1:2]
        anchor_ymax = anchors[..., 2:3]
        anchor_xmax = anchors[..., 3:4]
        anchor_h = anchor_ymax - anchor_ymin + 1.0
        anchor_w = anchor_xmax - anchor_xmin + 1.0
        anchor_yc = anchor_ymin + 0.5 * anchor_h
        anchor_xc = anchor_xmin + 0.5 * anchor_w

        decoded_boxes_yc = dy * anchor_h + anchor_yc
        decoded_boxes_xc = dx * anchor_w + anchor_xc
        decoded_boxes_h = tf.exp(dh) * anchor_h
        decoded_boxes_w = tf.exp(dw) * anchor_w

        decoded_boxes_ymin = decoded_boxes_yc - 0.5 * decoded_boxes_h
        decoded_boxes_xmin = decoded_boxes_xc - 0.5 * decoded_boxes_w
        decoded_boxes_ymax = decoded_boxes_ymin + decoded_boxes_h - 1.0
        decoded_boxes_xmax = decoded_boxes_xmin + decoded_boxes_w - 1.0

        decoded_boxes = tf.concat(
            [decoded_boxes_ymin, decoded_boxes_xmin,
             decoded_boxes_ymax, decoded_boxes_xmax],
            axis=-1)
        return decoded_boxes
//  [python]
def decode_boxes(encoded_boxes, anchors, weights=None):
    """Decode boxes.

    Args:
      encoded_boxes: a tensor whose last dimension is 4 representing the
        coordinates of encoded boxes in ymin, xmin, ymax, xmax order.
      anchors: a tensor whose shape is the same as, or `broadcastable` to `boxes`,
        representing the coordinates of anchors in ymin, xmin, ymax, xmax order.
      weights: None or a list of four float numbers used to scale coordinates.

    Returns:
      encoded_boxes: a tensor whose shape is the same as `boxes` representing the
        decoded box targets.
    """
    if encoded_boxes.shape[-1] != 4:
        raise ValueError(
            'encoded_boxes.shape[-1] is {:d}, but must be 4.'
                .format(encoded_boxes.shape[-1]))

    with tf.name_scope('decode_boxes'):
        encoded_boxes = tf.cast(encoded_boxes, dtype=anchors.dtype)
        dy = encoded_boxes[..., 0:1]
        dx = encoded_boxes[..., 1:2]
        dh = encoded_boxes[..., 2:3]
        dw = encoded_boxes[..., 3:4]
        if weights:
            dy /= weights[0]
            dx /= weights[1]
            dh /= weights[2]
            dw /= weights[3]
        dh = tf.minimum(dh, BBOX_XFORM_CLIP)
        dw = tf.minimum(dw, BBOX_XFORM_CLIP)

        anchor_ymin = anchors[..., 0:1]
        anchor_xmin = anchors[..., 1:2]
        anchor_ymax = anchors[..., 2:3]
        anchor_xmax = anchors[..., 3:4]
        anchor_h = anchor_ymax - anchor_ymin + 1.0
        anchor_w = anchor_xmax - anchor_xmin + 1.0
        anchor_yc = anchor_ymin + 0.5 * anchor_h
        anchor_xc = anchor_xmin + 0.5 * anchor_w

        decoded_boxes_yc = dy * anchor_h + anchor_yc
        decoded_boxes_xc = dx * anchor_w + anchor_xc
        decoded_boxes_h = tf.exp(dh) * anchor_h
        decoded_boxes_w = tf.exp(dw) * anchor_w

        decoded_boxes_ymin = decoded_boxes_yc - 0.5 * decoded_boxes_h
        decoded_boxes_xmin = decoded_boxes_xc - 0.5 * decoded_boxes_w
        decoded_boxes_ymax = decoded_boxes_ymin + decoded_boxes_h - 1.0
        decoded_boxes_xmax = decoded_boxes_xmin + decoded_boxes_w - 1.0

        decoded_boxes = tf.concat(
            [decoded_boxes_ymin, decoded_boxes_xmin,
             decoded_boxes_ymax, decoded_boxes_xmax],
            axis=-1)
        return decoded_boxes
//  [python]
def testResourcesWithCond(self):

    def create_v1_saved_model():
      saved_model_dir = os.path.join(self.get_temp_dir(), 'resources_with_cond')
      with tf.Graph().as_default():
        with tf.compat.v1.Session() as sess:
          in_tensor = tf.compat.v1.placeholder(
              shape=[1], dtype=tf.float32, name='input')

          def body(i, arr):
            n = tf.raw_ops.StackPushV2(
                handle=arr, elem=tf.cast(i, dtype=tf.float32))
            return n, arr

          arr = tf.raw_ops.StackV2(max_size=10, elem_type=tf.float32)
          n, result_arr = tf.cond(in_tensor < 10, lambda: body(0, arr),
                                  lambda: body(1, arr))

          with ops.control_dependencies([result_arr, n]):
            out_tensor = tf.raw_ops.StackPopV2(
                handle=result_arr, elem_type=tf.float32)

          inputs = {'x': in_tensor}
          outputs = {'a': out_tensor}
          saved_model.simple_save(sess, saved_model_dir, inputs, outputs)
      return saved_model_dir

    saved_model_dir = create_v1_saved_model()

    converter = lite.TFLiteConverterV2.from_saved_model(saved_model_dir)
    converter.target_spec.supported_ops = [
        tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS
    ]
    tflite_model = converter.convert()
    self.assertIsNotNone(tflite_model)

    # Check values from converted model.
    interpreter = Interpreter(model_content=tflite_model)
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()

    interpreter.allocate_tensors()

    input_data = np.array([1.0], dtype=np.float32)
    interpreter.set_tensor(input_details[0]['index'], input_data)

    interpreter.invoke()
    actual_value = interpreter.get_tensor(output_details[0]['index'])
    self.assertEqual(0.0, actual_value)
//  [python]
def testResourcesWithWhile(self):

    def create_v1_saved_model():
      saved_model_dir = os.path.join(self.get_temp_dir(),
                                     'resources_with_while')
      with tf.Graph().as_default():
        with tf.compat.v1.Session() as sess:
          in_tensor = tf.compat.v1.placeholder(
              shape=[1], dtype=tf.float32, name='input')

          def cond(i, arr, m):
            del arr
            del m
            return i < 10

          def body(i, arr, m):
            del m
            n = tf.raw_ops.StackPushV2(
                handle=arr, elem=tf.cast(i, dtype=tf.float32))
            return i + 1, arr, n

          arr = tf.raw_ops.StackV2(max_size=10, elem_type=tf.float32)
          _, result_arr, n = tf.while_loop(cond, body, [0, arr, 0.0])

          with ops.control_dependencies([result_arr, n]):
            out_tensor = tf.raw_ops.StackPopV2(
                handle=result_arr, elem_type=tf.float32)

          inputs = {'x': in_tensor}
          outputs = {'a': out_tensor}
          saved_model.simple_save(sess, saved_model_dir, inputs, outputs)
      return saved_model_dir

    saved_model_dir = create_v1_saved_model()

    converter = lite.TFLiteConverterV2.from_saved_model(saved_model_dir)
    converter.target_spec.supported_ops = [
        tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS
    ]
    tflite_model = converter.convert()
    self.assertIsNotNone(tflite_model)

    # Check values from converted model.
    interpreter = Interpreter(model_content=tflite_model)
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()

    interpreter.allocate_tensors()

    input_data = np.array([1.0], dtype=np.float32)
    interpreter.set_tensor(input_details[0]['index'], input_data)

    interpreter.invoke()
    actual_value = interpreter.get_tensor(output_details[0]['index'])
    self.assertEqual(9.0, actual_value)
//  [python]
def mixup(images, labels, token_label=None, alpha=0.4, min_mix_weight=0):
    batch_size = tf.shape(images)[0]
    mix_weight = sample_beta_distribution([batch_size], alpha, alpha)
    mix_weight = tf.maximum(mix_weight, 1.0 - mix_weight)

    if min_mix_weight > 0:
        mix_weight = tf.where(mix_weight > 1 - min_mix_weight, tf.ones_like(mix_weight), mix_weight)

    label_mix_weight = tf.expand_dims(mix_weight, -1)
    img_mix_weight = tf.reshape(mix_weight, [batch_size, 1, 1, 1])

    labels = labels
    shuffle_index = tf.random.shuffle(tf.range(batch_size))
    images = images * img_mix_weight + tf.gather(images, shuffle_index) * (1.0 - img_mix_weight)
    labels = labels * label_mix_weight + tf.gather(labels, shuffle_index) * (1 - label_mix_weight)
    if token_label is None:
        return images, labels
    else:
        token_label = token_label * img_mix_weight + tf.gather(token_label, shuffle_index) * (1 - img_mix_weight)
        return images, labels, token_label
//  [python]
def mixup(images, labels, token_label=None, alpha=0.4, min_mix_weight=0):
    # mix_weight = tfp.distributions.Beta(alpha, alpha).sample([batch_size, 1])
    batch_size = tf.shape(images)[0]
    mix_weight = sample_beta_distribution([batch_size], alpha, alpha)
    mix_weight = tf.maximum(mix_weight, 1.0 - mix_weight)

    # For min_mix_weight=0.1, regard values with `> 0.9` as no mixup, this probability is near `1 - alpha`
    # alpha: no_mixup --> {0.1: 0.8128, 0.2: 0.6736, 0.4: 0.4793, 0.6: 0.3521, 0.8: 0.2636, 1.0: 0.2000}
    if min_mix_weight > 0:
        mix_weight = tf.where(mix_weight > 1 - min_mix_weight, tf.ones_like(mix_weight), mix_weight)

    label_mix_weight = tf.cast(tf.expand_dims(mix_weight, -1), "float32")
    img_mix_weight = tf.cast(tf.reshape(mix_weight, [batch_size, 1, 1, 1]), images.dtype)

    labels = tf.cast(labels, "float32")
    # images = images * img_mix_weight + images[::-1] * (1.0 - img_mix_weight)
    # labels = labels * label_mix_weight + labels[::-1] * (1 - label_mix_weight)
    shuffle_index = tf.random.shuffle(tf.range(batch_size))
    images = images * img_mix_weight + tf.gather(images, shuffle_index) * (1.0 - img_mix_weight)
    labels = labels * label_mix_weight + tf.gather(labels, shuffle_index) * (1 - label_mix_weight)
    if token_label is None:
        return images, labels
    else:
        # token_label shape `[batch, path_height, patch_width, one_hot_labels]`
        token_label = token_label * img_mix_weight + tf.gather(token_label, shuffle_index) * (1 - img_mix_weight)
        return images, labels, token_label
//  [python]
def random_crop_fraction(size, scale=(0.08, 1.0), ratio=(0.75, 1.3333333), log_distribute=True, compute_dtype="float32"):
    height, width = t.cast(size[0], dtype=compute_dtype), t.cast(size[1], dtype=compute_dtype)
    area = height * width
    scale_max = t.minimum(t.minimum(height * height * ratio[1] / area, width * width / ratio[0] / area), scale[1])
    target_area = tf.random.uniform((), scale[0], scale_max, dtype=compute_dtype) * area

    ratio_min = t.maximum(target_area / (height * height), ratio[0])
    ratio_max = t.minimum(width * width / target_area, ratio[1])
    if log_distribute:  # More likely to select a smaller value
        log_min, log_max = t.math.log(ratio_min), t.math.log(ratio_max)
        aspect_ratio = tf.random.uniform((), log_min, log_max, dtype=compute_dtype)
        aspect_ratio = t.math.exp(aspect_ratio)
    else:
        aspect_ratio = tf.random.uniform((), ratio_min, ratio_max, dtype=compute_dtype)

    ww_crop = t.cast(t.math.floor(t.sqrt(target_area * aspect_ratio)), "int32")
    hh_crop = t.cast(t.math.floor(t.sqrt(target_area / aspect_ratio)), "int32")
    # tf.print(">>>> height, width, hh_crop, ww_crop, hh_fraction_min, hh_fraction_max:", height, width, hh_crop, ww_crop, hh_fraction_min, hh_fraction_max)
    # return hh_crop, ww_crop, target_area, hh_fraction_min, hh_fraction_max, hh_fraction
    return hh_crop, ww_crop
//  [python]
def random_crop_fraction(size, scale=(0.08, 1.0), ratio=(0.75, 1.3333333), log_distribute=True, compute_dtype="float32"):
    height, width = tf.cast(size[0], dtype=compute_dtype), tf.cast(size[1], dtype=compute_dtype)
    area = height * width
    scale_max = tf.minimum(tf.minimum(height * height * ratio[1] / area, width * width / ratio[0] / area), scale[1])
    target_area = tf.random.uniform((), scale[0], scale_max, dtype=compute_dtype) * area

    ratio_min = tf.maximum(target_area / (height * height), ratio[0])
    ratio_max = tf.minimum(width * width / target_area, ratio[1])
    if log_distribute:  # More likely to select a smaller value
        log_min, log_max = tf.math.log(ratio_min), tf.math.log(ratio_max)
        aspect_ratio = tf.random.uniform((), log_min, log_max, dtype=compute_dtype)
        aspect_ratio = tf.math.exp(aspect_ratio)
    else:
        aspect_ratio = tf.random.uniform((), ratio_min, ratio_max, dtype=compute_dtype)

    ww_crop = tf.cast(tf.math.floor(tf.sqrt(target_area * aspect_ratio)), "int32")
    hh_crop = tf.cast(tf.math.floor(tf.sqrt(target_area / aspect_ratio)), "int32")
    # tf.print(">>>> height, width, hh_crop, ww_crop, hh_fraction_min, hh_fraction_max:", height, width, hh_crop, ww_crop, hh_fraction_min, hh_fraction_max)
    # return hh_crop, ww_crop, target_area, hh_fraction_min, hh_fraction_max, hh_fraction
    return hh_crop, ww_crop
//  [python]
def get_box(mix_weight, height, width, dtype="int32"):
    cut_rate_half = tf.math.sqrt(1.0 - mix_weight) / 2
    cut_h_half, cut_w_half = tf.to_int32(cut_rate_half * float(height)), tf.to_int32(cut_rate_half * float(width))
    cut_h_half, cut_w_half = tf.maximum(cut_h_half, 1), tf.maximum(cut_w_half, 1)
    # center_y = tf.random.uniform((), minval=cut_h_half, maxval=height - cut_h_half, dtype=dtype)
    # center_x = tf.random.uniform((), minval=cut_w_half, maxval=width - cut_w_half, dtype=dtype)
    # return center_y - cut_h_half, center_x - cut_w_half, cut_h_half * 2, cut_w_half * 2

    # Can be non-square on border
    center_y = tf.random.uniform((), minval=0, maxval=height, dtype=dtype)
    center_x = tf.random.uniform((), minval=0, maxval=width, dtype=dtype)
    yl = tf.clip_by_value(center_y - cut_h_half, 0, height)
    yr = tf.clip_by_value(center_y + cut_h_half, 0, height)
    xl = tf.clip_by_value(center_x - cut_w_half, 0, width)
    xr = tf.clip_by_value(center_x + cut_w_half, 0, width)
    return yl, xl, yr - yl, xr - xl
//  [python]
def get_box(mix_weight, height, width, dtype="int32"):
    cut_rate_half = tf.math.sqrt(1.0 - mix_weight) / 2
    cut_h_half, cut_w_half = tf.cast(cut_rate_half * float(height), dtype), tf.cast(cut_rate_half * float(width), dtype)
    cut_h_half, cut_w_half = tf.maximum(cut_h_half, 1), tf.maximum(cut_w_half, 1)
    # center_y = tf.random.uniform((), minval=cut_h_half, maxval=height - cut_h_half, dtype=dtype)
    # center_x = tf.random.uniform((), minval=cut_w_half, maxval=width - cut_w_half, dtype=dtype)
    # return center_y - cut_h_half, center_x - cut_w_half, cut_h_half * 2, cut_w_half * 2

    # Can be non-square on border
    center_y = tf.random.uniform((), minval=0, maxval=height, dtype=dtype)
    center_x = tf.random.uniform((), minval=0, maxval=width, dtype=dtype)
    yl = tf.clip_by_value(center_y - cut_h_half, 0, height)
    yr = tf.clip_by_value(center_y + cut_h_half, 0, height)
    xl = tf.clip_by_value(center_x - cut_w_half, 0, width)
    xr = tf.clip_by_value(center_x + cut_w_half, 0, width)
    return yl, xl, yr - yl, xr - xl
//  [python]
def call(self, inputs, training=None):
    if training is None:
      training = tf_keras.backend.learning_phase()
    result = []
    for i, layer in enumerate(self.aspp_layers):
      x = layer(inputs, training=training)
      # Apply resize layer to the end of the last set of layers.
      if i == len(self.aspp_layers) - 1:
        x = tf.image.resize(tf.cast(x, tf.float32), tf.shape(inputs)[1:3])
      result.append(tf.cast(x, inputs.dtype))
    result = tf.concat(result, axis=-1)
    result = self.projection(result, training=training)
    return result
//  [python]
def evaluation_process_crop_resize(image, target_shape=(224, 224), central_crop=1.0, resize_method="bilinear", antialias=False):
    # image = datapoint["image"]
    if len(image.shape) < 3:
        image = tf_imread(image)
    if central_crop > 0:  # Do not crop if central_crop == -1
        shape = tf.shape(image)
        height, width = shape[0], shape[1]
        crop_size = t.cast((central_crop * t.cast(t.minimum(height, width), t.float32)), t.int32)
        y, x = (height - crop_size) // 2, (width - crop_size) // 2
        image = tf.image.crop_to_bounding_box(image, y, x, crop_size, crop_size)
    image = tf.image.resize(image, target_shape, method=resize_method, antialias=antialias)
    return image
//  [python]
def evaluation_process_crop_resize(image, target_shape=(224, 224), central_crop=1.0, resize_method="bilinear", antialias=False):
    # image = datapoint["image"]
    if len(image.shape) < 3:
        image = tf_imread(image)
    if central_crop > 0:  # Do not crop if central_crop == -1
        shape = tf.shape(image)
        height, width = shape[0], shape[1]
        crop_size = tf.cast((central_crop * tf.cast(tf.minimum(height, width), tf.float32)), tf.int32)
        y, x = (height - crop_size) // 2, (width - crop_size) // 2
        image = tf.image.crop_to_bounding_box(image, y, x, crop_size, crop_size)
    image = tf.image.resize(image, target_shape, method=resize_method, antialias=antialias)
    return image
//  [python]
def __bbox_loss__(self, bbox_true_valid, bbox_pred_valid):
        vv = tf.cond(
            tf.shape(bbox_true_valid)[0] == 0,
            lambda: 0.0,
            lambda: tf.losses.huber(bbox_true_valid, bbox_pred_valid, self.delta),
            # lambda: self.huber(tf.expand_dims(bbox_true_valid, -1), tf.expand_dims(bbox_pred_valid, -1)) / 4.0,
        )
        return tf.to_float(vv)
//  [python]
def __bbox_loss__(self, bbox_true_valid, bbox_pred_valid):
        # https://github.com/google/automl/tree/master/efficientdet/tf2/train_lib.py#L409
        # error = tf.subtract(bbox_pred_valid, bbox_true_valid)
        # abs_error = tf.abs(error)
        # regression_loss = tf.where(abs_error <= self.delta, 0.5 * tf.square(error), self.delta * abs_error - 0.5 * tf.square(self.delta))
        # regression_loss / self.delta -> torch one
        # regression_loss = tf.where(abs_error <= self.delta, 0.5 * (abs_error ** 2) / self.delta, abs_error - 0.5 * self.delta)
        # tf.losses.huber <--> tf.keras.losses.Huber(reduction=tf.keras.losses.Reduction.AUTO)
        vv = tf.cond(
            tf.shape(bbox_true_valid)[0] == 0,
            lambda: 0.0,
            lambda: tf.losses.huber(bbox_true_valid, bbox_pred_valid, self.delta),
            # lambda: self.huber(tf.expand_dims(bbox_true_valid, -1), tf.expand_dims(bbox_pred_valid, -1)) / 4.0,
        )
        return tf.cast(vv, bbox_pred_valid.dtype)
//  [python]
def evaluation_process_resize_crop(image, target_shape=(224, 224), central_crop=1.0, resize_method="bilinear", antialias=False):
    # image = datapoint["image"]
    if len(image.shape) < 3:
        image = tf_imread(image)
    shape = tf.shape(image)
    height, width = shape[0], shape[1]
    min_border = tf.to_float(tf.minimum(height, width))
    scale_size = tf.to_float(tf.minimum(*target_shape)) / central_crop
    hh_scale = tf.to_int32(tf.floor(tf.to_float(height) * scale_size / min_border))
    ww_scale = tf.to_int32(tf.floor(tf.to_float(width) * scale_size / min_border))
    image = tf.image.resize(image, (hh_scale, ww_scale), method=resize_method, antialias=antialias)

    y, x = (hh_scale - target_shape[0]) // 2, (ww_scale - target_shape[1]) // 2
    image = tf.image.crop_to_bounding_box(image, y, x, target_shape[0], target_shape[1])
    return image
//  [python]
def evaluation_process_resize_crop(image, target_shape=(224, 224), central_crop=1.0, resize_method="bilinear", antialias=False):
    # image = datapoint["image"]
    if len(image.shape) < 3:
        image = tf_imread(image)
    shape = tf.shape(image)
    height, width = shape[0], shape[1]
    min_border = tf.cast(tf.minimum(height, width), tf.float32)
    scale_size = tf.cast(tf.minimum(*target_shape), tf.float32) / central_crop
    hh_scale = tf.cast(tf.floor(tf.cast(height, tf.float32) * scale_size / min_border), tf.int32)
    ww_scale = tf.cast(tf.floor(tf.cast(width, tf.float32) * scale_size / min_border), tf.int32)
    image = tf.image.resize(image, (hh_scale, ww_scale), method=resize_method, antialias=antialias)

    y, x = (hh_scale - target_shape[0]) // 2, (ww_scale - target_shape[1]) // 2
    image = tf.image.crop_to_bounding_box(image, y, x, target_shape[0], target_shape[1])
    return image
//  [python]
def __call__(self, image, token_label=None):
        # image = datapoint["image"]
        if len(image.shape) < 2:
            image = tf_imread(image)
        channel = image.shape[-1]

        flip_left_right, scale_hh, scale_ww, crop_hh, crop_ww = t.constant(False, dtype=t.bool), 1, 1, 0, 0  # Init value
        if self.random_crop_min > 0 and self.random_crop_min < 1:
            image, scale_hh, scale_ww, crop_hh, crop_ww = random_crop_and_resize_image(
                image, self.target_shape, scale=(self.random_crop_min, 1.0), method=self.resize_method, antialias=self.resize_antialias
            )
        # elif self.central_crop > 0:
        #     image = tf.image.central_crop(image, self.central_crop)
        else:
            image = tf.image.resize(image, self.target_shape, method=self.resize_method, antialias=self.resize_antialias)

        if self.magnitude >= 0:
            # tf.image.random_flip_left_right
            flip_left_right = t.random.uniform(()) < 0.5
            image = tf.cond(flip_left_right, lambda: tf.image.flip_left_right(image), lambda: image)
        if self.magnitude > 0:
            image = self.randaug(image)
        if self.random_erasing_prob > 0:
            image = self.random_erasing(image)

        image = t.cast(image, t.float32)
        image.set_shape([*self.target_shape[:2], channel])

        # label = datapoint["label"]
        if self.use_token_label and token_label is not None:
            token_label = self.token_label_align(token_label, flip_left_right, scale_hh, scale_ww, crop_hh, crop_ww)
            return image, token_label
        else:
            return image
//  [python]
def __call__(self, image, token_label=None):
        # image = datapoint["image"]
        if len(image.shape) < 2:
            image = tf_imread(image)
        channel = image.shape[-1]

        flip_left_right, scale_hh, scale_ww, crop_hh, crop_ww = tf.cast(False, tf.bool), 1, 1, 0, 0  # Init value
        if self.random_crop_min > 0 and self.random_crop_min < 1:
            image, scale_hh, scale_ww, crop_hh, crop_ww = random_crop_and_resize_image(
                image, self.target_shape, scale=(self.random_crop_min, 1.0), method=self.resize_method, antialias=self.resize_antialias
            )
        # elif self.central_crop > 0:
        #     image = tf.image.central_crop(image, self.central_crop)
        else:
            image = tf.image.resize(image, self.target_shape, method=self.resize_method, antialias=self.resize_antialias)

        if self.magnitude >= 0:
            # tf.image.random_flip_left_right
            flip_left_right = tf.random.uniform(()) < 0.5
            image = tf.cond(flip_left_right, lambda: tf.image.flip_left_right(image), lambda: image)
        if self.magnitude > 0:
            image = self.randaug(image)
        if self.random_erasing_prob > 0:
            image = self.random_erasing(image)

        image = tf.cast(image, tf.float32)
        image.set_shape([*self.target_shape[:2], channel])

        # label = datapoint["label"]
        if self.use_token_label and token_label is not None:
            token_label = self.token_label_align(token_label, flip_left_right, scale_hh, scale_ww, crop_hh, crop_ww)
            return image, token_label
        else:
            return image
//  [python]
def __dfl_loss__(self, bboxes_true_encoded, bboxes_pred, labels_true):
        target_low_bound = t.cast(t.floor(bboxes_true_encoded), bboxes_pred.dtype)
        target_up_bound = target_low_bound + 1
        weight_low = target_up_bound - bboxes_true_encoded

        bboxes_pred_reg = t.reshape(bboxes_pred, [-1, 4, self.regression_len // 4])
        dfl_loss_low = K.sparse_categorical_crossentropy(target_low_bound, bboxes_pred_reg, from_logits=True) * weight_low
        dfl_loss_up = K.sparse_categorical_crossentropy(target_up_bound, bboxes_pred_reg, from_logits=True) * (1 - weight_low)
        dfl_loss = t.reduce_mean(dfl_loss_low, axis=-1) + t.reduce_mean(dfl_loss_up, axis=-1)
        return dfl_loss
//  [python]
def __dfl_loss__(self, bboxes_true_encoded, bboxes_pred, labels_true):
        target_low_bound = tf.cast(tf.floor(bboxes_true_encoded), bboxes_pred.dtype)
        target_up_bound = target_low_bound + 1
        weight_low = target_up_bound - bboxes_true_encoded

        bboxes_pred_reg = tf.reshape(bboxes_pred, [-1, 4, self.regression_len // 4])
        dfl_loss_low = K.sparse_categorical_crossentropy(target_low_bound, bboxes_pred_reg, from_logits=True) * weight_low
        dfl_loss_up = K.sparse_categorical_crossentropy(target_up_bound, bboxes_pred_reg, from_logits=True) * (1 - weight_low)
        dfl_loss = tf.reduce_mean(dfl_loss_low, axis=-1) + tf.reduce_mean(dfl_loss_up, axis=-1)
        return dfl_loss
//  [python]
def pixel_nms(heat,kernel=[3,3],threshold=0.3):
        hmax=tf.nn.max_pool(heat,ksize=[1]+kernel+[1],strides=[1,1,1,1],padding='SAME')
        mask=tf.cast(tf.logical_and(tf.equal(hmax,heat),tf.greater(hmax,threshold)),tf.float32)
        return mask*heat
//  [python]
def update_state(self, y_true, y_pred, sample_weight=None):
        pick = tf.where(y_true[:, :, -1] > 0)
        cls_true_valid = tf.argmax(tf.gather_nd(y_true[:, :, 4:-1], pick), axis=-1)
        cls_pred_valid = tf.argmax(tf.gather_nd(y_pred[:, :, 4:], pick), axis=-1)
        cls_acc = tf.reduce_mean(tf.to_float(cls_true_valid == cls_pred_valid))
        # tf.assert_less(cls_acc, 1.1)
        self.cls_acc.assign_add(cls_acc)
        self.count.assign_add(1.0)
//  [python]
def update_state(self, y_true, y_pred, sample_weight=None):
        pick = tf.where(y_true[:, :, -1] > 0)
        cls_true_valid = tf.argmax(tf.gather_nd(y_true[:, :, 4:-1], pick), axis=-1)
        cls_pred_valid = tf.argmax(tf.gather_nd(y_pred[:, :, 4:], pick), axis=-1)
        cls_acc = tf.reduce_mean(tf.cast(cls_true_valid == cls_pred_valid, "float32"))
        # tf.assert_less(cls_acc, 1.1)
        self.cls_acc.assign_add(cls_acc)
        self.count.assign_add(1.0)
//  [python]
def _topk(scores,k=100):
        B,H,W,C = wmlt.combined_static_and_dynamic_shape(scores)
        scores = tf.reshape(scores,[B,-1])
        topk_scores,topk_inds = tf.nn.top_k(scores,k=k)
        topk_classes = topk_inds%C
        topk_inds = topk_inds//C
        topk_ys = tf.to_float(topk_inds//W)
        topk_xs = tf.to_float(topk_inds%W)
        return topk_scores,topk_inds,topk_classes,topk_ys,topk_xs
//  [python]
def _topk(scores,k=100):
        B,H,W,C = wmlt.combined_static_and_dynamic_shape(scores)
        scores = tf.reshape(scores,[B,-1])
        topk_scores,topk_inds = tf.nn.top_k(scores,k=k)
        topk_classes = topk_inds%C
        topk_inds = topk_inds//C
        topk_ys = tf.cast(topk_inds//W,tf.float32)
        topk_xs = tf.cast(topk_inds%W,tf.float32)
        return topk_scores,topk_inds,topk_classes,topk_ys,topk_xs
//  [python]
def call(self, y_true, y_pred):
        bbox_pred, class_pred = y_pred[:, :, :4], y_pred[:, :, 4:]
        bbox_true, class_true, anchor_mark = y_true[:, :, :4], y_true[:, :, 4:-1], y_true[:, :, -1]
        exclude_ignored_pick = tf.where(anchor_mark != -1)
        valid_pick = tf.where(anchor_mark == 1)
        num_positive_anchors = tf.maximum(tf.shape(valid_pick)[0], 1)

        # tf.gather_nd works better than tf.gather
        class_true_valid, class_pred_valid = tf.gather_nd(class_true, exclude_ignored_pick), tf.gather_nd(class_pred, exclude_ignored_pick)
        bbox_true_valid, bbox_pred_valid = tf.gather_nd(bbox_true, valid_pick), tf.gather_nd(bbox_pred, valid_pick)

        cls_loss = self.__focal_loss__(class_true_valid, class_pred_valid)  # divide before sum, if meet inf
        bbox_loss = self.__bbox_loss__(bbox_true_valid, bbox_pred_valid)
        cls_loss, bbox_loss = tf.reduce_sum(cls_loss) / num_positive_anchors, tf.reduce_sum(bbox_loss) / num_positive_anchors

        # Calulate accuracy here, will use it in metrics
        self.class_acc.assign(tf.reduce_mean(tf.to_float(tf.argmax(class_pred_valid, axis=-1) == tf.argmax(class_true_valid, axis=-1))))

        # return bbox_loss
        tf.print(" - cls_loss:", cls_loss, "- bbox_loss:", bbox_loss, end="\r")
        return cls_loss + bbox_loss * self.bbox_loss_weight