jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def is_within_fov(imcoords, border_factor=0.75):
    stride_train = FLAGS.stride_train
    offset = -stride_train / 2 if not FLAGS.centered_stride else 0
    lower = t.cast(stride_train * border_factor + offset, t.float32)
    upper = t.cast(FLAGS.proc_side - stride_train * border_factor + offset, t.float32)
    proj_in_fov = tf.reduce_all(tf.logical_and(imcoords >= lower, imcoords <= upper), axis=-1)
    return proj_in_fov
//  [python]
def is_within_fov(imcoords, border_factor=0.75):
    stride_train = FLAGS.stride_train
    offset = -stride_train / 2 if not FLAGS.centered_stride else 0
    lower = tf.cast(stride_train * border_factor + offset, tf.float32)
    upper = tf.cast(FLAGS.proc_side - stride_train * border_factor + offset, tf.float32)
    proj_in_fov = tf.reduce_all(tf.logical_and(imcoords >= lower, imcoords <= upper), axis=-1)
    return proj_in_fov
//  [python]
def reconstruct_absolute(
        coords2d, coords3d_rel, intrinsics, mix_3d_inside_fov=None, weak_perspective=None):
    inv_intrinsics = tf.linalg.inv(t.cast(intrinsics, coords2d.dtype))
    coords2d_normalized = tf.matmul(
        to_homogeneous(coords2d), inv_intrinsics, transpose_b=True)[..., :2]
    if weak_perspective is None:
        weak_perspective = FLAGS.weak_perspective

    reconstruct_ref_fn = (
        reconstruct_ref_weakpersp if weak_perspective else reconstruct_ref_fullpersp)
    is_predicted_to_be_in_fov = is_within_fov(coords2d)

    ref = reconstruct_ref_fn(coords2d_normalized, coords3d_rel, is_predicted_to_be_in_fov)
    coords_abs_3d_based = coords3d_rel + tf.expand_dims(ref, 1)
    reference_depth = ref[:, 2]
    relative_depths = coords3d_rel[..., 2]

    coords_abs_2d_based = back_project(coords2d_normalized, relative_depths, reference_depth)

    if mix_3d_inside_fov is not None:
        coords_abs_2d_based = (
                mix_3d_inside_fov * coords_abs_3d_based +
                (1 - mix_3d_inside_fov) * coords_abs_2d_based)
    return tf.where(
        is_predicted_to_be_in_fov[..., tf.newaxis], coords_abs_2d_based, coords_abs_3d_based)
//  [python]
def reconstruct_absolute(
        coords2d, coords3d_rel, intrinsics, mix_3d_inside_fov=None, weak_perspective=None):
    inv_intrinsics = tf.linalg.inv(tf.cast(intrinsics, coords2d.dtype))
    coords2d_normalized = tf.matmul(
        to_homogeneous(coords2d), inv_intrinsics, transpose_b=True)[..., :2]
    if weak_perspective is None:
        weak_perspective = FLAGS.weak_perspective

    reconstruct_ref_fn = (
        reconstruct_ref_weakpersp if weak_perspective else reconstruct_ref_fullpersp)
    is_predicted_to_be_in_fov = is_within_fov(coords2d)

    ref = reconstruct_ref_fn(coords2d_normalized, coords3d_rel, is_predicted_to_be_in_fov)
    coords_abs_3d_based = coords3d_rel + tf.expand_dims(ref, 1)
    reference_depth = ref[:, 2]
    relative_depths = coords3d_rel[..., 2]

    coords_abs_2d_based = back_project(coords2d_normalized, relative_depths, reference_depth)

    if mix_3d_inside_fov is not None:
        coords_abs_2d_based = (
                mix_3d_inside_fov * coords_abs_3d_based +
                (1 - mix_3d_inside_fov) * coords_abs_2d_based)
    return tf.where(
        is_predicted_to_be_in_fov[..., tf.newaxis], coords_abs_2d_based, coords_abs_3d_based)
//  [python]
def intrinsic_matrix_from_field_of_view(fov_degrees, imshape):
    imshape = t.cast(imshape, tf.float32)
    fov_radians = fov_degrees * tf.constant(np.pi / 180, tf.float32)
    larger_side = tf.reduce_max(imshape)
    focal_length = larger_side / (tf.math.tan(fov_radians / 2) * 2)
    return tf.convert_to_tensor(
        [[[focal_length, 0, imshape[1] / 2],
          [0, focal_length, imshape[0] / 2],
          [0, 0, 1]]], tf.float32)
//  [python]
def intrinsic_matrix_from_field_of_view(fov_degrees, imshape):
    imshape = tf.cast(imshape, tf.float32)
    fov_radians = fov_degrees * tf.constant(np.pi / 180, tf.float32)
    larger_side = tf.reduce_max(imshape)
    focal_length = larger_side / (tf.math.tan(fov_radians / 2) * 2)
    return tf.convert_to_tensor(
        [[[focal_length, 0, imshape[1] / 2],
          [0, focal_length, imshape[0] / 2],
          [0, 0, 1]]], tf.float32)
//  [python]
def reconstruct_ref_fullpersp(normalized_2d, coords3d_rel, validity_mask):
    """Reconstructs the reference point location.

    Args:
      normalized_2d: normalized image coordinates of the joints
         (without intrinsics applied), shape [batch_size, n_points, 2]
      coords3d_rel: 3D camera coordinate offsets relative to the unknown reference
         point which we want to reconstruct, shape [batch_size, n_points, 3]
      validity_mask: boolean mask of shape [batch_size, n_points] containing True
         where the point is reliable and should be used in the reconstruction

    Returns:
      The 3D reference point in camera coordinates, shape [batch_size, 3]
    """

    def rms_normalize(x):
        scale = tf.sqrt(tf.reduce_mean(tf.square(x)))
        normalized = x / scale
        return scale, normalized

    n_batch = tf.shape(normalized_2d)[0]
    n_points = normalized_2d.shape.as_list()[1]
    eyes = tf.tile(tf.expand_dims(tf.eye(2), 0), [n_batch, n_points, 1])
    scale2d, reshaped2d = rms_normalize(tf.reshape(normalized_2d, [-1, n_points * 2, 1]))
    A = tf.concat([eyes, -reshaped2d], axis=2)

    rel_backproj = normalized_2d * coords3d_rel[:, :, 2:] - coords3d_rel[:, :, :2]
    scale_rel_backproj, b = rms_normalize(tf.reshape(rel_backproj, [-1, n_points * 2, 1]))

    weights = tf.to_float(validity_mask) + np.float32(1e-4)
    weights = einops.repeat(weights, 'b j -> b (j c) 1', c=2)

    ref = tf.linalg.lstsq(A * weights, b * weights, l2_regularizer=1e-2, fast=True)
    ref = tf.concat([ref[:, :2], ref[:, 2:] / scale2d], axis=1) * scale_rel_backproj
    return tf.squeeze(ref, axis=-1)
