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)文件,在终端输入一下命令:
- mo
- mo --input_model D:\resnet_model.onnx
- 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()