jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def batch_norm(
    x: Union[tf.Tensor, tf.Variable],
    mean: Union[tf.Tensor, tf.Variable],
    variance: Union[tf.Tensor, tf.Variable],
    /,
    *,
    scale: Optional[Union[tf.Tensor, tf.Variable]] = None,
    offset: Optional[Union[tf.Tensor, tf.Variable]] = None,
    training: Optional[bool] = False,
    eps: Optional[float] = 1e-5,
    momentum: Optional[float] = 1e-1,
    data_format: Optional[str] = "NSC",
    out: Optional[
        Tuple[
            Union[tf.Tensor, tf.Variable],
            Union[tf.Tensor, tf.Variable],
            Union[tf.Tensor, tf.Variable],
        ]
    ] = None,
) -> Tuple[
    Union[tf.Tensor, tf.Variable],
    Union[tf.Tensor, tf.Variable],
    Union[tf.Tensor, tf.Variable],
]:
    xdims = len(x.shape)
    if data_format == "NCS":
        x = tf.transpose(x, perm=(0, *range(2, xdims), 1))

    runningmean = mean
    runningvariance = variance
    if training:
        n = tf.size(x) if xdims == 1 else tf.divide(tf.size(x), tf.shape(x)[-1])
        n = tf.cast(n, x.dtype) if n.dtype != x.dtype else n
        dims = (0, *range(1, xdims - 1))
        mean = tf.math.reduce_mean(x, axis=dims)
        variance = tf.math.reduce_variance(x, axis=dims)
        runningmean = (1 - momentum) * runningmean + momentum * mean
        runningvariance = (1 - momentum) * runningvariance + momentum * variance * n / (
            n - 1
        )

    inv = 1.0 / tf.math.sqrt(variance + eps)
    offset = 0 if offset is None else offset
    if scale is not None:
        inv = tf.math.multiply(inv, scale)
    xnormalized = tf.math.add(tf.math.multiply(x, inv), offset)
    xnormalized = tf.math.subtract(xnormalized, tf.math.multiply(mean, inv))
    # the above approach is faster than tf.nn.batch_normalization

    if data_format == "NCS":
        xnormalized = tf.transpose(
            xnormalized, perm=(0, xdims - 1, *range(1, xdims - 1))
        )

    return xnormalized, runningmean, runningvariance
//  [python]
def convert_class_logits_to_softmax(multiclass_scores, temperature=1.0):
  """Converts multiclass logits to softmax scores after applying temperature.

  Args:
    multiclass_scores: float32 tensor of shape
      [num_instances, num_classes] representing the score for each box for each
      class.
    temperature: Scale factor to use prior to applying softmax. Larger
      temperatures give more uniform distruibutions after softmax.

  Returns:
    multiclass_scores: float32 tensor of shape
      [num_instances, num_classes] with scaling and softmax applied.
  """

  # Multiclass scores must be stored as logits. Apply temp and softmax.
  multiclass_scores_scaled = tf.truediv(
      multiclass_scores, temperature, name='scale_logits')
  multiclass_scores = tf.nn.softmax(multiclass_scores_scaled, name='softmax')

  return multiclass_scores
//  [python]
def convert_class_logits_to_softmax(multiclass_scores, temperature=1.0):
  """Converts multiclass logits to softmax scores after applying temperature.

  Args:
    multiclass_scores: float32 tensor of shape
      [num_instances, num_classes] representing the score for each box for each
      class.
    temperature: Scale factor to use prior to applying softmax. Larger
      temperatures give more uniform distruibutions after softmax.

  Returns:
    multiclass_scores: float32 tensor of shape
      [num_instances, num_classes] with scaling and softmax applied.
  """

  # Multiclass scores must be stored as logits. Apply temp and softmax.
  multiclass_scores_scaled = tf.divide(
      multiclass_scores, temperature, name='scale_logits')
  multiclass_scores = tf.nn.softmax(multiclass_scores_scaled, name='softmax')

  return multiclass_scores
//  [python]
def CalculateKeypointCenters(boxes):
  """Helper function to compute feature centers, from RF boxes.

  Args:
    boxes: [N, 4] float tensor.

  Returns:
    centers: [N, 2] float tensor.
  """
  return tf.div(
      tf.add(
          tf.gather(boxes, [0, 1], axis=1), tf.gather(boxes, [2, 3], axis=1)),
      2.0)
//  [python]
def CalculateKeypointCenters(boxes):
  """Helper function to compute feature centers, from RF boxes.

  Args:
    boxes: [N, 4] float tensor.

  Returns:
    centers: [N, 2] float tensor.
  """
  return tf.divide(
      tf.add(
          tf.gather(boxes, [0, 1], axis=1), tf.gather(boxes, [2, 3], axis=1)),
      2.0)
//  [python]
def tf_calculate_ADoLP(I_all):
    I1, I2, I3, I4 = I_all[:,:,:,:1], I_all[:,:,:,1:2], I_all[:,:,:,2:3], I_all[:,:,:,3:4]
    I = 0.5 * (I1 + I2 + I3 + I4)+1e-4
    Q = I1 - I3 
    U = I2 - I4
    zero_mat = tf.zeros(tf.shape(I1), tf.float32)
    ones_mat = 1e-4 * tf.ones(tf.shape(I1), tf.float32)
    Q = tf.where(tf.equal(Q, zero_mat), ones_mat, Q)
    DoLP = tf.sqrt(tf.square(Q)+tf.square(U))/I
    AoLP = 0.5*tf.atan(U/Q)
    #AoLP = (AoLP + 0.786)/(2*0.786)
    return AoLP, DoLP
//  [python]
def tf_calculate_ADoLP(I_all):
    I1, I2, I3, I4 = I_all[:,:,:,:1], I_all[:,:,:,1:2], I_all[:,:,:,2:3], I_all[:,:,:,3:4]
    I = 0.5 * (I1 + I2 + I3 + I4)+1e-4
    Q = I1 - I3 
    U = I2 - I4
    zero_mat = tf.zeros(tf.shape(I1), tf.float32)
    ones_mat = 1e-4 * tf.ones(tf.shape(I1), tf.float32)
    Q = tf.where(tf.equal(Q, zero_mat), ones_mat, Q)
    DoLP = tf.divide(tf.sqrt(tf.square(Q)+tf.square(U)), I)
    AoLP = 0.5*tf.atan(U/Q)
    #AoLP = (AoLP + 0.786)/(2*0.786)
    return AoLP, DoLP
