WongKinYiu / ScaledYOLOv4

Scaled-YOLOv4: Scaling Cross Stage Partial Network
GNU General Public License v3.0
2.02k stars 574 forks source link

loss instability #145

Open fede-qopius opened 3 years ago

fede-qopius commented 3 years ago

Hi Guys, I guess there is an instability in the loss function due to a division by zero. can you please confirm that?

`def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False):

Returns the IoU of box1 to box2. box1 is 4, box2 is nx4

  box2 = box2.T

  # Get the coordinates of bounding boxes
  if x1y1x2y2:  # x1, y1, x2, y2 = box1
      b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
      b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
  else:  # transform from xywh to xyxy
      b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2
      b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2
      b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2
      b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2

  # Intersection area
  inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
          (torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)

  # Union Area
  w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + 1e-16  #add by me
  w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + 1e-16 # add by me
  union = (w1 * h1 ) + w2 * h2 - inter # remove 1e-16

  iou = inter / union  # iou

  if GIoU or DIoU or CIoU:
      cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1)  # convex (smallest enclosing box) width
      ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1)  # convex height
      if GIoU:  # Generalized IoU https://arxiv.org/pdf/1902.09630.pdf
          c_area = cw * ch + 1e-16  # convex area
          return iou - (c_area - union) / c_area  # GIoU
      if DIoU or CIoU:  # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
          # convex diagonal squared
          c2 = cw ** 2 + ch ** 2 + 1e-16
          # centerpoint distance squared
          rho2 = ((b2_x1 + b2_x2) - (b1_x1 + b1_x2)) ** 2 / 4 + ((b2_y1 + b2_y2) - (b1_y1 + b1_y2)) ** 2 / 4
          if DIoU:
              return iou - rho2 / c2  # DIoU
          elif CIoU:  # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
              v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2)  #INSTABILITY
              with torch.no_grad():
                  alpha = v / (1 - iou + v + 1e-16)
              return iou - (rho2 / c2 + v * alpha)  # CIoU

  return iou`

I commented here above what I think can create the issue

WongKinYiu commented 3 years ago

w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + 1e-16 #add by me w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + 1e-16 # add by me

yes.

union = (w1 * h1 ) + w2 * h2 - inter # remove 1e-16

need not to remove.