CUDA执行提供程序初始化失败,InsightFace仅使用CPU
我在一台装有RTX 4060 Laptop GPU的 Windows 11机器上尝试让InsightFace具备GPU加速运行。然而,InsightFace无法使用CUDA,总是回退到CPUExecutionProvider。
环境信息
- 操作系统:Windows 11
- 显卡:NVIDIA GeForce RTX 4060 Laptop GPU
- 驱动程序:610.47
- CUDA UMD版本:13.3
- Python版本:3.14.5
- InsightFace:最新版
- onnxruntime-gpu:1.25.1
- cuDNN:9.2.3
- 我用于该项目的代码在此处
``` import cv2 from insightface.app import FaceAnalysis
# Initialize InsightFace app = FaceAnalysis(name='buffalo_l') app.prepare(ctx_id=0) # Use -1 for CPU
# Open webcam cap = cv2.VideoCapture(0)
while True: ret, frame = cap.read() if not ret: break
# Detect faces
faces = app.get(frame)
# Draw results
for face in faces:
x1, y1, x2, y2 = map(int, face.bbox)
# Draw bounding box
cv2.rectangle(frame, (x1, y1), (x2, y2),
(0, 255, 0), 2)
# Display detection score
score = face.det_score
cv2.putText(
frame,
f"{score:.2f}",
(x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX,
0.6,
(0, 255, 0),
2
)
# Face embedding (512-dimensional vector)
embedding = face.embedding
print("Embedding shape:", embedding.shape)
# Show output
cv2.imshow("InsightFace Webcam", frame)
# Press q to quit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release() cv2.destroyAllWindows() ```
解决方案
ctx_id=0 在较新版本中并不总是足以强制使用GPU,因此你需要在初始化应用时显式定义提供程序。
app = FaceAnalysis(name='buffalo_l', providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
另外,请确保没有与 onnxruntime-gpu 一起安装的标准 onnxruntime 包。如果两者都存在,Python将默认使用CPU版本,完全忽略你的GPU。
pip uninstall onnxruntime onnxruntime-gpu -y
pip install onnxruntime-gpu==1.25.1
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。