code: def directpose_losses(
      self,
      instances,
      hm_target=None,
      hm_weight=None,
      hm_offset_target=None,
      hm_pred=None,
      hm_offset_pred=None,
  ):
      num_classes = instances.logits_pred.size(1)
      assert num_classes == self.num_classes
  
      labels = instances.labels.flatten()
  
      pos_inds = torch.nonzero(labels != num_classes).squeeze(1)
      num_pos_local = pos_inds.numel()
      num_gpus = get_world_size()
      total_num_pos = reduce_sum(
          pos_inds.new_tensor([num_pos_local])
      ).item()
      num_pos_avg = max(total_num_pos / num_gpus, 1.0)
  
      # prepare one_hot
      class_target = torch.zeros_like(instances.logits_pred)
      class_target[pos_inds, labels[pos_inds]] = 1
  
      class_loss = (
          sigmoid_focal_loss_jit(
              instances.logits_pred,
              class_target,
              alpha=self.focal_loss_alpha,
              gamma=self.focal_loss_gamma,
              reduction="sum",
          )
          / num_pos_avg
      )
  
      instances = instances[pos_inds]
      instances.pos_inds = pos_inds
  
      ctrness_targets = compute_ctrness_targets(
          instances.bbox_reg_targets
      )
      ctrness_targets_sum = ctrness_targets.sum()
      loss_denorm = max(
          reduce_sum(ctrness_targets_sum).item() / num_gpus, 1e-6
      )
      instances.gt_ctrs = ctrness_targets
  
      if pos_inds.numel() > 0:
          ctrness_loss = (
              F.binary_cross_entropy_with_logits(
                  instances.ctrness_pred,
                  ctrness_targets,
                  reduction="sum",
              )
              / num_pos_avg
          )
  
          kpt_reg_targets_vis = instances.kpt_reg_targets.reshape(
              -1, self.num_kpts, 3
          )
          kpt_reg_vis = kpt_reg_targets_vis[:, :, 2] > 0
          pos_kpt = kpt_reg_vis.sum(dim=1)
          kpt_reg_pred = (
              instances.kpt_reg_pred.reshape(
                  len(instances), self.num_kpts, -1
              )[:, :, 0:2]
              * kpt_reg_vis[:, :, None]
          )
          kpt_reg_loss = smooth_l1_loss(
              kpt_reg_pred,
              kpt_reg_targets_vis[:, :, 0:2],
              beta=self.kpt_l1_beta,
              reduction="none",
          )
          kpt_reg_loss = kpt_reg_loss.mean(dim=-1).sum(dim=1)
          kpt_reg_loss /= pos_kpt.clamp(min=1.0)
          kpt_reg_loss = (
              kpt_reg_loss * ctrness_targets * pos_kpt
          ).sum() / (ctrness_targets * pos_kpt).sum().clamp(min=1.0)
          # kpt_reg_loss = kpt_reg_loss.sum() / loss_denorm
  
          if self.enable_bbox_branch:
              bbox_reg_loss = (
                  self.loc_loss_func(
                      instances.bbox_reg_pred,
                      instances.bbox_reg_targets,
                      ctrness_targets,
                  )
                  / loss_denorm
              )
  
          if self.enable_kpt_vis_branch:
              kpt_vis_pred = instances.kpt_reg_pred.reshape(
                  len(instances), self.num_kpts, -1
              )[:, :, 2]
              kpt_vis_target = kpt_reg_targets_vis[:, :, 2]
              kpt_vis_target[kpt_vis_target > 1] = 1
              total_kpt_pos = reduce_sum(
                  kpt_vis_target.new_tensor([kpt_vis_target.sum()])
              ).item()
              num_pos_kpt_avg = max(total_kpt_pos / num_gpus, 1.0)
              # kpt_vis_loss = sigmoid_focal_loss_jit(
              #     kpt_vis_pred,
              #     kpt_vis_target,
              #     alpha=self.focal_loss_alpha,
              #     gamma=self.focal_loss_gamma,
              #     reduction="sum",
              # ) / num_pos_kpt_avg
              kpt_vis_loss = (
                  F.binary_cross_entropy_with_logits(
                      kpt_vis_pred, kpt_vis_target, reduction="sum"
                  )
                  / num_pos_kpt_avg
              )
  
          if self.loss_on_locator:
              kpt_locator_reg_pred = (
                  instances.kpt_locator_reg_pred.reshape(
                      -1, self.num_kpts, 2
                  )
                  * kpt_reg_vis[:, :, None]
              )
              kpt_locator_reg_loss = smooth_l1_loss(
                  kpt_locator_reg_pred,
                  kpt_reg_targets_vis[:, :, 0:2],
                  beta=self.kpt_l1_beta,
                  reduction="none",
              )
              kpt_locator_reg_loss = kpt_locator_reg_loss.sum(
                  dim=1
              ).sum(dim=1)
              kpt_locator_reg_loss[pos_kpt > 0] /= pos_kpt[pos_kpt > 0]
              kpt_locator_reg_loss = (
                  kpt_locator_reg_loss.sum() / loss_denorm
              )
      else:
          if self.enable_bbox_branch:
              bbox_reg_loss = instances.bbox_reg_pred.sum() * 0
          if self.enable_kpt_vis_branch:
              kpt_vis_loss = instances.kpt_reg_pred.sum() * 0
          if self.loss_on_locator:
              kpt_locator_reg_loss = instances.kpt_reg_pred.sum() * 0
          ctrness_loss = instances.ctrness_pred.sum() * 0
          kpt_reg_loss = instances.kpt_reg_pred.sum() * 0
  
      if self.enable_hm_branch:
          if self.hm_type == "Gaussian":
              if self.hm_loss_type == "mse":
                  hm_loss = (
                      self.hm_loss(hm_pred, hm_target, hm_weight)
                      * self.hm_loss_weight
                  )
              elif self.hm_loss_type == "focal":
                  hm_loss = (
                      self.hm_loss(hm_pred, hm_target)
                      * self.hm_loss_weight
                  )
  
              if self.predict_hm_offset:
                  hm_offset_target_vis = hm_offset_target.permute(
                      0, 2, 3, 1, 4
                  ).reshape(-1, self.num_kpts, 3)
                  hm_offset_vis = hm_offset_target_vis[:, :, 2] > 0
                  hm_offset_pred = (
                      hm_offset_pred.permute(0, 2, 3, 1)
                      .reshape(-1, 2)[:, None, :]
                      .expand(-1, self.num_kpts, -1)
                      * hm_offset_vis[:, :, None]
                  )
                  pos_kpt = hm_offset_vis.sum(dim=1)
                  hm_offset_loss = smooth_l1_loss(
                      hm_offset_pred,
                      hm_offset_target_vis[:, :, 0:2],
                      beta=self.kpt_l1_beta,
                      reduction="none",
                  )
                  hm_offset_loss = hm_offset_loss.sum(dim=1).sum(dim=1)
                  hm_offset_loss[pos_kpt > 0] /= pos_kpt[pos_kpt > 0]
                  hm_offset_loss = hm_offset_loss.sum() / loss_denorm
          elif self.hm_type == "BinaryLabels":
              pos_joint_inds = torch.nonzero(
                  hm_target.flatten()
              ).squeeze(1)
              num_pos_local = pos_joint_inds.numel()
              total_num_pos = reduce_sum(
                  pos_joint_inds.new_tensor([num_pos_local])
              ).item()
              num_joint_pos_avg = max(total_num_pos / num_gpus, 1.0)
              hm_loss = (
                  sigmoid_focal_loss_jit(
                      hm_pred,
                      hm_target,
                      alpha=self.focal_loss_alpha,
                      gamma=self.focal_loss_gamma,
                      reduction="sum",
                  )
                  / max(num_joint_pos_avg, 1.0)
                  * self.hm_loss_weight
              )
  
      losses = {
          "loss_directpose_cls": class_loss,
          "loss_directpose_ctr": ctrness_loss,
          "loss_directpose_kpt_loc": kpt_reg_loss,
      }
      if self.enable_bbox_branch:
          losses["loss_directpose_bbox_loc"] = bbox_reg_loss
      if self.loss_on_locator:
          losses[
              "loss_directpose_kpt_locator_loc"
          ] = kpt_locator_reg_loss
      if self.enable_hm_branch:
          losses["loss_directpose_hm"] = hm_loss
          if self.predict_hm_offset:
              losses["loss_directpose_hm_offset"] = hm_offset_loss
      if self.enable_kpt_vis_branch:
          losses["loss_directpose_kpt_vis"] = kpt_vis_loss
      extras = {
          "instances": instances,
          "loss_denorm": loss_denorm,
      }
      return extras, losses
  
imports: import logging
  import os
  from typing import List
  import numpy
  import torch
  from torch import nn
  import torch.nn.functional
  from torchvision.ops import nms
  from ...data.structures import Instances, Boxes
  from ...utils.comm import get_world_size
  from ...nn.focal_loss import sigmoid_focal_loss_jit
  from ...nn.smooth_l1_loss import smooth_l1_loss
  from ...utils.comm import reduce_sum
  from ...nn.nms import ml_nms, oks_nms, close_kpt_nms
  from ...nn.iou_loss import IOULoss
  from ...nn.keypoint_loss import WeightedMSELoss, HMFocalLoss
label_desc: Use image node_id 7 for calling the DirectPoseOutputs obj's underlying member method code with example usage: obj.directpose_losses(instances, hm_target, hm_weight, hm_offset_target, hm_pred, hm_offset_pred) and returns: extras,  losses