Repository: ultralytics/yolov5

Issue:
divided by 0 error
Before submitting a bug report, please be aware that your issue **must be reproducible** with all of the following, otherwise it is non-actionable, and we can not help you:
 - **Current repo**: run `git fetch && git status -uno` to check and `git pull` to update repo
 - **Common dataset**: coco.yaml or coco128.yaml
 - **Common environment**: Colab, Google Cloud, or Docker image. See https://github.com/ultralytics/yolov5#reproduce-our-environment
 
If this is a custom dataset/training question you **must include** your `train*.jpg`, `test*.jpg` and `results.png` figures, or we can not help you. You can generate these with `utils.plot_results()`.


## 🐛 Bug
in utils.py line 345:

```
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)
                with torch.no_grad():
                    alpha = v / (1 - iou + v)   ### should add + 1e-16 to avoid alpha = NAN.
                return iou - (rho2 / c2 + v * alpha)  # CIoU
```


## To Reproduce (REQUIRED)

it's hard to reproduce, I encounter it when I was trying some new target assigning  methods..

Output:
alpha = Nan.


## Expected behavior
alpha  be a normal value between 0 -  1


## Environment
If applicable, add screenshots to help explain your problem.

 - OS: [e.g. Ubuntu]
 - GPU [e.g. 2080 Ti]


## Additional context
Add any other context about the problem here.
it's hard to reproduce, I encounter it when I was trying some new target assigning  methods..



Discussion:
@AlexWang1900 ah, thanks for the bug report! It looks like c2 is already protected, so then alpha may be the only unprotected divide as you say. Yes we should add 1e-16 here. Would you like to submit a PR for this or I can include it in the next commit?
I will summit a PR thanks!😀



---Original---
From: "Glenn Jocher"<notifications@github.com&gt;
Date: Wed, Jul 29, 2020 02:26 AM
To: "ultralytics/yolov5"<yolov5@noreply.github.com&gt;;
Cc: "Mention"<mention@noreply.github.com&gt;;"AlexWang1900"<41888506@qq.com&gt;;
Subject: Re: [ultralytics/yolov5] divided by 0 error (#541)




 
@AlexWang1900 ah, thanks for the bug report! It looks like c2 is already protected, so then alpha may be the only unprotected divide as you say. Yes we should add 1e-16 here. Would you like to submit a PR for this or I can include it in the next commit?
 
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.

Resolving patch:
diff --git a/train.py b/train.py
--- a/train.py
+++ b/train.py
@@ -377,7 +377,7 @@ def train(hyp, tb_writer, opt, device):
 
                 # Save last, best and delete
                 torch.save(ckpt, last)
-                if (best_fitness == fi) and not final_epoch:
+                if best_fitness == fi: 
                     torch.save(ckpt, best)
                 del ckpt
         # end epoch ----------------------------------------------------------------------------------------------------
diff --git a/utils/activations.py b/utils/activations.py
--- a/utils/activations.py
+++ b/utils/activations.py
@@ -61,3 +61,16 @@ class Mish(nn.Module):  # https://github.com/digantamisra98/Mish
     @staticmethod
     def forward(x):
         return x * F.softplus(x).tanh()
+        
+        
+# FReLU https://arxiv.org/abs/2007.11824 --------------------------------------
+class FReLU(nn.Module):
+    def __init__(self, c1, k=3):  # ch_in, kernel
+        super().__init()__()
+        self.conv = nn.Conv2d(c1, c1, k, 1, 1, groups=c1)
+        self.bn = nn.BatchNorm2d(c1)
+        
+    @staticmethod
+    def forward(self, x):
+        return torch.max(x, self.bn(self.conv(x)))
+        
diff --git a/utils/utils.py b/utils/utils.py
--- a/utils/utils.py
+++ b/utils/utils.py
@@ -346,8 +346,8 @@ def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False):
             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)
                 with torch.no_grad():
-                    alpha = v / (1 - iou + v)
-                return iou - (rho2 / c2 + v * alpha)  # CIoU
+                    alpha = v / (1 - iou + v + 1e-16)
+                return iou - (rho2 / c2 + v * alpha )  # CIoU
 
     return iou
 


Test patch: