How to perform sum pooling?


(Fanq15) #1

In image retrieval tasks, the sum pooling is a common operator.
But I can’t find it in pytorch.
Could anyone tell me how to perform sum pooling in CNN?


(Francisco Massa) #2

You can view the feature map as a 1d tensor, and call sum on it.


(Fanq15) #3

Thanks! It’s the key to handle the CNN tensor!


(jdhao) #4

For anyone who is also interested on how to do it exactly. Suppose the feature map is of size N*C*H*W, after sum-pooling, it will become tensor of size N*C (N image, each has a feature vector of dimension C). Here is a code snippet to do it,

# suppose  x is the feature map after some layer
 x = torch.sum(x.view(x.size(0), x.size(1), -1), dim=2)

The above code flattens the 2nd and 3rd dimension of original tensor to a vector and calculate sum on the newly created tensor on dimension 2, which is exactly what sum-pooling does.


(Adeel Zafar) #5

How to perform Sum pooling with view if one needs to use a particular kernel size and stride just like nn.Maxpool2d ? In this case input is N*C*W_in*H_in and output should be N*C*W_out*H_out

.