jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def _softmax_with_mask(self, um_batch, m_mask_batch):
        exp_um_batch = tf.exp(um_batch)  # [N, M]
        masked_batch = exp_um_batch * m_mask_batch  # [N, M]
        sum_2d_batch = tf.expand_dims(tf.reduce_sum(masked_batch, 1), -1)  # [N, 1]
        p_batch = tf.divide(masked_batch, sum_2d_batch, name='p')  # [N, M]
        return p_batch
//  [python]
def validation_loss(self, train, x_test):
		"""
		root mean squared error loss between the predicted and actual ratings
		"""
		# make predictions from the model
		predictions = self.inference(train)
		# check to see what values are zero in test set as the data is sparse
		mask=tf.where(tf.equal(x_test,0.0), tf.zeros_like(x_test), x_test)
		# num of non zero values in the test set
		num_test_labels=tf.cast(tf.count_nonzero(mask),dtype=tf.float32)
		# convert to boolean 
		bool_mask=tf.cast(mask,dtype=tf.bool) 
		predictions = tf.where(bool_mask, predictions, tf.zeros_like(predictions))
		mse=self.compute_loss(predictions, x_test, num_test_labels)
		rmse=tf.sqrt(mse)
		ab_ops=tf.div(tf.reduce_sum(tf.abs(tf.subtract(x_test,predictions))),num_test_labels)
		return predictions, mse, rmse, ab_ops
//  [python]
def validation_loss(self, train, x_test):
		"""
		root mean squared error loss between the predicted and actual ratings
		"""
		# make predictions from the model
		predictions = self.inference(train)
		# check to see what values are zero in test set as the data is sparse
		mask=tf.where(tf.equal(x_test,0.0), tf.zeros_like(x_test), x_test)
		# num of non zero values in the test set
		num_test_labels=tf.cast(tf.count_nonzero(mask),dtype=tf.float32)
		# convert to boolean 
		bool_mask=tf.cast(mask,dtype=tf.bool) 
		predictions = tf.where(bool_mask, predictions, tf.zeros_like(predictions))
		mse=self.compute_loss(predictions, x_test, num_test_labels)
		rmse=tf.sqrt(mse)
		ab_ops=tf.divide(tf.reduce_sum(tf.abs(tf.subtract(x_test,predictions))),num_test_labels)
		return predictions, mse, rmse, ab_ops
//  [python]
batch_x_squash = tf.div(batch_feature, 255.)
//  [python]
batch_size =64

m_min = 0.2
m_max = 0.9
m = m_min

with tf.device('/gpu:0'):
     
    with slim.arg_scope([slim.variable], device='/cpu:0'):
        
        global_step = tf.Variable(0, name='global_step', trainable=False)
        
        """Define the dataflow graph."""
        m_op = tf.placeholder(dtype=tf.float32, shape=())

        batch_x_squash = tf.divide(batch_feature, 255.)
        batch_x = slim.batch_norm(batch_feature, center=False, is_training=True, trainable=True)
        
        output, pose_out = build_em(batch_x,batch_size, is_train=True,num_classes=num_classes)
        print(pose_out.get_shape())
        loss, _, _, _ = spread_loss( output,batch_size, pose_out, 
                                                    batch_x_squash, batch_labels, m)
        modeldir = './model3'
        acc,logits_idx = test_accuracy(output, batch_labels)
        
    lrn_rate = tf.maximum(tf.train.exponential_decay( 1e-3, global_step, decay_epoch, 0.8), 1e-5)
    opt = tf.train.AdamOptimizer()  

    grad = opt.compute_gradients(loss)
    grad_check = [tf.check_numerics(g, message='Gradient NaN Found!')
                  for g, _ in grad if g is not None] + [tf.check_numerics(loss, message='Loss NaN Found')]
//  [python]
def divide_safe(num, den, name=None):
  eps = 1e-8
  den += eps * tf.cast(tf.equal(den, 0), 'float32')
  return tf.divide(num, den, name=name)