//  [python]
def ApplyPcaAndWhitening(data,
                         pca_matrix,
                         pca_mean,
                         output_dim,
                         use_whitening=False,
                         pca_variances=None):
  """Applies PCA/whitening to data.

  Args:
    data: [N, dim] float tensor containing data which undergoes PCA/whitening.
    pca_matrix: [dim, dim] float tensor PCA matrix, row-major.
    pca_mean: [dim] float tensor, mean to subtract before projection.
    output_dim: Number of dimensions to use in output data, of type int.
    use_whitening: Whether whitening is to be used.
    pca_variances: [dim] float tensor containing PCA variances. Only used if
      use_whitening is True.

  Returns:
    output: [N, output_dim] float tensor with output of PCA/whitening operation.
  """
  output = tf.matmul(
      tf.subtract(data, pca_mean),
      tf.slice(pca_matrix, [0, 0], [output_dim, -1]),
      transpose_b=True,
      name='pca_matmul')

  # Apply whitening if desired.
  if use_whitening:
    output = tf.truediv(
        output,
        tf.sqrt(tf.slice(pca_variances, [0], [output_dim])),
        name='whitening')

  return output
//  [python]
def ApplyPcaAndWhitening(data,
                         pca_matrix,
                         pca_mean,
                         output_dim,
                         use_whitening=False,
                         pca_variances=None):
  """Applies PCA/whitening to data.

  Args:
    data: [N, dim] float tensor containing data which undergoes PCA/whitening.
    pca_matrix: [dim, dim] float tensor PCA matrix, row-major.
    pca_mean: [dim] float tensor, mean to subtract before projection.
    output_dim: Number of dimensions to use in output data, of type int.
    use_whitening: Whether whitening is to be used.
    pca_variances: [dim] float tensor containing PCA variances. Only used if
      use_whitening is True.

  Returns:
    output: [N, output_dim] float tensor with output of PCA/whitening operation.
  """
  output = tf.matmul(
      tf.subtract(data, pca_mean),
      tf.slice(pca_matrix, [0, 0], [output_dim, -1]),
      transpose_b=True,
      name='pca_matmul')

  # Apply whitening if desired.
  if use_whitening:
    output = tf.divide(
        output,
        tf.sqrt(tf.slice(pca_variances, [0], [output_dim])),
        name='whitening')

  return output
//  [python]
def _scale_and_softmax_logits(self, logits):
        """Scale logits then apply softmax."""
        scaled_logits = tf.truediv(logits, self._logit_scale, name='scale_logits')
        return tf.nn.softmax(scaled_logits, name='convert_scores')
//  [python]
def _scale_and_softmax_logits(self, logits):
        """Scale logits then apply softmax."""
        scaled_logits = tf.divide(logits, self._logit_scale, name='scale_logits')
        return tf.nn.softmax(scaled_logits, name='convert_scores')
//  [python]
def _compute_loss(self, prediction_tensor, target_tensor, weights):
    """Compute loss function.

    Args:
      prediction_tensor: A float tensor of shape [batch_size, num_anchors,
        num_classes] representing the predicted logits for each class
      target_tensor: A float tensor of shape [batch_size, num_anchors,
        num_classes] representing one-hot encoded classification targets
      weights: a float tensor of shape [batch_size, num_anchors]

    Returns:
      loss: a (scalar) tensor representing the value of the loss function
    """
    num_classes = prediction_tensor.get_shape().as_list()[-1]
    prediction_tensor = tf.div(
        prediction_tensor, self._logit_scale, name='scale_logit')
    per_row_cross_ent = (tf.nn.softmax_cross_entropy_with_logits(
        labels=tf.reshape(target_tensor, [-1, num_classes]),
        logits=tf.reshape(prediction_tensor, [-1, num_classes])))
    if self._anchorwise_output:
      return tf.reshape(per_row_cross_ent, tf.shape(weights)) * weights
    return tf.reduce_sum(per_row_cross_ent * tf.reshape(weights, [-1]))
//  [python]
def _compute_loss(self, prediction_tensor, target_tensor, weights):
    """Compute loss function.

    Args:
      prediction_tensor: A float tensor of shape [batch_size, num_anchors,
        num_classes] representing the predicted logits for each class
      target_tensor: A float tensor of shape [batch_size, num_anchors,
        num_classes] representing one-hot encoded classification targets
      weights: a float tensor of shape [batch_size, num_anchors]

    Returns:
      loss: a (scalar) tensor representing the value of the loss function
    """
    num_classes = prediction_tensor.get_shape().as_list()[-1]
    prediction_tensor = tf.divide(
        prediction_tensor, self._logit_scale, name='scale_logit')
    per_row_cross_ent = (tf.nn.softmax_cross_entropy_with_logits(
        labels=tf.reshape(target_tensor, [-1, num_classes]),
        logits=tf.reshape(prediction_tensor, [-1, num_classes])))
    if self._anchorwise_output:
      return tf.reshape(per_row_cross_ent, tf.shape(weights)) * weights
    return tf.reduce_sum(per_row_cross_ent * tf.reshape(weights, [-1]))
//  [python]
def _compute_loss(self, prediction_tensor, target_tensor, weights):
        """Compute loss function.

        Args:
          prediction_tensor: A float tensor of shape [batch_size, num_anchors,
            num_classes] representing the predicted logits for each class
          target_tensor: A float tensor of shape [batch_size, num_anchors,
            num_classes] representing one-hot encoded classification targets
          weights: a float tensor of shape [batch_size, num_anchors]

        Returns:
          loss: a float tensor of shape [batch_size, num_anchors]
            representing the value of the loss function.
        """
        num_classes = prediction_tensor.get_shape().as_list()[-1]
        prediction_tensor = tf.truediv(
            prediction_tensor, self._logit_scale, name='scale_logit')
        per_row_cross_ent = (tf.nn.softmax_cross_entropy_with_logits(
            labels=tf.reshape(target_tensor, [-1, num_classes]),
            logits=tf.reshape(prediction_tensor, [-1, num_classes])))
        return tf.reshape(per_row_cross_ent, tf.shape(weights)) * weights
//  [python]
def _compute_loss(self, prediction_tensor, target_tensor, weights):
        """Compute loss function.

        Args:
          prediction_tensor: A float tensor of shape [batch_size, num_anchors,
            num_classes] representing the predicted logits for each class
          target_tensor: A float tensor of shape [batch_size, num_anchors,
            num_classes] representing one-hot encoded classification targets
          weights: a float tensor of shape [batch_size, num_anchors]

        Returns:
          loss: a float tensor of shape [batch_size, num_anchors]
            representing the value of the loss function.
        """
        num_classes = prediction_tensor.get_shape().as_list()[-1]
        prediction_tensor = tf.divide(
            prediction_tensor, self._logit_scale, name='scale_logit')
        per_row_cross_ent = (tf.nn.softmax_cross_entropy_with_logits(
            labels=tf.reshape(target_tensor, [-1, num_classes]),
            logits=tf.reshape(prediction_tensor, [-1, num_classes])))
        return tf.reshape(per_row_cross_ent, tf.shape(weights)) * weights