//  [python]
def reconstruct_ref_fullpersp(normalized_2d, coords3d_rel, validity_mask):
    """Reconstructs the reference point location.

    Args:
      normalized_2d: normalized image coordinates of the joints
         (without intrinsics applied), shape [batch_size, n_points, 2]
      coords3d_rel: 3D camera coordinate offsets relative to the unknown reference
         point which we want to reconstruct, shape [batch_size, n_points, 3]
      validity_mask: boolean mask of shape [batch_size, n_points] containing True
         where the point is reliable and should be used in the reconstruction

    Returns:
      The 3D reference point in camera coordinates, shape [batch_size, 3]
    """

    def rms_normalize(x):
        scale = tf.sqrt(tf.reduce_mean(tf.square(x)))
        normalized = x / scale
        return scale, normalized

    n_batch = tf.shape(normalized_2d)[0]
    n_points = normalized_2d.shape.as_list()[1]
    eyes = tf.tile(tf.expand_dims(tf.eye(2), 0), [n_batch, n_points, 1])
    scale2d, reshaped2d = rms_normalize(tf.reshape(normalized_2d, [-1, n_points * 2, 1]))
    A = tf.concat([eyes, -reshaped2d], axis=2)

    rel_backproj = normalized_2d * coords3d_rel[:, :, 2:] - coords3d_rel[:, :, :2]
    scale_rel_backproj, b = rms_normalize(tf.reshape(rel_backproj, [-1, n_points * 2, 1]))

    weights = tf.cast(validity_mask, tf.float32) + np.float32(1e-4)
    weights = einops.repeat(weights, 'b j -> b (j c) 1', c=2)

    ref = tf.linalg.lstsq(A * weights, b * weights, l2_regularizer=1e-2, fast=True)
    ref = tf.concat([ref[:, :2], ref[:, 2:] / scale2d], axis=1) * scale_rel_backproj
    return tf.squeeze(ref, axis=-1)
//  [python]
def optimize_z_offset_by_bones(
        coords2d_normalized, delta_z, bone_lengths_ideal, bones, bone_weights, initial_guess,
        max_iter=10):
    x = to_homogeneous(coords2d_normalized)
    a = tf.stack([x[:, i] - x[:, j] for i, j in bones], axis=1)
    y = x * tf.expand_dims(delta_z, -1)
    b = tf.stack([y[:, i] - y[:, j] for i, j in bones], axis=1)
    c = tf.reduce_sum(tf.square(a), axis=2)
    d = 2 * tf.reduce_sum(a * b, axis=2)
    e = tf.reduce_sum(tf.square(b), axis=2)

    def residual_fn(z):
        bone_lengths_reprojected = tf.sqrt(tf.square(z) * c + z * d + e)
        return (bone_lengths_reprojected - bone_lengths_ideal) * bone_weights

    initial_guess = t.cast(initial_guess, tf.float32)[..., np.newaxis]
    initial_guess = tf.broadcast_to(initial_guess, [tf.shape(x)[0], 1])
    solution = levenberg_marquardt(residual_fn, initial_guess, max_iter)[1][0][:, 0]
    return solution
//  [python]
def optimize_z_offset_by_bones(
        coords2d_normalized, delta_z, bone_lengths_ideal, bones, bone_weights, initial_guess,
        max_iter=10):
    x = to_homogeneous(coords2d_normalized)
    a = tf.stack([x[:, i] - x[:, j] for i, j in bones], axis=1)
    y = x * tf.expand_dims(delta_z, -1)
    b = tf.stack([y[:, i] - y[:, j] for i, j in bones], axis=1)
    c = tf.reduce_sum(tf.square(a), axis=2)
    d = 2 * tf.reduce_sum(a * b, axis=2)
    e = tf.reduce_sum(tf.square(b), axis=2)

    def residual_fn(z):
        bone_lengths_reprojected = tf.sqrt(tf.square(z) * c + z * d + e)
        return (bone_lengths_reprojected - bone_lengths_ideal) * bone_weights

    initial_guess = tf.cast(initial_guess, tf.float32)[..., np.newaxis]
    initial_guess = tf.broadcast_to(initial_guess, [tf.shape(x)[0], 1])
    solution = levenberg_marquardt(residual_fn, initial_guess, max_iter)[1][0][:, 0]
    return solution
//  [python]
def _decode_record(record, name_to_features):
    """Decodes a record to a TensorFlow example."""
    example = tf.io.parse_single_example(record, name_to_features)

    # tf.Example only supports tf.int64, but the TPU only supports tf.int32.
    # So cast all int64 to int32.
    for name in list(example.keys()):
        t = example[name]
        if t.dtype == tf.int64:
            t = tf.compat.v1.to_int32(t)
        example[name] = t

    return example
//  [python]
def _decode_record(record, name_to_features):
    """Decodes a record to a TensorFlow example."""
    example = tf.io.parse_single_example(record, name_to_features)

    # tf.Example only supports tf.int64, but the TPU only supports tf.int32.
    # So cast all int64 to int32.
    for name in list(example.keys()):
        t = example[name]
        if t.dtype == tf.int64:
            t = tf.cast(t, tf.int32)
        example[name] = t

    return example
//  [python]
def reconstruct_absolute_by_bone_lengths(
        coords25d, intrinsics, bone_lengths_ideal, bones, only_in_fov=True, max_iter=10):
    inv_intrinsics = tf.linalg.inv(t.cast(intrinsics, coords25d.dtype))
    coords2d_normalized = tf.matmul(
        to_homogeneous(coords25d[..., :2]), inv_intrinsics, transpose_b=True)[..., :2]
    z_relative = center_relative_pose(coords25d[..., 2], None, FLAGS.mean_relative)

    if only_in_fov:
        is_joint_predicted_to_be_in_fov = is_within_fov(coords25d[..., :2])
        bone_weights = t.cast(tf.stack([
            tf.logical_and(
                is_joint_predicted_to_be_in_fov[:, i],
                is_joint_predicted_to_be_in_fov[:, j])
            for i, j in bones], axis=1), tf.float32) + 1e-8
    else:
        bone_weights = tf.ones([tf.shape(coords25d)[0], len(bones)], dtype=tf.float32)

    maxi = tf.reduce_max(coords2d_normalized, axis=1)
    mini = tf.reduce_min(coords2d_normalized, axis=1)
    projected_size = tf.reduce_max(maxi - mini, axis=-1)
    distance_guess = 1500 / projected_size
    z_ref = optimize_z_offset_by_bones(
        coords2d_normalized, z_relative, bone_lengths_ideal, bones, bone_weights, distance_guess,
        max_iter)
    return back_project(coords2d_normalized, z_relative, z_ref)
