with the fc layers. I just want to test loading and saving the normal net for now.
To test the loaded net. I load the net and call the test method with the same test_data I tested the net before.
I tried a lot not but I can’t find any problem.
Thats the net class I use in the python file I train the model:
cfg = {
'VGG11': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'VGG13': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'VGG16': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],
'VGG19': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
}
class VGG(nn.Module):
def __init__(self, vgg_name):
super(VGG, self).__init__()
self.features = self._make_layers(cfg[vgg_name])
self.classifier = nn.Linear(512, 2)
def forward(self, x):
out = self.features(x)
out = out.view(out.size(0), -1)
out = self.classifier(out)
return out
def _make_layers(self, cfg):
layers = []
in_channels = 3
for x in cfg:
if x == 'M':
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
else:
layers += [nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
nn.BatchNorm2d(x),
nn.ReLU(inplace=True)]
in_channels = x
layers += [nn.AvgPool2d(kernel_size=1, stride=1)]
return nn.Sequential(*layers)
# Initialisieren des Netzes
net = VGG('VGG11')
And I save it like that:
# trainiertes Netzspeichern
torch.save(net.state_dict(),'VGGNet11.pt')
In the python file I want to test the trained net I load it like that:
cfg = {
'VGG11': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'VGG13': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'VGG16': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],
'VGG19': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
}
class VGG(nn.Module):
def __init__(self, vgg_name):
super(VGG, self).__init__()
self.features = self._make_layers(cfg[vgg_name])
self.classifier = nn.Linear(512, 2)
def forward(self, x):
out = self.features(x)
out = out.view(out.size(0), -1)
out = self.classifier(out)
return out
def _make_layers(self, cfg):
layers = []
in_channels = 3
for x in cfg:
if x == 'M':
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
else:
layers += [nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
nn.BatchNorm2d(x),
nn.ReLU(inplace=True)]
in_channels = x
layers += [nn.AvgPool2d(kernel_size=1, stride=1)]
return nn.Sequential(*layers)
# Initialisieren des Netzes
net = VGG('VGG11')
net.load_state_dict(torch.load('VGGNet11.pt'))
Both python files are located in the same folder.
I tested both nets(the loaded and the pre-loaded after training) on one images now and both nets compute a different output. Thats means the mistake has to happen while loading or saving. Im pretty confused now.
Thats the method I tested both nets with:
def wtf():
net.eval()
img = Image.open('file path....')
img_tensor = Variable(transform(img)).unsqueeze_(0)
outputs = net(img_tensor)
print(outputs)
I found out something new. I tried something in my training python file. I trained the net and called the method wtf. Then I saved the net and loaded it straight again and called the wtf method with the loaded net again. Now I get the same outputs from both nets. Now im totally confused but that means that the problem is not to save the net. The problem is to load the net in the new python file.
Thats the code I run after training:
def wtf():
net.eval()
img = Image.open('path...')
img_tensor = Variable(transform(img)).unsqueeze_(0)
outputs = net(img_tensor)
print('output net: {}'.format(outputs))
def wtf2():
loaded_net.eval()
img = Image.open('path...')
img_tensor = Variable(transform(img)).unsqueeze_(0)
outputs = loaded_net(img_tensor)
print('output loaded_net: {}'.format(outputs))
wtf()
torch.save(net.state_dict(),'VGGNet11.pt')
loaded_net = VGG('VGG11')
loaded_net.load_state_dict(torch.load('VGGNet11.pt'))
wtf2()
output net: Variable containing:
-1.3904 1.3422
[torch.FloatTensor of size 1x2]
output loaded_net: Variable containing:
-1.3904 1.3422
[torch.FloatTensor of size 1x2]