//  [python]
def make_dropout_mask(self, shape, keep_prob):
        keep_prob = tf.convert_to_tensor(keep_prob, dtype=tf.float32)
        random_tensor = keep_prob + tf.random_uniform(shape, dtype=tf.float32)
        binary_tensor = tf.floor(random_tensor)
        dropout_mask = tf.truediv(binary_tensor, keep_prob)
        return dropout_mask
//  [python]
def make_dropout_mask(self, shape, keep_prob):
        keep_prob = tf.convert_to_tensor(keep_prob, dtype=tf.float32)
        random_tensor = keep_prob + tf.random_uniform(shape, dtype=tf.float32)
        binary_tensor = tf.floor(random_tensor)
        dropout_mask = tf.divide(binary_tensor, keep_prob)
        return dropout_mask
//  [python]
def AngleAxisToRotationMatrix(angle_axis):
        """ angle_axis is a 3d vector whose direction points to the rotation axis and whose norm is the angle (in radians) """
        with tf.variable_scope("AngleAxisToRotationMatrix"):
            theta = tf.norm(angle_axis)

            cos = tf.cos(theta)
            sin = tf.sin(theta)
            xyz = tf.truediv(angle_axis, theta)

            x = xyz[0]
            y = xyz[1]
            z = xyz[2]

            # when theta > 0
            R00 = cos + x * x * (1. - cos)
            R10 = sin * z + x * y * (1. - cos)
            R20 = -sin * y + x * z * (1. - cos)
            Rcol0 = tf.stack([R00, R10, R20], axis=0)

            R01 = x * y * (1. - cos) - z * sin
            R11 = cos + y * y * (1. - cos)
            R21 = x * sin + y * z * (1. - cos)
            Rcol1 = tf.stack([R01, R11, R21], axis=0)

            R02 = y * sin + x * z * (1. - cos)
            R12 = -x * sin + y * z * (1. - cos)
            R22 = cos + z * z * (1. - cos)
            Rcol2 = tf.stack([R02, R12, R22], axis=0)

            R = tf.stack([Rcol0, Rcol1, Rcol2], axis=1)

            # when theta == 0
            R_00 = tf.ones([], dtype=angle_axis.dtype)
            R_10 = angle_axis[2]
            R_20 = -angle_axis[1]
            R_col0 = tf.stack([R_00, R_10, R_20], axis=0)

            R_01 = -angle_axis[2]
            R_11 = tf.ones([], dtype=angle_axis.dtype)
            R_21 = angle_axis[0]
            R_col1 = tf.stack([R_01, R_11, R_21], axis=0)

            R_02 = angle_axis[1]
            R_12 = -angle_axis[0]
            R_22 = tf.ones([], dtype=angle_axis.dtype)
            R_col2 = tf.stack([R_02, R_12, R_22], axis=0)

            R_ = tf.stack([R_col0, R_col1, R_col2], axis=1)

            return tf.cond(tf.greater(theta, 0), lambda: R, lambda: R_)
//  [python]
def AngleAxisToRotationMatrix(angle_axis):
        """ angle_axis is a 3d vector whose direction points to the rotation axis and whose norm is the angle (in radians) """
        with tf.variable_scope("AngleAxisToRotationMatrix"):
            theta = tf.norm(angle_axis)

            cos = tf.cos(theta)
            sin = tf.sin(theta)
            xyz = tf.divide(angle_axis, theta)

            x = xyz[0]
            y = xyz[1]
            z = xyz[2]

            # when theta > 0
            R00 = cos + x * x * (1. - cos)
            R10 = sin * z + x * y * (1. - cos)
            R20 = -sin * y + x * z * (1. - cos)
            Rcol0 = tf.stack([R00, R10, R20], axis=0)

            R01 = x * y * (1. - cos) - z * sin
            R11 = cos + y * y * (1. - cos)
            R21 = x * sin + y * z * (1. - cos)
            Rcol1 = tf.stack([R01, R11, R21], axis=0)

            R02 = y * sin + x * z * (1. - cos)
            R12 = -x * sin + y * z * (1. - cos)
            R22 = cos + z * z * (1. - cos)
            Rcol2 = tf.stack([R02, R12, R22], axis=0)

            R = tf.stack([Rcol0, Rcol1, Rcol2], axis=1)

            # when theta == 0
            R_00 = tf.ones([], dtype=angle_axis.dtype)
            R_10 = angle_axis[2]
            R_20 = -angle_axis[1]
            R_col0 = tf.stack([R_00, R_10, R_20], axis=0)

            R_01 = -angle_axis[2]
            R_11 = tf.ones([], dtype=angle_axis.dtype)
            R_21 = angle_axis[0]
            R_col1 = tf.stack([R_01, R_11, R_21], axis=0)

            R_02 = angle_axis[1]
            R_12 = -angle_axis[0]
            R_22 = tf.ones([], dtype=angle_axis.dtype)
            R_col2 = tf.stack([R_02, R_12, R_22], axis=0)

            R_ = tf.stack([R_col0, R_col1, R_col2], axis=1)

            return tf.cond(tf.greater(theta, 0), lambda: R, lambda: R_)
//  [python]
def _compute_loss(self, prediction_tensor, target_tensor, weights):
        """Compute loss function.

        Args:
          prediction_tensor: A float tensor of shape [batch_size, num_anchors,
            num_classes] representing the predicted logits for each class
          target_tensor: A float tensor of shape [batch_size, num_anchors,
            num_classes] representing logit classification targets
          weights: a float tensor of shape [batch_size, num_anchors]

        Returns:
          loss: a float tensor of shape [batch_size, num_anchors]
            representing the value of the loss function.
        """
        num_classes = prediction_tensor.get_shape().as_list()[-1]
        target_tensor = self._scale_and_softmax_logits(target_tensor)
        prediction_tensor = tf.div(prediction_tensor, self._logit_scale,
                                      name='scale_logits')

        per_row_cross_ent = (tf.nn.softmax_cross_entropy_with_logits(
            labels=tf.reshape(target_tensor, [-1, num_classes]),
            logits=tf.reshape(prediction_tensor, [-1, num_classes])))
        return tf.reshape(per_row_cross_ent, tf.shape(weights)) * weights
//  [python]
def _compute_loss(self, prediction_tensor, target_tensor, weights):
        """Compute loss function.

        Args:
          prediction_tensor: A float tensor of shape [batch_size, num_anchors,
            num_classes] representing the predicted logits for each class
          target_tensor: A float tensor of shape [batch_size, num_anchors,
            num_classes] representing logit classification targets
          weights: a float tensor of shape [batch_size, num_anchors]

        Returns:
          loss: a float tensor of shape [batch_size, num_anchors]
            representing the value of the loss function.
        """
        num_classes = prediction_tensor.get_shape().as_list()[-1]
        target_tensor = self._scale_and_softmax_logits(target_tensor)
        prediction_tensor = tf.divide(prediction_tensor, self._logit_scale,
                                      name='scale_logits')

        per_row_cross_ent = (tf.nn.softmax_cross_entropy_with_logits(
            labels=tf.reshape(target_tensor, [-1, num_classes]),
            logits=tf.reshape(prediction_tensor, [-1, num_classes])))
        return tf.reshape(per_row_cross_ent, tf.shape(weights)) * weights
