Loading pretrained weights into new model

Thanks for the update. I can’t comment on the accuracy, as I don’t have any specific expectations which pretrained model should or should not work on which custom dataset.

i tried replacing conv2d with weights from vgg layers like this (copy output of pool layers by matching the output dimension of my model) but the accuracy is almost same, no significant improvement. Please help me, am i missing something? Is this correct way?

class fcn32(nn.Module):
    def __init__(self):
        super(fcn32, self).__init__()
        #self.conv1_1 = nn.Conv2d(3, 64, 3, 1, 1)
        self.conv1_1 = vgg16.features[0]
        #self.bn1_1 = nn.BatchNorm2d()
        self.relu1_1 = nn.ReLU()
        #self.conv1_2 = nn.Conv2d(64, 64, 3, 1, 1)
        self.conv1_2 = vgg16.features[2]
        #self.bn1_2 = nn.BatchNorm2d()
        self.relu1_2 = nn.ReLU()
        self.pool1 = nn.MaxPool2d(2, 2)#/2

        #self.conv2_1 = nn.Conv2d(64, 128, 3, 1, 1)
        self.conv2_1 = vgg16.features[5]
        # self.bn2_1 = nn.BatchNorm2d()
        self.relu2_1 = nn.ReLU()
        #self.conv2_2 = nn.Conv2d(128, 128, 3, 1, 1)
        self.conv2_2 = vgg16.features[7]
        # self.bn2_2 = nn.BatchNorm2d()
        self.relu2_2 = nn.ReLU()
        self.pool2 = nn.MaxPool2d(2, 2)#/4

        #self.conv3_1 = nn.Conv2d(128, 256, 3, 1, 1)
        self.conv3_1 = vgg16.features[10]
        # self.bn3_1 = nn.BatchNorm2d()
        self.relu3_1 = nn.ReLU()
        #self.conv3_2 = nn.Conv2d(256, 256, 3, 1, 1)
        self.conv3_2 = vgg16.features[14]
        # self.bn3_2 = nn.BatchNorm2d()
        self.relu3_2 = nn.ReLU()
        self.pool3 = nn.MaxPool2d(2, 2)#/8

        #self.conv4_1 = nn.Conv2d(256, 512, 3, 1, 1)
        self.conv4_1 = vgg16.features[17]
        # self.bn4_1 = nn.BatchNorm2d()
        self.relu4_1 = nn.ReLU()
        #self.conv4_2 = nn.Conv2d(512, 512, 3, 1, 1)
        self.conv4_2 = vgg16.features[28]
        # self.bn4_2 = nn.BatchNorm2d()
        self.relu4_2 = nn.ReLU()
        self.pool4 = nn.MaxPool2d(2, 2)  # /16

        self.conv5_1 = nn.Conv2d(512, 1024, 3, 1, 1)
        # self.bn5_1 = nn.BatchNorm2d()
        self.relu5_1 = nn.ReLU()
        self.conv5_2 = nn.Conv2d(1024, 1024, 3, 1, 1)
        # self.bn5_2 = nn.BatchNorm2d()
        self.relu5_2 = nn.ReLU()
        self.pool5 = nn.MaxPool2d(2, 2)  # /32

        self.fc1 = nn.Conv2d(1024, 4096, 3, 1, 1)
        self.relu6 = nn.ReLU()
        self.drop6 = nn.Dropout2d()

        self.fc2 = nn.Conv2d(4096, 4096, 1)
        self.relu7 = nn.ReLU()
        self.drop7 = nn.Dropout2d()

        self.fc3 = nn.Conv2d(4096, 32, 1)
        self.up = nn.ConvTranspose2d(32, 32, kernel_size=32, stride=32)

    def forward(self, input):
        #print(input.shape)

        x = self.conv1_1(input)
        x = self.relu1_1(x)

        x = self.conv1_2(x)
        x = self.relu1_2(x)
        x = self.pool1(x)
        #print(x.shape)

        x = self.conv2_1(x)
        x = self.relu2_1(x)

        x = self.conv2_2(x)
        x = self.relu2_2(x)
        x = self.pool2(x)
        #print(x.shape)

        x = self.conv3_1(x)
        x = self.relu3_1(x)

        x = self.conv3_2(x)
        x = self.relu3_2(x)
        x = self.pool3(x)
        #print(x.shape)

        x = self.conv4_1(x)
        x = self.relu4_1(x)

        x = self.conv4_2(x)
        x = self.relu4_2(x)
        x = self.pool4(x)
        #print(x.shape)
        x = self.conv5_1(x)
        x = self.relu5_1(x)
        x = self.conv5_2(x)
        x = self.relu5_2(x)
        x = self.pool5(x)
        #print(x.shape)
        x = self.fc1(x)
        x = self.relu6(x)
        x = self.drop6(x)
        #print(x.shape)
        x = self.fc2(x)
        x = self.relu7(x)
        x = self.drop7(x)
        #print(x.shape)
        x = self.fc3(x)
        #print(x.shape)
        x = self.up(x)
        #print(x.shape)
        return x

I still cannot comment on the model accuracy and what the expectations are.
Could you describe a bit more why your model architecture using the VGG16 features should increase the accuracy, i.e. are you trying to reimplement a knowingly good model based on previous work?

@ptrblck thank you, but is this correct way of loading pretrained weights?

Edit: when I use output of pool layers like above, I get mean iou of 70%.(I want this method so that I can access intermediate pool layers to build other variations of fcn models for segmentation. Is this the right way? I like this method because i am not changing my original model, I am just changing the conv blocks with pretrained conv block weights.

When I use all vgg 16 features and classifier at the end like below I get mean iou of 96%. My doubt is how come this one gives good iou than above mentioned method.

class fcn(nn.Module):
def **init**(self):
super(fcn, self).**init**()
self.features = vgg16.features
self.classifier = nn.Sequential(nn.Conv2d(512, 4096, 7),nn.ReLU(inplace=True),nn.Conv2d(4096, 4096, 1),nn.ReLU(inplace=True),nn.Conv2d(4096, 32, 1),nn.ConvTranspose2d(32, 32, 224, stride=32)
)

def forward(self, x):
x = self.features(x)
x = self.classifier(x)
#print(x.shape)
return x