How to convert CNN to FCN?

To train a classifier in CIFAR100, I have already trained a classifier via VGGnets, but how can I convert my VGGnets to FCN?

My VGGnets code like below:

class Unit(nn.Module):
    def __init__(self,in_channels,out_channels):
        super(Unit,self).__init__()
 
        self.conv = nn.Conv2d(in_channels=in_channels,kernel_size=3,out_channels=out_channels,stride=1,padding=1)
        self.bn = nn.BatchNorm2d(num_features=out_channels)
        self.relu = nn.ReLU()
 
    def forward(self,input):
        output = self.conv(input)
        output = self.bn(output)
        output = self.relu(output)
 
        return output

class Model(nn.Module):
    def __init__(self,num_classes=100):
        super(Model,self).__init__()
 
        self.net = nn.Sequential(
            Unit(in_channels=3,out_channels=32),
            Unit(in_channels=32, out_channels=32),
            Unit(in_channels=32, out_channels=32),

            nn.MaxPool2d(kernel_size=2),

            Unit(in_channels=32, out_channels=64),
            Unit(in_channels=64, out_channels=64),
            Unit(in_channels=64, out_channels=64),
            Unit(in_channels=64, out_channels=64),

            nn.MaxPool2d(kernel_size=2),

            Unit(in_channels=64, out_channels=128),
            Unit(in_channels=128, out_channels=128),
            Unit(in_channels=128, out_channels=128),
            Unit(in_channels=128, out_channels=128),

            nn.MaxPool2d(kernel_size=2),

            Unit(in_channels=128, out_channels=128),
            Unit(in_channels=128, out_channels=128),
            Unit(in_channels=128, out_channels=128),

            nn.MaxPool2d(kernel_size=2))       
 
        self.dense = nn.Sequential(
            nn.Linear(in_features=2*2*128,out_features=num_classes))

    def forward(self, input):
        output = self.net(input)
        output = output.view(-1,2*2*128)
        output = self.dense(output)
        return output

Sincerely waiting for your help!

Hi,

I’m not sure what you mean by converting it to an FCN?
Fully connected networks and convolutional ones have different weight structure so there isn’t a one to one mapping between them when you train.
Could you give more context of why you want to do that?

Just a task and it requires me to train a FCN in CIFAR100.
Can I convert linear layer to conv layer and add convtranspose layer as the last layer?
Thank you for your help!

Just a task and it requires me to train a FCN in CIFAR100.

Then just linearize the input as a batch of 1D Tensors and apply an FCN to it. No need to consider CNN architectures.

Okay! I will have a try!
Thanks a lot!