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

a bug in nms #31

Open
njustymk opened this issue Jul 12, 2020 · 2 comments
Open

a bug in nms #31

njustymk opened this issue Jul 12, 2020 · 2 comments

Comments

@njustymk
Copy link

njustymk commented Jul 12, 2020

def nms(boxes, scores, overlap=0.5, top_k=200):
keep = scores.new(scores.size(0)).zero_().long()
if boxes.numel() == 0:
return keep
x1 = boxes[:, 0]
y1 = boxes[:, 1]
x2 = boxes[:, 2]
y2 = boxes[:, 3]
area = torch.mul(x2 - x1, y2 - y1)
v, idx = scores.sort(0) # sort in ascending order
# I = I[v >= 0.01]
idx = idx[-top_k:] # indices of the top-k largest vals
xx1 = boxes.new()
yy1 = boxes.new()
xx2 = boxes.new()
yy2 = boxes.new()
w = boxes.new()
h = boxes.new()

# keep = torch.Tensor()
count = 0
while idx.numel() > 0:
    i = idx[-1]  # index of current largest val
    # keep.append(i)
    keep[count] = i
    count += 1
    if idx.size(0) == 1:
        break
    idx = idx[:-1]  # remove kept element from view
    # load bboxes of next highest vals
    torch.index_select(x1, 0, idx, out=xx1)
    torch.index_select(y1, 0, idx, out=yy1)
    torch.index_select(x2, 0, idx, out=xx2)
    torch.index_select(y2, 0, idx, out=yy2)
    # store element-wise max with next highest score
    xx1 = torch.clamp(xx1, min=x1[i])
    yy1 = torch.clamp(yy1, min=y1[i])
    xx2 = torch.clamp(xx2, max=x2[i])
    yy2 = torch.clamp(yy2, max=y2[i])
    w.resize_as_(xx2)
    h.resize_as_(yy2)
    w = xx2 - xx1
    h = yy2 - yy1
    # check sizes of xx1 and xx2.. after each iteration
    w = torch.clamp(w, min=0.0)
    h = torch.clamp(h, min=0.0)
    inter = w * h
    # IoU = i / (area(a) + area(b) - i)
    rem_areas = torch.index_select(area, 0, idx)  # load remaining areas)
    union = (rem_areas - inter) + area[i]
    IoU = inter / union  # store result in iou
    # keep only elements with an IoU <= overlap
    idx = idx[IoU.le(overlap)]
**_return keep, count_**

the two return are not same ,it may take error:
layers/functions/detection.py", line 67, in forward
ValueError: not enough values to unpack (expected 2, got 0)

@Rui-coder
Copy link

Hello, I also find this problem, my resolution is change the code 'if boxes.numel()==0 return keep' to '... ... return keep, 0'. Because the count means the kept boxes number, if boxes' num is 0, then the keep's num also should be 0.

@yxlijun
Copy link
Owner

yxlijun commented Apr 13, 2022 via email

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

No branches or pull requests

3 participants