微信公众号:OpenCV学堂
关注获取更多计算机视觉与深度学习知识
前言
Faster-RCNN转ONNX
model, // 模型文件
args, // 输入图像
f, // 保存模型文件
export_params=True, // 导出全部参数
verbose=False, // 默认False
training=TrainingMode.EVAL, // 推理模型
input_names=None, // 输入节点名称
output_names=None, // 输出节点名称
opset_version=None // op版本
do_constant_folding=True //
dynamic_axes=None // 是否支持动态大小输入
把Faster-RCNN转行为ONNX模型的脚本如下:
model = tv.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
dummy_input = torch.randn(1, 3, 1333, 800)
model.eval()
model(dummy_input)
im = torch.zeros(1, 3, 1333, 800).to("cpu")
torch.onnx.export(model, im,
"faster_rcnn.onnx",
verbose=False,
opset_version=11,
training=torch.onnx.TrainingMode.EVAL,
do_constant_folding=True,
input_names=['input'],
output_names=['output'],
dynamic_axes={'input': {0: 'batch', 2: 'height', 3: 'width'}}
)
ONNXRUNTIME部署运行
transform = torchvision.transforms.Compose([torchvision.transforms.ToTensor()])
sess_options = ort.SessionOptions()
# Below is for optimizing performance
sess_options.intra_op_num_threads = 24
# sess_options.execution_mode = ort.ExecutionMode.ORT_PARALLEL
sess_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
ort_session = ort.InferenceSession("faster_rcnn.onnx", sess_options=sess_options)
src = cv.imread("D:/images/cars.jpg")
image = cv.cvtColor(src, cv.COLOR_BGR2RGB)
blob = transform(image)
c, h, w = blob.shape
input_x = blob.view(1, c, h, w)
def to_numpy(tensor):
return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()
# compute ONNX Runtime output prediction
ort_inputs = {ort_session.get_inputs()[0].name: to_numpy(input_x)}
ort_outs = ort_session.run(None, ort_inputs)
boxes = ort_outs[0] # boxes
labels = ort_outs[1] # labels
scores = ort_outs[2] # scores
print(boxes.shape, boxes.dtype, labels.shape, labels.dtype, scores.shape, scores.dtype)
index = 0
for x1, y1, x2, y2 in boxes:
if scores[index] > 0.5:
cv.rectangle(src, (np.int32(x1), np.int32(y1)),
(np.int32(x2), np.int32(y2)), (0, 255, 255), 1, 8, 0)
label_id = labels[index]
label_txt = coco_names[str(label_id)]
cv.putText(src, label_txt, (np.int32(x1), np.int32(y1)), cv.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 255), 1)
index += 1
cv.imshow("Faster-RCNN Detection Demo", src)
cv.waitKey(0)
cv.destroyAllWindows()
自定义对象检测Faster-RCNN模型转换为ONNX部署
扫码下面的路线图,认真学习,可以get以上全部技能!从Faster-RCNN训练到模型部署,即可获取!
扫码查看OpenCV+Pytorch系统化学习路线图
推荐阅读
CV全栈开发者说 - 从传统算法到深度学习怎么修炼
2022入坑深度学习,我选择Pytorch框架!
Pytorch轻松实现经典视觉任务
教程推荐 | Pytorch框架CV开发-从入门到实战
OpenCV4 C++学习 必备基础语法知识三
OpenCV4 C++学习 必备基础语法知识二
OpenCV4.5.4 人脸检测+五点landmark新功能测试
OpenCV4.5.4人脸识别详解与代码演示
OpenCV二值图象分析之Blob分析找圆
OpenCV4.5.x DNN + YOLOv5 C++推理
OpenCV4.5.4 直接支持YOLOv5 6.1版本模型推理