
在 Windows 上部署 PyTorch 模型的三种主流方式
-
-
- [方案一:使用 TorchScript (官方原生,适合 C++ 调用或高性能 Python 服务)](#方案一:使用 TorchScript (官方原生,适合 C++ 调用或高性能 Python 服务))
-
- [步骤 1: 导出模型为 TorchScript](#步骤 1: 导出模型为 TorchScript)
- [步骤 2: 在 Windows 上部署 (Python 端加载)](#步骤 2: 在 Windows 上部署 (Python 端加载))
- [步骤 3: (可选) C++ 部署 (完全脱离 Python)](#步骤 3: (可选) C++ 部署 (完全脱离 Python))
- [方案二:构建 Web API 服务 (最常用,适合后端服务)](#方案二:构建 Web API 服务 (最常用,适合后端服务))
-
- [步骤 1: 安装依赖](#步骤 1: 安装依赖)
- [步骤 2: 创建 `main.py`](#步骤 2: 创建
main.py) - [步骤 3: 运行服务](#步骤 3: 运行服务)
- [步骤 4: Windows 开机自启 (作为服务)](#步骤 4: Windows 开机自启 (作为服务))
- [方案三:打包为桌面 exe 程序 (适合交付给最终用户)](#方案三:打包为桌面 exe 程序 (适合交付给最终用户))
-
- [步骤 1: 安装 PyInstaller 和 GUI 库](#步骤 1: 安装 PyInstaller 和 GUI 库)
- [步骤 2: 编写带 GUI 的推理脚本 `app.py`](#步骤 2: 编写带 GUI 的推理脚本
app.py) - [步骤 3: 打包成 EXE](#步骤 3: 打包成 EXE)
- [Windows 部署特别注意事项](#Windows 部署特别注意事项)
- 总结推荐
-
摘要:本文介绍了在Windows系统上部署PyTorch模型的三种主流方法。方案一通过TorchScript实现高性能推理,支持Python和C++调用;方案二使用FastAPI构建Web API服务,适合后端调用;方案三通过PyInstaller打包为桌面exe程序,便于交付给终端用户。每种方案都包含详细步骤,涵盖模型导出、加载推理、服务部署和GUI集成等关键环节,可根据不同应用场景(高性能推理、Web服务或桌面应用)灵活选择。
在 Windows 上部署 PyTorch 模型主要有三种主流方式,取决于你的具体需求(是用于高性能推理 、Web 服务 API ,还是桌面应用程序)。
以下是三种最常用方案的详细步骤:
方案一:使用 TorchScript (官方原生,适合 C++ 调用或高性能 Python 服务)
适用场景 :需要脱离 Python 解释器依赖(C++ 部署),或者在 Python 中追求比原生 model.forward 更快的推理速度。
步骤 1: 导出模型为 TorchScript
在你的训练代码或单独的脚本中,将训练好的模型转换为脚本格式。
python
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")
步骤 2: 在 Windows 上部署 (Python 端加载)
创建一个独立的推理脚本 inference.py,它不依赖训练代码,只依赖导出的 .pt 文件。
python
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())
步骤 3: (可选) C++ 部署 (完全脱离 Python)
如果你需要极致的性能或集成到现有的 C++ Windows 软件中:
- 下载 LibTorch : 去 PyTorch 官网 选择 "LibTorch",操作系统选 "Windows",语言选 "C++",计算平台选 "CUDA" (如果有显卡) 或 "CPU"。
- 配置 Visual Studio :
- 新建 C++ 项目。
- 在属性页中配置
Include Directories和Library Directories指向解压后的 LibTorch 文件夹 (include,lib)。 - 链接
torch_cpu.lib或torch_cuda.lib等相关库。
- 编写 C++ 代码 : 使用
torch::jit::load("model.pt")加载并推理。
方案二:构建 Web API 服务 (最常用,适合后端服务)
适用场景:需要通过 HTTP 请求调用模型(如前端网页、移动端 App 调用),使用 FastAPI 或 Flask。
步骤 1: 安装依赖
打开 Windows PowerShell 或 CMD:
bash
pip install fastapi uvicorn[standard] pillow python-multipart
# 如果还没装 torch
pip install torch torchvision
步骤 2: 创建 main.py
python
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
步骤 3: 运行服务
在终端运行:
bash
uvicorn main:app --host 0.0.0.0 --port 8000
现在你可以访问 http://localhost:8000/docs 查看 Swagger UI 界面并上传测试图片。
步骤 4: Windows 开机自启 (作为服务)
为了让它在后台一直运行:
- 使用 NSSM (Non-Sucking Service Manager) 工具。
- 下载 nssm.exe。
- 命令行运行
nssm install PyTorchService。 - 在弹出的 GUI 中:
- Path : 填写你的 Python 路径 (例如
C:\Users\YourName\venv\Scripts\python.exe)。 - Arguments : 填写
-m uvicorn main:app --host 0.0.0.0 --port 8000。 - Startup directory: 填写你的代码所在文件夹。
- Path : 填写你的 Python 路径 (例如
- 点击 "Install service",然后在 Windows 服务管理器中启动该服务。
方案三:打包为桌面 exe 程序 (适合交付给最终用户)
适用场景:需要发给没有 Python 环境的普通用户使用,带图形界面 (GUI)。
步骤 1: 安装 PyInstaller 和 GUI 库
bash
pip install pyinstaller pyside6 # 或者 tkinter (内置)
步骤 2: 编写带 GUI 的推理脚本 app.py
python
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())
步骤 3: 打包成 EXE
由于 PyTorch 很大,打包需要特殊参数。
bash
pyinstaller --noconfirm --onefile --windowed --add-data "resnet18_windows.pt;." app.py
注意:Windows 下 --add-data 使用分号 ; 分隔,Linux/Mac 使用冒号 :。
重要提示:
- 首次打包可能非常大(几百 MB 甚至 1GB+),因为包含了整个 PyTorch 库。
- 如果遇到内存错误,尝试添加
--exclude-module排除不需要的库,或者使用 UPX 压缩(但有时会导致 PyTorch 崩溃,需测试)。 - 生成的
.exe文件在dist文件夹下,可以直接发给任何 Windows 电脑运行(无需安装 Python)。
Windows 部署特别注意事项
- 路径问题 :
- Windows 路径使用反斜杠
\,但在 Python 字符串中建议用正斜杠/或原始字符串r"C:\path"。 - 使用
os.path.join或pathlib来处理路径,保证兼容性。
- Windows 路径使用反斜杠
- CUDA 驱动 :
- 如果使用 GPU 部署,目标机器必须安装与 PyTorch 版本匹配的 NVIDIA 显卡驱动。
- 不需要在目标机器安装 CUDA Toolkit (cuDNN 等已包含在 PyTorch wheel 包或 LibTorch 中),只要显卡驱动够新即可。
- 防火墙 :
- 如果是 Web API 部署,Windows Defender 防火墙可能会拦截 8000 端口。首次运行时需允许通过防火墙。
- 性能优化 (Windows 特有) :
- 在推理前设置线程数:
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) 是最平衡、最稳健的选择。