//  [python]
def safe_div(x, y):
  return tf.where(tf.not_equal(y, 0), tf.truediv(x, y), tf.zeros_like(x))
//  [python]
def safe_div(x, y):
  return tf.where(tf.not_equal(y, 0), tf.divide(x, y), tf.zeros_like(x))
//  [python]
def discriminative_instance_loss(y_true, y_pred,
                                 delta_v=0.5,
                                 delta_d=1.5,
                                 gamma=1e-3):
    """Discriminative loss between an output tensor and a target tensor.

    Args:
        y_true: A tensor of the same shape as ``y_pred``.
        y_pred: A tensor of the vector embedding

    Returns:
        tensor: Output tensor.
    """
    y_pred = tf.convert_to_tensor(y_pred)
    y_true = K.cast(y_true, y_pred.dtype)

    def temp_norm(ten, axis=None):
        if axis is None:
            axis = 1 if K.image_data_format() == 'channels_first' else K.ndim(ten) - 1
        return K.sqrt(K.epsilon() + K.sum(K.square(ten), axis=axis))

    rank = K.ndim(y_pred)
    channel_axis = 1 if K.image_data_format() == 'channels_first' else rank - 1
    axes = [x for x in list(range(rank)) if x != channel_axis]

    # Compute variance loss
    cells_summed = tf.tensordot(y_true, y_pred, axes=[axes, axes])
    nonzeros = tf.math.count_nonzero(y_true, axis=axes)
    n_pixels = K.cast(nonzeros, dtype=y_pred.dtype) + K.epsilon()
    n_pixels_expand = K.expand_dims(n_pixels, axis=1) + K.epsilon()
    mu = tf.divide(cells_summed, n_pixels_expand)

    delta_v = K.constant(delta_v, dtype=y_pred.dtype)
    mu_tensor = tf.tensordot(y_true, mu, axes=[[channel_axis], [0]])
    L_var_1 = y_pred - mu_tensor
    L_var_2 = K.square(K.relu(temp_norm(L_var_1) - delta_v))
    L_var_3 = tf.tensordot(L_var_2, y_true, axes=[axes, axes])
    L_var_4 = tf.divide(L_var_3, n_pixels)
    L_var = K.mean(L_var_4)

    # Compute distance loss
    mu_a = K.expand_dims(mu, axis=0)
    mu_b = K.expand_dims(mu, axis=1)

    diff_matrix = tf.subtract(mu_b, mu_a)
    L_dist_1 = temp_norm(diff_matrix)
    L_dist_2 = K.square(K.relu(K.constant(2 * delta_d, dtype=y_pred.dtype) - L_dist_1))
    diag = K.constant(0, dtype=y_pred.dtype) * tf.linalg.diag_part(L_dist_2)
    L_dist_3 = tf.linalg.set_diag(L_dist_2, diag)
    L_dist = K.mean(L_dist_3)

    # Compute regularization loss
    L_reg = gamma * temp_norm(mu)
    L = L_var + L_dist + K.mean(L_reg)

    return L
//  [python]
def call(self, X):
        sigmaI = 1e9
        errmsg = "Dimension Mismatch, X is [_, self.dataDimension]"
        assert (len(X.shape) == 2 and int(X.shape[1]) == self.dataDimension), errmsg

        X_ = tf.matmul(self.Z, X, transpose_b=True) / self.projectionDimension

        W_ = self.W[0:(self.numClasses)]
        V_ = self.V[0:(self.numClasses)]

        __nodeProb = []
        __nodeProb.append(1)

        score_ = __nodeProb[0] * tf.multiply(tf.matmul(W_, X_), tf.tanh(self.sigma * tf.matmul(V_, X_)))
        for i in range(1, self.totalNodes):
            W_ = self.W[i * self.numClasses:((i + 1) * self.numClasses)]
            V_ = self.V[i * self.numClasses:((i + 1) * self.numClasses)]

            T_ = tf.reshape(self.T[int(np.ceil(i / 2.0) - 1.0)], [-1, self.projectionDimension])
            prob = (1 + ((-1)**(i + 1)) * tf.tanh(tf.multiply(sigmaI, tf.matmul(T_, X_))))

            prob = prob / 2.0
            prob = __nodeProb[int(np.ceil(i / 2.0) - 1.0)] * prob
            __nodeProb.append(prob)
            score_ += __nodeProb[i] * tf.multiply(tf.matmul(W_, X_), tf.tanh(self.sigma * tf.matmul(V_, X_)))

        self.score = score_
        #return score_
        # Classification.
        if (self.isRegression == False):
            if self.numClasses > 2:
                self.prediction = tf.argmax(tf.transpose(self.score), 1)
            else:
                self.prediction = tf.argmax(
                    tf.concat([tf.transpose(self.score),
                               0 * tf.transpose(self.score)], 1), 1)
        # Regression.
        elif (self.isRegression == True):
            # For regression , scores are the actual predictions, just return them.
            self.prediction = self.score

        return self.prediction
//  [python]
def call(self, X):
        sigmaI = 1e9
        errmsg = "Dimension Mismatch, X is [_, self.dataDimension]"
        assert (len(X.shape) == 2 and int(X.shape[1]) == self.dataDimension), errmsg

        X_ = tf.divide(tf.matmul(self.Z, X, transpose_b=True), self.projectionDimension)

        W_ = self.W[0:(self.numClasses)]
        V_ = self.V[0:(self.numClasses)]

        __nodeProb = []
        __nodeProb.append(1)

        score_ = __nodeProb[0] * tf.multiply(tf.matmul(W_, X_), tf.tanh(self.sigma * tf.matmul(V_, X_)))
        for i in range(1, self.totalNodes):
            W_ = self.W[i * self.numClasses:((i + 1) * self.numClasses)]
            V_ = self.V[i * self.numClasses:((i + 1) * self.numClasses)]

            T_ = tf.reshape(self.T[int(np.ceil(i / 2.0) - 1.0)], [-1, self.projectionDimension])
            prob = (1 + ((-1)**(i + 1)) * tf.tanh(tf.multiply(sigmaI, tf.matmul(T_, X_))))

            prob = tf.divide(prob, 2.0)
            prob = __nodeProb[int(np.ceil(i / 2.0) - 1.0)] * prob
            __nodeProb.append(prob)
            score_ += __nodeProb[i] * tf.multiply(tf.matmul(W_, X_), tf.tanh(self.sigma * tf.matmul(V_, X_)))

        self.score = score_
        #return score_
        # Classification.
        if (self.isRegression == False):
            if self.numClasses > 2:
                self.prediction = tf.argmax(tf.transpose(self.score), 1)
            else:
                self.prediction = tf.argmax(
                    tf.concat([tf.transpose(self.score),
                               0 * tf.transpose(self.score)], 1), 1)
        # Regression.
        elif (self.isRegression == True):
            # For regression , scores are the actual predictions, just return them.
            self.prediction = self.score

        return self.prediction
