[Model deployment] Load and run the Pytorch model using the opencv Python API

OpenCV can call PyTorch models. By converting the PyTorch model to ONNX format, and then using the dnn module in OpenCV to load and run the model.

Here is a sample code to load and run a PyTorch model using OpenCV:

import cv2
import numpy as np
import torch
import torchvision.transforms as transforms

# 加载模型
model = torch.load('model.pth')
model.eval()

# 转换模型为ONNX格式
dummy_input = torch.randn(1, 3, 224, 224)
input_names = ['input']
output_names = ['output']
torch.onnx.export(model, dummy_input, 'model.onnx', verbose=True, input_names=input_names, output_names=output_names)

# 加载ONNX模型
net = cv2.dnn.readNetFromONNX('model.onnx')

# 加载图像
image = cv2.imread('image.jpg')

# 图像预处理
transform = transforms.Compose([
    transforms.ToPILImage(),
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
image = transform(image).unsqueeze(0)

# 进行推理
blob = cv2.dnn.blobFromTensor(image.numpy(), size=(224, 224))
net.setInput(blob)
output = net.forward()

# 处理输出结果
output = torch.from_numpy(output)
_, preds = torch.max(output, 1)

# 显示结果
print(preds.item())

illustrate:

  1. First, load the PyTorch model using the torch.load function.
  2. To convert the model to ONNX format, use the torch.onnx.export function to convert the model to ONNX format and specify the names of the input and output.
  3. Use the cv2.dnn.readNetFromONNX function to load the ONNX model.
  4. Load the image, use the cv2.imread function to load the image.
  5. Preprocess the image and use the transforms.Compose function in the torchvision.transforms module to convert the image into the format required by the model.
  6. For inference, use the cv2.dnn.blobFromTensor function to convert the image into the format required by the model, and input it into the model for inference.
  7. Process the output results, use the torch.from_numpy function to convert the output of the model to PyTorch Tensor, and use the torch.max function to obtain the final prediction result.
  8. Display the results, and use the print function to output the prediction results.

Note: When using OpenCV to load and run the PyTorch model, the model needs to be converted to ONNX format. In addition, since the data types and color channel order used by PyTorch and OpenCV are different, conversion of data format and color channel is required.

Guess you like

Origin blog.csdn.net/qq_43456016/article/details/129410335