//  [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.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])))
    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 tf_calculate_adolp(i1, i2, i3, i4):
    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.truediv(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(i1, i2, i3, i4):
    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 CameraIteration(self,conv1,conv2,fx,fy,ox,oy,p,D,R,T):

        with tf.name_scope("initialization"):

            conv1_shape=conv1.get_shape()
            nbatch    =int(conv1_shape[0])
            npixels   =int(conv1_shape[1])
            
            nchannels1=int(conv1_shape[2])
            nchannels2=int(2*nchannels1)
            nchannels3=nchannels1+nchannels2

        with tf.name_scope("warp_compute"): 
            Rp = tf.matmul(R,p)
            RP = tf.multiply(Rp,tf.tile(tf.transpose(D,[0,2,1]),[1,3,1]))
            RPT= tf.add(RP,tf.tile(T,[1,1,npixels]))

            Z  = RPT[:,2,:]
            X  = RPT[:,0,:]
            Y  = RPT[:,1,:]

            x  = tf.div(X,Z)
            y  = tf.div(Y,Z)

            px=fx*x+ox
            py=fy*y+oy

        with tf.name_scope("warp_conv"): 

            with tf.name_scope("weighting"):
                _conv2,_mask=utils.interpolate2d(conv2,px,py)
                mask   = tf.expand_dims(_mask,axis=-1)
                _diff  = tf.expand_dims(_conv2[:,:,0:nchannels1]-conv1,-1)
                _gradx = tf.expand_dims(_conv2[:,:,nchannels1:nchannels2],-1)
                _grady = tf.expand_dims(_conv2[:,:,nchannels2:nchannels3],-1)

                diff   = tf.matmul(_diff,mask)
                grad   = tf.concat([tf.matmul(_gradx,mask),tf.matmul(_grady,mask)],-1)

        with tf.name_scope("lambda_prediction"):

            avg_residual     =tf.reduce_mean(tf.abs(tf.squeeze(diff,axis=-1)),axis=1,keep_dims=True)
            lambda_prediction=tf.pow(tf.norm(avg_residual,axis=-1,keepdims=True),2.0)

        with tf.name_scope("FirstEstimation"):

            with tf.name_scope("Solve"):

                jacobianMatrixGeometry=self.CameraJacobianMatrix(x,y,Z,fx,fy)
                AtA=tf.reduce_sum(tf.matmul(jacobianMatrixGeometry,tf.matmul(tf.matmul(grad,grad,transpose_a=True),jacobianMatrixGeometry),transpose_a=True),axis=1)
                Atb=tf.reduce_sum(tf.matmul(jacobianMatrixGeometry,tf.matmul(grad,diff,transpose_a=True),transpose_a=True),axis=1)

                diag   = tf.matrix_diag_part(AtA)
                AtA    = AtA+tf.matrix_diag(tf.squeeze(tf.matmul(tf.expand_dims(diag,axis=-1)+1e-5,lambda_prediction),axis=-1))
                if not qr:
                    motion = tf.matmul(tf.matrix_inverse(AtA),Atb)
                else:
                    q_full,r_full=tf.qr(AtA,full_matrices=True)
                    motion=tf.linalg.solve(r_full,tf.matmul(q_full,Atb,transpose_a=True))

            with tf.name_scope("Update"):
                wx,wy,wz,tx,ty,tz=tf.unstack(motion,num=6,axis=1)
                dr=self.AngleaAxisRotation(wx,wy,wz)
                dt=tf.stack([tx,ty,tz],axis=1)
                updatedR=tf.matmul(dr,R)
                updatedT=tf.add(dt,tf.matmul(dr,T))
        return updatedR,updatedT,tf.reduce_sum(mask)/npixels
//  [python]
def CameraIteration(self,conv1,conv2,fx,fy,ox,oy,p,D,R,T):

        with tf.name_scope("initialization"):

            conv1_shape=conv1.get_shape()
            nbatch    =int(conv1_shape[0])
            npixels   =int(conv1_shape[1])
            
            nchannels1=int(conv1_shape[2])
            nchannels2=int(2*nchannels1)
            nchannels3=nchannels1+nchannels2

        with tf.name_scope("warp_compute"): 
            Rp = tf.matmul(R,p)
            RP = tf.multiply(Rp,tf.tile(tf.transpose(D,[0,2,1]),[1,3,1]))
            RPT= tf.add(RP,tf.tile(T,[1,1,npixels]))

            Z  = RPT[:,2,:]
            X  = RPT[:,0,:]
            Y  = RPT[:,1,:]

            x  = tf.divide(X,Z)
            y  = tf.divide(Y,Z)

            px=fx*x+ox
            py=fy*y+oy

        with tf.name_scope("warp_conv"): 

            with tf.name_scope("weighting"):
                _conv2,_mask=utils.interpolate2d(conv2,px,py)
                mask   = tf.expand_dims(_mask,axis=-1)
                _diff  = tf.expand_dims(_conv2[:,:,0:nchannels1]-conv1,-1)
                _gradx = tf.expand_dims(_conv2[:,:,nchannels1:nchannels2],-1)
                _grady = tf.expand_dims(_conv2[:,:,nchannels2:nchannels3],-1)

                diff   = tf.matmul(_diff,mask)
                grad   = tf.concat([tf.matmul(_gradx,mask),tf.matmul(_grady,mask)],-1)

        with tf.name_scope("lambda_prediction"):

            avg_residual     =tf.reduce_mean(tf.abs(tf.squeeze(diff,axis=-1)),axis=1,keep_dims=True)
            lambda_prediction=tf.pow(tf.norm(avg_residual,axis=-1,keepdims=True),2.0)

        with tf.name_scope("FirstEstimation"):

            with tf.name_scope("Solve"):

                jacobianMatrixGeometry=self.CameraJacobianMatrix(x,y,Z,fx,fy)
                AtA=tf.reduce_sum(tf.matmul(jacobianMatrixGeometry,tf.matmul(tf.matmul(grad,grad,transpose_a=True),jacobianMatrixGeometry),transpose_a=True),axis=1)
                Atb=tf.reduce_sum(tf.matmul(jacobianMatrixGeometry,tf.matmul(grad,diff,transpose_a=True),transpose_a=True),axis=1)

                diag   = tf.matrix_diag_part(AtA)
                AtA    = AtA+tf.matrix_diag(tf.squeeze(tf.matmul(tf.expand_dims(diag,axis=-1)+1e-5,lambda_prediction),axis=-1))
                if not qr:
                    motion = tf.matmul(tf.matrix_inverse(AtA),Atb)
                else:
                    q_full,r_full=tf.qr(AtA,full_matrices=True)
                    motion=tf.linalg.solve(r_full,tf.matmul(q_full,Atb,transpose_a=True))

            with tf.name_scope("Update"):
                wx,wy,wz,tx,ty,tz=tf.unstack(motion,num=6,axis=1)
                dr=self.AngleaAxisRotation(wx,wy,wz)
                dt=tf.stack([tx,ty,tz],axis=1)
                updatedR=tf.matmul(dr,R)
                updatedT=tf.add(dt,tf.matmul(dr,T))
        return updatedR,updatedT,tf.reduce_sum(mask)/npixels
//  [python]
def instancenorm(inputdata, epsilon=1e-5, data_format='NHWC', use_affine=True, name=None):
        """

        :param name:
        :param inputdata:
        :param epsilon:
        :param data_format:
        :param use_affine:
        :return:
        """
        shape = inputdata.get_shape().as_list()
        if len(shape) != 4:
            raise ValueError("Input data of instancebn layer has to be 4D tensor")

        if data_format == 'NHWC':
            axis = [1, 2]
            ch = shape[3]
            new_shape = [1, 1, 1, ch]
        else:
            axis = [2, 3]
            ch = shape[1]
            new_shape = [1, ch, 1, 1]
        if ch is None:
            raise ValueError("Input of instancebn require known channel!")

        mean, var = tf.nn.moments(inputdata, axis, keep_dims=True)

        if not use_affine:
            return tf.div(inputdata - mean, tf.sqrt(var + epsilon), name='output')

        beta = tf.get_variable('beta', [ch], initializer=tf.constant_initializer())
        beta = tf.reshape(beta, new_shape)
        gamma = tf.get_variable('gamma', [ch], initializer=tf.constant_initializer(1.0))
        gamma = tf.reshape(gamma, new_shape)
        return tf.nn.batch_normalization(inputdata, mean, var, beta, gamma, epsilon, name=name)
//  [python]
def instancenorm(inputdata, epsilon=1e-5, data_format='NHWC', use_affine=True, name=None):
        """

        :param name:
        :param inputdata:
        :param epsilon:
        :param data_format:
        :param use_affine:
        :return:
        """
        shape = inputdata.get_shape().as_list()
        if len(shape) != 4:
            raise ValueError("Input data of instancebn layer has to be 4D tensor")

        if data_format == 'NHWC':
            axis = [1, 2]
            ch = shape[3]
            new_shape = [1, 1, 1, ch]
        else:
            axis = [2, 3]
            ch = shape[1]
            new_shape = [1, ch, 1, 1]
        if ch is None:
            raise ValueError("Input of instancebn require known channel!")

        mean, var = tf.nn.moments(inputdata, axis, keep_dims=True)

        if not use_affine:
            return tf.divide(inputdata - mean, tf.sqrt(var + epsilon), name='output')

        beta = tf.get_variable('beta', [ch], initializer=tf.constant_initializer())
        beta = tf.reshape(beta, new_shape)
        gamma = tf.get_variable('gamma', [ch], initializer=tf.constant_initializer(1.0))
        gamma = tf.reshape(gamma, new_shape)
        return tf.nn.batch_normalization(inputdata, mean, var, beta, gamma, epsilon, name=name)
//  [python]
def _average_per_instance(map_tensor: tf.Tensor, panoptic_labels: tf.Tensor,
                          instance_panoptic_labels: tf.Tensor,
                          instance_area: tf.Tensor) -> tf.Tensor:
  """Finds the average of the values in map_tensor over each instance."""

  # For each instance (in the 3rd dim), generate a map that has, per-pixel:
  # - The input value if that pixel belongs to the instance.
  # - Zero otherwise.
  pixel_in_instance = per_instance_masks(panoptic_labels,
                                         instance_panoptic_labels)

  map_dtype = map_tensor.dtype
  num_instances = tf.size(instance_panoptic_labels)
  map_or_zero = tf.where(pixel_in_instance, tf.expand_dims(map_tensor, 0),
                         tf.zeros([num_instances, 1, 1], dtype=map_dtype))

  # Average the semantic probabilities over each instance.
  instance_total_prob = tf.math.reduce_sum(map_or_zero, axis=[1, 2])
  instance_avg_prob = tf.truediv(instance_total_prob,
                                tf.cast(instance_area, map_dtype))

  return instance_avg_prob
//  [python]
def _average_per_instance(map_tensor: tf.Tensor, panoptic_labels: tf.Tensor,
                          instance_panoptic_labels: tf.Tensor,
                          instance_area: tf.Tensor) -> tf.Tensor:
  """Finds the average of the values in map_tensor over each instance."""

  # For each instance (in the 3rd dim), generate a map that has, per-pixel:
  # - The input value if that pixel belongs to the instance.
  # - Zero otherwise.
  pixel_in_instance = per_instance_masks(panoptic_labels,
                                         instance_panoptic_labels)

  map_dtype = map_tensor.dtype
  num_instances = tf.size(instance_panoptic_labels)
  map_or_zero = tf.where(pixel_in_instance, tf.expand_dims(map_tensor, 0),
                         tf.zeros([num_instances, 1, 1], dtype=map_dtype))

  # Average the semantic probabilities over each instance.
  instance_total_prob = tf.math.reduce_sum(map_or_zero, axis=[1, 2])
  instance_avg_prob = tf.divide(instance_total_prob,
                                tf.cast(instance_area, map_dtype))

  return instance_avg_prob
//  [python]
def norm_boxes_graph(boxes, shape):
    """Converts boxes from pixel coordinates to normalized coordinates.
    boxes: [..., (y1, x1, y2, x2)] in pixel coordinates
    shape: [..., (height, width)] in pixels

    Note: In pixel coordinates (y2, x2) is outside the box. But in normalized
    coordinates it's inside the box.

    Returns:
        [..., (y1, x1, y2, x2)] in normalized coordinates
    """
    h, w = tf.split(tf.cast(shape, tf.float32), 2)
    scale = tf.concat([h, w, h, w], axis=-1) - tf.constant(1.0)
    shift = tf.constant([0., 0., 1., 1.])
    return tf.truediv(boxes - shift, scale)
//  [python]
def norm_boxes_graph(boxes, shape):
    """Converts boxes from pixel coordinates to normalized coordinates.
    boxes: [..., (y1, x1, y2, x2)] in pixel coordinates
    shape: [..., (height, width)] in pixels

    Note: In pixel coordinates (y2, x2) is outside the box. But in normalized
    coordinates it's inside the box.

    Returns:
        [..., (y1, x1, y2, x2)] in normalized coordinates
    """
    h, w = tf.split(tf.cast(shape, tf.float32), 2)
    scale = tf.concat([h, w, h, w], axis=-1) - tf.constant(1.0)
    shift = tf.constant([0., 0., 1., 1.])
    return tf.divide(boxes - shift, scale)
//  [python]
def call(self, inputs):
        gamma=tf.square(self.eta, name=None)
        alpha=tf.negative(self.xi, name=None)
        alpha=tf.exp(alpha, name=None)+1
        alpha=tf.truediv(1, alpha, name=None)
        si=tf.multiply(gamma, inputs, name=None)
        si=tf.negative(si, name=None)
        si=tf.exp(si, name=None)
        si=tf.multiply(si, alpha, name=None)
        si = si / (tf.reduce_max(si, axis = -1, keepdims=True) + 0.0001)
        return si
//  [python]
def call(self, inputs):
        gamma=tf.square(self.eta, name=None)
        alpha=tf.negative(self.xi, name=None)
        alpha=tf.exp(alpha, name=None)+1
        alpha=tf.divide(1, alpha, name=None)
        si=tf.multiply(gamma, inputs, name=None)
        si=tf.negative(si, name=None)
        si=tf.exp(si, name=None)
        si=tf.multiply(si, alpha, name=None)
        si = si / (tf.reduce_max(si, axis = -1, keepdims=True) + 0.0001)
        return si
//  [python]
def normal(v):
    norm = tf.norm(v)
    if norm == 0:
        return v
    return tf.truediv(v, norm)
//  [python]
def normal(v):
    norm = tf.norm(v)
    if norm == 0:
        return v
    return tf.divide(v, norm)
//  [python]
def call(self, inputs):
        beta=tf.square(self.beta, name=None)
        beta_sum=tf.reduce_sum(beta, -1, keepdims=True)
        u=tf.div(beta, beta_sum, name=None)
        inputs_new=tf.expand_dims(inputs, -1)
        for i in range(self.input_dim):
          if i==0:
            mass_prototype_i=tf.multiply(u[i,:], inputs_new[:,i], name=None)
            mass_prototype=tf.expand_dims(mass_prototype_i, -2)
          if i>0:
            mass_prototype_i=tf.expand_dims(tf.multiply(u[i,:], inputs_new[:,i], name=None), -2)
            mass_prototype=tf.concat([mass_prototype, mass_prototype_i], -2)
        mass_prototype=tf.convert_to_tensor(mass_prototype)
        return mass_prototype
//  [python]
def call(self, inputs):
        beta=tf.square(self.beta, name=None)
        beta_sum=tf.reduce_sum(beta, -1, keepdims=True)
        u=tf.divide(beta, beta_sum, name=None)
        inputs_new=tf.expand_dims(inputs, -1)
        for i in range(self.input_dim):
          if i==0:
            mass_prototype_i=tf.multiply(u[i,:], inputs_new[:,i], name=None)
            mass_prototype=tf.expand_dims(mass_prototype_i, -2)
          if i>0:
            mass_prototype_i=tf.expand_dims(tf.multiply(u[i,:], inputs_new[:,i], name=None), -2)
            mass_prototype=tf.concat([mass_prototype, mass_prototype_i], -2)
        mass_prototype=tf.convert_to_tensor(mass_prototype)
        return mass_prototype
//  [python]
def _decode_block(self, input_tensor, previous_feats_tensor,
                      out_channels_nums, name, kernel_size=4,
                      stride=2, use_bias=False,
                      previous_kernel_size=4, need_activate=True):
        """

        :param input_tensor:
        :param previous_feats_tensor:
        :param out_channels_nums:
        :param kernel_size:
        :param previous_kernel_size:
        :param use_bias:
        :param stride:
        :param name:
        :return:
        """
        with tf.variable_scope(name_or_scope=name):

            deconv_weights_stddev = tf.sqrt(
                tf.div(tf.constant(2.0, tf.float32),
                          tf.mul(tf.cast(previous_kernel_size * previous_kernel_size, tf.float32),
                                      tf.cast(tf.shape(input_tensor)[3], tf.float32)))
            )
            deconv_weights_init = tf.truncated_normal_initializer(
                mean=0.0, stddev=deconv_weights_stddev)

            deconv = self.deconv2d(
                inputdata=input_tensor, out_channel=out_channels_nums, kernel_size=kernel_size,
                stride=stride, use_bias=use_bias, w_init=deconv_weights_init,
                name='deconv'
            )

            deconv = self.layerbn(inputdata=deconv, is_training=self._is_training, name='deconv_bn')

            deconv = self.relu(inputdata=deconv, name='deconv_relu')

            fuse_feats = tf.add(
                previous_feats_tensor, deconv, name='fuse_feats'
            )

            if need_activate:

                fuse_feats = self.layerbn(
                    inputdata=fuse_feats, is_training=self._is_training, name='fuse_gn'
                )

                fuse_feats = self.relu(inputdata=fuse_feats, name='fuse_relu')

        return fuse_feats
//  [python]
def _decode_block(self, input_tensor, previous_feats_tensor,
                      out_channels_nums, name, kernel_size=4,
                      stride=2, use_bias=False,
                      previous_kernel_size=4, need_activate=True):
        """

        :param input_tensor:
        :param previous_feats_tensor:
        :param out_channels_nums:
        :param kernel_size:
        :param previous_kernel_size:
        :param use_bias:
        :param stride:
        :param name:
        :return:
        """
        with tf.variable_scope(name_or_scope=name):

            deconv_weights_stddev = tf.sqrt(
                tf.divide(tf.constant(2.0, tf.float32),
                          tf.multiply(tf.cast(previous_kernel_size * previous_kernel_size, tf.float32),
                                      tf.cast(tf.shape(input_tensor)[3], tf.float32)))
            )
            deconv_weights_init = tf.truncated_normal_initializer(
                mean=0.0, stddev=deconv_weights_stddev)

            deconv = self.deconv2d(
                inputdata=input_tensor, out_channel=out_channels_nums, kernel_size=kernel_size,
                stride=stride, use_bias=use_bias, w_init=deconv_weights_init,
                name='deconv'
            )

            deconv = self.layerbn(inputdata=deconv, is_training=self._is_training, name='deconv_bn')

            deconv = self.relu(inputdata=deconv, name='deconv_relu')

            fuse_feats = tf.add(
                previous_feats_tensor, deconv, name='fuse_feats'
            )

            if need_activate:

                fuse_feats = self.layerbn(
                    inputdata=fuse_feats, is_training=self._is_training, name='fuse_gn'
                )

                fuse_feats = self.relu(inputdata=fuse_feats, name='fuse_relu')

        return fuse_feats
//  [python]
def loss_fn(W,b,x_data,y_target):
    logits = tf.subtract(tf.matmul(x_data, W),b)
    norm_term = tf.div(tf.reduce_sum(tf.multiply(tf.transpose(W),W)),2)
    classification_loss = tf.reduce_mean(tf.maximum(0., tf.subtract(FLAGS.delta, tf.multiply(logits, y_target))))
    total_loss = tf.add(tf.multiply(FLAGS.C_param,classification_loss), tf.multiply(FLAGS.Reg_param,norm_term))
    return total_loss
//  [python]
def loss_fn(W,b,x_data,y_target):
    logits = tf.subtract(tf.matmul(x_data, W),b)
    norm_term = tf.divide(tf.reduce_sum(tf.multiply(tf.transpose(W),W)),2)
    classification_loss = tf.reduce_mean(tf.maximum(0., tf.subtract(FLAGS.delta, tf.multiply(logits, y_target))))
    total_loss = tf.add(tf.multiply(FLAGS.C_param,classification_loss), tf.multiply(FLAGS.Reg_param,norm_term))
    return total_loss
//  [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 _fake_quant_bias(self, bias_tensor, input_scale, weight_scales, scope, init_moving_scale_op):
        with tf.variable_scope(scope, reuse=tf.AUTO_REUSE):
            with self._graph.gradient_override_map({'Round': 'Identity'}):
                with tf.control_dependencies([init_moving_scale_op]):
                    weight_scales_reshape = tf.reshape(weight_scales, bias_tensor.shape)
                    bias_scale = input_scale * weight_scales_reshape
                    bias_scale = bias_scale + tf.cast(tf.less(tf.abs(bias_scale), 1e-6), tf.float32) * self._eps
                    before_round = tf.truediv(bias_tensor, bias_scale, name=_MNN_mark_start_name)
                    before_round = before_round + 1e-3 * tf.sign(before_round)
                    quant_bias = tf.round(before_round)
                    new_bias = quant_bias * bias_scale
                    new_bias_tensor = tf.cond(tf.constant(True), lambda: new_bias, lambda: bias_tensor, name=_MNN_mark_end_name)
                    
                    return new_bias_tensor
//  [python]
def _fake_quant_bias(self, bias_tensor, input_scale, weight_scales, scope, init_moving_scale_op):
        with tf.variable_scope(scope, reuse=tf.AUTO_REUSE):
            with self._graph.gradient_override_map({'Round': 'Identity'}):
                with tf.control_dependencies([init_moving_scale_op]):
                    weight_scales_reshape = tf.reshape(weight_scales, bias_tensor.shape)
                    bias_scale = input_scale * weight_scales_reshape
                    bias_scale = bias_scale + tf.cast(tf.less(tf.abs(bias_scale), 1e-6), tf.float32) * self._eps
                    before_round = tf.divide(bias_tensor, bias_scale, name=_MNN_mark_start_name)
                    before_round = before_round + 1e-3 * tf.sign(before_round)
                    quant_bias = tf.round(before_round)
                    new_bias = quant_bias * bias_scale
                    new_bias_tensor = tf.cond(tf.constant(True), lambda: new_bias, lambda: bias_tensor, name=_MNN_mark_end_name)
                    
                    return new_bias_tensor
//  [python]
def theta(self):
    """Returns the matrix of per-arm feature weights.

    The returned matrix has shape (num_actions, context_dim).
    It's equivalent to a stacking of theta vectors from the paper.
    """
    thetas = []
    for k in range(self._num_models):
      if self._use_eigendecomp:
        model_index = policy_utilities.get_model_index(
            k, self._accepts_per_arm_features
        )
        q_t_b = tf.matmul(
            self.eig_matrix[model_index],
            tf.expand_dims(self.data_vector[model_index], axis=-1),
            transpose_a=True,
        )
        lambda_inv = tf.truediv(
            tf.ones_like(self.eig_vals[model_index]),
            self.eig_vals[model_index] + self._tikhonov_weight,
        )
        theta_k = tf.squeeze(
            tf.matmul(
                self.eig_matrix[model_index],
                tf.einsum('j,jk->jk', lambda_inv, q_t_b),
            ),
            axis=-1,
        )
      else:
        theta_k = tf.squeeze(
            linalg.conjugate_gradient(
                self._cov_matrix_list[k]
                + self._tikhonov_weight
                * tf.eye(self._overall_context_dim, dtype=self._dtype),
                tf.expand_dims(self._data_vector_list[k], axis=-1),
            ),
            axis=-1,
        )
      thetas.append(theta_k)

    return tf.stack(thetas, axis=0)
//  [python]
def theta(self):
    """Returns the matrix of per-arm feature weights.

    The returned matrix has shape (num_actions, context_dim).
    It's equivalent to a stacking of theta vectors from the paper.
    """
    thetas = []
    for k in range(self._num_models):
      if self._use_eigendecomp:
        model_index = policy_utilities.get_model_index(
            k, self._accepts_per_arm_features
        )
        q_t_b = tf.matmul(
            self.eig_matrix[model_index],
            tf.expand_dims(self.data_vector[model_index], axis=-1),
            transpose_a=True,
        )
        lambda_inv = tf.divide(
            tf.ones_like(self.eig_vals[model_index]),
            self.eig_vals[model_index] + self._tikhonov_weight,
        )
        theta_k = tf.squeeze(
            tf.matmul(
                self.eig_matrix[model_index],
                tf.einsum('j,jk->jk', lambda_inv, q_t_b),
            ),
            axis=-1,
        )
      else:
        theta_k = tf.squeeze(
            linalg.conjugate_gradient(
                self._cov_matrix_list[k]
                + self._tikhonov_weight
                * tf.eye(self._overall_context_dim, dtype=self._dtype),
                tf.expand_dims(self._data_vector_list[k], axis=-1),
            ),
            axis=-1,
        )
      thetas.append(theta_k)

    return tf.stack(thetas, axis=0)
//  [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, either [batch_size, num_anchors,
        num_classes] or [batch_size, num_anchors, 1]. If the shape is
        [batch_size, num_anchors, 1], all the classses are equally weighted.

    Returns:
      loss: a float tensor of shape [batch_size, num_anchors]
        representing the value of the loss function.
    """
    weights = tf.reduce_mean(weights, axis=2)
    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, either [batch_size, num_anchors,
        num_classes] or [batch_size, num_anchors, 1]. If the shape is
        [batch_size, num_anchors, 1], all the classses are equally weighted.

    Returns:
      loss: a float tensor of shape [batch_size, num_anchors]
        representing the value of the loss function.
    """
    weights = tf.reduce_mean(weights, axis=2)
    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 _score_converter_fn_with_logit_scale(tf_score_converter_fn, logit_scale):
  """Create a function to scale logits then apply a Tensorflow function."""
  def score_converter_fn(logits):
    scaled_logits = tf.div(logits, logit_scale, name='scale_logits')
    return tf_score_converter_fn(scaled_logits, name='convert_scores')
  score_converter_fn.__name__ = '%s_with_logit_scale' % (
      tf_score_converter_fn.__name__)
  return score_converter_fn
