def set_keypoint_visibilities(keypoints, initial_keypoint_visibilities=None):
  """Sets keypoint visibilities based on valid/invalid keypoints.

  Some keypoint operations set invisible keypoints (e.g. cropped keypoints) to
  NaN, without affecting any keypoint "visibility" variables. This function is
  used to update (or create) keypoint visibilities to agree with visible /
  invisible keypoint coordinates.

  Args:
    keypoints: a float32 tensor of shape [num_instances, num_keypoints, 2].
    initial_keypoint_visibilities: a boolean tensor of shape
      [num_instances, num_keypoints]. If provided, will maintain the visibility
      designation of a keypoint, so long as the corresponding coordinates are
      not NaN. If not provided, will create keypoint visibilities directly from
      the values in `keypoints` (i.e. NaN coordinates map to False, otherwise
      they map to True).

  Returns:
    keypoint_visibilities: a bool tensor of shape [num_instances, num_keypoints]
    indicating whether a keypoint is visible or not.
  """
  if initial_keypoint_visibilities is not None:
    keypoint_visibilities = tf.cast(initial_keypoint_visibilities, tf.bool)
  else:
    keypoint_visibilities = tf.ones_like(keypoints[:, :, 0], dtype=tf.bool)

  keypoints_with_nan = tf.math.reduce_any(tf.math.is_nan(keypoints), axis=2)
  keypoint_visibilities = tf.where(
      keypoints_with_nan,
      tf.zeros_like(keypoint_visibilities, dtype=tf.bool),
      keypoint_visibilities)
  return keypoint_visibilities
Sets keypoint visibilities based on valid/invalid keypoints.

  Some keypoint operations set invisible keypoints (e.g. cropped keypoints) to
  NaN, without affecting any keypoint "visibility" variables. This function is
  used to update (or create) keypoint visibilities to agree with visible /
  invisible keypoint coordinates.

  Args:
    keypoints: a float32 tensor of shape [num_instances, num_keypoints, 2].
    initial_keypoint_visibilities: a boolean tensor of shape
      [num_instances, num_keypoints]. If provided, will maintain the visibility
      designation of a keypoint, so long as the corresponding coordinates are
      not NaN. If not provided, will create keypoint visibilities directly from
      the values in `keypoints` (i.e. NaN coordinates map to False, otherwise
      they map to True).

  Returns:
    keypoint_visibilities: a bool tensor of shape [num_instances, num_keypoints]
    indicating whether a keypoint is visible or not.
  
inference_api/src/main/object_detection/core/keypoint_ops.py