code: def diou_loss(
      pred: Tensor, target: Tensor, eps: float = 1e-7
  ) -> Tensor:
      r"""Implementation of `Distance-IoU Loss: Faster and Better
      Learning for Bounding Box Regression https://arxiv.org/abs/1911.08287`_.
  
      Code is modified from https://github.com/Zzh-tju/DIoU.
  
      Args:
          pred (Tensor): Predicted bboxes of format (x1, y1, x2, y2),
              shape (n, 4).
          target (Tensor): Corresponding gt bboxes, shape (n, 4).
          eps (float): Epsilon to avoid log(0).
  
      Return:
          Tensor: Loss tensor.
      """
      # overlap
      lt = torch.max(pred[:, :2], target[:, :2])
      rb = torch.min(pred[:, 2:], target[:, 2:])
      wh = (rb - lt).clamp(min=0)
      overlap = wh[:, 0] * wh[:, 1]
  
      # union
      ap = (pred[:, 2] - pred[:, 0]) * (pred[:, 3] - pred[:, 1])
      ag = (target[:, 2] - target[:, 0]) * (target[:, 3] - target[:, 1])
      union = ap + ag - overlap + eps
  
      # IoU
      ious = overlap / union
  
      # enclose area
      enclose_x1y1 = torch.min(pred[:, :2], target[:, :2])
      enclose_x2y2 = torch.max(pred[:, 2:], target[:, 2:])
      enclose_wh = (enclose_x2y2 - enclose_x1y1).clamp(min=0)
  
      cw = enclose_wh[:, 0]
      ch = enclose_wh[:, 1]
  
      c2 = cw**2 + ch**2 + eps
  
      b1_x1, b1_y1 = pred[:, 0], pred[:, 1]
      b1_x2, b1_y2 = pred[:, 2], pred[:, 3]
      b2_x1, b2_y1 = target[:, 0], target[:, 1]
      b2_x2, b2_y2 = target[:, 2], target[:, 3]
  
      left = ((b2_x1 + b2_x2) - (b1_x1 + b1_x2)) ** 2 / 4
      right = ((b2_y1 + b2_y2) - (b1_y1 + b1_y2)) ** 2 / 4
      rho2 = left + right
  
      # DIoU
      dious = ious - rho2 / c2
      loss = 1 - dious
      return loss
  
imports: import math
  import warnings
  from typing import Optional
  import torch
  import torch.nn
  from torch import Tensor
  from mmdet.registry import MODELS
  from mmdet.structures.bbox import bbox_overlaps
  from .utils import weighted_loss
label_desc: Use image node_id 4 for calling a global function with example usage: diou_loss(pred, target, eps) and returns: loss