//  [python]
def _score_converter_fn_with_logit_scale(tf_score_converter_fn, logit_scale):
  """Create a function to scale logits then apply a Tensorflow function."""
  def score_converter_fn(logits):
    scaled_logits = tf.divide(logits, logit_scale, name='scale_logits')
    return tf_score_converter_fn(scaled_logits, name='convert_scores')
  score_converter_fn.__name__ = '%s_with_logit_scale' % (
      tf_score_converter_fn.__name__)
  return score_converter_fn
//  [python]
def get_img_points(sample_pc, trans_mat_right):
    # sample_pc B*N*3
    size_lst = sample_pc.get_shape().as_list()
    homo_pc = tf.concat((sample_pc, tf.ones((size_lst[0], size_lst[1], 1),dtype=np.float32)),axis= -1)
    print("homo_pc.get_shape()", homo_pc.get_shape())
    pc_xyz = tf.matmul(homo_pc, trans_mat_right)
    print("pc_xyz.get_shape()", pc_xyz.get_shape()) # B * N * 3
    pc_xy = tf.div(pc_xyz[:,:,:2], tf.expand_dims(pc_xyz[:,:,2], axis = 2))
    mintensor = tf.constant([0.0,0.0], dtype=tf.float32)
    maxtensor = tf.constant([136.0,136.0], dtype=tf.float32)
    return tf.minimum(maxtensor, tf.maximum(mintensor, pc_xy))
//  [python]
def get_img_points(sample_pc, trans_mat_right):
    # sample_pc B*N*3
    size_lst = sample_pc.get_shape().as_list()
    homo_pc = tf.concat((sample_pc, tf.ones((size_lst[0], size_lst[1], 1),dtype=np.float32)),axis= -1)
    print("homo_pc.get_shape()", homo_pc.get_shape())
    pc_xyz = tf.matmul(homo_pc, trans_mat_right)
    print("pc_xyz.get_shape()", pc_xyz.get_shape()) # B * N * 3
    pc_xy = tf.divide(pc_xyz[:,:,:2], tf.expand_dims(pc_xyz[:,:,2], axis = 2))
    mintensor = tf.constant([0.0,0.0], dtype=tf.float32)
    maxtensor = tf.constant([136.0,136.0], dtype=tf.float32)
    return tf.minimum(maxtensor, tf.maximum(mintensor, pc_xy))
//  [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, either [batch_size, num_anchors,
        num_classes] or [batch_size, num_anchors, 1]. If the shape is
        [batch_size, num_anchors, 1], all the classses are equally weighted.

    Returns:
      loss: a float tensor of shape [batch_size, num_anchors]
        representing the value of the loss function.
    """
    weights = tf.reduce_mean(weights, axis=2)
    num_classes = prediction_tensor.get_shape().as_list()[-1]
    target_tensor = self._scale_and_softmax_logits(target_tensor)
    prediction_tensor = tf.truediv(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, either [batch_size, num_anchors,
        num_classes] or [batch_size, num_anchors, 1]. If the shape is
        [batch_size, num_anchors, 1], all the classses are equally weighted.

    Returns:
      loss: a float tensor of shape [batch_size, num_anchors]
        representing the value of the loss function.
    """
    weights = tf.reduce_mean(weights, axis=2)
    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 inference(self, inputdata, name, reuse=False):
        """
        Main routine to construct the network
        :param inputdata:
        :param name:
        :param reuse:
        :return:
        """
        with tf.variable_scope(name_or_scope=name, reuse=reuse):
            # centerlized data
            inputdata = tf.truediv(inputdata, 255.0)

            # first apply the cnn feature extraction stage
            cnn_out = self._feature_sequence_extraction(
                inputdata=inputdata, name='feature_extraction_module'
            )

            # second apply the map to sequence stage
            sequence = self._map_to_sequence(
                inputdata=cnn_out, name='map_to_sequence_module'
            )

            # third apply the sequence label stage
            net_out, raw_pred = self._sequence_label(
                inputdata=sequence, name='sequence_rnn_module'
            )

        return net_out
