Home > Enterprise >  Viewing Pytorch weights from a *.pth file
Viewing Pytorch weights from a *.pth file

Time:04-06

I have a .pth file created with Pytorch with weights. How would I be able to view the weights from this file?

I tried this code to load and view but it was not working (as a newbie, I might be entirely wrong)-

import torch
import torchvision.models as models

torch.save('weights\kharif_crops_final.pth')

models.load_state_dict(torch.load('weights\kharif_crops_final.pth'))
models.eval()
print(models)

CodePudding user response:

import torch

model = torch.load('path')
print(model)

(Verify and confirm)

CodePudding user response:

PATH = 'weights\kharif_crops_final.pth'
state = {'model': model.state_dict()}
torch.save(state, PATH)
model.load_state_dict(torch.load(PATH)['model'])
# print weights
for k, v in model.named_parameters():
    print(k, v)
  • Related