下面是源代码,我用来加载.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)
我.pth
使用torch.onnx.export
.
现在,如何单独使用 ONNX 文件而不使用该.pth
文件来编写类似于上述的预测脚本。有可能这样做吗?