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):
    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(
            [,, torch.tensor(solute_lens).to(device),
            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

Using PyTorch Inside a Django App:

I hope this will help you get started!!!