'Tensor' object is not callable with apply function

Hello everyone! I am getting an error for which I can’t resolve by myself. I am getting the error : “TypeError: ‘Tensor’ object is not callable” and trying multiple solutions found online resulted no improvement.

My code is the following:

`tens = FaceBoxesBasicTransform(image)#nd array
print(tens.shape)
print(type(tens))
tens = Variable(torch.from_numpy(tens).unsqueeze(0))
if use_cuda:
tens = tens.cuda()

print(type(tens))#<------------- <class 'torch.Tensor'>
t1 = time.time()
y = net.apply(tens)# error here`

The net variable is from this class : `class FaceBox(nn.Module):

def __init__(self, cfg, phase='train'):
    super(FaceBox, self).__init__()
    self.phase = phase
    # model
    self.conv1 = nn.Conv2d(3, 24, kernel_size=7,
                           stride=4, padding=3, bias=False)
    self.bn1 = nn.BatchNorm2d(24)
    self.conv2 = nn.Conv2d(48, 64, kernel_size=5,
                           stride=2, padding=2, bias=False)
    self.bn2 = nn.BatchNorm2d(64)

    self.inception1 = Inception()
    self.inception2 = Inception()
    self.inception3 = Inception()

    self.conv3_1 = conv_bn_relu(128, 128, kernel_size=1)
    self.conv3_2 = conv_bn_relu(
        128, 256, kernel_size=3, stride=2, padding=1)
    self.conv4_1 = conv_bn_relu(256, 128, kernel_size=1)
    self.conv4_2 = conv_bn_relu(
        128, 256, kernel_size=3, stride=2, padding=1)

    self.multilbox = MultiBoxLayer()

    if self.phase == 'test':
        self.softmax = nn.Softmax(dim=-1)
        self.test_det = Detect(cfg)

@staticmethod
def forward(self, x):
    img_size = x.size()[2:]
    print(img_size)
    source = []

    print(img_size)

    x = self.conv1(x)
    x = self.bn1(x)
    x = F.relu(torch.cat((F.relu(x), F.relu(-x)), 1))`

Thanks in advance for anyone who can help !

nn.Module.apply expects a function as its input and won’t work if you pass a tensor to it.
From the docs:

Applies fn recursively to every submodule (as returned by .children()) as well as self. Typical use includes initializing the parameters of a model (see also torch.nn.init).

If you want to execute the forward pass with the tensor as the input, call the model directly:

output = net(tens)

Thanks for you response. Unfortunately I still get an error despite your advice. The error is : TypeError: FaceBox.forward() missing 1 required positional argument: 'x'. I guess it expects to pass somehow self in it then the tensor but by tinkering with it I couldn’t find anything.

The error occured at the line ‘y = net(tens)’

Remove the @staticmethod and it should work.

removing the “@staticmethod” yields this error : “RuntimeError: Legacy autograd function with non-static forward method is deprecated. Please use new-style autograd function with static forward method. (Example: Automatic differentiation package - torch.autograd — PyTorch 1.12 documentation)”

In your original code you were using a custom nn.Module, which does not expect the staticmethod.
The new error is raised if you are defining a custom autograd.Function which does need the staticmethod decorator, so it seems you are mixing up different use cases.