【发布时间】:2023-01-04 01:05:11
【问题描述】:
下面是源代码,我用它来加载.pth文件并进行多类图像分类预测。
model = Classifier() # The Model Class.
model.load_state_dict(torch.load('<PTH-FILE-HERE>.pth'))
model = model.to(device)
model.eval()
# prediction function to test images
def predict(img_path):
image = Image.open(img_path)
resize = transforms.Compose(
[ transforms.Resize((256,256)), transforms.ToTensor()])
image = resize(image)
image = image.to(device)
y_result = model(image.unsqueeze(0))
result_idx = y_result.argmax(dim=1)
print(result_idx)
我使用 torch.onnx.export 将 .pth 文件转换为 ONNX 文件。
现在,如何通过单独使用 ONNX 文件而不使用 .pth 文件来编写类似于上述脚本的预测脚本。?
有可能这样做吗?
【问题讨论】: