Load model from checkpoint.pth.tar

torch.save({
‘epoch’: epoch,
‘model’: net,
‘model_state_dict’: net.state_dict(),
‘best_mean_iu’: meanIU_best,
}, os.path.join(model_path, ‘checkpoint.pth.tar’))
i save model like this.

checkpoint = torch.load(‘checkpoint.pth.tar’)
net = torch.load(checkpoint[‘model’])

but i try to load model from checkpoint, it would appear error like this:

Traceback (most recent call last):
File “/home/liuyf/DenseNet_clockwork/CamVid_DenseNet/camvid_train.py”, line 35, in
net = torch.load(checkpoint[‘model’])
File “/usr/local/lib/python2.7/dist-packages/torch/serialization.py”, line 231, in load
return _load(f, map_location, pickle_module)
File “/usr/local/lib/python2.7/dist-packages/torch/serialization.py”, line 364, in _load
return legacy_load(f)
File “/usr/local/lib/python2.7/dist-packages/torch/serialization.py”, line 299, in legacy_load
with closing(tarfile.open(fileobj=f, mode=‘r:’, format=tarfile.PAX_FORMAT)) as tar,
File “/usr/lib/python2.7/tarfile.py”, line 1691, in open
return func(name, filemode, fileobj, **kwargs)
File “/usr/lib/python2.7/tarfile.py”, line 1721, in taropen
return cls(name, mode, fileobj, **kwargs)
File “/usr/lib/python2.7/tarfile.py”, line 1579, in init
self.offset = self.fileobj.tell()
File “/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py”, line 262, in getattr
type(self).name, name))
AttributeError: ‘DataParallel’ object has no attribute ‘tell’

3 Likes

Can anyone provide some help for me?

Corrected line:

net = checkpoint['model']
1 Like

Thank you for your answer