3人参与 • 2026-04-03 • Windows
摘要:本文介绍了在windows系统上部署pytorch模型的三种主流方法。方案一通过torchscript实现高性能推理,支持python和c++调用;方案二使用fastapi构建web api服务,适合后端调用;方案三通过pyinstaller打包为桌面exe程序,便于交付给终端用户。每种方案都包含详细步骤,涵盖模型导出、加载推理、服务部署和gui集成等关键环节,可根据不同应用场景(高性能推理、web服务或桌面应用)灵活选择。
在 windows 上部署 pytorch 模型主要有三种主流方式,取决于你的具体需求(是用于高性能推理、web 服务 api,还是桌面应用程序)。
以下是三种最常用方案的详细步骤:
适用场景:需要脱离 python 解释器依赖(c++ 部署),或者在 python 中追求比原生 model.forward 更快的推理速度。
在你的训练代码或单独的脚本中,将训练好的模型转换为脚本格式。
import torch
import torchvision.models as models
# 1. 加载训练好的模型 (确保处于评估模式)
model = models.resnet18(weights='imagenet1k_v1') # 示例模型
model.eval()
# 2. 创建示例输入 (用于追踪或脚本化)
# 假设输入是 batch_size=1, 3通道, 224x224的图片
example_input = torch.rand(1, 3, 224, 224)
# 3. 跟踪模式 (tracing) - 适合控制流简单的模型
traced_script_module = torch.jit.trace(model, example_input)
# 或者 脚本模式 (scripting) - 适合有复杂控制流(if/for)的模型
# traced_script_module = torch.jit.script(model)
# 4. 保存模型
traced_script_module.save("resnet18_windows.pt")
print("模型已导出为 resnet18_windows.pt")
创建一个独立的推理脚本 inference.py,它不依赖训练代码,只依赖导出的 .pt 文件。
import torch
import torchvision.transforms as transforms
from pil import image
# 1. 加载 torchscript 模型
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = torch.jit.load("resnet18_windows.pt")
model.to(device)
model.eval()
# 2. 预处理图片
transform = transforms.compose([
transforms.resize(256),
transforms.centercrop(224),
transforms.totensor(),
transforms.normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
# 3. 推理
img = image.open("test_image.jpg").convert("rgb")
input_tensor = transform(img).unsqueeze(0).to(device)
with torch.no_grad():
output = model(input_tensor)
print("预测结果:", output.argmax(dim=1).item())
如果你需要极致的性能或集成到现有的 c++ windows 软件中:
include directories 和 library directories 指向解压后的 libtorch 文件夹 (include, lib)。torch_cpu.lib 或 torch_cuda.lib 等相关库。torch::jit::load("model.pt") 加载并推理。适用场景:需要通过 http 请求调用模型(如前端网页、移动端 app 调用),使用 fastapi 或 flask。
打开 windows powershell 或 cmd:
pip install fastapi uvicorn[standard] pillow python-multipart # 如果还没装 torch pip install torch torchvision
from fastapi import fastapi, file, uploadfile
import torch
import torchvision.transforms as transforms
from pil import image
import io
app = fastapi()
# 全局加载模型 (避免每次请求都加载)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = torch.jit.load("resnet18_windows.pt") # 使用方案一中导出的模型
model.to(device)
model.eval()
transform = transforms.compose([
transforms.resize(256),
transforms.centercrop(224),
transforms.totensor(),
transforms.normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
@app.post("/predict/")
async def predict(file: uploadfile = file(...)):
# 读取图片
image_data = await file.read()
image = image.open(io.bytesio(image_data)).convert("rgb")
# 预处理
input_tensor = transform(image).unsqueeze(0).to(device)
# 推理
with torch.no_grad():
output = model(input_tensor)
prediction = output.argmax(dim=1).item()
return {"filename": file.filename, "class_id": prediction}
# 启动命令: uvicorn main:app --reload --host 0.0.0.0 --port 8000
在终端运行:
uvicorn main:app --host 0.0.0.0 --port 8000
现在你可以访问 http://localhost:8000/docs 查看 swagger ui 界面并上传测试图片。
为了让它在后台一直运行:
nssm install pytorchservice。c:\users\yourname\venv\scripts\python.exe)。-m uvicorn main:app --host 0.0.0.0 --port 8000。适用场景:需要发给没有 python 环境的普通用户使用,带图形界面 (gui)。
pip install pyinstaller pyside6 # 或者 tkinter (内置)
import sys
import torch
from pyside6.qtwidgets import qapplication, qmainwindow, qpushbutton, qlabel, qfiledialog, qvboxlayout, qwidget
from pyside6.qtgui import qpixmap
from pil import image
import torchvision.transforms as transforms
import io
# 加载模型 (全局)
model = torch.jit.load("resnet18_windows.pt")
model.eval()
transform = transforms.compose([...]) # 同上
class mainwindow(qmainwindow):
def __init__(self):
super().__init__()
self.setwindowtitle("pytorch windows 演示")
layout = qvboxlayout()
self.label = qlabel("请上传图片")
self.btn = qpushbutton("选择图片并预测")
self.btn.clicked.connect(self.predict)
layout.addwidget(self.label)
layout.addwidget(self.btn)
container = qwidget()
container.setlayout(layout)
self.setcentralwidget(container)
def predict(self):
file_path, _ = qfiledialog.getopenfilename(self, "选择图片", "", "images (*.png *.jpg)")
if file_path:
img = image.open(file_path).convert("rgb")
input_tensor = transform(img).unsqueeze(0)
with torch.no_grad():
out = model(input_tensor)
res = out.argmax(dim=1).item()
self.label.settext(f"预测类别 id: {res}")
if __name__ == "__main__":
app = qapplication(sys.argv)
window = mainwindow()
window.show()
sys.exit(app.exec())
由于 pytorch 很大,打包需要特殊参数。
pyinstaller --noconfirm --onefile --windowed --add-data "resnet18_windows.pt;." app.py
注意:windows 下 --add-data 使用分号 ; 分隔,linux/mac 使用冒号 :。
重要提示:
--exclude-module 排除不需要的库,或者使用 upx 压缩(但有时会导致 pytorch 崩溃,需测试)。.exe 文件在 dist 文件夹下,可以直接发给任何 windows 电脑运行(无需安装 python)。\,但在 python 字符串中建议用正斜杠 / 或原始字符串 r"c:\path"。os.path.join 或 pathlib 来处理路径,保证兼容性。torch.set_num_threads(1)。windows 上多线程有时反而因为上下文切换导致变慢,特别是在 cpu 推理时。torch.backends.cudnn.benchmark = true (仅限 nvidia gpu) 可以加速固定输入的推理。| 需求 | 推荐方案 | 难度 | 性能 |
|---|---|---|---|
| 内部微服务/api | 方案二 (fastapi + torchscript) | ⭐⭐ | ⭐⭐⭐⭐ |
| 集成到 c++ 软件 | 方案一 (libtorch c++) | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
| 给小白用户的工具 | 方案三 (pyinstaller exe) | ⭐⭐⭐ | ⭐⭐⭐ |
| 快速原型验证 | 直接运行 python 脚本 | ⭐ | ⭐⭐ |
对于大多数 windows 部署场景,方案二 (fastapi + torchscript) 是最平衡、最稳健的选择。
以上就是在windows上部署pytorch模型的三种主流方法的详细内容,更多关于windows部署pytorch模型的资料请关注代码网其它相关文章!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论