.caffemodel
)是Caffe框架特有的模型格式,它包含了网络的结构和训练后的参数。Caffe模型通常与Caffe框架紧密绑定,移植性相对较差。.onnx
)是由ONNX(Open Neural Network Exchange)联盟推出的开放格式,旨在实现不同深度学习框架之间的模型互操作性。ONNX模型可以被多种框架如PyTorch、TensorFlow等支持,具有更好的跨平台特性。Caffe模型应用代码(使用OpenCV进行人脸检测):
python# 假设已有Caffe模型和权重文件
net = cv2.dnn.readNetFromCaffe('deploy.prototxt', 'face_mask_detection.caffemodel')
blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0))
net.setInput(blob)
detections = net.forward()
# 处理detections结果...
ONNX模型应用代码(使用ONNX Runtime进行推理):
pythonimport onnxruntime as ort
import numpy as np
import cv2
# 加载ONNX模型
session = ort.InferenceSession("face_mask_detection.onnx")
# 读取图像并预处理
image = cv2.imread('input_image.jpg')
input_tensor = np.array(image, dtype=np.float32).transpose((2, 0, 1)).reshape((1,) + image.shape)
input_name = session.get_inputs()[0].name
# 进行推理
outputs = session.run(None, {input_name: input_tensor})
# 处理outputs结果...
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。