code: def fcos_losses(self, instances):
      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.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:
          reg_loss = (
              self.loc_loss_func(
                  instances.reg_pred,
                  instances.reg_targets,
                  ctrness_targets,
              )
              / loss_denorm
          )
  
          ctrness_loss = (
              F.binary_cross_entropy_with_logits(
                  instances.ctrness_pred,
                  ctrness_targets,
                  reduction="sum",
              )
              / num_pos_avg
          )
      else:
          reg_loss = instances.reg_pred.sum() * 0
          ctrness_loss = instances.ctrness_pred.sum() * 0
  
      losses = {
          "loss_fcos_cls": class_loss,
          "loss_fcos_loc": reg_loss,
          "loss_fcos_ctr": ctrness_loss,
      }
      extras = {"instances": instances, "loss_denorm": loss_denorm}
      return extras, losses
  
imports: import logging
  import math
  from typing import List, Dict
  import torch
  from torch import nn
  from torch.nn import functional
  from ...nn.shape_spec import ShapeSpec
  from ...nn.deform_conv import DeformConvWithChangeableStride
  from ...nn.group_norm import NaiveGroupNorm
  from ...data.structures import Instances, Boxes
  from ...nn.focal_loss import sigmoid_focal_loss_jit
  from ...nn.nms import ml_nms
  from ...utils.loss import IOULoss
  from ...utils.comm import get_world_size, reduce_sum, compute_locations
label_desc: Use image node_id 7 for calling the FCOSOutputs obj's underlying member method code with example usage: obj.fcos_losses(instances) and returns: extras,  losses