使用 Django 使用 .tar 文件中的 pytorch 保存权重进行推理

Doing inference using pytorch saved weights in .tar file using django

我正在做一个机器学习项目,我需要在网页上显示预测。该网页是使用 Django 构建的。我有预测函数和模型的权重,但如何在 Django 代码中集成预测函数、模型和权重并进行预测。

我的预测码

def predicting(model, device, loader):
    model.eval()
    total_preds = torch.Tensor()
    total_labels = torch.Tensor()
    with torch.no_grad():
        for solute_graphs, solvent_graphs, solute_lens, solvent_lens in loader:
            outputs, i_map = model(
            [solute_graphs.to(device), solvent_graphs.to(device), torch.tensor(solute_lens).to(device),
             torch.tensor(solvent_lens).to(device)])   
            print(outputs)
            total_preds = torch.cat((total_preds, outputs.cpu()), 0)
    return total_preds.numpy().flatten()

我已将权重保存在 .tar 文件中,因此我需要 运行 模型,同时加载预测权重。我不知道在哪里保存我的 PyTorch 模型和权重以使用 Django 进行推理。请帮忙

在 30 分钟内使用 Django 在 Heroku 部署 PyTorch 深度学习分类器

https://www.youtube.com/watch?v=MLk2We1rJPs

在 Django 应用程序中使用 PyTorch:

https://stefanbschneider.github.io/blog/pytorch-django

希望这对您入门有帮助!!!