TypeError: max_pool2d_with_indices(): argument 'input' (position 1) must be Tensor, not Tensor

When SPP is invoked, the system reports errors:

import torch
import math
import torch.nn.functional as F
def spatial_pyramid_pool(previous_conv, num_sample, previous_conv_size, out_pool_size):
    for i in range(len(out_pool_size)):
        kernel_size = (math.ceil(previous_conv_size[0] / out_pool_size[i]), math.ceil(previous_conv_size[1] / out_pool_size[i]))
        stride = (math.ceil(previous_conv_size[0] / out_pool_size[i]), math.ceil(previous_conv_size[1] / out_pool_size[i]))
        pooling = (
            math.floor((kernel_size[0] * out_pool_size[i] - previous_conv_size[0] + 1) / 2), math.floor((kernel_size[1] * out_pool_size[i] - previous_conv_size[1] + 1) / 2))
       x = (F.max_pool2d(previous_conv, kernel_size=kernel_size, stride=stride, padding=pooling)).view(num_sample, -1)

        if(i == 0):
            spp = x.view(num_sample, -1)
            # nn.MaxPool2d.view()  已经试过,不对
            # print("spp size:",spp.size())
            # print("size:",spp.size())
            spp = torch.cat((spp, x.view(num_sample, -1)), 1)
    return spp

 File "C:/Users/15044/pycharmProjects/cell/CNN_SPP/recognition.py", line 161, in <module>
    logits = inference(x, False, regularizer)
  File "C:/Users/15044/pycharmProjects/cell/CNN_SPP/recognition.py", line 126, in inference
    spp = spatial_pyramid_pool(relu4, 63, [256, 256], [1, 4])
  File "C:\Users\15044\pycharmProjects\cell\CNN_SPP\spp_layer.py", line 38, in spatial_pyramid_pool
    x = (F.max_pool2d(previous_conv, kernel_size=kernel_size, stride=stride, padding=pooling)).view(num_sample, -1)
  File "D:\Users\15044\Anaconda3\lib\site-packages\torch\_jit_internal.py", line 132, in fn
    return if_false(*args, **kwargs)
  File "D:\Users\15044\Anaconda3\lib\site-packages\torch\nn\functional.py", line 425, in _max_pool2d
    input, kernel_size, stride, padding, dilation, ceil_mode)[0]
  File "D:\Users\15044\Anaconda3\lib\site-packages\torch\nn\functional.py", line 417, in max_pool2d_with_indices
    return torch._C._nn.max_pool2d_with_indices(input, kernel_size, _stride, padding, dilation, ceil_mode)
TypeError: max_pool2d_with_indices(): argument 'input' (position 1) must be Tensor, not Tensor

I’m not sure, which setup throws this error, since I’ve tried a few and all seem to work:

previous_conv = torch.randn(1, 3, 4, 4)
num_sample = 1
previous_conv_size = [3, 3]
out_pool_size = [2, 2]

spatial_pyramid_pool(previous_conv, num_sample, previous_conv_size, out_pool_size)

Could you post a code snippet to reproduce this issue?

PS: I’ve formatted your code so that I could easily copy it. You can add code snippets by wrapping them in three backticks ``` :wink:

I think the software version caused this error.
I changed the code as follows,this error no longer appears, but a new error has occurred.
change “import torch.nn.functional as F” to “import torch.nn as nn”
change"x = (F.max_pool2d(previous_conv, kernel_size=kernel_size, stride=stride, padding=pooling)).view(num_sample, -1)" to “x = nn.MaxPool2d(previous_conv, kernel_size, stride, pooling)”
The new error is “AttributeError: ‘MaxPool2d’ object has no attribute ‘view’”
I don’t kown why,could you heip me?

nn.MaxPool2d is a module, so you would have to pass the tensor to it:

pool = nn.MaxPool2d(kernel_size, stride)
x = pool(previous_conv)

It looks like you are trying to pass the tensor directly to the constructor of the class.
The functional approach (using F.max_pool2d) should also work.

The code you’ve posted looks like Tensorflow code, so you might be luckier asking on stack overflow. :wink: