Pytorch模型迁移和迁移学习
1. 利用resnet18做迁移学习
import torch
from torchvision import models
if __name__ == "__main__":
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
device = 'cpu'
print("-----device:{}".format(device))
print("-----Pytorch version:{}".format(torch.__version__))
input_tensor = torch.zeros(1, 3, 100, 100)
print('input_tensor:', input_tensor.shape)
pretrained_file = "model/resnet18-5c106cde.pth"
model = models.resnet18()
model.load_state_dict(torch.load(pretrained_file))
model.eval()
out = model(input_tensor)
print("out:", out.shape, out[0, 0:10])imp