How to convert to linear

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?
this is my code

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.conv5(x)
        x = x.view(x.size(0), -1)
        x = self.linear1(x)

        return x

Look into

then set input of linear to heightwidthchannels and output to whatever you need

so i dont need to convert 2d am i correct

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

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?

so am i correct nn. Flatten will be easiest for me

am i correct you mean layer?

output size of conv layers is sometimes a mystery for me, there is a formula (How to calculate the output size after Conv2d in pytorch?), 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.

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)

what do you mean by GAN

Generative Adversarial Network

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

If you are dealing with constant size inputs, what I normally do is passing a dumpy input like dummpyInput = torch.zeros(someBatchSize, inputDim1, inputDim2) 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.
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.

can you explain what is the nn.squent ?

Sure, this is the doc’s.
What sequential do is to gather a serias of layers into one so you can call all the layers upon execution.
for example:

conv_module = nn.Sequential(

So now if you can conv_module(someTensor) you will get as out the tensor processed via all the layers inside the sequential.

so am i correct i should put all my network in the sequential? is the output result willl be the same or not really

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.