'Tensor' object is not callable with apply function

Maxlanglet: <p>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.</p>
<p>My code is the following:</p>
<p>`tens = FaceBoxesBasicTransform(image)<span class="hashtag">#nd</span> array<br>
print(tens.shape)<br>
print(type(tens))<br>
tens = Variable(torch.from_numpy(tens).unsqueeze(0))<br>
if use_cuda:<br>
tens = tens.cuda()</p>
<pre><code>print(type(tens))#&lt;------------- &lt;class 'torch.Tensor'&gt;
t1 = time.time()
y = net.apply(tens)# error here`
</code></pre>
<p>The net variable is from this class : `class FaceBox(nn.Module):</p>
<pre><code>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))`
</code></pre>
<p>Thanks in advance for anyone who can help !</p>

ptrblck: <p><code>nn.Module.apply</code> expects a function as its input and won’t work if you pass a tensor to it.<br>
From the <a href="https://pytorch.org/docs/stable/generated/torch.nn.Module.html?highlight=apply#torch.nn.Module.apply">docs</a>:</p>
<blockquote>
<p>Applies <code>fn</code> recursively to every submodule (as returned by <code>.children()</code>) as well as self. Typical use includes initializing the parameters of a model (see also <a href="https://pytorch.org/docs/stable/nn.init.html#nn-init-doc">torch.nn.init</a>).</p>
</blockquote>
<p>If you want to execute the forward pass with the tensor as the input, call the model directly:</p>
<pre data-code-wrap="python"><code class="lang-python">output = net(tens)
</code></pre>

Maxlanglet: <p>Thanks for you response. Unfortunately I still get an error despite your advice. The error is : <code>TypeError: FaceBox.forward() missing 1 required positional argument: 'x'</code>. I guess it expects to pass somehow self in it then the tensor but by tinkering with it I couldn’t find anything.</p>
<p>The error occured at the line ‘y = net(tens)’</p>

ptrblck: <p>Remove the <code>@staticmethod</code> and it should work.</p>

Maxlanglet: <p>removing the “<span class="mention">@staticmethod</span>” 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: <a href="https://pytorch.org/docs/stable/autograd.html#torch.autograd.Function" class="inline-onebox" rel="noopener nofollow ugc">Automatic differentiation package - torch.autograd — PyTorch 1.12 documentation</a>)”</p>

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