Django.fun

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

I am doing a machine learning project where I need to display the predictions on a webpage. The webpage is build using Django. I have predictions function and the weights of the model but how to integrate the predictions function, model, and weights in the Django code and do predictions.

My prediction code

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()

I have saved the weights in .tar file so I need to run the model while loading the weights for prediction. I have no idea where to keep my PyTorch model and the weights to do inference using Django. please help.

Answers: 1

Answered by iou, Oct. 17, 2021, 7:36 p.m.

Deploy a PyTorch deep learning classifier at Heroku using Django in 30 minutes

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

Using PyTorch Inside a Django App:

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

I hope this will help you get started!!!