python版pytorch模型转openvino及调用

news2024/9/25 11:11:41

一、openvino安装

参看官方文档https://www.intel.com/content/www/us/en/developer/tools/openvino-toolkit/download.html
在这里插入图片描述
在这里插入图片描述
安装命令是根据上面的选择生成。这里安装了pytorch和onnx依赖。

二、pytorch模型转opnvino模型推理

import os
import time
import cv2
import numpy as np
import torch

from openvino.runtime import Core
from openvino.tools import mo


img_path = r'./000000002306.jpg'
model_path = 'pure_pose.pt'
## 加载onnx模型
model = torch.load(model_path)
model.eval()
## onnx模型转openvino
model_ir = mo.convert_model(
    model, input_shape=[1,3, 256, 192],
    mean_values = [123.675, 116.28 , 103.53],
    scale_values=[58.395, 57.12 , 57.375],
    compress_to_fp16=True
)

## 图片预处理
image = cv2.cvtColor(
    src=cv2.imread(filename=str(img_path)),
    code=cv2.COLOR_BGR2RGB,
)
resized_image,ratio, (dw, dh) = letterbox(image,new_shape=(256,192))

# # # Convert the image shape to a shape and a data type expected by the network
# # # for OpenVINO IR model: (1, 3, 512, 512).
input_image = np.expand_dims(np.transpose(resized_image, (2, 0, 1)), 0)

ie = Core()
compiled_model_ir = ie.compile_model(model=model_ir, device_name="CPU")
# Get the names of input and output layers.
input_layer_ir = compiled_model_ir.input(0)
output_layer_ir = compiled_model_ir.output(0)

# Do inference on the input image.
start_time = time.perf_counter()
result = compiled_model_ir([input_image])[output_layer_ir]
end_time = time.perf_counter()
print(
    f"Inference finished. Inference time: {end_time-start_time:.3f} seconds, "
    f"FPS: {1/(end_time-start_time):.2f}."
)

在pytorch转openvino模型的时候,已包含归一化操作过程。在推理的时候不需要对输入图片做归一化操作。

三、onnx模型转opnvino模型推理

1. onnx模型转openvino模型

在上面的安装文件夹openvino_env文件夹下找到mo_onnx.py文件。我的路径:openvino_env/lib/python3.9/site-packages/openvino/tools/mo/mo_onnx.py
根据下面的脚本将onnx模型转成openvino模型

python ../openvino_env/lib/python3.9/site-packages/openvino/tools/mo/mo_onnx.py \
  --input_model ./mobilenet_load.onnx  \
  --output_dir ./openvino_model \
  --input_shape "[1,3,256,192]" \
  --mean_values="[123.675, 116.28 , 103.53]" \
  --scale_values="[58.395, 57.12 , 57.375]" \
  --data_type FP16

生成下面三个文件
在这里插入图片描述

2. 调用openvino模型进行推理

import time
import cv2
import numpy as np
from openvino.runtime import Core
img_path = r'./000000002306.jpg'
from utils import letterbox

image = cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2RGB)

# Convert the resized images to network input shape
resized_image,ratio, (dw, dh) = letterbox(image,new_shape=(256,192))
h, w, c = resized_image.shape
input_image = np.expand_dims(np.transpose(resized_image, (2, 0, 1)), 0)

# Load the network in Inference Engine
core = Core()
model_ir = core.read_model(model="openvino_model/mobilenet_load.xml")
compiled_model_ir = core.compile_model(model=model_ir, device_name="CPU")

# Get output layer
output_layer_ir = compiled_model_ir.output(0)

# Run inference on the input image
# Do inference on the input image.
start_time = time.perf_counter()
res_ir = compiled_model_ir([input_image])[output_layer_ir]
end_time = time.perf_counter()
print(
    f"Inference finished. Inference time: {end_time-start_time:.3f} seconds, "
    f"FPS: {1/(end_time-start_time):.2f}."
)

比较通过pytorch和onnx转成的openvino模型的推理时间,差不多。openvino模型推理时间大概是pytorch模型推理时间的1/5.

模型推理时间(s)
openvino0.010
onnx0.015
pytorch0.048

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/439425.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

易基因:MeRIP-seq等揭示m6A甲基化修饰对抗病毒基因表达的转录调控机制|Cell Rep

大家好,这里是专注表观组学十余年,领跑多组学科研服务的易基因。 2021年03月02日,杜克大学医学中心的分子遗传学和微生物学系Stacy M. Horner教授团队在《Cell Reports》(IF: 9.995)杂志发表了题为“Post-transcripti…

LeetCode算法小抄--归并排序详解及应用

LeetCode算法小抄--归并排序详解及应用 归并排序详解及应用[912. 排序数组](https://leetcode.cn/problems/sort-an-array/)[315. 计算右侧小于当前元素的个数](https://leetcode.cn/problems/count-of-smaller-numbers-after-self/)[hard]--华为笔试[493. 翻转对](https://lee…

Java版本工程项目管理系统源码-全面的工程项目管理

​ ​工程项目管理系统是指从事工程项目管理的企业(以下简称工程项目管理企业)受业主委托,按照合同约定,代表业主对工程项目的组织实施进行全过程或若干阶段的管理和服务。 如今建筑行业竞争激烈,内卷严重&#xff0c…

供应链优化的好处是什么?

企业可以从优质的供应链优化软件中获得许多好处: 成本降低 通过供应链优化,可以消除许多不必要的成本,从而简化业务运营费用。所有重复或无效的流程也可以根据需要消除或自动化。重点可以转移到通过准确及时交付来满足客户的需求上。通过供…

OpUtils网络端口扫描

网络端口扫描程序使 IT 管理员能够密切监视端口,这些端口是网络通信的端点。网络端口支持不同实体之间的数据传输,并支持运行各种服务或应用程序。在日常工作中,网络管理员的任务是扫描和监控数百个交换机端口。网络端口扫描程序工具通过帮助…

Intellij IDEA-插件开发环境搭建

插件开发有两种方式 1.基于Gradle(新版IDEA,按钮名称叫做IDE Plugin,具体多新我也不清楚,反正2023的必须是这种) 2.基于Dev kit(旧版IDEA,按钮名称叫做IntelliJ Platform Plugin,具体…

【GitLab私有仓库】在Linux上用Gitlab搭建自己的私有库并配置cpolar内网穿透

前言 GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的Web服务。 Gitlab是被广泛使用的基于git的开源代码管理平台, 基于Ruby on Rails构建, 主要针对软件开发过程中产生的代码和文档进行管理, Gitlab主要针对…

基于Udacity模拟器的端到端自动驾驶决策

1 端到端自动驾驶决策 端到端自动驾驶决策的输入为车辆的感知信息,如摄像头信息,输出为车辆的前轮转角和摄像头等信息。 如上图所示,为英伟达公司的端到端自动驾驶决策框架,其CNN网络如下图所示,其中包括一个归一化…

狂神说Docker

思维导图: 文章目录 1.Docker概念与安装1.1 概述1.2 安装1.3 卸载1.4 Docker的执行镜像原理1.5 Docker为什么比Vm快1.6 Docker底层原理 2.Docker常用命令2.1 帮助命令2.2 镜像命令2.3 容器命令2.4 其他常用命令 3.小练习3.1 Docker下安装Nginx3.2 Docker安装Tomcat…

[网络安全提高篇] 一二〇.恶意软件动态分析经典沙箱Cape批量提取动态API特征

终于忙完初稿,开心地写一篇博客。 “网络安全提高班”新的100篇文章即将开启,包括Web渗透、内网渗透、靶场搭建、CVE复现、攻击溯源、实战及CTF总结,它将更加聚焦,更加深入,也是作者的慢慢成长史。换专业确实挺难的,Web渗透也是块硬骨头,但我也试试,看看自己未来四年究…

SpringBoot设计了哪些可拓展的机制?

SpringBoot核心源码 public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) { ...this.primarySources new LinkedHashSet(Arrays.asList(primarySources));// Servletthis.webApplicationType WebApplicationType.deduceFromClass…

sentinel配置文件

转载自 https://blog.csdn.net/u012441222/article/details/80751390 Redis的哨兵机制是官方推荐的一种高可用&#xff08;HA&#xff09;方案&#xff0c;我们在使用Redis的主从结构时&#xff0c;如果主节点挂掉&#xff0c;这时是不能自动进行主备切换和通知客户端主节点下…

涨点神器:基于Yolov8小目标遮挡物性能提升(SEAM、MultiSEAM)

1.遮挡物检测简介 不同的目标检测应用场景有不同的检测难点,小目标、多尺度以及背景复杂等问题,被遮挡的物体仍然是最先进的物体检测器面临的挑战。本文尝试解决待测目标相互遮挡带来的检测困难,对于人脸遮挡提出了一个名为 SEAM 的注意力模块并引入了排斥损失来解决它,引…

机器学习入门实例-加州房价预测-3(选择与训练模型+调参)

选择与训练模型 使用线性回归 from sklearn.linear_model import LinearRegressionfrom sklearn.metrics import mean_squared_errorlin_reg LinearRegression()lin_reg.fit(housing_prepared, housing_labels)housing_predictions lin_reg.predict(housing_prepared)lin_ms…

直播预告 | TDengine Apache SeaTunnel 联合应用最佳实践

TDengine 自诞生之日起&#xff0c;除产品层面的技术创新和实力提升外&#xff0c;也在大力完善自身产品生态&#xff0c;以此进一步满足用户的业务需求、提升使用体验。 近日&#xff0c;TDengine 与 Apache SeaTunnel 展开集成合作&#xff0c;双方将于 4 月 18 日 19:00 联…

二、Java 并发编程(5)

本章概要 线程上下文切换 线程上下文切换的流程导致线程上下文切换的原因 Java中的阻塞队列 阻塞队列的主要操作Java中阻塞队列的实现 2.7 线程上下文切换 CPU 利用时间片轮询来为每个任务都服务一定的时间&#xff0c;然后把当前任务的状态保存下来&#xff0c;继续服务下…

IO多路转接—select,poll,epoll

目录 select 函数介绍 select基本工作流程 select的优缺点及适用场景 poll poll的优缺点 epoll epoll的相关系统调用 epoll_create epoll_ctl epoll_wait epoll工作原理 epoll服务器编写 epoll的优点 epoll工作方式 select 函数介绍 系统提供select函数来实现多路复…

Spring核心设计思想

目录 前言&#xff1a; Spring是什么 什么是IoC 传统开发思想 IoC开发思想 Spring IoC 什么是DI 小结&#xff1a; 前言&#xff1a; 官网中提出&#xff1a;Spring makes programming Java quicker, easier, and safer for everybody. Spring’s focus on speed, simp…

YOLOv7+单目测距(python)

YOLOv7单目测距&#xff08;python&#xff09; 1. 相关配置2. 测距原理3. 相机标定3.1&#xff1a;标定方法13.2&#xff1a;标定方法2 4. 相机测距4.1 测距添加4.2 主代码 5. 实验效果 相关链接 1. YOLOV5 单目测距&#xff08;python&#xff09; 2. YOLOV5 双目测距&…

基于springboot的招聘信息管理系统源码数据库论文

目 录 1 绪 论 1.1 课题背景与意义 1.2 系统实现的功能 1.3 课题研究现状 2系统相关技术 2.1 Java语言介绍 2.2 B/S架构 2.3 MySQL 数据库介绍 2.4 MySQL环境配置 2.5 SpringBoot框架 3系统需求分析 3.1系统功能 3.2可行性研究 3.2.1 经济可行性 …