在CentOS上集成PyTorch可以通过多种方法实现,以下是一些常见的方法:
更新系统:
sudo yum update -y
安装依赖项:
sudo yum install -y python3 python3-pip python3-devel
安装PyTorch:
pip3 install torch torchvision torchaudio
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu120
验证安装:
import torch
print(torch.__version__)
print(torch.cuda.is_available())
安装Anaconda或Miniconda:
创建虚拟环境:
conda create -n pytorch_env python3.9
conda activate pytorch_env
安装PyTorch:
conda install pytorch torchvision torchaudio cpuonly -c pytorch
conda install pytorch torchvision torchaudio cudatoolkit 11.3 -c pytorch
conda install pytorch torchvision torchaudio cudatoolkit 11.6 -c pytorch
conda install pytorch torchvision torchaudio cudatoolkit 11.7 -c pytorch
conda install pytorch torchvision torchaudio cudatoolkit 12.0 -c pytorch
验证安装:
import torch
print(torch.__version__)
print(torch.cuda.is_available())
创建Dockerfile:
FROM pytorch/pytorch:latest
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
构建Docker镜像:
docker build -t pytorch-resnet18 .
运行Docker容器:
docker run -p 5000:5000 pytorch-resnet18
TorchScript:
import torch
import torchvision.models as models
model = models.resnet18()
example = torch.rand(1, 3, 224, 224)
traced_script_module = torch.jit.trace(model, example)
traced_script_module.save("traced_model.pt")
import torch
class MyModule(torch.nn.Module):
def __init__(self, n, m):
super(MyModule, self).__init__()
self.weight = torch.nn.Parameter(torch.rand(n, m))
def forward(self, input):
if input.sum() > 0:
output = self.weight.mv(input)
else:
output = self.weight @ input
return output
my_module = MyModule(10, 20)
scripted_module = torch.jit.script(my_module)
scripted_module.save("scripted_model.pt")
ONNX:
转换为ONNX:
import torch
import torchvision.models as models
from torch.utils.data import DataLoader, TensorDataset
from PIL import Image
class SimpleCNN(nn.Module):
def __init__(self):
super(SimpleCNN, self).__init__()
self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1)
self.relu = nn.ReLU()
self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc = nn.Linear(16 * 16 * 16, 10)
def forward(self, x):
x = self.conv1(x)
x = self.relu(x)
x = self.maxpool(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
model = SimpleCNN()
model.load_state_dict(torch.load('model.pth'))
model.eval()
def preprocess_image(image_path):
transform = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
image = Image.open(image_path)
image_tensor = transform(image).unsqueeze(0)
return image_tensor
# 推理流程
image_path = 'test_image.jpg'
input_tensor = preprocess_image(image_path)
with torch.no_grad():
output = model(input_tensor)
_, predicted = torch.max(output.data, 1)
print(f"Predicted class: {predicted.item()}")
# 转换为ONNX
dummy_input = input_tensor.clone().detach()
torch.onnx.export(model, dummy_input, "simple_cnn.onnx", verbose=True)
使用ONNX Runtime进行推理:
import onnx
import onnxruntime as ort
# 加载ONNX模型
onnx_model = onnx.load("simple_cnn.onnx")
onnx.checker.check_model(onnx_model)
# 创建推理会话
ort_session = ort.InferenceSession("simple_cnn.onnx")
# 推理
input_name = ort_session.get_inputs()[0].name
output_name = ort_session.get_outputs()[0].name
ort_inputs = {input_name: input_tensor.numpy()}
ort_outs = ort_session.run([output_name], ort_inputs)
print(f"Predicted class: {ort_outs[0][0]}")
[1,2,3,4,5,6,7,8,9,10,11]