//  [python]
def inference(self, inputdata, name, reuse=False):
        """
        Main routine to construct the network
        :param inputdata:
        :param name:
        :param reuse:
        :return:
        """
        with tf.variable_scope(name_or_scope=name, reuse=reuse):
            # centerlized data
            inputdata = tf.divide(inputdata, 255.0)

            # first apply the cnn feature extraction stage
            cnn_out = self._feature_sequence_extraction(
                inputdata=inputdata, name='feature_extraction_module'
            )

            # second apply the map to sequence stage
            sequence = self._map_to_sequence(
                inputdata=cnn_out, name='map_to_sequence_module'
            )

            # third apply the sequence label stage
            net_out, raw_pred = self._sequence_label(
                inputdata=sequence, name='sequence_rnn_module'
            )

        return net_out
//  [python]
train_dataset = (
    train_dataset.map(lambda x, y:
                      (tf.truediv(tf.cast(x, tf.float32), 255.0),
                       tf.reshape(tf.one_hot(y, 10), (-1, 10))))
)
//  [python]
training_epochs = 6
batch_size = 600

# Import MNIST data
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

train_dataset = (
    tf.data.Dataset.from_tensor_slices((tf.reshape(x_train, [-1, 784]), y_train))
    .batch(batch_size)
    .shuffle(1000)
)

