Hi, I trained the following model:
import torch.nn as nn
import torch
class LambdaBase(nn.Sequential):
def __init__(self, fn, *args):
super(LambdaBase, self).__init__(*args)
self.lambda_func = fn
def forward_prepare(self, input):
output = []
for module in self._modules.values():
output.append(module(input))
return output if output else input
class Lambda(LambdaBase):
def forward(self, input):
return self.lambda_func(self.forward_prepare(input))
model = nn.Sequential( # Sequential,
nn.Conv2d(3,64,(3, 3),(1, 1),(1, 1)),
nn.ReLU(),
nn.Conv2d(64,64,(3, 3),(1, 1),(1, 1)),
nn.ReLU(),
nn.Dropout(0.25),
nn.MaxPool2d((4, 4),(4, 4)),
nn.BatchNorm2d(64,0.001,0.9,True),
nn.Conv2d(64,128,(3, 3),(1, 1),(1, 1)),
nn.ReLU(),
nn.Conv2d(128,128,(3, 3),(1, 1),(1, 1)),
nn.ReLU(),
nn.Dropout(0.25),
nn.MaxPool2d((4, 4),(4, 4)),
nn.BatchNorm2d(128,0.001,0.9,True),
nn.Conv2d(128,256,(3, 3),(1, 1),(1, 1)),
nn.ReLU(),
nn.Conv2d(256,256,(3, 3),(1, 1),(1, 1)),
nn.ReLU(),
nn.Dropout(0.25),
nn.MaxPool2d((4, 4),(4, 4)),
nn.BatchNorm2d(256,0.001,0.9,True),
nn.Conv2d(256,128,(1, 1)),
nn.ReLU(),
Lambda(lambda x: x.view(x.size(0),-1)), # Reshape,
nn.Sequential(Lambda(lambda x: x.view(1,-1) if 1==len(x.size()) else x ),nn.Linear(3072,128)), # Linear,
#nn.Linear(3072,128)
)
Then I load the parameters and train the model like this:
model.load_state_dict(torch.load("pretrained_model.pth"))
for i in epochs:
if(i % 100 == 0):
model.train(False)
...validation process...
if(current_score > best_score):
torch.save(model.state_dict(), "best_model.pth")
model.train(True)
...training process...
But, when I reload the saved model best_model.pth
, it shows low performance as model before training, though it showed best performance when training.
Following is how performance changes:
- before training・・・70% accuracy
- after training・・・87% accuracy => OK, that’s best score and I save the model as
"best_model.pth"
- after loading
"best_model.pth"
(expected to have 87% accuracy)・・・70% accuracy
Do you know why this thing occurs?
Thanks.