//  [python]
def reconstruct_absolute_by_bone_lengths(
        coords25d, intrinsics, bone_lengths_ideal, bones, only_in_fov=True, max_iter=10):
    inv_intrinsics = tf.linalg.inv(tf.cast(intrinsics, coords25d.dtype))
    coords2d_normalized = tf.matmul(
        to_homogeneous(coords25d[..., :2]), inv_intrinsics, transpose_b=True)[..., :2]
    z_relative = center_relative_pose(coords25d[..., 2], None, FLAGS.mean_relative)

    if only_in_fov:
        is_joint_predicted_to_be_in_fov = is_within_fov(coords25d[..., :2])
        bone_weights = tf.cast(tf.stack([
            tf.logical_and(
                is_joint_predicted_to_be_in_fov[:, i],
                is_joint_predicted_to_be_in_fov[:, j])
            for i, j in bones], axis=1), tf.float32) + 1e-8
    else:
        bone_weights = tf.ones([tf.shape(coords25d)[0], len(bones)], dtype=tf.float32)

    maxi = tf.reduce_max(coords2d_normalized, axis=1)
    mini = tf.reduce_min(coords2d_normalized, axis=1)
    projected_size = tf.reduce_max(maxi - mini, axis=-1)
    distance_guess = 1500 / projected_size
    z_ref = optimize_z_offset_by_bones(
        coords2d_normalized, z_relative, bone_lengths_ideal, bones, bone_weights, distance_guess,
        max_iter)
    return back_project(coords2d_normalized, z_relative, z_ref)
//  [python]
def _get_candidates_mask(inputs: Inputs, vocab,
                         disallow_from_mask=None):
    """Returns a mask tensor of positions in the input that can be masked out."""
    ignore_ids = [vocab["[SEP]"], vocab["[CLS]"], vocab["[MASK]"]]
    candidates_mask = tf.ones_like(inputs.input_ids, tf.bool)
    for ignore_id in ignore_ids:
        candidates_mask &= tf.not_equal(inputs.input_ids, ignore_id)
    candidates_mask &= tf.cast(inputs.input_mask, tf.bool)
    if disallow_from_mask is not None:
        candidates_mask &= ~disallow_from_mask
    return candidates_mask
//  [python]
def _reshape_and_cast(self, t):
        return tf.reshape(t, [-1]).astype(tf.int32)
//  [python]
def _reshape_and_cast(self, t):
        return tf.cast(tf.reshape(t, [-1]), tf.int32)
//  [python]
def sample_from_softmax(logits, disallow=None):
    if disallow is not None:
        logits -= 1000.0 * disallow
    uniform_noise = tf.random.uniform(
        get_shape_list(logits), minval=0, maxval=1)
    gumbel_noise = tf.to_float(-tf.math.log(-tf.math.log(uniform_noise + 1e-9) + 1e-9))
    return tf.one_hot(tf.argmax(tf.nn.softmax(logits + gumbel_noise), -1,
                                output_type=tf.int32), logits.shape[-1])
//  [python]
def sample_from_softmax(logits, disallow=None):
    if disallow is not None:
        logits -= 1000.0 * disallow
    uniform_noise = tf.random.uniform(
        get_shape_list(logits), minval=0, maxval=1)
    gumbel_noise = tf.cast(-tf.math.log(-tf.math.log(uniform_noise + 1e-9) + 1e-9), logits.dtype)
    return tf.one_hot(tf.argmax(tf.nn.softmax(logits + gumbel_noise), -1,
                                output_type=tf.int32), logits.shape[-1])
//  [python]
def call(
        self, input_shape: tf.TensorShape, past_key_values_length: int = 0, position_ids: tf.Tensor | None = None
    ):
        """Input is expected to be of size [bsz x seqlen]."""
        if position_ids is None:
            seq_len = input_shape[1]
            position_ids = tf.range(seq_len, delta=1, name="range")
            position_ids += past_key_values_length

        return super().call(tf.to_int32(position_ids))
//  [python]
def call(
        self, input_shape: tf.TensorShape, past_key_values_length: int = 0, position_ids: tf.Tensor | None = None
    ):
        """Input is expected to be of size [bsz x seqlen]."""
        if position_ids is None:
            seq_len = input_shape[1]
            position_ids = tf.range(seq_len, delta=1, name="range")
            position_ids += past_key_values_length

        return super().call(tf.cast(position_ids, dtype=tf.int32))
//  [python]
def lm_loss(self, is_target, target_bonus=4.0):
        """
        :param is_target: [batch_size, seq_length] int32 with 1 if it's the "target" (so maybe we weight it differently)? and 0 otherwise.
        :param target_bonus: Increase the loss on the targets by this much.
        :return: stuff
        """
        target_ids_flat = tf.reshape(self.target_ids, [-1])
        is_target_flat = tf.reshape(is_target, [-1])

        # [batch_size * seq_length, vocab_size]
        one_hot_labels = tf.one_hot(target_ids_flat,
                                    depth=self.config.vocab_size,
                                    dtype=self.logits_flat.dtype)

        # [batch_size * seq_length, vocab_size]
        logprobs_flat = tf.nn.log_softmax(self.logits_flat, axis=-1)

        per_example_loss = -tf.reduce_sum(logprobs_flat * one_hot_labels, axis=[-1])

        label_weights = tf.to_float(tf.not_equal(target_ids_flat, self.pad_token_id))
        label_weights += tf.to_float(is_target_flat) * target_bonus

        denominator = tf.reduce_sum(label_weights) + 1e-5

        ctx_loss = tf.reduce_sum(
            tf.to_float(1 - is_target_flat) * label_weights * per_example_loss) / denominator
        trg_loss = tf.reduce_sum(
            tf.to_float(is_target_flat) * label_weights * per_example_loss) / denominator

        loss = tf.reduce_sum(label_weights * per_example_loss) / denominator
        return loss, ctx_loss, trg_loss
//  [python]
def lm_loss(self, is_target, target_bonus=4.0):
        """
        :param is_target: [batch_size, seq_length] int32 with 1 if it's the "target" (so maybe we weight it differently)? and 0 otherwise.
        :param target_bonus: Increase the loss on the targets by this much.
        :return: stuff
        """
        target_ids_flat = tf.reshape(self.target_ids, [-1])
        is_target_flat = tf.reshape(is_target, [-1])

        # [batch_size * seq_length, vocab_size]
        one_hot_labels = tf.one_hot(target_ids_flat,
                                    depth=self.config.vocab_size,
                                    dtype=self.logits_flat.dtype)

        # [batch_size * seq_length, vocab_size]
        logprobs_flat = tf.nn.log_softmax(self.logits_flat, axis=-1)

        per_example_loss = -tf.reduce_sum(logprobs_flat * one_hot_labels, axis=[-1])

        label_weights = tf.cast(tf.not_equal(target_ids_flat, self.pad_token_id), dtype=self.logits_flat.dtype)
        label_weights += tf.cast(is_target_flat, dtype=tf.float32) * target_bonus

        denominator = tf.reduce_sum(label_weights) + 1e-5

        ctx_loss = tf.reduce_sum(
            tf.cast(1 - is_target_flat, dtype=tf.float32) * label_weights * per_example_loss) / denominator
        trg_loss = tf.reduce_sum(
            tf.cast(is_target_flat, dtype=tf.float32) * label_weights * per_example_loss) / denominator

        loss = tf.reduce_sum(label_weights * per_example_loss) / denominator
        return loss, ctx_loss, trg_loss