train_dataset = (
    train_dataset.map(lambda x, y:
                      (tf.divide(tf.cast(x, tf.float32), 255.0),
                       tf.reshape(tf.one_hot(y, 10), (-1, 10))))
)
//  [python]
def build_graph(xyz, radius, nn_uplimit, num_sample, sample_method=None):
    intra_idx, intra_cnt, intra_dst = neighbor_fn(xyz, xyz, radius=radius,
                                                  nnsample=nn_uplimit)

    if num_sample is not None:
        if sample_method == 'random':
            sample_index = random_sample(num_sample, xyz)
        elif sample_method == 'FPS':
            sample_index = farthest_point_sample(num_sample, xyz)
        elif sample_method == 'IDS':
            prob = tf.div(tf.reduce_sum(intra_dst, axis=-1), tf.cast(intra_cnt,dtype=tf.float32))
            sample_index = inverse_density_sample(num_sample, prob)
        else:
            raise ValueError('Unknown sampling method.')

        batch_size = tf.shape(xyz)[0]
        batch_indices = tf.tile(tf.reshape(tf.range(batch_size), (-1, 1, 1)), (1, num_sample, 1))
        indices = tf.concat([batch_indices, tf.expand_dims(sample_index, axis=2)], axis=2)
    else:
        indices = None

    return intra_idx, intra_cnt, intra_dst, indices