//  [python]
def call(self, inputs, **kwargs):
        """inputs 是一个列表"""
        query, candidate = inputs 
        # 计算两个向量的二范数
        query_norm = tf.norm(query, axis=self.axis) # (B, 1)
        candidate_norm = tf.norm(candidate, axis=self.axis)
        # 计算相似度
        scores = tf.reduce_sum(tf.multiply(query, candidate), axis=-1)#(B,1)
        # 相似度除以二范数, 防止除零
        scores = tf.truediv(scores, query_norm * candidate_norm + 1e-8)
        # 对score的范围限制到(-1, 1)之间
        scores = tf.clip_by_value(scores, -1, 1)
        # 乘以温度系数
        score = scores * self.temperature 
        return score
//  [python]
def call(self, inputs, **kwargs):
        """inputs 是一个列表"""
        query, candidate = inputs 
        # 计算两个向量的二范数
        query_norm = tf.norm(query, axis=self.axis) # (B, 1)
        candidate_norm = tf.norm(candidate, axis=self.axis)
        # 计算相似度
        scores = tf.reduce_sum(tf.multiply(query, candidate), axis=-1)#(B,1)
        # 相似度除以二范数, 防止除零
        scores = tf.divide(scores, query_norm * candidate_norm + 1e-8)
        # 对score的范围限制到(-1, 1)之间
        scores = tf.clip_by_value(scores, -1, 1)
        # 乘以温度系数
        score = scores * self.temperature 
        return score
//  [python]
def __call__(self, X, sigmaI):
        '''
        Function to build the Bonsai Tree graph
        Expected Dimensions

        X is [_, self.dataDimension]
        '''
        errmsg = "Dimension Mismatch, X is [_, self.dataDimension]"
        assert (len(X.shape) == 2 and int(
            X.shape[1]) == self.dataDimension), errmsg
        if self.score is not None:
            return self.score, self.X_

        X_ = tf.div(tf.matmul(self.Z, X, transpose_b=True),
                       self.projectionDimension)

        W_ = self.W[0:(self.numClasses)]
        V_ = self.V[0:(self.numClasses)]

        self.__nodeProb = []
        self.__nodeProb.append(1)

        score_ = self.__nodeProb[0] * tf.multiply(
            tf.matmul(W_, X_), tf.tanh(self.sigma * tf.matmul(V_, X_)))
        for i in range(1, self.totalNodes):
            W_ = self.W[i * self.numClasses:((i + 1) * self.numClasses)]
            V_ = self.V[i * self.numClasses:((i + 1) * self.numClasses)]

            T_ = tf.reshape(self.T[int(np.ceil(i / 2.0) - 1.0)],
                            [-1, self.projectionDimension])
            prob = (1 + ((-1)**(i + 1)) *
                    tf.tanh(tf.multiply(sigmaI, tf.matmul(T_, X_))))

            prob = tf.div(prob, 2.0)
            prob = self.__nodeProb[int(np.ceil(i / 2.0) - 1.0)] * prob
            self.__nodeProb.append(prob)
            score_ += self.__nodeProb[i] * tf.multiply(
                tf.matmul(W_, X_), tf.tanh(self.sigma * tf.matmul(V_, X_)))

        self.score = score_
        self.X_ = X_
        return self.score, self.X_
//  [python]
def __call__(self, X, sigmaI):
        '''
        Function to build the Bonsai Tree graph
        Expected Dimensions

        X is [_, self.dataDimension]
        '''
        errmsg = "Dimension Mismatch, X is [_, self.dataDimension]"
        assert (len(X.shape) == 2 and int(
            X.shape[1]) == self.dataDimension), errmsg
        if self.score is not None:
            return self.score, self.X_

        X_ = tf.divide(tf.matmul(self.Z, X, transpose_b=True),
                       self.projectionDimension)

        W_ = self.W[0:(self.numClasses)]
        V_ = self.V[0:(self.numClasses)]

        self.__nodeProb = []
        self.__nodeProb.append(1)

        score_ = self.__nodeProb[0] * tf.multiply(
            tf.matmul(W_, X_), tf.tanh(self.sigma * tf.matmul(V_, X_)))
        for i in range(1, self.totalNodes):
            W_ = self.W[i * self.numClasses:((i + 1) * self.numClasses)]
            V_ = self.V[i * self.numClasses:((i + 1) * self.numClasses)]

            T_ = tf.reshape(self.T[int(np.ceil(i / 2.0) - 1.0)],
                            [-1, self.projectionDimension])
            prob = (1 + ((-1)**(i + 1)) *
                    tf.tanh(tf.multiply(sigmaI, tf.matmul(T_, X_))))

            prob = tf.divide(prob, 2.0)
            prob = self.__nodeProb[int(np.ceil(i / 2.0) - 1.0)] * prob
            self.__nodeProb.append(prob)
            score_ += self.__nodeProb[i] * tf.multiply(
                tf.matmul(W_, X_), tf.tanh(self.sigma * tf.matmul(V_, X_)))

        self.score = score_
        self.X_ = X_
        return self.score, self.X_
//  [python]
def downsample(image, coordinates, **config):
    with tf.name_scope('gaussian_blur'):
        k_size = config['blur_size']
        kernel = cv.getGaussianKernel(k_size, 0)[:, 0]
        kernel = np.outer(kernel, kernel).astype(np.float32)
        kernel = tf.reshape(tf.convert_to_tensor(kernel), [k_size]*2+[1, 1])
        pad_size = int(k_size/2)
        image = tf.pad(image, [[pad_size]*2, [pad_size]*2, [0, 0]], 'REFLECT')
        image = tf.expand_dims(image, axis=0)  # add batch dim
        image = tf.nn.depthwise_conv2d(image, kernel, [1, 1, 1, 1], 'VALID')[0]

    with tf.name_scope('downsample'):
        ratio = tf.truediv(tf.convert_to_tensor(config['resize']), tf.shape(image)[0:2])
        coordinates = coordinates * tf.cast(ratio, tf.float32)
        image = tf.image.resize_images(image, config['resize'],
                                       method=tf.image.ResizeMethod.BILINEAR)

    return image, coordinates
