Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Learn about build targets #6863

Closed
1 task done
WZMIAOMIAO opened this issue Mar 5, 2022 · 4 comments
Closed
1 task done

Learn about build targets #6863

WZMIAOMIAO opened this issue Mar 5, 2022 · 4 comments
Labels
question Further information is requested

Comments

@WZMIAOMIAO
Copy link

WZMIAOMIAO commented Mar 5, 2022

Search before asking

Question

First of all, thank you for your great works.

I read and learn the code:

yolov5/utils/loss.py

Lines 169 to 222 in 8a66eba

def build_targets(self, p, targets):
# Build targets for compute_loss(), input targets(image,class,x,y,w,h)
na, nt = self.na, targets.shape[0] # number of anchors, targets
tcls, tbox, indices, anch = [], [], [], []
gain = torch.ones(7, device=targets.device) # normalized to gridspace gain
ai = torch.arange(na, device=targets.device).float().view(na, 1).repeat(1, nt) # same as .repeat_interleave(nt)
targets = torch.cat((targets.repeat(na, 1, 1), ai[:, :, None]), 2) # append anchor indices
g = 0.5 # bias
off = torch.tensor([[0, 0],
[1, 0], [0, 1], [-1, 0], [0, -1], # j,k,l,m
# [1, 1], [1, -1], [-1, 1], [-1, -1], # jk,jm,lk,lm
], device=targets.device).float() * g # offsets
for i in range(self.nl):
anchors = self.anchors[i]
gain[2:6] = torch.tensor(p[i].shape)[[3, 2, 3, 2]] # xyxy gain
# Match targets to anchors
t = targets * gain
if nt:
# Matches
r = t[:, :, 4:6] / anchors[:, None] # wh ratio
j = torch.max(r, 1 / r).max(2)[0] < self.hyp['anchor_t'] # compare
# j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n)=wh_iou(anchors(3,2), gwh(n,2))
t = t[j] # filter
# Offsets
gxy = t[:, 2:4] # grid xy
gxi = gain[[2, 3]] - gxy # inverse
j, k = ((gxy % 1 < g) & (gxy > 1)).T
l, m = ((gxi % 1 < g) & (gxi > 1)).T
j = torch.stack((torch.ones_like(j), j, k, l, m))
t = t.repeat((5, 1, 1))[j]
offsets = (torch.zeros_like(gxy)[None] + off[:, None])[j]
else:
t = targets[0]
offsets = 0
# Define
b, c = t[:, :2].long().T # image, class
gxy = t[:, 2:4] # grid xy
gwh = t[:, 4:6] # grid wh
gij = (gxy - offsets).long()
gi, gj = gij.T # grid xy indices
# Append
a = t[:, 6].long() # anchor indices
indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices
tbox.append(torch.cat((gxy - gij, gwh), 1)) # box
anch.append(anchors[a]) # anchors
tcls.append(c) # class
return tcls, tbox, indices, anch

My guess is that offsets are introduced to get more positive samples. Then, I want to know whether I understand correctly. Here is the diagram I drew:

image

If I don't understand correctly, I hope you can give me some tips. Thank you!

Additional

No response

@WZMIAOMIAO WZMIAOMIAO added the question Further information is requested label Mar 5, 2022
@WZMIAOMIAO WZMIAOMIAO changed the title Learn about built targets Learn about build targets Mar 6, 2022
@glenn-jocher
Copy link
Member

@WZMIAOMIAO yes this is correct!

@WZMIAOMIAO
Copy link
Author

@WZMIAOMIAO yes this is correct!

thank you for your reply.

@WZMIAOMIAO
Copy link
Author

#6998

@myasser63
Copy link

@WZMIAOMIAO Can you explain this more in your summary?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants