重磅干货,第一时间送达
讯享网
-
训练一个深度学习模型; -
使用不同的推理框架对模型进行推理转换; -
在应用平台运行转换好的模型。
简介:
使用场景:

讯享网
使用方法:
讯享网import onnx
from onnx import helper, shape_inference
from onnx import TensorProto
# 预处理:创建一个包含两个节点的模型,Y是未知的
node1 = helper.make_node(“Transpose”, [“X”], [“Y”], perm=[1, 0, 2])
node2 = helper.make_node(“Trans
pose”, [“Y”], [“Z”], perm=[1, 0, 2])
graph = helper.make_graph(
[node1, node2],
“two-transposes”,
[helper.make_tensor_value_info(“X”, TensorProto.FLOAT, (2, 3, 4))],
[helper.make_tensor_value_info(“Z”, TensorProto.FLOAT, (2, 3, 4))],
)
original_model = helper.make_model(graph, producer_name=“onnx-examples”)
# 检查模型并打印Y的信息
onnx.checker.check_model(original_model)
print(f”Before shape inference, the shape info of Y is:\n{original_model.graph.value_info}“)
# 在模型上进行推理
inferred_model = shape_inference.infer_shapes(original_model)
# 检查模型并打印Y的信息
onnx.checker.check_model(inferred_model)
print(f”After shape inference, the shape info of Y is:\n{inferred_model.graph.value_info}“)
简介:
使用场景:
框架特点:
-
支持卷积神经网络,支持多输入和多分支结构,可计算部分分支 -
无任何第三方库依赖,不依赖 BLAS/NNPACK 等计算框架 -
纯 C++ 实现,跨平台,支持 Android / iOS 等 -
ARM Neon 汇编级良心优化,计算速度极快 -
精细的内存管理和数据结构设计,内存占用极低 -
支持多核并行计算加速,ARM big.LITTLE CPU 调度优化 -
支持基于全新低消耗的 Vulkan API GPU 加速 -
可扩展的模型设计,支持 8bit 量化和半精度浮点存储,可导入 caffe/pytorch/mxnet/onnx/darknet/keras/tensorflow(mlir) 模型 -
支持直接内存零拷贝引用加载网络模型 -
可注册自定义层实现并扩展
使用方法:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include “net.h”
int main()
{
// opencv读取输入图片
cv::Mat img = cv::imread(“image.ppm”, CV_LOAD_IMAGE_GRAYSCALE);
int w = img.cols;
int h = img.rows;
// 减均值以及缩放操作,最后输入数据的值域为[-1,1]
ncnn::Mat in = ncnn::Mat::from_pixels_resize(img.data, ncnn::Mat::PIXEL_GRAY, w, h, 60, 60);
float mean[1] = { 128.f };
float norm[1] = { 1/128.f };
in.substract_mean_normalize(mean, norm);
// 构建NCNN的net,并加载转换好的模型
ncnn::Net net;
net.load_param(“model.param”);
net.load_model(“model.bin”);
// 创建网络提取器,设置网络输入,线程数,light模式等等
ncnn::Extractor ex = net.create_extractor();
ex.set_light_mode(true);
ex.set_num_threads(4);
ex.input(“data”, in);
// 调用extract接口,完成网络推理,获得输出结果
ncnn::Mat feat;
ex.extract(“output”, feat);
return 0;
简介:
使用场景:
框架特点:
-
在边缘启用基于CNN的深度学习推理 -
支持通过英特尔®Movidius™VPU在英特尔®CPU,英特尔®集成显卡,英特尔®神经计算棒2和英特尔®视觉加速器设计之间进行异构执行 -
通过易于使用的计算机视觉功能库和预先优化的内核加快上市时间 -
包括对计算机视觉标准(包括OpenCV *和OpenCL™)的优化调用
使用方法:
讯享网#include <openvino/openvino.hpp>
// 1.创建 OpenVINO™ 核心以管理可用设备和读取模型对象
ov::Core core;
// 2.为特定设备编译模型
ov::CompiledModel compiled_model = core.compile_model(“model.onnx”, “AUTO”);
// 3.创建推理请求
ov::InferRequest infer_request = compiled_model.create_infer_request();
// 4.设置输入
// 获取模型的输入端口
auto input_port = compiled_model.input();
// 从外部存储器创建张量
ov::Tensor input_tensor(input_port.get_element_type(), input_port.get_shape(), memory_ptr);
// 为模型设置一个输入张量
infer_request.set_input_tensor(input_tensor);
// 5.开始推理
infer_request.start_async();
infer_request.wait();
// 6.处理推理结果
// 通过tensor_name获取输出张量
auto output = infer_request.get_tensor(“tensor_name”);
const float *output_buffer = output.data<const float>();
// output_buffer[] - 访问输出张量数据
// 7.释放分配的对象(仅适用于C)
ov_shape_free(&input_shape);
ov_tensor_free(output_tensor);
ov_output_const_port_free(input_port);
ov_tensor_free(tensor);
ov_infer_request_free(infer_request);
ov_compiled_model_free(compiled_model);
ov_model_free(model);
ov_core_free(core);
// 为项目创建结构
project/
├── CMakeLists.txt - CMake file to build
├── … - Additional folders like includes/
└── src/ - source folder
└── main.cpp
build/ - build directory
…
讯享网// 创建 Cmake 脚本
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 11)
find_package(OpenVINO REQUIRED)
add_executable(\({TARGET_NAME} src/main.cpp)<br /><br />target_link_libraries(\){TARGET_NAME} PRIVATE openvino::runtime)
// 构建项目
cd build/
cmake /project
cmake –build .
简介:
使用场景:
框架特点:
使用方法:
-
使用 TF-TRT -
从文件自动转换 ONNX -
使用 TensorRT API 手动构建网络(C++或python)
-
在 TensorFlow 中部署 -
使用独立的 TensorRT 运行时 API -
使用 NVIDIA Triton 推理服务器
简介:
使用场景:
框架特点:
-
端到端加速:内置快速 ML 推理和处理,即使在普通硬件上也能加速 -
一次构建,随处部署:统一解决方案适用于安卓、iOS、桌面/云、Web 和物联网 -
即用型解决方案:展示框架全部功能的尖端 ML 解决方案 -
免费和开源:Apache 2.0下的框架和解决方案,完全可扩展和可定制
使用方法:
讯享网import cv2
import mediapipe as mp
mp_face_detection = mp.solutions.face_detection
mp_drawing = mp.solutions.drawing_utils
# 对于静态图像:
IMAGE_FILES = []
with mp_face_detection.FaceDetection(
model_selection=1, min_detection_confidence=0.5) as face_detection:
for idx, file in enumerate(IMAGE_FILES):
image = cv2.imread(file)
# 将BGR图像转换为RGB并使用MediaPipe人脸检测对其进行处理.
results = face_detection.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# 绘制每张人脸的人脸检测.
if not results.detections:
continue
annotated_image = image.copy()
for detection in results.detections:
print(‘Nose tip:’)
print(mp_face_detection.get_key_point(
detection, mp_face_detection.FaceKeyPoint.NOSE_TIP))
mp_drawing.draw_detection(annotated_image, detection)
cv2.imwrite(’/tmp/annotated_image’ + str(idx) + ’.png’, annotated_image)
# 用于网络摄像头输入:
cap = cv2.VideoCapture(0)
with mp_face_detection.FaceDetection(
model_selection=0, min_detection_confidence=0.5) as face_detection:
while cap.isOpened():
success, image = cap.read()
if not success:
print(“Ignoring empty camera frame.”)
# 如果加载视频,请使用“中断”而不是“继续”.
continue
# 若要提高性能,可以选择将图像标记为不可写以通过引用传递.
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = face_detection.process(image)
# 在图像上绘制人脸检测注释.
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.detections:
for detection in results.detections:
mp_drawing.draw_detection(image, detection)
# 水平翻转图像以获得自拍视图显示.
cv2.imshow(‘MediaPipe Face Detection’, cv2.flip(image, 1))
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
|
|
|
|
|
|
|
|
|
|
|
|
-
腾讯公司开发的移动端平台部署工具——NCNN; -
Intel公司针对自家设备开开发的部署工具——OpenVINO; -
NVIDIA公司针对自家GPU开发的部署工具——TensorRT; -
Google针对自家硬件设备和深度学习框架开发的部署工具——Mediapipe; -
由微软、亚马逊 、Facebook 和 IBM 等公司共同开发的开放神经网络交换格式——ONNX;
-
ONNXRuntime 是可以运行在多平台 (Windows,Linux,Mac,Android,iOS) 上的一款推理框架,它接受 ONNX 格式的模型输入,支持 GPU 和 CPU 的推理。唯一不足就是 ONNX 节点粒度较细,推理速度有时候比其他推理框架如 TensorRT 较低。 -
NCNN是针对手机端的部署。优势是开源较早,有非常稳定的社区,开源影响力也较高。 -
OpenVINO 是 Intel 家出的针对 Intel 出品的 CPU 和 GPU 友好的一款推理框架,同时它也是对接不同训练框架如 TensorFlow,Pytorch,Caffe 等。不足之处可能是只支持 Intel 家的硬件产品。 -
TensorRT 针对 NVIDIA 系列显卡具有其他框架都不具备的优势,如果运行在 NVIDIA 显卡上, TensorRT 一般是所有框架中推理最快的。一般的主流的训练框架如TensorFlow 和 Pytorch 都能转换成 TensorRT 可运行的模型。当然了,TensorRT 的限制就是只能运行在 NVIDIA 显卡上,同时不开源 kernel。 -
MediaPipe 不支持除了tensorflow之外的其他深度学习框架。MediaPipe 的主要用例是使用推理模型和其他可重用组件对应用机器学习管道进行快速原型设计。MediaPipe 还有助于将机器学习技术部署到各种不同硬件平台上的演示和应用程序中,为移动、桌面/云、web和物联网设备构建世界级ML解决方案和应用程序。
-
https://learn.microsoft.com/zh-cn/windows/ai/ -
https://github.com/Tencent/ncnn -
https://zhuanlan.zhihu.com/p/ -
https://github.com/google/mediapipe -
https://www.zhihu.com/question//answer/
ABOUT 关于我们 深蓝学院是专注于人工智能的在线教育平台,已有数万名伙伴在深蓝学院平台学习,很多都来自于国内外知名院校,比如清华、北大等。


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/177100.html