Onv2d(): argument 'input' (position 1) must be Tensor, not NoneType

username: Archy_dragon
cooked: <p>Hi I try to create CNN network and this error pop up when i try to run it can anyone help</p>
<pre><code class="lang-auto">class gface(nn.Module):
    def __init__(self, bottleneck_setting=MobiFace_bottleneck_setting, final=False):
        super(gface, self).__init__()
        self.final = final
        self.conv1 = ConvBlock(3, 64, 3, 2, 1)

        self.dw_conv1 = ConvBlock(64, 64, 3, 1, 1, dw=True)

        self.conv3 = ConvBlock1(64, 64, 3, 1, 1, dw=True)

        self.conv4= ConvBlock5(64,128,3, 1, 1)

        self.conv5= ConvBlock5(128,256,3, 1, 1)

        self.conv6= ConvBlock5(256,512,3, 1, 1)

        self.conv7 = ConvBlock(512, 512, 1, 1, 0, linear=True)

        #self.linear1 = nn.Linear(512*7*7, 512)

        self.linear7 = ConvBlock4(512, 512, (7, 6), 1, 0, dw=True, linear=True)

        self.linear1 = ConvBlock4(512, 128, 1, 1, 0, linear=True)

        self.prelu1 = nn.PReLU()



    def forward(self, x):
        x = self.conv1(x)
        x = self.dw_conv1(x)
        x= self.conv3(x)
        x=self.conv4(x)
        x= self.conv5(x)
        x=self.conv6(x)
        x=self.conv7(x)
        #x = x.view(x.size(0), -1)
        #x = self.linear1(x)
        #if self.final is False:
         # x = self.prelu1(x)
        x = self.linear7(x)
        x = self.linear1(x)
        x = x.view(x.size(0), -1)
        return x
        
        


</code></pre>
<pre><code class="lang-auto">class ConvBlock5(nn.Module):
    def __init__(self, inp, oup, k, s, p, dw=False, linear=False):
        super(ConvBlock5, self).__init__()
        self.linear = linear
        self.conv1 = nn.Conv2d(inp, inp *2, 1, 1, 0, bias=False)
        self.batch1 = nn.BatchNorm2d(inp * 2)
        self.PRelu1=nn.PReLU(inp * 2)
        self.max= nn.MaxPool2d((2, 2), 2)


        self.dw_conv2=nn.Conv2d(inp * 2, inp * 2, k, s, p,groups=inp * 2, bias=False)
        self.batch2=nn.BatchNorm2d(inp * 2)
        self.PRelu2=nn.PReLU(inp * 2)
        self.max1=  nn.MaxPool2d((2, 2), 2)
        

        self.conv3= nn.Conv2d(inp * 2, oup, 1, 1, 0, bias=False)
        self.batch3=nn.BatchNorm2d(oup)


        

        #self.maxP = x

    def forward(self, x):
        x = self.conv1(x)
        x = self.batch1(x)
        x=self.PRelu1(x)
        x= self.max(x)
        self.dw_conv2(x)
        self.batch2(x)
        self.PRelu2(x)
        self.conv3(x)
        self.batch3(x)
       # self.max1(x)
        #self.conv4(x)
        #self.dw_conv3
       # self.batch4(x)
</code></pre>
<pre><code class="lang-auto">class ConvBlock4(nn.Module):
    def __init__(self, inp, oup, k, s, p, dw=False, linear=False):
        super(ConvBlock4, self).__init__()
        self.linear = linear
        if dw:
            self.conv = nn.Conv2d(inp, oup, k, s, p, groups=inp, bias=False)
        else:
            self.conv = nn.Conv2d(inp, oup, k, s, p, bias=False)
        self.bn = nn.BatchNorm2d(oup)
        if not linear:
            self.prelu = nn.PReLU(oup)
    def forward(self, x):
        x = self.conv(x)
        x = self.bn(x)
        if self.linear:
            return x
        else:
            return self.prelu(x)

</code></pre>
<pre><code class="lang-auto">class ConvBlock1(nn.Module):
    def __init__(self, inp, oup, k, s, p, dw=False, linear=False):
        super(ConvBlock1, self).__init__()
        self.linear = linear
        self.conv = nn.Conv2d(inp, oup, k, s, p, groups=inp, bias=False)
        self.bn = nn.BatchNorm2d(oup)
        #self.RELU=nn.ReLU(oup)
        self.prelu = nn.PReLU(oup)
        self.Avgpoo = nn.MaxPool2d((3,3), 2)

        #self.maxP = x

    def forward(self, x):
        x = self.conv(x)
        x = self.bn(x)
        x=self.prelu(x)
        x=self.Avgpoo(x)
</code></pre>
<p>this is when i try to run example</p>
<pre><code class="lang-auto">if __name__ == "__main__":
    input = Variable(torch.FloatTensor(2, 3, 112, 96))
    net = gface()
    print(net)
    x = net(input)
    print(x.shape)
</code></pre>
<p>and this is error</p>
<pre><code class="lang-auto">&lt;ipython-input-17-d11d5e404b1f&gt; in &lt;module&gt;()
      3     net = gface()
      4     print(net)
----&gt; 5     x = net(input)
      6     print(x.shape)

6 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight)
    414                             _pair(0), self.dilation, self.groups)
    415         return F.conv2d(input, weight, self.bias, self.stride,
--&gt; 416                         self.padding, self.dilation, self.groups)
    417 
    418     def forward(self, input: Tensor) -&gt; Tensor:

TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not NoneType
</code></pre>

username: ptrblck
cooked: <p>Your modules are missing the <code>return</code> statement also you are not reassigning the activations in the second part of this <code>forward</code> method:</p>
<pre><code class="lang-python">        x = self.conv1(x)
        x = self.batch1(x)
        x=self.PRelu1(x)
        x= self.max(x)
        self.dw_conv2(x)
        self.batch2(x)
        self.PRelu2(x)
        self.conv3(x)
        self.batch3(x)
</code></pre>

username: Archy_dragon
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="93322">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>you are not reassigning the activations</p>
</blockquote>
</aside>
<p>what do you man by this part?</p>

username: ptrblck
cooked: <p>You have to reassign the output of a module to a variable.<br>
Your current code doesn’t use the output:</p>
<pre><code class="lang-python">        self.dw_conv2(x)
        self.batch2(x)
        self.PRelu2(x)
        self.conv3(x)
        self.batch3(x)
</code></pre>
<p>and it should be changed to:</p>
<pre><code class="lang-python">        x = self.dw_conv2(x)
        x = self.batch2(x)
        x = self.PRelu2(x)
        x = self.conv3(x)
        x = self.batch3(x)
</code></pre>