pytorch加载模型:举例
resnet152 = torch.load(’/models/resnet152-b121ed2d.pth’)
报错:
TypeError: ‘collections.OrderedDict’ object is not callable
解决:
上面pth文件包含的是 state_dict 而不是模型本身,因此需要先实例化模型再加载参数。
resnet152 = models.resnet152(pretrained=False).eval().cuda()
resnet152 .load_state_dict(torch.load(’/models/resnet152-b121ed2d.pth’))
若改为resnet152 .load_state_dict(’/models/resnet152-b121ed2d.pth’)
会报错AttributeError: ‘str’ object has no attribute ‘copy’
参考:
https://stackoverflow.com/questions/48419626/pytorch-cant-load-cnn-model-and-do-prediction-typeerror-collections-orderedd
https://blog.csdn.net/weixin_42245329/article/details/105638836