如何加载和使用预训练的 PyTorch Inception V3 模型对图像进行分类
How to load and use a pretained PyTorch InceptionV3 model to classify an image
我遇到了与 How can I load and use a PyTorch (.pth.tar) model 相同的问题,但没有公认的答案,也没有我能弄清楚如何遵循给出的建议的答案。
我是 PyTorch 的新手。我正在尝试加载此处引用的预训练 PyTorch 模型:https://github.com/macaodha/inat_comp_2018
我很确定我漏掉了一些胶水。
# load the model
import torch
model=torch.load("iNat_2018_InceptionV3.pth.tar",map_location='cpu')
# try to get it to classify an image
imsize = 256
loader = transforms.Compose([transforms.Scale(imsize), transforms.ToTensor()])
def image_loader(image_name):
"""load image, returns cuda tensor"""
image = Image.open(image_name)
image = loader(image).float()
image = Variable(image, requires_grad=True)
image = image.unsqueeze(0)
return image.cpu() #assumes that you're using CPU
image = image_loader("test-image.jpg")
产生错误:
in ()
----> 1 model.predict(image)
AttributeError: 'dict' object has no attribute 'predict
问题
您的 model
实际上不是模特。保存的时候,除了参数,还有模型的其他信息,有点类似字典的形式。
因此,torch.load("iNat_2018_InceptionV3.pth.tar")
只是 returns dict
,当然没有名为 predict
.
的属性
model=torch.load("iNat_2018_InceptionV3.pth.tar",map_location='cpu')
type(model)
# dict
解决方案
在这种情况下,在一般情况下,您首先需要做的是根据官方指南 "Load models".
实例化您想要的模型 class
# First try
from torchvision.models import Inception3
v3 = Inception3()
v3.load_state_dict(model['state_dict']) # model that was imported in your code.
但是,直接输入 model['state_dict']
会引发一些关于 Inception3
参数形状不匹配的错误。
重要的是要知道 Inception3
在实例化后发生了什么变化。幸运的是,你可以在原作者的 train_inat.py
.
中找到
# What the author has done
model = inception_v3(pretrained=True)
model.fc = nn.Linear(2048, args.num_classes) #where args.num_classes = 8142
model.aux_logits = False
既然我们知道要更改什么,让我们先对进行一些修改。
# Second try
from torchvision.models import Inception3
v3 = Inception3()
v3.fc = nn.Linear(2048, 8142)
v3.aux_logits = False
v3.load_state_dict(model['state_dict']) # model that was imported in your code.
至此,模型加载成功!
我遇到了与 How can I load and use a PyTorch (.pth.tar) model 相同的问题,但没有公认的答案,也没有我能弄清楚如何遵循给出的建议的答案。
我是 PyTorch 的新手。我正在尝试加载此处引用的预训练 PyTorch 模型:https://github.com/macaodha/inat_comp_2018
我很确定我漏掉了一些胶水。
# load the model
import torch
model=torch.load("iNat_2018_InceptionV3.pth.tar",map_location='cpu')
# try to get it to classify an image
imsize = 256
loader = transforms.Compose([transforms.Scale(imsize), transforms.ToTensor()])
def image_loader(image_name):
"""load image, returns cuda tensor"""
image = Image.open(image_name)
image = loader(image).float()
image = Variable(image, requires_grad=True)
image = image.unsqueeze(0)
return image.cpu() #assumes that you're using CPU
image = image_loader("test-image.jpg")
产生错误:
in () ----> 1 model.predict(image)
AttributeError: 'dict' object has no attribute 'predict
问题
您的 model
实际上不是模特。保存的时候,除了参数,还有模型的其他信息,有点类似字典的形式。
因此,torch.load("iNat_2018_InceptionV3.pth.tar")
只是 returns dict
,当然没有名为 predict
.
model=torch.load("iNat_2018_InceptionV3.pth.tar",map_location='cpu')
type(model)
# dict
解决方案
在这种情况下,在一般情况下,您首先需要做的是根据官方指南 "Load models".
实例化您想要的模型 class# First try
from torchvision.models import Inception3
v3 = Inception3()
v3.load_state_dict(model['state_dict']) # model that was imported in your code.
但是,直接输入 model['state_dict']
会引发一些关于 Inception3
参数形状不匹配的错误。
重要的是要知道 Inception3
在实例化后发生了什么变化。幸运的是,你可以在原作者的 train_inat.py
.
# What the author has done
model = inception_v3(pretrained=True)
model.fc = nn.Linear(2048, args.num_classes) #where args.num_classes = 8142
model.aux_logits = False
既然我们知道要更改什么,让我们先对进行一些修改。
# Second try
from torchvision.models import Inception3
v3 = Inception3()
v3.fc = nn.Linear(2048, 8142)
v3.aux_logits = False
v3.load_state_dict(model['state_dict']) # model that was imported in your code.
至此,模型加载成功!