ssd.pytorch icon indicating copy to clipboard operation
ssd.pytorch copied to clipboard

questions in detection.py

Open afcedf opened this issue 7 years ago • 11 comments

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?

afcedf avatar May 24 '18 07:05 afcedf

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".

zxjzxj9 avatar Jun 07 '18 08:06 zxjzxj9

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)

pyaf avatar Oct 09 '18 15:10 pyaf

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.]

anotherTK avatar Dec 05 '18 09:12 anotherTK

@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.

pengfeidip avatar Jan 08 '19 12:01 pengfeidip

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.

pengfeidip avatar Jan 08 '19 12:01 pengfeidip

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.

qigtang avatar May 15 '19 23:05 qigtang

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.

qigtang avatar May 15 '19 23:05 qigtang

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

pengfeidip avatar Jun 05 '19 11:06 pengfeidip

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

lc17721825 avatar Jun 27 '19 12:06 lc17721825

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:)

atsushi-tmdu avatar Jul 15 '19 06:07 atsushi-tmdu

should be flt[(rank > net.detect.top_k).unsqueeze(-1).expand_as(flt)]=0 it will change the value of output You can verify that, in each image, output will only have top_k boxes with conf>0.

calebx89 avatar Jan 05 '23 16:01 calebx89