How to convert to linear

Archy_dragon: <p>HI i currently build CNN but im not sure how would i able to convert my 2D cnn so it able to process to linear can anyone help?<br>
this is my code</p>
<pre><code class="lang-auto">class MobiFace2(nn.Module):
    def __init__(self, bottleneck_setting=MobiFace_bottleneck_setting, final_linear=False):
        super(MobiFace2, self).__init__()
        self.final_linear = final_linear

        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(256, 512, 1, 1, 0, linear=True)

        self.linear1 = nn.Linear(not sure*not sure*512, 512)



    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)
        


        return x
</code></pre>

Topsoil: <p>Look into <a href="https://pytorch.org/docs/stable/generated/torch.nn.Flatten.html#torch.nn.Flatten" rel="nofollow noopener">https://pytorch.org/docs/stable/generated/torch.nn.Flatten.html#torch.nn.Flatten</a></p>
<p>then set input of linear to height<em>width</em>channels and output to whatever you need</p>

Archy_dragon: <p>so i dont need to convert 2d am i correct</p>

Topsoil: <p>Well, you migh try to first flatten your raw image, then concat with features vector, then pass it into linear layer, which will have the output size of height * width * channels, then tensor.reshape it into a shape of (batch,channels,height,width) and then pass it to convolutions, but that method has more steps and for me personally just feels harder</p>

Archy_dragon: <p>just wonder when you try to flatten your data right now i dont know after each filter what is my image size how to flatten the image?</p>

Archy_dragon: <p>so am i correct <code>nn.</code> <code>Flatten</code> will be easiest for me</p>

Archy_dragon: <aside class="quote no-group" data-username="Topsoil" data-post="2" data-topic="93315">
<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/topsoil/48/12027_2.png" class="avatar"> Topsoil:</div>
<blockquote>
<p>then set input of linear to height <em>width</em> channels</p>
</blockquote>
</aside>
<p>am i correct you mean layer?</p>

Topsoil: <p>output size of conv layers is sometimes a mystery for me, there is a formula (<a href="https://discuss.pytorch.org/t/how-to-calculate-the-output-size-after-conv2d-in-pytorch/20405" class="inline-onebox">How to calculate the output size after Conv2d in pytorch?</a>), but my general rule of thumb is that if kernel size is 3, use padding of 1 to keep size the same, if kernel is 5, use padding of 2, etc. and dont you even sized kernels.</p>
<p>You can flatten tensors using nn.Flatten, there are other ways like reshaping, but they might corrupt your data on the way through your network and cause unexpected problems (for me it was network generating checkboard garbage at output, it was a GAN, replacing .reshape with .permute solved the issue)</p>

Archy_dragon: <p>what do you mean by GAN</p>

Topsoil: <p>Generative Adversarial Network</p>

Archy_dragon: <p>when i read the link<br>
<code>torch.nn.</code> <code>Flatten</code> ( <em>start_dim: int = 1</em> ,  <em>end_dim: int = -1</em> )<br>
Just want to make sure am i correct start_dim that will be number of input chanel and end_dim that will be 1 ?</p>

SaharCarmel: <p>If you are dealing with constant size inputs, what I normally do is passing a dumpy input like <code>dummpyInput = torch.zeros(someBatchSize, inputDim1, inputDim2)</code> to the model.conv_layer() and then check the output shape so I can observe the output of the conv layer with my input size.<br>
In order to do that efficiently you should gather the conv part and the ff part in  nn.Sequential class. So you can pass an input only to the conv part of the model.</p>

Archy_dragon: <p>can you explain what is the nn.squent ?</p>

SaharCarmel: <p>Sure, this is the <a href="https://pytorch.org/docs/stable/generated/torch.nn.Sequential.html#torch.nn.Sequential" rel="nofollow noopener">doc’s</a>.<br>
What sequential do is to gather  a serias of layers into one so you can call all the layers upon execution.<br>
for example:</p>
<pre><code class="lang-auto">conv_module = nn.Sequential(
          nn.Conv2d(1,20,5),
          nn.ReLU(),
          nn.Conv2d(20,64,5),
          nn.ReLU()
        )
</code></pre>
<p>So now if you can <code> conv_module(someTensor)</code> you will get as out the tensor processed via all the layers inside the sequential.</p>

Archy_dragon: <p>so am i correct i should put all my network in the sequential? is the output result willl be the same or not really</p>

SaharCarmel: <p>I prefer to have the feature extractor and the ff parts taken apart, if you will put all the network together the outcome should be the same. But i won’t solve your problem since the linear part still needs as an input the incoming size.</p>