Skip to content

OpenVINO 支持

import { Card, CardGrid, Tabs, TabItem } from ‘@astrojs/starlight/components’;

OpenVINO™(Open Visual Inference and Neural Network Optimization)是英特尔推出的一款开源工具包,用于优化和加速 AI 推理。在迷你主机上使用 OpenVINO 可以充分发挥英特尔处理器的 AI 加速能力。

  • 多硬件支持:CPU、GPU、NPU、VPU 等多种硬件加速
  • 模型优化:自动量化和剪枝,减小模型体积
  • 跨平台:Windows、Linux、macOS 全支持
  • 广泛兼容:支持 TensorFlow、PyTorch、ONNX 等主流框架
  • 开源免费:完全开源,商业友好
处理器型号NPU 支持GPU 支持推荐场景
N95/N100轻量推理(CPU)
N200/N305中等负载(CPU)
Core i3-1215U✅ Iris Xe视觉推理
Core i5-1235U✅ Iris Xe图像/视频
Core Ultra 5/7✅ AI Boost✅ Arc重度 AI 任务
处理器型号AI 加速推荐工具
Ryzen 7020ROCm / ONNX
Ryzen 7040✅ XDNAONNX Runtime
  • 操作系统

    • Windows 10/11(推荐 11 23H2+)
    • Ubuntu 20.04/22.04/24.04 LTS
    • macOS 12+(仅 CPU)
  • Python:3.8 - 3.11

  • 存储空间:至少 2GB

Terminal window
# 创建虚拟环境
python -m venv openvino_env
openvino_env\Scripts\activate
# 安装 OpenVINO
pip install openvino-dev
# 验证安装
python -c "import openvino as ov; print(ov.__version__)"
  1. 访问 OpenVINO 官网
  2. 下载 Windows 安装程序
  3. 运行安装程序,按提示完成安装
  4. 配置环境变量
Terminal window
# 更新系统
sudo apt update && sudo apt upgrade -y
# 安装依赖
sudo apt install -y python3-venv build-essential cmake
# 创建虚拟环境
python3 -m venv openvino_env
source openvino_env/bin/activate
# 安装 OpenVINO
pip install openvino-dev
# 验证安装
python -c "import openvino as ov; print(ov.__version__)"
Terminal window
# 创建虚拟环境
python3 -m venv openvino_env
source openvino_env/bin/activate
# 安装 OpenVINO(仅 CPU)
pip install openvino
# 验证安装
python -c "import openvino as ov; print(ov.__version__)"
import openvino as ov
core = ov.Core()
# 列出所有可用设备
print("可用设备:")
for device in core.available_devices:
print(f" - {device}")
# 获取设备详细信息
for device in core.available_devices:
full_name = core.get_property(device, "FULL_DEVICE_NAME")
print(f"\n{device}: {full_name}")
import openvino as ov
core = ov.Core()
# CPU 优化
core.set_property("CPU", {
"NUM_STREAMS": "4", # 并发流数
"INFERENCE_NUM_THREADS": "8", # 线程数
"ENABLE_CPU_PINNING": "YES" # CPU 绑定
})
# GPU 优化(如果有独立 GPU)
core.set_property("GPU", {
"NUM_STREAMS": "2",
"GPU_CACHE_DIR": "./cache" # 模型缓存目录
})
# NPU 优化(Core Ultra 系列)
core.set_property("NPU", {
"NUM_STREAMS": "1",
"NPU_COMPILER_MODE": "BASIC" # 编译模式
})
Terminal window
# 使用模型优化器
mo --input_model model.pb \
--output_dir converted_model \
--input_shape [1,224,224,3]
import torch
from openvino.tools import mo
# 导出 PyTorch 模型
model = torch.load('model.pth')
dummy_input = torch.randn(1, 3, 224, 224)
# 转换为 OpenVINO 格式
ov_model = mo.convert_model(
model,
example_input=dummy_input,
input=((1,3,224,224),)
)
# 保存模型
from openvino.runtime import serialize
serialize(ov_model, "model.xml")
from openvino.tools import mo
# 转换 ONNX 模型
ov_model = mo.convert_model(
"model.onnx",
compress_to_fp16=True # 压缩到 FP16
)
# 保存
from openvino.runtime import serialize
serialize(ov_model, "model.xml")

使用 OpenVINO 进行图像分类推理:

