Skip to content

Commit

Permalink
.detach() on bias init (ultralytics#8044)
Browse files Browse the repository at this point in the history
make init bias better, 
I think `detach` is more safer than `data`
  • Loading branch information
tcmyxc authored and tdhooghe committed Jun 10, 2022
1 parent 97358e7 commit a75f79b
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions models/yolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ def _initialize_biases(self, cf=None): # initialize biases into Detect(), cf is
# cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1.
m = self.model[-1] # Detect() module
for mi, s in zip(m.m, m.stride): # from
b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85)
b.data[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image)
b.data[:, 5:] += math.log(0.6 / (m.nc - 0.999999)) if cf is None else torch.log(cf / cf.sum()) # cls
b = mi.bias.view(m.na, -1).detach() # conv.bias(255) to (3,85)
b[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image)
b[:, 5:] += math.log(0.6 / (m.nc - 0.999999)) if cf is None else torch.log(cf / cf.sum()) # cls
mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)

def _print_biases(self):
Expand Down

0 comments on commit a75f79b

Please sign in to comment.