I have been using ResNet for transfer learning, but wanted to try Inception to see if I’d get better results. My way of doing this for ResNet is as follows:
class ResNet18(nn.Module):
def __init__(self, orig_model):
super(ResNet18, self).__init__()
self.drop = nn.Dropout2d(0.5).to(device)
self.bn = nn.BatchNorm2d(512).to(device)
self.bn2 = nn.BatchNorm1d(256)
self.bn3 = nn.BatchNorm1d(100)
self.orig = nn.Sequential(*(list(orig_model.children())[:-1])).to(device)
for param in self.orig.parameters():
param.requires_grad = True
# Replace the last fully-connected layer
# Parameters of newly constructed modules have requires_grad=True by default
self.fc = nn.Linear(512, 256).to(device)
self.fc2 = nn.Linear(256, 100).to(device)
self.fc3 = nn.Linear(100, 2).to(device)
def forward(self, x):
x = self.orig(x)
x = self.bn(x)
x = x.view(x.size(0), -1)
x = F.relu(self.bn2(self.fc(x)))
x = self.drop(x)
x = F.relu(self.bn3(self.fc2(x)))
x = self.drop(x)
x = F.relu(self.fc3(x))
p = F.softmax(x, dim=1)
return x, p
However, when I try this for Inception V3, I get errors. I have seen other solutions where they show how to manipulate the last FC layer. But I want to add more layers, and I haven’t seen a thread that shows a concrete example of this.