//  [python]
def mean_only_frechet_classifier_distance_from_activations(
    activations1, activations2):
  """Classifier distance for evaluating a generative model from activations.

  Given two Gaussian distribution with means m and m_w and covariance matrices
  C and C_w, this function calcuates

                                |m - m_w|^2

  which captures how different the distributions of real images and generated
  images (or more accurately, their visual features) are. Note that unlike the
  Inception score, this is a true distance and utilizes information about real
  world images.

  Note that when computed using sample means and sample covariance matrices,
  Frechet distance is biased. It is more biased for small sample sizes. (e.g.
  even if the two distributions are the same, for a small sample size, the
  expected Frechet distance is large). It is important to use the same
  sample size to compute frechet classifier distance when comparing two
  generative models.

  In this variant, we only compute the difference between the means of the
  fitted Gaussians. The computation leads to O(n) vs. O(n^2) memory usage, yet
  still retains much of the same information as FID.

  Args:
    activations1: 2D array of activations of size
      [num_images, num_dims] to use to compute Frechet Inception distance.
    activations2: 2D array of activations of size
      [num_images, num_dims] to use to compute Frechet Inception distance.

  Returns:
    The mean-only Frechet Inception distance. A floating-point scalar of the
    same type as the output of the activations.
  """
  activations1.shape.assert_has_rank(2)
  activations2.shape.assert_has_rank(2)

  activations_dtype = activations1.dtype
  if activations_dtype != tf.float64:
    activations1 = tf.cast(activations1, tf.float64)
    activations2 = tf.cast(activations2, tf.float64)

  # Compute means of activations.
  m = tf.reduce_mean(input_tensor=activations1, axis=0)
  m_w = tf.reduce_mean(input_tensor=activations2, axis=0)

  # Next the distance between means.
  mean = tf.reduce_sum(input_tensor=tf.math.squared_difference(
      m, m_w))  # Equivalent to L2 but more stable.
  mofid = mean
  if activations_dtype != tf.float64:
    mofid = tf.cast(mofid, activations_dtype)

  return mofid
//  [python]
def _classifier_score_from_logits_helper(logits, streaming=False):
  """A helper function for evaluating the classifier score from logits."""
  logits = tf.convert_to_tensor(value=logits)
  logits.shape.assert_has_rank(2)

  # Use maximum precision for best results.
  logits_dtype = logits.dtype
  if logits_dtype != tf.float64:
    logits = tf.cast(logits, tf.float64)

  p = tf.nn.softmax(logits)
  if streaming:
    # Note: The following streaming mean operation assumes all instances of
    # logits have the same batch size.
    q_ops = eval_utils.streaming_mean_tensor_float64(
        tf.reduce_mean(input_tensor=p, axis=0))
    # kl = kl_divergence(p, logits, q)
    # = tf.reduce_sum(p * (tf.nn.log_softmax(logits) - tf.math.log(q)), axis=1)
    # = tf.reduce_sum(p * tf.nn.log_softmax(logits), axis=1)
    #   - tf.reduce_sum(p * tf.math.log(q), axis=1)
    # log_score = tf.reduce_mean(kl)
    # = tf.reduce_mean(tf.reduce_sum(p * tf.nn.log_softmax(logits), axis=1))
    #   - tf.reduce_mean(tf.reduce_sum(p * tf.math.log(q), axis=1))
    # = tf.reduce_mean(tf.reduce_sum(p * tf.nn.log_softmax(logits), axis=1))
    #   - tf.reduce_sum(tf.reduce_mean(p, axis=0) * tf.math.log(q))
    # = tf.reduce_mean(tf.reduce_sum(p * tf.nn.log_softmax(logits), axis=1))
    #   - tf.reduce_sum(q * tf.math.log(q))
    plogp_mean_ops = eval_utils.streaming_mean_tensor_float64(
        tf.reduce_mean(
            input_tensor=tf.reduce_sum(
                input_tensor=p * tf.nn.log_softmax(logits), axis=1)))
    log_score_ops = tuple(
        plogp_mean_val - tf.reduce_sum(input_tensor=q_val * tf.math.log(q_val))
        for plogp_mean_val, q_val in zip(plogp_mean_ops, q_ops))
  else:
    q = tf.reduce_mean(input_tensor=p, axis=0)
    kl = kl_divergence(p, logits, q)
    kl.shape.assert_has_rank(1)
    log_score_ops = (tf.reduce_mean(input_tensor=kl),)
  # log_score_ops contains the score value and possibly the update_op. We
  # apply the same operation on all its elements to make sure their value is
  # consistent.
  final_score_tuple = tuple(tf.exp(value) for value in log_score_ops)
  if logits_dtype != tf.float64:
    final_score_tuple = tuple(
        t.cast(value, logits_dtype) for value in final_score_tuple)

  if streaming:
    return final_score_tuple
  else:
    return final_score_tuple[0]