//  [python]
def build_graph(xyz, radius, nn_uplimit, num_sample, sample_method=None):
    intra_idx, intra_cnt, intra_dst = neighbor_fn(xyz, xyz, radius=radius,
                                                  nnsample=nn_uplimit)

    if num_sample is not None:
        if sample_method == 'random':
            sample_index = random_sample(num_sample, xyz)
        elif sample_method == 'FPS':
            sample_index = farthest_point_sample(num_sample, xyz)
        elif sample_method == 'IDS':
            prob = tf.divide(tf.reduce_sum(intra_dst, axis=-1), tf.cast(intra_cnt,dtype=tf.float32))
            sample_index = inverse_density_sample(num_sample, prob)
        else:
            raise ValueError('Unknown sampling method.')

        batch_size = tf.shape(xyz)[0]
        batch_indices = tf.tile(tf.reshape(tf.range(batch_size), (-1, 1, 1)), (1, num_sample, 1))
        indices = tf.concat([batch_indices, tf.expand_dims(sample_index, axis=2)], axis=2)
    else:
        indices = None

    return intra_idx, intra_cnt, intra_dst, indices
//  [python]
def unpool3d(inputs, nn_index, nn_count, nn_dist, scope, method):
    """ 3D unpooling

    Args:
        inputs: 3-D tensor BxNxC
        nn_index: int32 array, neighbor indices
        nn_count: int32 array, number of neighbors
        nn_dist: float32 array, neighbor (sqrt) distances for weight computation
        scope: string
        method: string, the unpooling method

    Returns:
        Variable tensor
    """
    with tf.variable_scope(scope) as sc:
        if method == 'mean':
            outputs = tf_unpool3d.mean_interpolate(inputs, nn_index, nn_count)
        elif method == 'weighted':
            sum_nn_dist = tf.reduce_sum(nn_dist, axis=-1, keepdims=True)
            epsilon = 1e-7
            weight = tf.div(nn_dist+epsilon, sum_nn_dist+epsilon)
            outputs = tf_unpool3d.weighted_interpolate(inputs, weight, nn_index, nn_count)
        else:
            raise ValueError("Unknow unpooling method %s." % method)

        return outputs
