Skip to content

Commit 05aae17

Browse files
torch.split() 1.7.0 compatibility fix (#7102)
* Update loss.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update loss.py Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a600bae commit 05aae17

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

utils/loss.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,15 @@ def __init__(self, model, autobalance=False):
108108
if g > 0:
109109
BCEcls, BCEobj = FocalLoss(BCEcls, g), FocalLoss(BCEobj, g)
110110

111-
det = de_parallel(model).model[-1] # Detect() module
112-
self.balance = {3: [4.0, 1.0, 0.4]}.get(det.nl, [4.0, 1.0, 0.25, 0.06, 0.02]) # P3-P7
113-
self.ssi = list(det.stride).index(16) if autobalance else 0 # stride 16 index
111+
m = de_parallel(model).model[-1] # Detect() module
112+
self.balance = {3: [4.0, 1.0, 0.4]}.get(m.nl, [4.0, 1.0, 0.25, 0.06, 0.02]) # P3-P7
113+
self.ssi = list(m.stride).index(16) if autobalance else 0 # stride 16 index
114114
self.BCEcls, self.BCEobj, self.gr, self.hyp, self.autobalance = BCEcls, BCEobj, 1.0, h, autobalance
115+
self.na = m.na # number of anchors
116+
self.nc = m.nc # number of classes
117+
self.nl = m.nl # number of layers
118+
self.anchors = m.anchors
115119
self.device = device
116-
for k in 'na', 'nc', 'nl', 'anchors':
117-
setattr(self, k, getattr(det, k))
118120

119121
def __call__(self, p, targets): # predictions, targets
120122
lcls = torch.zeros(1, device=self.device) # class loss
@@ -129,7 +131,8 @@ def __call__(self, p, targets): # predictions, targets
129131

130132
n = b.shape[0] # number of targets
131133
if n:
132-
pxy, pwh, _, pcls = pi[b, a, gj, gi].tensor_split((2, 4, 5), dim=1) # target-subset of predictions
134+
# pxy, pwh, _, pcls = pi[b, a, gj, gi].tensor_split((2, 4, 5), dim=1) # faster, requires torch 1.8.0
135+
pxy, pwh, _, pcls = pi[b, a, gj, gi].split((2, 2, 1, self.nc), 1) # target-subset of predictions
133136

134137
# Regression
135138
pxy = pxy.sigmoid() * 2 - 0.5

0 commit comments

Comments
 (0)