//  [python]
def downsample(image, coordinates, **config):
    with tf.name_scope('gaussian_blur'):
        k_size = config['blur_size']
        kernel = cv.getGaussianKernel(k_size, 0)[:, 0]
        kernel = np.outer(kernel, kernel).astype(np.float32)
        kernel = tf.reshape(tf.convert_to_tensor(kernel), [k_size]*2+[1, 1])
        pad_size = int(k_size/2)
        image = tf.pad(image, [[pad_size]*2, [pad_size]*2, [0, 0]], 'REFLECT')
        image = tf.expand_dims(image, axis=0)  # add batch dim
        image = tf.nn.depthwise_conv2d(image, kernel, [1, 1, 1, 1], 'VALID')[0]

    with tf.name_scope('downsample'):
        ratio = tf.divide(tf.convert_to_tensor(config['resize']), tf.shape(image)[0:2])
        coordinates = coordinates * tf.cast(ratio, tf.float32)
        image = tf.image.resize_images(image, config['resize'],
                                       method=tf.image.ResizeMethod.BILINEAR)

    return image, coordinates
//  [python]
def divide(x, y):
    """
    Computes Python style division of x by y.

    Parameters
    ----------
    x : tensor
        A Tensor
    y : tensor
        A Tensor

    Returns
    -------
        A Tensor with same shape as input

    Examples
    ---------
    >>> import tensorlayerx as tlx
    >>> x = tlx.ops.constant(value=[0.9142202  0.72091234])
    >>> y = tlx.ops.divide(x, x)

    """

    return x / y
//  [python]
def divide(x, y):
    """
    Computes Python style division of x by y.

    Parameters
    ----------
    x : tensor
        A Tensor
    y : tensor
        A Tensor

    Returns
    -------
        A Tensor with same shape as input

    Examples
    ---------
    >>> import tensorlayerx as tlx
    >>> x = tlx.ops.constant(value=[0.9142202  0.72091234])
    >>> y = tlx.ops.divide(x, x)

    """

    return tf.divide(x, y)
//  [python]
def get_transform(FLAGS):
  # Generate random tansformation
  ang = np.deg2rad(1.0)
  tx = tf.random.uniform(shape = [FLAGS.batch_size, 1], minval = -2.0, maxval = 2.0, dtype = tf.float32)
  ty = tf.random.uniform(shape = [FLAGS.batch_size, 1], minval = -2.0, maxval = 2.0, dtype = tf.float32)
  r = tf.random.uniform(shape = [FLAGS.batch_size, 1], minval = -ang, maxval = ang, dtype = tf.float32)
  z = tf.random.uniform(shape = [FLAGS.batch_size, 1], minval = 0.97, maxval = 1.03, dtype = tf.float32)
  hx = tf.random.uniform(shape = [FLAGS.batch_size, 1], minval = -ang, maxval = ang, dtype = tf.float32)
  hy = tf.random.uniform(shape = [FLAGS.batch_size, 1], minval = -ang, maxval = ang, dtype = tf.float32)
  sx = FLAGS.img_shapes[0]
  sy = FLAGS.img_shapes[1]

  # Construct transformation matrix
  a = hx - r
  b = hy + r
  T1 = z*tf.cos(a)/tf.cos(hx)
  T2 = z*tf.sin(a)/tf.cos(hx) 
  T3 = (sx*tf.cos(hx)-sx*z*tf.cos(a) + 2*tx*z*tf.cos(a) - sy*z*tf.sin(a) + 2*ty*z*tf.sin(a))/(2*tf.cos(hx))
  T4 = z*tf.sin(b)/tf.cos(hy)
  T5 = z*tf.cos(b)/tf.cos(hy)
  T6 = (sy*tf.cos(hy)-sy*z*tf.cos(b)+2*ty*z*tf.cos(b)-sx*z*tf.sin(b)+2*tx*z*tf.sin(b))/(2*tf.cos(hy))
  T7 = tf.zeros ([FLAGS.batch_size, 2], 'float32')
  T = tf.concat([T1, T2, T3, T4, T5, T6, T7], 1)
  return T
//  [python]
def get_transform(FLAGS):
  # Generate random tansformation
  ang = np.deg2rad(1.0)
  tx = tf.random.uniform(shape = [FLAGS.batch_size, 1], minval = -2.0, maxval = 2.0, dtype = tf.float32)
  ty = tf.random.uniform(shape = [FLAGS.batch_size, 1], minval = -2.0, maxval = 2.0, dtype = tf.float32)
  r = tf.random.uniform(shape = [FLAGS.batch_size, 1], minval = -ang, maxval = ang, dtype = tf.float32)
  z = tf.random.uniform(shape = [FLAGS.batch_size, 1], minval = 0.97, maxval = 1.03, dtype = tf.float32)
  hx = tf.random.uniform(shape = [FLAGS.batch_size, 1], minval = -ang, maxval = ang, dtype = tf.float32)
  hy = tf.random.uniform(shape = [FLAGS.batch_size, 1], minval = -ang, maxval = ang, dtype = tf.float32)
  sx = FLAGS.img_shapes[0]
  sy = FLAGS.img_shapes[1]

  # Construct transformation matrix
  a = hx - r
  b = hy + r
  T1 = tf.divide(z*tf.cos(a), tf.cos(hx))
  T2 = tf.divide(z*tf.sin(a), tf.cos(hx)) 
  T3 = tf.divide(sx*tf.cos(hx)-sx*z*tf.cos(a) + 2*tx*z*tf.cos(a) - sy*z*tf.sin(a) + 2*ty*z*tf.sin(a), 2*tf.cos(hx))
  T4 = tf.divide(z*tf.sin(b), tf.cos(hy))
  T5 = tf.divide(z*tf.cos(b), tf.cos(hy))
  T6 = tf.divide(sy*tf.cos(hy)-sy*z*tf.cos(b)+2*ty*z*tf.cos(b)-sx*z*tf.sin(b)+2*tx*z*tf.sin(b), 2*tf.cos(hy))
  T7 = tf.zeros ([FLAGS.batch_size, 2], 'float32')
  T = tf.concat([T1, T2, T3, T4, T5, T6, T7], 1)
  return T
//  [python]
def _reduce_mean(x):
    nelem = _nelem(x)
    x = _nan2zero(x)
    return tf.reduce_sum(x) / nelem
//  [python]
def _reduce_mean(x):
    nelem = _nelem(x)
    x = _nan2zero(x)
    return tf.divide(tf.reduce_sum(x), nelem)
//  [python]
def sum_normalize(cs, axis=3):
        reduce_sum = tf.reduce_sum(cs, axis, keep_dims=True, name='sum')
        return tf.div(cs, reduce_sum, name='sumNormalized')
//  [python]
def sum_normalize(cs, axis=3):
        reduce_sum = tf.reduce_sum(cs, axis, keep_dims=True, name='sum')
        return tf.divide(cs, reduce_sum, name='sumNormalized')
//  [python]
def l2_normalize_channelwise(features):
        norms = tf.norm(features, ord='euclidean', axis=3, name='norm')
        # expanding the norms tensor to support broadcast division
        norms_expanded = tf.expand_dims(norms, 3)
        features = tf.truediv(features, norms_expanded, name='normalized')
        return features
