Hi Prithviraj!
The short answer is that a Linear can perform a Conv2d as a special
case (assuming appropriate reshaping of the various tensors).
First make sure you understand how Conv2d and Linear treat the
dimensions of the tensors passed into them – which dimensions are
“channels,” which are 2d dimensions over which to convolve, and
so on.



 Xanthan:

I’m not sure whether the nn.Linear layer does convolution operation and if it doesn’t then the convolution operation needs to be done.


Assume that we fully flatten (except for the batch dimension)
the tensor passed into Linear so that it has shape
[nBatch, nChannels * height * width] (and Linear is
Linear (nChannels * height * width, output_channels)).
Then Linear doesn’t perform a convolution, per se, but it is
more general than a convolution in that you could construct a
Linear (that has lots of zeroes for weights) that performs a
convolution (assuming that you appropriately reshape the
output tensor).


if(layer=="last"):
      output_channels=5
                return nn.Sequential(
                    nn.Conv2d(64*8, output_channels, kernel_size, stride, padding, bias=False),
                    nn.Linear(output_channels,output_channels),
                    nn.Softmax(dim=1)
                    )

or it should be like this:
if(layer=="last"):
      output_channels=5
                return nn.Sequential(
                    nn.Linear(64*8,output_channels),
                    nn.Softmax(dim=1)
                    )



Note that your first version does not have a non-linearity in between
the Conv2d and Linear layers.  They therefore combine into a single
linear operation on your input tensor.  They can therefore be replaced
by a single Linear layer (assuming that you pay proper attention to
the shapes of the tensors and are willing to flatten the input tensor).
To say this a little bit differently, a Linear is the most general,
fully-connected layer, so it can perform any linear operation, including,
as a special case, a convolution, or a convolution followed by a
Linear (assuming no intervening non-linearity).
(As an aside, assuming that you’re performing “global” classification,
that is assigning the whole image to a single class, rather than
per-pixel classification, such as semantic segmentation, by the time
you get to the “last” layer you have probably reduced things down
global features rather than features that still maintain some spatial
location.  So in your “last” layer there would no longer be any spatial
dimensions over which to convolve.)
Best.
K. Frank