OpenVINO2020.1.0安装和使用

OpenVINO安装非常容易,可以直接用命令行安装.在终端输入:

  •  pip install openvino-dev[onnx,pytorch]==2022.1.0

执行完这个命令就算是安装好了

安装完成,装换一个ONNX格式的文件,以VGG16为例先转成ONNX,生成ONNX结尾的文件,代码如下:

import torch
from openvino.runtime import Core


model = models.VGG16(pretrained=True)

model.eval()

model.cpu()

dummy_input1 = torch.randn(1, 3, 224, 224)

torch.onnx.export(model, (dummy_input1), "VGG16_model.onnx", verbose=True) 

 

然后引用ONNX格式的文件生成IR格式(xml/bin)文件,在终端输入一下命令:

  1. mo
  2. mo --input_model D:\resnet_model.onnx
  3.  omz_downloader --name person-detection-0200

代码如下:
import cv2
import numpy as np
from openvino.runtime import Core

with open('../unet-pytorch-main/imagenet.txt') as f:
    labels = [line.strip() for line in f.readlines()]

def resnet_demo():
    ie = Core()
    # model = ie.read_model(model="resnet_model.onnx")
    model = ie.read_model(model="best.xml")
    compiled_model = ie.compile_model(model=model, device_name="CPU")

    output_layer = compiled_model.output(0)

    means = np.zeros((512, 512, 3), dtype=np.float32)
    means[: ,:] = (0.485, 0.456, 0.406)
    dev = np.zeros((512, 512, 3), dtype=np.float32)
    dev[: ,:] = (0.229, 0.224, 0.225)

    image = cv2.imread("D:/Program Files/PycharmProjects/untitled2/unet-pytorch-main/Medical_Datasetss/Labels/10.png")
    rgb = cv2.cvtColor(image, code=cv2.COLOR_BGR2RGB)

    # resize to MobileNet image shape
    input_image = cv2.resize(src=rgb, dsize=(512, 512))
    blob_img = np.float32(input_image) / 255.0
    input_x = (blob_img - means) / dev
    input_x = input_x.transpose((2, 0, 1))
    input_x = np.expand_dims(input_x, 0)
    print(input_x.shape)

    result_infer = compiled_model([input_x])[output_layer]
    result_index = np.argmax(result_infer)
    cv2.putText(image, labels[1], (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2)
    cv2.imshow("OpenVINO2022 + Pythorch unet", image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__ == "__main__":
    resnet_demo()
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值