//  [python]
def l2_normalize_channelwise(features):
        norms = tf.norm(features, ord='euclidean', axis=3, name='norm')
        # expanding the norms tensor to support broadcast division
        norms_expanded = tf.expand_dims(norms, 3)
        features = tf.divide(features, norms_expanded, name='normalized')
        return features
//  [python]
def look_at(eye, center, world_up):
    """Computes camera viewing matrices.

    Functionality mimes gluLookAt (third_party/GL/glu/include/GLU/glu.h).

    Args:
      eye: 2-D float32 tensor with shape [batch_size, 3] containing the XYZ world
          space position of the camera.
      center: 2-D float32 tensor with shape [batch_size, 3] containing a position
          along the center of the camera's gaze.
      world_up: 2-D float32 tensor with shape [batch_size, 3] specifying the
          world's up direction; the output camera will have no tilt with respect
          to this direction.

    Returns:
      A [batch_size, 4, 4] float tensor containing a right-handed camera
      extrinsics matrix that maps points from world space to points in eye space.
    """
    batch_size = center.shape[0].value
    vector_degeneracy_cutoff = 1e-6
    forward = center - eye
    forward_norm = tf.norm(forward, ord='euclidean', axis=1, keep_dims=True)
    tf.assert_greater(
        forward_norm,
        vector_degeneracy_cutoff,
        message='Camera matrix is degenerate because eye and center are close.')
    forward = tf.truediv(forward, forward_norm)

    to_side = tf.cross(forward, world_up)
    to_side_norm = tf.norm(to_side, ord='euclidean', axis=1, keep_dims=True)
    tf.assert_greater(
        to_side_norm,
        vector_degeneracy_cutoff,
        message='Camera matrix is degenerate because up and gaze are close or'
        'because up is degenerate.')
    to_side = tf.truediv(to_side, to_side_norm)
    cam_up = tf.cross(to_side, forward)

    w_column = tf.constant(
        batch_size * [[0., 0., 0., 1.]], dtype=tf.float32)  # [batch_size, 4]
    w_column = tf.reshape(w_column, [batch_size, 4, 1])
    view_rotation = tf.stack(
        [to_side, cam_up, -forward,
         tf.zeros_like(to_side, dtype=tf.float32)],
        axis=1)  # [batch_size, 4, 3] matrix
    view_rotation = tf.concat(
        [view_rotation, w_column], axis=2)  # [batch_size, 4, 4]

    identity_batch = tf.tile(tf.expand_dims(tf.eye(3), 0), [batch_size, 1, 1])
    view_translation = tf.concat([identity_batch, tf.expand_dims(-eye, 2)], 2)
    view_translation = tf.concat(
        [view_translation,
         tf.reshape(w_column, [batch_size, 1, 4])], 1)
    camera_matrices = tf.matmul(view_rotation, view_translation)
    return camera_matrices
//  [python]
def look_at(eye, center, world_up):
    """Computes camera viewing matrices.

    Functionality mimes gluLookAt (third_party/GL/glu/include/GLU/glu.h).

    Args:
      eye: 2-D float32 tensor with shape [batch_size, 3] containing the XYZ world
          space position of the camera.
      center: 2-D float32 tensor with shape [batch_size, 3] containing a position
          along the center of the camera's gaze.
      world_up: 2-D float32 tensor with shape [batch_size, 3] specifying the
          world's up direction; the output camera will have no tilt with respect
          to this direction.

    Returns:
      A [batch_size, 4, 4] float tensor containing a right-handed camera
      extrinsics matrix that maps points from world space to points in eye space.
    """
    batch_size = center.shape[0].value
    vector_degeneracy_cutoff = 1e-6
    forward = center - eye
    forward_norm = tf.norm(forward, ord='euclidean', axis=1, keep_dims=True)
    tf.assert_greater(
        forward_norm,
        vector_degeneracy_cutoff,
        message='Camera matrix is degenerate because eye and center are close.')
    forward = tf.divide(forward, forward_norm)

    to_side = tf.cross(forward, world_up)
    to_side_norm = tf.norm(to_side, ord='euclidean', axis=1, keep_dims=True)
    tf.assert_greater(
        to_side_norm,
        vector_degeneracy_cutoff,
        message='Camera matrix is degenerate because up and gaze are close or'
        'because up is degenerate.')
    to_side = tf.divide(to_side, to_side_norm)
    cam_up = tf.cross(to_side, forward)

    w_column = tf.constant(
        batch_size * [[0., 0., 0., 1.]], dtype=tf.float32)  # [batch_size, 4]
    w_column = tf.reshape(w_column, [batch_size, 4, 1])
    view_rotation = tf.stack(
        [to_side, cam_up, -forward,
         tf.zeros_like(to_side, dtype=tf.float32)],
        axis=1)  # [batch_size, 4, 3] matrix
    view_rotation = tf.concat(
        [view_rotation, w_column], axis=2)  # [batch_size, 4, 4]

    identity_batch = tf.tile(tf.expand_dims(tf.eye(3), 0), [batch_size, 1, 1])
    view_translation = tf.concat([identity_batch, tf.expand_dims(-eye, 2)], 2)
    view_translation = tf.concat(
        [view_translation,
         tf.reshape(w_column, [batch_size, 1, 4])], 1)
    camera_matrices = tf.matmul(view_rotation, view_translation)
    return camera_matrices
//  [python]
def _mean_pool(self, rnn_outputs, batch_size, max_char_len, max_word_len, char_lens):
        """
        Perform mean-pooling after the character-RNN layer.

        :param rnn_outputs: hidden states of all the time steps after the character-RNN layer
        :return: mean of the hidden states over every time step
        """
        # perform mean pooling over characters
        rnn_outputs = tf.reduce_mean(rnn_outputs, reduction_indices=1)

        # In order to avoid 0 padding affect the mean, multiply by `n / m` where `n` is
        # `max_char_len` and `m` is `char_lens`
        rnn_outputs = tf.multiply(rnn_outputs, tf.cast(max_char_len, tf.float32))  # multiply by `n`

        # swap the dimensions in order to divide by an appropriate value for each time step
        rnn_outputs = tf.transpose(rnn_outputs)

        rnn_outputs = tf.truediv(rnn_outputs, tf.cast(char_lens, tf.float32))  # divide by `m`
        rnn_outputs = tf.transpose(rnn_outputs)  # shape back to the original shape

        # batch and word-len dimensions were merged before running character-RNN so shape it back
        rnn_outputs = tf.reshape(rnn_outputs, [batch_size, max_word_len, self._char_rnn_size * 2])

        # there are NaN due to padded words (with char_len=0) so convert those NaN to 0
        rnn_outputs = tf.where(tf.is_nan(rnn_outputs), tf.zeros_like(rnn_outputs), rnn_outputs)

        return rnn_outputs
