在Ubuntu上部署PyTorch模型可以分为几个主要步骤,包括环境搭建、模型转换和部署。以下是一个详细的指南:
安装Anaconda或Miniconda:
创建并激活虚拟环境:
conda create -n pytorch_env python=3.8
conda activate pytorch_env
安装PyTorch和CUDA:
conda install pytorch torchvision torchaudio cudatoolkit=12.1 -c pytorch -c nvidia
pip install torch torchvision torchaudio
验证安装:
import torch
print(torch.__version__)
print(torch.cuda.is_available())
使用TorchScript:
scripted_module = torch.jit.script(model)
scripted_module.save(model.pt)
使用ONNX:
torch.onnx.export(model, input, model.onnx)
使用TorchServe:
pip install torchserve
torchserve --start --model_name=model --model_path=/path/to/model.pt --runtime_version=1.8
使用Flask或Django:
from flask import Flask, request, jsonify
import torch
import torchvision.transforms as transforms
from PIL import Image
app = Flask(__name__)
# 加载模型
model = torch.load('path_to_your_model.pt', map_location=torch.device('cpu'))
model.eval()
# 定义图像预处理
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
@app.route('/predict', methods=['POST'])
def predict():
if request.method == 'POST':
file = request.files['image']
image = Image.open(file.stream)
image = transform(image).unsqueeze(0) # 添加batch维度
with torch.no_grad():
output = model(image)
_, predicted_idx = torch.max(output, 1)
return jsonify({'prediction': int(predicted_idx.item())})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
运行Flask应用:
python app.py
测试模型:
curl -X POST -F "image=@path_to_your_image.jpg" http://localhost:5000/predict
部署到生产环境:
pip install gunicorn
gunicorn -w 4 -b 0.0.0.0:5000 app:app
FROM python:3.8-slim
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "app:app"]
docker build -t your-model-app .
docker run -p 5000:5000 your-model-app
通过以上步骤,你可以在Ubuntu系统下成功部署PyTorch模型。根据你的具体需求,可以选择使用TorchServe、Flask或Django等进行部署。