import openvino as ov
import numpy as np
from PIL import Image
# 加载模型
core = ov.Core()
model = core.read_model("model.xml")
compiled_model = core.compile_model(model, "CPU") # 或 "NPU"
# 创建推理请求
infer_request = compiled_model.create_infer_request()
# 预处理图像
image = Image.open("test.jpg")
image = image.resize((224, 224))
input_data = np.array(image).transpose(2, 0, 1)[np.newaxis, ...]
input_data = input_data.astype(np.float32) / 255.0
# 推理
output = infer_request.infer([input_data])
predicted_class = np.argmax(output[0])
print(f"预测类别: {predicted_class}")

使用 YOLO 模型进行目标检测:

import openvino as ov
import cv2
import numpy as np
# 加载 YOLOv8 OpenVINO 模型
core = ov.Core()
model = core.read_model("yolov8n.xml")
compiled_model = core.compile_model(model, "AUTO") # 自动选择最佳设备
# 读取图像
frame = cv2.imread("test.jpg")
[height, width, _] = frame.shape
# 预处理
input_blob = cv2.resize(frame, (640, 640))
input_blob = input_blob.transpose(2, 0, 1)
input_blob = input_blob.astype(np.float32) / 255.0
input_blob = input_blob[np.newaxis, ...]
# 推理
output = compiled_model([input_blob])[compiled_model.output(0)]
# 后处理和绘制
predictions = output[0]
for detection in predictions:
confidence = detection[4]
if confidence > 0.5:
x, y, w, h = detection[:4]
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imwrite("result.jpg", frame)

使用 OpenVINO 运行量化 LLM:

import openvino as ov
from transformers import AutoTokenizer
# 加载分词器
tokenizer = AutoTokenizer.from_pretrained("model_path")
# 配置 OpenVINO
core = ov.Core()
core.set_property("NPU", {"NPU_COMPILER_MODE": "BASIC"})
# 加载模型
model = core.read_model("llm_model.xml")
compiled_model = core.compile_model(model, "NPU")
# 生成文本
prompt = "为什么选择迷你主机作为 AI 服务器?"
inputs = tokenizer(prompt, return_tensors="np")
output = compiled_model(inputs)
response = tokenizer.decode(output[0], skip_special_tokens=True)
print(response)

实时视频流处理:

import openvino as ov
import cv2
# 加载模型
core = ov.Core()
model = core.read_model("model.xml")
compiled_model = core.compile_model(model, "GPU") # 使用 GPU 加速
# 打开摄像头
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
# 预处理
input_blob = cv2.resize(frame, (224, 224))
input_blob = input_blob.transpose(2, 0, 1)[np.newaxis, ...]
input_blob = input_blob.astype(np.float32) / 255.0
# 推理
result = compiled_model([input_blob])[0]
# 显示结果
cv2.imshow("OpenVINO Inference", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
  • 处理器:Intel Core i5-1235U(12核,Iris Xe 核显)
  • 内存:32GB DDR4
  • 系统:Ubuntu 22.04 LTS
模型设备推理时间吞吐量
ResNet-50CPU45ms22 FPS
ResNet-50GPU12ms83 FPS
YOLOv8nCPU80ms12 FPS
YOLOv8nGPU25ms40 FPS
LLaMA-7B (4-bit)CPU850ms1.2 tok/s
LLaMA-7B (4-bit)NPU120ms8.3 tok/s
优化项原始优化后提升
模型大小500MB125MB75% ↓
推理时间100ms25ms4x ↑
内存占用2GB800MB60% ↓
  • OpenVINO:英特尔平台优先,推理专用
  • ONNX Runtime:跨平台,兼容性好
  • TensorFlow Lite:移动端优先
  • TensorRT:NVIDIA GPU 专用

可以,但仅支持 CPU 推理。对于轻量级模型(如 MobileNet、TinyYOLO)仍能获得不错的性能。

import openvino as ov
core = ov.Core()
print("NPU" in core.available_devices)

如果返回 True,则 NPU 可用。

  • TensorFlow (PB, SavedModel)
  • PyTorch (PTH, PT)
  • ONNX
  • Keras (H5)
  • PaddlePaddle
  • Open Model Zoo:预优化的公开模型
  • Hugging Face:导出 OpenVINO 格式
  • ONNX Model Zoo:转换为 OpenVINO