Ssd.pytorch: questions in detection.py

Created on 24 May 2018  路  10Comments  路  Source: amdegroot/ssd.pytorch

In line 61
flt[(rank < self.top_k).unsqueeze(-1).expand_as(flt)].fill_(0)
why rank < self.top_k? I think it should be rank > self.top_k. The position not in top_k need to be filled with value 0.
Am I right? Could you give me some suggestions?

Most helpful comment

According to my observation, this line:
flt[(rank < self.top_k).unsqueeze(-1).expand_as(flt)].fill_(0)
has no effect. Only when writing
flt[(rank < self.top_k).unsqueeze(-1).expand_as(flt)] = 0
will the code modify the data in flt.
And also I think the code is wrong, it shall be rank > self.top_k. Since the filtering will not take effect, the final result may seem to be "right".

All 10 comments

According to my observation, this line:
flt[(rank < self.top_k).unsqueeze(-1).expand_as(flt)].fill_(0)
has no effect. Only when writing
flt[(rank < self.top_k).unsqueeze(-1).expand_as(flt)] = 0
will the code modify the data in flt.
And also I think the code is wrong, it shall be rank > self.top_k. Since the filtering will not take effect, the final result may seem to be "right".

Hey guys, could anyone of you explain me this snippet?
here

        flt = output.contiguous().view(num, -1, 5)
        _, idx = flt[:, :, 0].sort(1, descending=True)
        _, rank = idx.sort(1)
        flt[(rank < self.top_k).unsqueeze(-1).expand_as(flt)].fill_(0)
return output

why are dealing with flt when we are not using it anywhere? (we are returning output not `flt)

it's a bug should be fixed:
from

# this would not change the output element as '[]' operator will create a new object.
flt[(rank < self.top_k).unsqueeze(-1).expand_as(flt)].fill_(0)

to

flt[(rank >= self.top_k).unsqueeze(-1).expand_as(flt)] = 0

[actually, this would not have much impact on the results as the self.top_k equals to 200, and you can find the valid boxes number would less than it.]

@pyaf Because the flt is a view of output, if you change the value of flt, the value of output will be changed too...

Although I can't understand the following code

_, idx = flt[:, :, 0].sort(1, descending=True)   
_, rank = idx.sort(1)
flt[(rank < self.top_k).unsqueeze(-1).expand_as(flt)].fill_(0)

--------- sorry, I was half wrong --------

in #124

@FostorHUNT I am sorry that I can't agree with you. In fact, use the mask (torch.uint8) as slice, the tensor will not share the memory.

it's a bug should be fixed:
from

# this would not change the output element as '[]' operator will create a new object.
flt[(rank < self.top_k).unsqueeze(-1).expand_as(flt)].fill_(0)

to

flt[(rank >= self.top_k).unsqueeze(-1).expand_as(flt)] = 0

[actually, this would not have much impact on the results as the self.top_k equals to 200, and you can find the valid boxes number would less than it.]

@tkianai In your code , the comment "# this would not change the output element as '[]' operator will create a new object." is right? I did a small experiment , I dont think the comment is right.... And my pytorch version is 0.4.1

--------- sorry, I was half wrong --------

in #124

@FostorHUNT I am sorry that I can't agree with you. In fact, use the mask (torch.uint8) as slice, the tensor will not share the memory.

I have the same question regarding that part of the code too.

flt[(rank < self.top_k).unsqueeze(-1).expand_as(flt)].fill_(0)

First, rank > self.top_k is making sense, not rank < self.top_k. Second this code is making a copy, so it has no effect. Third, output has size (batch_size,

According to my observation, this line:
flt[(rank < self.top_k).unsqueeze(-1).expand_as(flt)].fill_(0)
has no effect. Only when writing
flt[(rank < self.top_k).unsqueeze(-1).expand_as(flt)] = 0
will the code modify the data in flt.
And also I think the code is wrong, it shall be rank > self.top_k. Since the filtering will not take effect, the final result may seem to be "right".

Yes, I agree with you. Found the exact same problem while reading through the code.

it's a bug should be fixed:
from

# this would not change the output element as '[]' operator will create a new object.
flt[(rank < self.top_k).unsqueeze(-1).expand_as(flt)].fill_(0)

to

flt[(rank >= self.top_k).unsqueeze(-1).expand_as(flt)] = 0

[actually, this would not have much impact on the results as the self.top_k equals to 200, and you can find the valid boxes number would less than it.]

@tkianai In your code , the comment "# this would not change the output element as '[]' operator will create a new object." is right? I did a small experiment , I dont think the comment is right.... And my pytorch version is 0.4.1

--------- sorry, I was half wrong --------

in #124

@FostorHUNT I am sorry that I can't agree with you. In fact, use the mask (torch.uint8) as slice, the tensor will not share the memory.

The code will have effect, since it originally limit self.top_k for each class. Then it limit top_k to all combined classes.

According to my observation, this line:
flt[(rank < self.top_k).unsqueeze(-1).expand_as(flt)].fill_(0)
has no effect. Only when writing
flt[(rank < self.top_k).unsqueeze(-1).expand_as(flt)] = 0
will the code modify the data in flt.
And also I think the code is wrong, it shall be rank > self.top_k. Since the filtering will not take effect, the final result may seem to be "right".

You are right!!!!

  1. The code has no effect, it cannot modify the data;
  2. There is bug about rank < self.top_k, it should be rank > self.top_k

Hello, everyone. I think it should be ''rank >= self.top_k'' rather than ''rank > self.top_k''. Because the index starts from 0 and ''rank > self.top_k'' will select the top k+1 result. @pengfeidip

a = torch.rand(3,4)

a
tensor([[0.8526, 0.0782, 0.6625, 0.6688],
[0.3444, 0.5876, 0.9804, 0.4733],
[0.0099, 0.4177, 0.0619, 0.1186]])

b = a.view(4,-1)

b
tensor([[0.8526, 0.0782, 0.6625],
[0.6688, 0.3444, 0.5876],
[0.9804, 0.4733, 0.0099],
[0.4177, 0.0619, 0.1186]])

_, idx = b.sort(1, descending=True)
_, rank = idx.sort(1)
b[(rank>1)]=0

b
tensor([[0.8526, 0.0000, 0.6625],
[0.6688, 0.0000, 0.5876],
[0.9804, 0.4733, 0.0000],
[0.4177, 0.0000, 0.1186]])
a
tensor([[0.8526, 0.0000, 0.6625, 0.6688],
[0.0000, 0.5876, 0.9804, 0.4733],
[0.0000, 0.4177, 0.0000, 0.1186]])

I hope this means something to you锛氾級

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LAWSSSS picture LAWSSSS  路  4Comments

DHZS picture DHZS  路  5Comments

JingpengSun picture JingpengSun  路  4Comments

xinyu1214 picture xinyu1214  路  5Comments

chienyiwang picture chienyiwang  路  3Comments