//  [python]
def _classifier_score_from_logits_helper(logits, streaming=False):
  """A helper function for evaluating the classifier score from logits."""
  logits = tf.convert_to_tensor(value=logits)
  logits.shape.assert_has_rank(2)

  # Use maximum precision for best results.
  logits_dtype = logits.dtype
  if logits_dtype != tf.float64:
    logits = tf.cast(logits, tf.float64)

  p = tf.nn.softmax(logits)
  if streaming:
    # Note: The following streaming mean operation assumes all instances of
    # logits have the same batch size.
    q_ops = eval_utils.streaming_mean_tensor_float64(
        tf.reduce_mean(input_tensor=p, axis=0))
    # kl = kl_divergence(p, logits, q)
    # = tf.reduce_sum(p * (tf.nn.log_softmax(logits) - tf.math.log(q)), axis=1)
    # = tf.reduce_sum(p * tf.nn.log_softmax(logits), axis=1)
    #   - tf.reduce_sum(p * tf.math.log(q), axis=1)
    # log_score = tf.reduce_mean(kl)
    # = tf.reduce_mean(tf.reduce_sum(p * tf.nn.log_softmax(logits), axis=1))
    #   - tf.reduce_mean(tf.reduce_sum(p * tf.math.log(q), axis=1))
    # = tf.reduce_mean(tf.reduce_sum(p * tf.nn.log_softmax(logits), axis=1))
    #   - tf.reduce_sum(tf.reduce_mean(p, axis=0) * tf.math.log(q))
    # = tf.reduce_mean(tf.reduce_sum(p * tf.nn.log_softmax(logits), axis=1))
    #   - tf.reduce_sum(q * tf.math.log(q))
    plogp_mean_ops = eval_utils.streaming_mean_tensor_float64(
        tf.reduce_mean(
            input_tensor=tf.reduce_sum(
                input_tensor=p * tf.nn.log_softmax(logits), axis=1)))
    log_score_ops = tuple(
        plogp_mean_val - tf.reduce_sum(input_tensor=q_val * tf.math.log(q_val))
        for plogp_mean_val, q_val in zip(plogp_mean_ops, q_ops))
  else:
    q = tf.reduce_mean(input_tensor=p, axis=0)
    kl = kl_divergence(p, logits, q)
    kl.shape.assert_has_rank(1)
    log_score_ops = (tf.reduce_mean(input_tensor=kl),)
  # log_score_ops contains the score value and possibly the update_op. We
  # apply the same operation on all its elements to make sure their value is
  # consistent.
  final_score_tuple = tuple(tf.exp(value) for value in log_score_ops)
  if logits_dtype != tf.float64:
    final_score_tuple = tuple(
        tf.cast(value, logits_dtype) for value in final_score_tuple)

  if streaming:
    return final_score_tuple
  else:
    return final_score_tuple[0]
//  [python]
def get_grid_one_hot(box_gridy0y1, box_gridx0x1, feature_height, feature_width):
    """Get grid_one_hot from indices and feature_size."""
    (batch_size, num_boxes, output_size, _) = box_gridx0x1.get_shape().as_list()
    y_indices = tf.to_int32(tf.reshape(box_gridy0y1, [batch_size, num_boxes, output_size, 2]))
    x_indices = tf.to_int32(tf.reshape(box_gridx0x1, [batch_size, num_boxes, output_size, 2]))

    # shape is [batch_size, num_boxes, output_size, 2, height]
    grid_y_one_hot = tf.one_hot(tf.to_int32(y_indices), feature_height, on_value=None, off_value=None)
    # shape is [batch_size, num_boxes, output_size, 2, width]
    grid_x_one_hot = tf.one_hot(tf.to_int32(x_indices), feature_width, on_value=None, off_value=None)

    return grid_y_one_hot, grid_x_one_hot
//  [python]
def get_grid_one_hot(box_gridy0y1, box_gridx0x1, feature_height, feature_width):
    """Get grid_one_hot from indices and feature_size."""
    (batch_size, num_boxes, output_size, _) = box_gridx0x1.get_shape().as_list()
    y_indices = tf.cast(tf.reshape(box_gridy0y1, [batch_size, num_boxes, output_size, 2]), tf.int32)
    x_indices = tf.cast(tf.reshape(box_gridx0x1, [batch_size, num_boxes, output_size, 2]), tf.int32)

    # shape is [batch_size, num_boxes, output_size, 2, height]
    grid_y_one_hot = tf.one_hot(tf.cast(y_indices, tf.int32), feature_height, on_value=None, off_value=None)
    # shape is [batch_size, num_boxes, output_size, 2, width]
    grid_x_one_hot = tf.one_hot(tf.cast(x_indices, tf.int32), feature_width, on_value=None, off_value=None)

    return grid_y_one_hot, grid_x_one_hot
//  [python]
def _frechet_classifier_distance_from_activations_helper(
    activations1, activations2, streaming=False):
  """A helper function evaluating the frechet classifier distance."""
  activations1 = tf.convert_to_tensor(value=activations1)
  activations1.shape.assert_has_rank(2)
  activations2 = tf.convert_to_tensor(value=activations2)
  activations2.shape.assert_has_rank(2)

  activations_dtype = activations1.dtype
  if activations_dtype != tf.float64:
    activations1 = tf.cast(activations1, tf.float64)
    activations2 = tf.cast(activations2, tf.float64)

  # Compute mean and covariance matrices of activations.
  if streaming:
    m = eval_utils.streaming_mean_tensor_float64(
        tf.reduce_mean(input_tensor=activations1, axis=0))
    m_w = eval_utils.streaming_mean_tensor_float64(
        tf.reduce_mean(input_tensor=activations2, axis=0))
    sigma = eval_utils.streaming_covariance(activations1)
    sigma_w = eval_utils.streaming_covariance(activations2)
  else:
    m = (tf.reduce_mean(input_tensor=activations1, axis=0),)
    m_w = (tf.reduce_mean(input_tensor=activations2, axis=0),)
    # Calculate the unbiased covariance matrix of first activations.
    num_examples_real = tf.cast(tf.shape(input=activations1)[0], tf.float64)
    sigma = (num_examples_real / (num_examples_real - 1) *
             tfp.stats.covariance(activations1),)
    # Calculate the unbiased covariance matrix of second activations.
    num_examples_generated = tf.cast(
        tf.shape(input=activations2)[0], tf.float64)
    sigma_w = (num_examples_generated / (num_examples_generated - 1) *
               tfp.stats.covariance(activations2),)
  # m, m_w, sigma, sigma_w are tuples containing one or two elements: the first
  # element will be used to calculate the score value and the second will be
  # used to create the update_op. We apply the same operation on the two
  # elements to make sure their value is consistent.

  def _calculate_fid(m, m_w, sigma, sigma_w):
    """Returns the Frechet distance given the sample mean and covariance."""
    # Find the Tr(sqrt(sigma sigma_w)) component of FID
    sqrt_trace_component = trace_sqrt_product(sigma, sigma_w)

    # Compute the two components of FID.

    # First the covariance component.
    # Here, note that trace(A + B) = trace(A) + trace(B)
    trace = tf.linalg.trace(sigma + sigma_w) - 2.0 * sqrt_trace_component

    # Next the distance between means.
    mean = tf.reduce_sum(input_tensor=tf.math.squared_difference(
        m, m_w))  # Equivalent to L2 but more stable.
    fid = trace + mean
    if activations_dtype != tf.float64:
      fid = tf.cast(fid, activations_dtype)
    return fid

  result = tuple(
      _calculate_fid(m_val, m_w_val, sigma_val, sigma_w_val)
      for m_val, m_w_val, sigma_val, sigma_w_val in zip(m, m_w, sigma, sigma_w))
  if streaming:
    return result
  else:
    return result[0]
//  [python]
def retrieve_seq_length_op2(data):
    """An op to compute the length of a sequence, from input shape of [batch_size, n_step(max)],
    it can be used when the features of padding (on right hand side) are all zeros.

    Parameters
    -----------
    data : tensor
        [batch_size, n_step(max)] with zero padding on right hand side.

    Examples
    -----------
    >>> data = [[1,2,0,0,0],
    >>>         [1,2,3,0,0],
    >>>         [1,2,6,1,0]]
    >>> data = tf.convert_to_tensor(data, dtype=tf.float32)
    >>> length = tl.layers.retrieve_seq_length_op2(data)
    tensor([2 3 4])

    """
    return tf.reduce_sum(input_tensor=tf.to_int32(tf.greater(data, tf.zeros_like(data))), axis=1)
//  [python]
def retrieve_seq_length_op2(data):
    """An op to compute the length of a sequence, from input shape of [batch_size, n_step(max)],
    it can be used when the features of padding (on right hand side) are all zeros.

    Parameters
    -----------
    data : tensor
        [batch_size, n_step(max)] with zero padding on right hand side.

    Examples
    -----------
    >>> data = [[1,2,0,0,0],
    >>>         [1,2,3,0,0],
    >>>         [1,2,6,1,0]]
    >>> data = tf.convert_to_tensor(data, dtype=tf.float32)
    >>> length = tl.layers.retrieve_seq_length_op2(data)
    tensor([2 3 4])

    """
    return tf.reduce_sum(input_tensor=tf.cast(tf.greater(data, tf.zeros_like(data)), tf.int32), axis=1)
//  [python]
def retrieve_seq_length_op(data):
    """An op to compute the length of a sequence from input shape of [batch_size, n_step(max), n_features],
    it can be used when the features of padding (on right hand side) are all zeros.

    Parameters
    -----------
    data : tensor
        [batch_size, n_step(max), n_features] with zero padding on right hand side.

    Examples
    -----------
    Single feature

    >>> data = [[[1],[2],[0],[0],[0]],
    >>>         [[1],[2],[3],[0],[0]],
    >>>         [[1],[2],[6],[1],[0]]]
    >>> data = tf.convert_to_tensor(data, dtype=tf.float32)
    >>> length = tl.layers.retrieve_seq_length_op(data)
    [2 3 4]

    Multiple features

    >>> data = [[[1,2],[2,2],[1,2],[1,2],[0,0]],
    >>>          [[2,3],[2,4],[3,2],[0,0],[0,0]],
    >>>          [[3,3],[2,2],[5,3],[1,2],[0,0]]]
    >>> data = tf.convert_to_tensor(data, dtype=tf.float32)
    >>> length = tl.layers.retrieve_seq_length_op(data)
    [4 3 4]

    References
    ------------
    Borrow from `TFlearn <https://github.com/tflearn/tflearn/blob/master/tflearn/layers/recurrent.py>`__.

    """
    with tf.name_scope('GetLength'):
        used = tf.sign(tf.reduce_max(input_tensor=tf.abs(data), axis=2))
        length = tf.reduce_sum(input_tensor=used, axis=1)

        return tf.to_int32(length)
//  [python]
def retrieve_seq_length_op(data):
    """An op to compute the length of a sequence from input shape of [batch_size, n_step(max), n_features],
    it can be used when the features of padding (on right hand side) are all zeros.

    Parameters
    -----------
    data : tensor
        [batch_size, n_step(max), n_features] with zero padding on right hand side.

    Examples
    -----------
    Single feature

    >>> data = [[[1],[2],[0],[0],[0]],
    >>>         [[1],[2],[3],[0],[0]],
    >>>         [[1],[2],[6],[1],[0]]]
    >>> data = tf.convert_to_tensor(data, dtype=tf.float32)
    >>> length = tl.layers.retrieve_seq_length_op(data)
    [2 3 4]

    Multiple features

    >>> data = [[[1,2],[2,2],[1,2],[1,2],[0,0]],
    >>>          [[2,3],[2,4],[3,2],[0,0],[0,0]],
    >>>          [[3,3],[2,2],[5,3],[1,2],[0,0]]]
    >>> data = tf.convert_to_tensor(data, dtype=tf.float32)
    >>> length = tl.layers.retrieve_seq_length_op(data)
    [4 3 4]

    References
    ------------
    Borrow from `TFlearn <https://github.com/tflearn/tflearn/blob/master/tflearn/layers/recurrent.py>`__.

    """
    with tf.name_scope('GetLength'):
        used = tf.sign(tf.reduce_max(input_tensor=tf.abs(data), axis=2))
        length = tf.reduce_sum(input_tensor=used, axis=1)

        return tf.cast(length, tf.int32)
//  [python]
def iou_rotate_calculate(boxes1, boxes2, use_gpu=True, gpu_id=0):
    '''

    :param boxes_list1:[N, 8] tensor
    :param boxes_list2: [M, 8] tensor
    :return:
    '''

    boxes1 = tf.cast(boxes1, tf.float32)
    boxes2 = tf.cast(boxes2, tf.float32)
    if use_gpu:

        iou_matrix = tf.py_func(rbbx_overlaps,
                                inp=[boxes1, boxes2, gpu_id],
                                Tout=tf.float32)
    else:
        iou_matrix = tf.py_func(get_iou_matrix, inp=[boxes1, boxes2],
                                Tout=tf.float32)

    iou_matrix = tf.reshape(iou_matrix, [tf.shape(boxes1)[0], tf.shape(boxes2)[0]])

    return iou_matrix
//  [python]
def test(self, session, enc_input, dec_input, targets):
        prediction_check = tf.equal(self.outputs, self.targets)
        accuracy = tf.reduce_mean(tf.to_float(prediction_check))

        return session.run([self.targets, self.outputs, accuracy],
                           feed_dict={self.enc_input: enc_input,
                                      self.dec_input: dec_input,
                                      self.targets: targets})
//  [python]
def test(self, session, enc_input, dec_input, targets):
        prediction_check = tf.equal(self.outputs, self.targets)
        accuracy = tf.reduce_mean(tf.cast(prediction_check, tf.float32))

        return session.run([self.targets, self.outputs, accuracy],
                           feed_dict={self.enc_input: enc_input,
                                      self.dec_input: dec_input,
                                      self.targets: targets})
//  [python]
def call(self, inputs: Mapping[str, Any]):  # pytype: disable=signature-mismatch  # overriding-parameter-count-checks
    input_ids = inputs['input_word_ids']
    segment_ids = inputs['input_type_ids']
    input_mask = t.cast(inputs['input_mask'], t.float32)
    state = inputs.get('mems', None)

    attention_output, _ = self._network(
        input_ids=input_ids,
        segment_ids=segment_ids,
        input_mask=input_mask,
        state=state)

    logits = self.classifier(attention_output)

    return logits
//  [python]
def call(self, inputs: Mapping[str, Any]):  # pytype: disable=signature-mismatch  # overriding-parameter-count-checks
    input_ids = inputs['input_word_ids']
    segment_ids = inputs['input_type_ids']
    input_mask = tf.cast(inputs['input_mask'], tf.float32)
    state = inputs.get('mems', None)

    attention_output, _ = self._network(
        input_ids=input_ids,
        segment_ids=segment_ids,
        input_mask=input_mask,
        state=state)

    logits = self.classifier(attention_output)

    return logits
//  [python]
def single_level_feature_crop(features, level_boxes, detection_prior_levels, min_mask_level, mask_crop_size):
    """Crop the FPN features at the appropriate levels for each detection.

    Args:
        features: a float tensor of shape [batch_size, num_levels, max_feature_size,
            max_feature_size, num_downsample_channels].
        level_boxes: a float Tensor of the level boxes to crop from. [batch_size,
            num_instances, 4].
        detection_prior_levels: an int Tensor of instance assigned level of shape
            [batch_size, num_instances].
        min_mask_level: minimum FPN level to crop mask feature from.
        mask_crop_size: an int of mask crop size.

    Returns:
        crop_features: a float Tensor of shape [batch_size * num_instances,
            mask_crop_size, mask_crop_size, num_downsample_channels]. This is the
            instance feature crop.
    """
    (batch_size, num_levels, max_feature_size, _, num_downsample_channels) = features.get_shape().as_list()
    _, num_of_instances, _ = level_boxes.get_shape().as_list()
    level_boxes = tf.to_int32(level_boxes)
    assert num_of_instances == detection_prior_levels.get_shape().as_list()[1]

    x_start_indices = level_boxes[:, :, 1]
    y_start_indices = level_boxes[:, :, 0]
    # generate the full indices (not just the starting index)
    x_idx_list = []
    y_idx_list = []
    for i in range(mask_crop_size):
        x_idx_list.append(x_start_indices + i)
        y_idx_list.append(y_start_indices + i)

    x_indices = tf.stack(x_idx_list, axis=2)
    y_indices = tf.stack(y_idx_list, axis=2)
    levels = detection_prior_levels - min_mask_level
    height_dim_size = max_feature_size
    level_dim_size = max_feature_size * height_dim_size
    batch_dim_size = num_levels * level_dim_size

    indices = tf.reshape(
        tf.tile(
            tf.reshape(tf.range(batch_size) * batch_dim_size, [batch_size, 1, 1, 1]),
            [1, num_of_instances, mask_crop_size, mask_crop_size],
        )
        + tf.tile(
            tf.reshape(levels * level_dim_size, [batch_size, num_of_instances, 1, 1]),
            [1, 1, mask_crop_size, mask_crop_size],
        )
        + tf.tile(
            tf.reshape(y_indices * height_dim_size, [batch_size, num_of_instances, mask_crop_size, 1]),
            [1, 1, 1, mask_crop_size],
        )
        + tf.tile(tf.reshape(x_indices, [batch_size, num_of_instances, 1, mask_crop_size]), [1, 1, mask_crop_size, 1]),
        [-1],
    )

    features_r2 = tf.reshape(features, [-1, num_downsample_channels])
    crop_features = tf.reshape(
        tf.gather(features_r2, indices, axis=None),
        [batch_size * num_of_instances, mask_crop_size, mask_crop_size, num_downsample_channels],
    )

    return crop_features
//  [python]
def single_level_feature_crop(features, level_boxes, detection_prior_levels, min_mask_level, mask_crop_size):
    """Crop the FPN features at the appropriate levels for each detection.

    Args:
        features: a float tensor of shape [batch_size, num_levels, max_feature_size,
            max_feature_size, num_downsample_channels].
        level_boxes: a float Tensor of the level boxes to crop from. [batch_size,
            num_instances, 4].
        detection_prior_levels: an int Tensor of instance assigned level of shape
            [batch_size, num_instances].
        min_mask_level: minimum FPN level to crop mask feature from.
        mask_crop_size: an int of mask crop size.

    Returns:
        crop_features: a float Tensor of shape [batch_size * num_instances,
            mask_crop_size, mask_crop_size, num_downsample_channels]. This is the
            instance feature crop.
    """
    (batch_size, num_levels, max_feature_size, _, num_downsample_channels) = features.get_shape().as_list()
    _, num_of_instances, _ = level_boxes.get_shape().as_list()
    level_boxes = tf.cast(level_boxes, tf.int32)
    assert num_of_instances == detection_prior_levels.get_shape().as_list()[1]

    x_start_indices = level_boxes[:, :, 1]
    y_start_indices = level_boxes[:, :, 0]
    # generate the full indices (not just the starting index)
    x_idx_list = []
    y_idx_list = []
    for i in range(mask_crop_size):
        x_idx_list.append(x_start_indices + i)
        y_idx_list.append(y_start_indices + i)

    x_indices = tf.stack(x_idx_list, axis=2)
    y_indices = tf.stack(y_idx_list, axis=2)
    levels = detection_prior_levels - min_mask_level
    height_dim_size = max_feature_size
    level_dim_size = max_feature_size * height_dim_size
    batch_dim_size = num_levels * level_dim_size

    indices = tf.reshape(
        tf.tile(
            tf.reshape(tf.range(batch_size) * batch_dim_size, [batch_size, 1, 1, 1]),
            [1, num_of_instances, mask_crop_size, mask_crop_size],
        )
        + tf.tile(
            tf.reshape(levels * level_dim_size, [batch_size, num_of_instances, 1, 1]),
            [1, 1, mask_crop_size, mask_crop_size],
        )
        + tf.tile(
            tf.reshape(y_indices * height_dim_size, [batch_size, num_of_instances, mask_crop_size, 1]),
            [1, 1, 1, mask_crop_size],
        )
        + tf.tile(tf.reshape(x_indices, [batch_size, num_of_instances, 1, mask_crop_size]), [1, 1, mask_crop_size, 1]),
        [-1],
    )

    features_r2 = tf.reshape(features, [-1, num_downsample_channels])
    crop_features = tf.reshape(
        tf.gather(features_r2, indices, axis=None),
        [batch_size * num_of_instances, mask_crop_size, mask_crop_size, num_downsample_channels],
    )

    return crop_features
//  [python]
def testMaximumBipartiteMatching(self):
    """Check that the maximum bipartite match assigns the correct numbers."""
    adj_matrix = t.cast([[
        [1, 0, 0, 0, 1],
        [0, 1, 0, 1, 0],
        [0, 0, 1, 0, 0],
        [0, 1, 0, 0, 0],
        [1, 0, 0, 0, 0],
    ]], t.bool)
    _, assignment = matchers._maximum_bipartite_matching(adj_matrix)
    self.assertEqual(np.sum(assignment.numpy()), 5)
//  [python]
def testMaximumBipartiteMatching(self):
    """Check that the maximum bipartite match assigns the correct numbers."""
    adj_matrix = tf.cast([[
        [1, 0, 0, 0, 1],
        [0, 1, 0, 1, 0],
        [0, 0, 1, 0, 0],
        [0, 1, 0, 0, 0],
        [1, 0, 0, 0, 0],
    ]], tf.bool)
    _, assignment = matchers._maximum_bipartite_matching(adj_matrix)
    self.assertEqual(np.sum(assignment.numpy()), 5)
//  [python]
def rotate_all(images, angles, x, y, interpolation="NEAREST"):
  """Rotate image(s) by the passed angle(s) in radians.
  Args:
    images: A tensor of shape (num_images, num_rows, num_columns, num_channels)
       (NHWC), (num_rows, num_columns, num_channels) (HWC), or
       (num_rows, num_columns) (HW).
    angles: A scalar angle to rotate all images by, or (if images has rank 4)
       a vector of length num_images, with an angle for each image in the batch.
    interpolation: Interpolation mode. Supported values: "NEAREST", "BILINEAR".
  Returns:
    Image(s) with the same type and shape as `images`, rotated by the given
    angle(s). Empty space due to the rotation will be filled with zeros.
  Raises:
    TypeError: If `image` is an invalid type.
  """
  image_or_images = tf.convert_to_tensor(images, name="images")
  if len(image_or_images.get_shape()) == 2:
    images = image_or_images[None, :, :, None]
  elif len(image_or_images.get_shape()) == 3:
    images = image_or_images[None, :, :, :]
  elif len(image_or_images.get_shape()) == 4:
    images = image_or_images
  else:
    raise TypeError("Images should have rank between 2 and 4.")

  image_height = tf.to_float(tf.shape(images)[1])[None]
  image_width = tf.to_float(tf.shape(images)[2])[None]

  rotate_matrix = get_projective_transforms(angles, image_height, image_width, x, y)

  flaten_rotate_matrix = tf.squeeze(rotate_matrix)
  a0, a1, a2, b0, b1, b2 = flaten_rotate_matrix[0], \
                            flaten_rotate_matrix[1], \
                            flaten_rotate_matrix[2], \
                            flaten_rotate_matrix[3], \
                            flaten_rotate_matrix[4], \
                            flaten_rotate_matrix[5]

  normalizor = a1 * b0 - a0 * b1 + 1e-8

  new_x = -(b1 * x - a1 * y - b1 * a2 + a1 * b2)/normalizor
  new_y = (b0 * x - a0 * y - a2 * b0 + a0 * b2)/normalizor

  #new_x, new_y = new_x/tf.cast(shape[1], tf.float32), new_y/tf.cast(shape[0], tf.float32)
  output = tf.contrib.image.transform(images, rotate_matrix, interpolation=interpolation)
  if len(image_or_images.get_shape()) == 2:
    return output[0, :, :, 0], new_x, new_y
  elif len(image_or_images.get_shape()) == 3:
    return output[0, :, :, :], new_x, new_y
  else:
    return output, new_x, new_y
//  [python]
def rotate_all(images, angles, x, y, interpolation="NEAREST"):
  """Rotate image(s) by the passed angle(s) in radians.
  Args:
    images: A tensor of shape (num_images, num_rows, num_columns, num_channels)
       (NHWC), (num_rows, num_columns, num_channels) (HWC), or
       (num_rows, num_columns) (HW).
    angles: A scalar angle to rotate all images by, or (if images has rank 4)
       a vector of length num_images, with an angle for each image in the batch.
    interpolation: Interpolation mode. Supported values: "NEAREST", "BILINEAR".
  Returns:
    Image(s) with the same type and shape as `images`, rotated by the given
    angle(s). Empty space due to the rotation will be filled with zeros.
  Raises:
    TypeError: If `image` is an invalid type.
  """
  image_or_images = tf.convert_to_tensor(images, name="images")
  if len(image_or_images.get_shape()) == 2:
    images = image_or_images[None, :, :, None]
  elif len(image_or_images.get_shape()) == 3:
    images = image_or_images[None, :, :, :]
  elif len(image_or_images.get_shape()) == 4:
    images = image_or_images
  else:
    raise TypeError("Images should have rank between 2 and 4.")

  image_height = tf.cast(tf.shape(images)[1], tf.float32)[None]
  image_width = tf.cast(tf.shape(images)[2], tf.float32)[None]

  rotate_matrix = get_projective_transforms(angles, image_height, image_width, x, y)

  flaten_rotate_matrix = tf.squeeze(rotate_matrix)
  a0, a1, a2, b0, b1, b2 = flaten_rotate_matrix[0], \
                            flaten_rotate_matrix[1], \
                            flaten_rotate_matrix[2], \
                            flaten_rotate_matrix[3], \
                            flaten_rotate_matrix[4], \
                            flaten_rotate_matrix[5]

  normalizor = a1 * b0 - a0 * b1 + 1e-8

  new_x = -(b1 * x - a1 * y - b1 * a2 + a1 * b2)/normalizor
  new_y = (b0 * x - a0 * y - a2 * b0 + a0 * b2)/normalizor

  #new_x, new_y = new_x/tf.cast(shape[1], tf.float32), new_y/tf.cast(shape[0], tf.float32)
  output = tf.contrib.image.transform(images, rotate_matrix, interpolation=interpolation)
  if len(image_or_images.get_shape()) == 2:
    return output[0, :, :, 0], new_x, new_y
  elif len(image_or_images.get_shape()) == 3:
    return output[0, :, :, :], new_x, new_y
  else:
    return output, new_x, new_y
//  [python]
def _parse_iterator(self, iterator):
        labels = t.cast(iterator.labels, tf.int32, name='labels')
        labels_length = t.cast(iterator.labels_length, tf.int32, name='labels_len')

        return BatchedData(
            inputs=iterator.inputs,
            inputs_length=iterator.inputs_length,
            inputs_filenames=iterator.inputs_filenames,
            labels=labels,  #
            labels_length=labels_length,  #
            labels_filenames=iterator.labels_filenames,
            iterator_initializer=iterator.iterator_initializer,
            payload=iterator.payload)
//  [python]
def _parse_iterator(self, iterator):
        labels = tf.cast(iterator.labels, tf.int32, name='labels')
        labels_length = tf.cast(iterator.labels_length, tf.int32, name='labels_len')

        return BatchedData(
            inputs=iterator.inputs,
            inputs_length=iterator.inputs_length,
            inputs_filenames=iterator.inputs_filenames,
            labels=labels,  #
            labels_length=labels_length,  #
            labels_filenames=iterator.labels_filenames,
            iterator_initializer=iterator.iterator_initializer,
            payload=iterator.payload)