//  [python]
def unpool3d(inputs, nn_index, nn_count, nn_dist, scope, method):
    """ 3D unpooling

    Args:
        inputs: 3-D tensor BxNxC
        nn_index: int32 array, neighbor indices
        nn_count: int32 array, number of neighbors
        nn_dist: float32 array, neighbor (sqrt) distances for weight computation
        scope: string
        method: string, the unpooling method

    Returns:
        Variable tensor
    """
    with tf.variable_scope(scope) as sc:
        if method == 'mean':
            outputs = tf_unpool3d.mean_interpolate(inputs, nn_index, nn_count)
        elif method == 'weighted':
            sum_nn_dist = tf.reduce_sum(nn_dist, axis=-1, keepdims=True)
            epsilon = 1e-7
            weight = tf.divide(nn_dist+epsilon, sum_nn_dist+epsilon)
            outputs = tf_unpool3d.weighted_interpolate(inputs, weight, nn_index, nn_count)
        else:
            raise ValueError("Unknow unpooling method %s." % method)

        return outputs
//  [python]
def round(x, decimals=0):
    if decimals == 0:
        return tf.round(x)
    x_dtype = x.dtype
    if tf.as_dtype(x_dtype).is_integer:
        # int
        if decimals > 0:
            return x
        # temporarilaly convert to floats
        factor = tf.cast(math.pow(10, decimals), config.floatx())
        x = tf.cast(x, config.floatx())
    else:
        # float
        factor = tf.cast(math.pow(10, decimals), x.dtype)
    x = tf.multiply(x, factor)
    x = tf.round(x)
    x = tf.truediv(x, factor)
    return tf.cast(x, x_dtype)
//  [python]
def round(x, decimals=0):
    if decimals == 0:
        return tf.round(x)
    x_dtype = x.dtype
    if tf.as_dtype(x_dtype).is_integer:
        # int
        if decimals > 0:
            return x
        # temporarilaly convert to floats
        factor = tf.cast(math.pow(10, decimals), config.floatx())
        x = tf.cast(x, config.floatx())
    else:
        # float
        factor = tf.cast(math.pow(10, decimals), x.dtype)
    x = tf.multiply(x, factor)
    x = tf.round(x)
    x = tf.divide(x, factor)
    return tf.cast(x, x_dtype)
//  [python]
def norm_boxes_graph(boxes, shape):
    """Converts boxes from pixel coordinates to normalized coordinates.
    boxes: [..., (y1, x1, y2, x2)] in pixel coordinates
    shape: [..., (height, width)] in pixels

    Note: In pixel coordinates (y2, x2) is outside the box. But in normalized
    coordinates it's inside the box.

    Returns:
        [..., (y1, x1, y2, x2)] in normalized coordinates
    """

    tensor_dtype = tf.float16 if K.floatx()!='float32' else tf.float32

    h, w = tf.split(tf.cast(shape, tensor_dtype), 2)
    scale = tf.concat([h, w, h, w], axis=-1) - tf.constant(1.0, dtype=tensor_dtype)
    shift = tf.constant([0., 0., 1., 1.], dtype=tensor_dtype)
    return tf.truediv(boxes - shift, scale)
//  [python]
def norm_boxes_graph(boxes, shape):
    """Converts boxes from pixel coordinates to normalized coordinates.
    boxes: [..., (y1, x1, y2, x2)] in pixel coordinates
    shape: [..., (height, width)] in pixels

    Note: In pixel coordinates (y2, x2) is outside the box. But in normalized
    coordinates it's inside the box.

    Returns:
        [..., (y1, x1, y2, x2)] in normalized coordinates
    """

    tensor_dtype = tf.float16 if K.floatx()!='float32' else tf.float32

    h, w = tf.split(tf.cast(shape, tensor_dtype), 2)
    scale = tf.concat([h, w, h, w], axis=-1) - tf.constant(1.0, dtype=tensor_dtype)
    shift = tf.constant([0., 0., 1., 1.], dtype=tensor_dtype)
    return tf.divide(boxes - shift, scale)
//  [python]
def divide(x1, x2):
    if not isinstance(x1, (int, float)):
        x1 = convert_to_tensor(x1)
    if not isinstance(x2, (int, float)):
        x2 = convert_to_tensor(x2)
    dtype = dtypes.result_type(
        getattr(x1, "dtype", type(x1)),
        getattr(x2, "dtype", type(x2)),
        float,
    )
    x1 = convert_to_tensor(x1, dtype)
    x2 = convert_to_tensor(x2, dtype)
    return tf.truediv(x1, x2)
//  [python]
def divide(x1, x2):
    if not isinstance(x1, (int, float)):
        x1 = convert_to_tensor(x1)
    if not isinstance(x2, (int, float)):
        x2 = convert_to_tensor(x2)
    dtype = dtypes.result_type(
        getattr(x1, "dtype", type(x1)),
        getattr(x2, "dtype", type(x2)),
        float,
    )
    x1 = convert_to_tensor(x1, dtype)
    x2 = convert_to_tensor(x2, dtype)
    return tf.divide(x1, x2)
//  [python]
def _normalize_labels(self, labels):
        """Normalize labels"""
        labels = tf.expand_dims(tf.cast(labels, tf.float32), -1)
        labels = tf.truediv(labels, 255)

        labels = tf.image.resize_images(labels, (388, 388))
        labels = tf.image.resize_image_with_crop_or_pad(labels, 572, 572)

        cond = tf.less(labels, 0.5 * tf.ones(tf.shape(labels)))
        labels = tf.where(cond, tf.zeros(tf.shape(labels)), tf.ones(tf.shape(labels)))

        return tf.one_hot(tf.squeeze(tf.cast(labels, tf.int32)), 2)