文章目录
前言
使用Libtorch在Windows环境下调用pytorch模型。
一、Libtorch下载
pytorch官网网址:https://pytorch.org/
下载完成之后,解压出来
二、配置Visual Studio环境
1.配置管理器
2.配置Release环境属性
将Libtorch中的lib文件夹里面的所有.lib文件都加到附加依赖项中。之后再将这些.lib文件拷贝到C:\Windows\System32文件夹中。
三、生成pt模型
# pth转pt
import os
import torch
from PIL import Image
from torchvision import transforms
from model import AlexNet
def main():
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print("using {} device.".format(device))
# create model
model = AlexNet(num_classes=5).to(device)
img_path = r'D:\项目\人脸识别\deep-learning-for-image-processing-master\data_set\flower_data\rose.jpg'
image = Image.open(img_path).convert('RGB')
data_transform = transforms.Compose(
[transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
img = data_transform(image)
img = img.unsqueeze(dim=0)
print(img.shape)
# load model weights
weights_path = "./AlexNet.pth"
assert os.path.exists(weights_path), "file: '{}' dose not exist.".format(weights_path)
testsize = 224
if torch.cuda.is_available():
modelState = torch.load