//  [python]
def _mean_pool(self, rnn_outputs, batch_size, max_char_len, max_word_len, char_lens):
        """
        Perform mean-pooling after the character-RNN layer.

        :param rnn_outputs: hidden states of all the time steps after the character-RNN layer
        :return: mean of the hidden states over every time step
        """
        # perform mean pooling over characters
        rnn_outputs = tf.reduce_mean(rnn_outputs, reduction_indices=1)

        # In order to avoid 0 padding affect the mean, multiply by `n / m` where `n` is
        # `max_char_len` and `m` is `char_lens`
        rnn_outputs = tf.multiply(rnn_outputs, tf.cast(max_char_len, tf.float32))  # multiply by `n`

        # swap the dimensions in order to divide by an appropriate value for each time step
        rnn_outputs = tf.transpose(rnn_outputs)

        rnn_outputs = tf.divide(rnn_outputs, tf.cast(char_lens, tf.float32))  # divide by `m`
        rnn_outputs = tf.transpose(rnn_outputs)  # shape back to the original shape

        # batch and word-len dimensions were merged before running character-RNN so shape it back
        rnn_outputs = tf.reshape(rnn_outputs, [batch_size, max_word_len, self._char_rnn_size * 2])

        # there are NaN due to padded words (with char_len=0) so convert those NaN to 0
        rnn_outputs = tf.where(tf.is_nan(rnn_outputs), tf.zeros_like(rnn_outputs), rnn_outputs)

        return rnn_outputs
//  [python]
def get_softmax_pairwise_scores(predicted_scores):
	exp_predicted_scores = 2 ** predicted_scores
	exp_predicted_scores = tf.div(exp_predicted_scores, tf.reduce_sum(exp_predicted_scores))
	pairwise_predicted_scores = exp_predicted_scores - tf.transpose(exp_predicted_scores)
	return pairwise_predicted_scores
//  [python]
def get_softmax_pairwise_scores(predicted_scores):
	exp_predicted_scores = 2 ** predicted_scores
	exp_predicted_scores = tf.divide(exp_predicted_scores, tf.reduce_sum(exp_predicted_scores))
	pairwise_predicted_scores = exp_predicted_scores - tf.transpose(exp_predicted_scores)
	return pairwise_predicted_scores
//  [python]
def project_tf(joint3d, calibK, calibR=None, calibt=None, calibDistCoef=None):
        """ This function projects the 3D hand to 2D using camera parameters
        """
        with tf.name_scope('project_tf'):
            x = joint3d
            if calibR is not None:
                x = tf.matmul(joint3d, calibR, transpose_b=True)
            if calibt is not None:
                x = x + calibt
            xi = tf.truediv(x[:, 0], x[:, 2])
            yi = tf.truediv(x[:, 1], x[:, 2])

            if calibDistCoef is not None:
                X2 = xi * xi
                Y2 = yi * yi
                XY = X2 * Y2
                R2 = X2 + Y2
                R4 = R2 * R2
                R6 = R4 * R2

                dc = calibDistCoef
                radial = 1.0 + dc[0] * R2 + dc[1] * R4 + dc[4] * R6
                tan_x = 2.0 * dc[2] * XY + dc[3] * (R2 + 2.0 * X2)
                tan_y = 2.0 * dc[3] * XY + dc[2] * (R2 + 2.0 * Y2)

                xi = radial * xi + tan_x
                yi = radial * yi + tan_y

            xp = tf.transpose(tf.stack([xi, yi], axis=0))
            pt = tf.matmul(xp, calibK[:2, :2], transpose_b=True) + calibK[:2, 2]
        return pt, x
//  [python]
def project_tf(joint3d, calibK, calibR=None, calibt=None, calibDistCoef=None):
        """ This function projects the 3D hand to 2D using camera parameters
        """
        with tf.name_scope('project_tf'):
            x = joint3d
            if calibR is not None:
                x = tf.matmul(joint3d, calibR, transpose_b=True)
            if calibt is not None:
                x = x + calibt
            xi = tf.divide(x[:, 0], x[:, 2])
            yi = tf.divide(x[:, 1], x[:, 2])

            if calibDistCoef is not None:
                X2 = xi * xi
                Y2 = yi * yi
                XY = X2 * Y2
                R2 = X2 + Y2
                R4 = R2 * R2
                R6 = R4 * R2

                dc = calibDistCoef
                radial = 1.0 + dc[0] * R2 + dc[1] * R4 + dc[4] * R6
                tan_x = 2.0 * dc[2] * XY + dc[3] * (R2 + 2.0 * X2)
                tan_y = 2.0 * dc[3] * XY + dc[2] * (R2 + 2.0 * Y2)

                xi = radial * xi + tan_x
                yi = radial * yi + tan_y

            xp = tf.transpose(tf.stack([xi, yi], axis=0))
            pt = tf.matmul(xp, calibK[:2, :2], transpose_b=True) + calibK[:2, 2]
        return pt, x
//  [python]
def tf_smooth_eig_vec(self):
        """Function that returns smoothed version of min eigen vector."""
        _, matrix_m = self.dual_object.get_full_psd_matrix()
        # Easier to think in terms of max so negating the matrix
        [eig_vals, eig_vectors] = tf.self_adjoint_eig(-matrix_m)
        exp_eig_vals = tf.exp(tf.div(eig_vals, self.smooth_placeholder))
        scaling_factor = tf.reduce_sum(exp_eig_vals)
        # Multiplying each eig vector by exponential of corresponding eig value
        # Scaling factor normalizes the vector to be unit norm
        eig_vec_smooth = tf.div(
            tf.matmul(eig_vectors, tf.diag(tf.sqrt(exp_eig_vals))),
            tf.sqrt(scaling_factor),
        )
        return tf.reshape(
            tf.reduce_sum(eig_vec_smooth, axis=1),
            shape=[eig_vec_smooth.shape[0].value, 1],
        )
//  [python]
def tf_smooth_eig_vec(self):
        """Function that returns smoothed version of min eigen vector."""
        _, matrix_m = self.dual_object.get_full_psd_matrix()
        # Easier to think in terms of max so negating the matrix
        [eig_vals, eig_vectors] = tf.self_adjoint_eig(-matrix_m)
        exp_eig_vals = tf.exp(tf.divide(eig_vals, self.smooth_placeholder))
        scaling_factor = tf.reduce_sum(exp_eig_vals)
        # Multiplying each eig vector by exponential of corresponding eig value
        # Scaling factor normalizes the vector to be unit norm
        eig_vec_smooth = tf.divide(
            tf.matmul(eig_vectors, tf.diag(tf.sqrt(exp_eig_vals))),
            tf.sqrt(scaling_factor),
        )
        return tf.reshape(
            tf.reduce_sum(eig_vec_smooth, axis=1),
            shape=[eig_vec_smooth.shape[0].value, 1],
        )