Hi,
I’ve created MyNet class nn.Module with 5 category classifcation problem. This model uses InceptionV3 model as feature extractor (I freezed first 2 conv layers, fine tune the rest of layers and add last layer with 5 outputs). At end of training, I’ve got 80% of accuracy in the train set before snapshot the model. After the training, I savded the model by torch.save(model.state_dict(), open(fname, 'w'))
. When I do testing, I create the model by model=MyNet()
and load the weights by model.load_state_dict(torch.load(model_fname))
, however, the prediction accuracy is random ~25% even in train set. Might I am doing something wrong?
class MyNet(nn.Module):
def __init__(self, pretrained=False, model_fname=None):
super(MyNet, self).__init__()
if model_fname is None:
self.inception_v3 = models.inception_v3(pretrained)
else:
self.inception_v3 = models.inception_v3()
self.inception_v3.load_state_dict(torch.load(model_fname))
# we ignore the first 2 layers of inception net
ignored_layers = ['Conv2d_1a',
'Conv2d_2a',
'Conv2d_2b']
for name, param in self.inception_v3.named_parameters():
if any([name.startswith(ignored) for ignored in ignored_layers]):
param.requires_grad = False # freeze the first 2 layers
# === adding classification layers ====
# 5 classes classification problem
self.fc = nn.Linear(self.inception_v3.fc.in_features, 5)
def forward(self, x):
# 299 x 299 x 3
x = self.inception_v3.Conv2d_1a_3x3(x)
# 149 x 149 x 32
x = self.inception_v3.Conv2d_2a_3x3(x)
# 147 x 147 x 32
x = self.inception_v3.Conv2d_2b_3x3(x)
# 147 x 147 x 64
x = F.max_pool2d(x, kernel_size=3, stride=2)
# 73 x 73 x 64
x = self.inception_v3.Conv2d_3b_1x1(x)
# 73 x 73 x 80
x = self.inception_v3.Conv2d_4a_3x3(x)
# 71 x 71 x 192
x = F.max_pool2d(x, kernel_size=3, stride=2)
# 35 x 35 x 192
x = self.inception_v3.Mixed_5b(x)
# 35 x 35 x 256
x = self.inception_v3.Mixed_5c(x)
# 35 x 35 x 288
x = self.inception_v3.Mixed_5d(x)
# 35 x 35 x 288
x = self.inception_v3.Mixed_6a(x)
# 17 x 17 x 768
x = self.inception_v3.Mixed_6b(x)
# 17 x 17 x 768
x = self.inception_v3.Mixed_6c(x)
# 17 x 17 x 768
x = self.inception_v3.Mixed_6d(x)
# 17 x 17 x 768
x = self.inception_v3.Mixed_6e(x)
# 17 x 17 x 768
if self.training and self.inception_v3.aux_logits:
# TODO, change this layer with 5 outputs
aux = self.inception_v3.AuxLogits(x)
# 17 x 17 x 768
x = self.inception_v3.Mixed_7a(x)
# 8 x 8 x 1280
x = self.inception_v3.Mixed_7b(x)
# 8 x 8 x 2048
x = self.inception_v3.Mixed_7c(x)
# 8 x 8 x 2048
x = F.avg_pool2d(x, kernel_size=8)
# 1 x 1 x 2048
x = F.dropout(x, training=self.training)
# 1 x 1 x 2048
x = x.view(x.size(0), -1)
# 2048
y = self.fc(x)
return y