Intel Movidius Neural Computer Stick 2使用

Intel Movidius Neural Computer Stick 2使用,第1张

接着博主前面的系列博客继续讲,这篇来介绍上Intel的第二代加速神经棒的使用,主要还是参考官网来配置。前面很多博客也都访问过多家公司的官网,比较下来,Intel的真的很强大,满满的技术感。

Intel® Neural Compute Stick 2Learn AI programming at the edge with the newest generation of this tiny, fanless deep learning device.https://www.intel.com/content/www/us/en/developer/tools/neural-compute-stick/overview.html这里博主使用的是第二代,我们可以从第一代产品的官网上看到停止通知

第一代的Movidius™ Neural Compute SDK (21889.html" class="superseo">NCSDK)不再更新了,都转用OpenVINO包来做了,且这个包同时支持第一代和第二代。

一. PC(Ubuntu 20.04)硬件环境下使用Intel NCS2

博主此时的软件环境和写此篇博客时保持一致,然后自己又在淘宝上买了一个神经棒,就可以开始实验起来了。

 1.安装OpenVINO Runtime Toolkit

可以参考我前面的博客第四部分(c++环境下使用OpenVINO)来安装

OpenVINO使用介绍_竹叶青lvye的博客-CSDN博客_openvino使用https://blog.csdn.net/jiugeshao/article/details/124288250?spm=1001.2014.3001.5502

注意环境变量配置结束后,这边还需要多一步,就是安装下神经棒的驱动,cd到如下目录下(结合自己的路径)

 执行命令

./install_NCS_udev_rules.sh

2. 列举当前支持的推断设备

参考官网,可以通过如下API来获取当前电脑上所能提供的推断设备

import openvino.runtime as ov

core = ov.Core()
for device in core.available_devices:
    print(device)

结果如下:

/home/sxhlvye/anaconda3/envs/testOpenVINO2/bin/python3.6 /home/sxhlvye/trial4/test_available.py
[E:] [BSL] found 0 ioexpander device
CPU
GNA
GPU
MYRIAD

Process finished with exit code 0

博主这边把NSC 2神经棒拔掉再运行上面代码,输出结果如下:

/home/sxhlvye/anaconda3/envs/testOpenVINO2/bin/python3.6 /home/sxhlvye/trial4/test_available.py
[E:] [BSL] found 0 ioexpander device
CPU
GNA
GPU

Process finished with exit code 0

可看到MYRIAD设备不存在了,符合期望结果。

 3.在神经棒上进行推断

参考前面博客OpenVINO使用介绍_竹叶青lvye的博客-CSDN博客_openvino使用

还是用这篇博客里生成的openVINO runtime中间模型Intermediate Representation (IR)来推断小猫图片,代码如下:

import cv2
import numpy
import openvino.runtime as ov
import numpy as np
import time
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions

import openvino.runtime as ov

core = ov.Core()
for device in core.available_devices:
    print(device)

cpu_device_name = core.get_property("CPU", "FULL_DEVICE_NAME")
print(cpu_device_name)

cpu_device_name = core.get_property("GNA", "FULL_DEVICE_NAME")
print(cpu_device_name)

cpu_device_name = core.get_property("GPU", "FULL_DEVICE_NAME")
print(cpu_device_name)

cpu_device_name = core.get_property("MYRIAD", "FULL_DEVICE_NAME")
print(cpu_device_name)

core = ov.Core()
model = core.read_model("./resnet/saved_model.xml")
#model.reshape([1,224,224,3])

compiled_model = core.compile_model(model, "MYRIAD")
infer_request = compiled_model.create_infer_request()

img = cv2.imread('2008_002682.jpg')
img = cv2.resize(img, (224, 224))
img_np = np.array(img, dtype=np.float32) / 255.

img_np = np.expand_dims(img_np, axis=0)
print(img_np.shape)

# Create tensor from external memory
input_tensor = ov.Tensor(array=img_np, shared_memory=False)

infer_request.set_input_tensor(input_tensor)

t_model = time.perf_counter()
infer_request.start_async()
infer_request.wait()
print(f'do inference cost:{time.perf_counter() - t_model:.8f}s')

# Get output tensor for model with one output
output = infer_request.get_output_tensor()
output_buffer = output.data

# output_buffer[] - accessing output tensor data
print(output_buffer.shape)
print('Predicted:', decode_predictions(output_buffer, top=5)[0])

print("ok")

执行出现如下错误:

Traceback (most recent call last):
  File "/home/sxhlvye/trial4/test_inference2.py", line 30, in
    compiled_model = core.compile_model(model, "MYRIAD")
  File "/home/sxhlvye/intel/openvino_2022/python/python3.6/openvino/runtime/ie_api.py", line 266, in compile_model
    super().compile_model(model, device_name, {} if config is None else config)
RuntimeError: Cannot get length of dynamic dimension

报错原因是模型输入shape不是静态的,所以得让shape固定,官网也有相关方面说明

Changing input shapes — OpenVINO™ documentation

这边只需将上面博主所写代码解注释掉即可#model.reshape([1,224,224,3]),修改后再次运行代码,结果如下:

/home/sxhlvye/anaconda3/envs/testOpenVINO2/bin/python3.6 /home/sxhlvye/trial4/test_inference2.py
2022-05-08 08:38:26.794065: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.11.0
[E:] [BSL] found 0 ioexpander device
CPU
GNA
GPU
MYRIAD
Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
GNA_SW
Intel(R) UHD Graphics 630 [0x3e9b] (iGPU)
Intel Movidius Myriad X VPU
(1, 224, 224, 3)
do inference cost:0.05454781s
(1, 1000)
Predicted: [('n01930112', 'nematode', 0.12646484), ('n03041632', 'cleaver', 0.03668213), ('n03838899', 'oboe', 0.029830933), ('n02783161', 'ballpoint', 0.025115967), ('n03804744', 'nail', 0.024139404)]
ok

Process finished with exit code 0

上面是跑在intel NCS 2上,博主这边也分别在CPU和GPU上进行了测试

上面代码对应处修改为 compiled_model = core.compile_model(model, "CPU"),运行下程序,结果如下(部分信息)

(1, 224, 224, 3)
do inference cost:0.02203732s
(1, 1000)
Predicted: [('n01930112', 'nematode', 0.13559894), ('n03041632', 'cleaver', 0.041396398), ('n03838899', 'oboe', 0.034457874), ('n02783161', 'ballpoint', 0.02541826), ('n04270147', 'spatula', 0.023189805)]
ok

Process finished with exit code 0

 上面代码对应处修改为 compiled_model = core.compile_model(model, "GPU"),运行下程序,结果如下(部分信息)

do inference cost:0.03005712s
(1, 1000)
Predicted: [('n01930112', 'nematode', 0.13559903), ('n03041632', 'cleaver', 0.041396033), ('n03838899', 'oboe', 0.03445781), ('n02783161', 'ballpoint', 0.025418187), ('n04270147', 'spatula', 0.023189766)]
ok

大家会奇怪,都跑在GPU上了,怎么速度还不如CPU呢,是因为OpenVINO不支持英伟达的显卡,所以并没有跑在博主GTX 1660Ti显卡上,而是跑在Intel(R) UHD Graphics 630 [0x3e9b] (iGPU)上面。官网这边也提到了

Configurations for Intel® Processor Graphics (GPU) with Intel® Distribution of OpenVINO™ toolkit — OpenVINO™ documentation

二.官网资料梳理

OpenVINO的官网资料零散部分着,跳跃链接实在太多,这里博主为了方便以后快速能找到对应知识点,简单进行了梳理。

正常我们要去看关于神经加速棒的介绍,会首先到此网站上来

Intel® Movidius™ Neural Compute Stick

上面一些重要信息是说了,第一个代已经停止更新SDK了,后面第二代神经棒就是统一用OpenVINO了,此包也支持第一代神经棒。同时此页面也提供了访问神经计算棒第二代介绍的入口。

NCS 2的介绍主页面

Get Started with Intel® Neural Compute Stick 2

重要信息如下

 1.人家说了是拿2019版的toolkit来示例介绍的,用最新的小伙伴们会发现,step-by-step按照官网整时,发现并没有改页面提到的demo示例程序,所以不用太纠结

2. 官网也是有很多NCS 2在树莓派系列上的使用介绍的

3. OpenVINO方面的使用示例可以参考ncappzoo

4. https://github.com/openvinotoolkit/open_model_zoo 里面也有很多丰富的资料

5.关于OpenVNO的安装、使用介绍可以从此页面上的入口处切换

 6.Get Started上是安装方面的介绍

 OpenVINO Runtime的安装方面,其中Using Installer是通过下载安装包方式来安装,也提到了如何去配置不同硬件的 *** 作(GPU、NCS 2等),配置环境变量;From PYPI是通过pip方式来安装,针对python环境;From APT是通过apt install方式来安装,对应Using Installer方式;From YUM是针对Red Hat Enterprise Linux 8, 64-bit系统;From Anaconda是用conda方式安装,针对python环境;using Docker是通过Docker方式安装。

博主常用的是Using Installer和From PYPI方式。

7.其它的关于OpemVINO Development的安装、开发demo都可以从上面页面找到入口。

8.模型如何转为OpenVINO模型,以及结合自己的使用见Documentation

9.对于博主来讲,应该主要是拿OpenVINO用来在推断阶段加速,虽然你官网提供了训练的简单示例

OpenVINO™ Training Extensions — OpenVINO™ documentation

 training_extensions/train.ipynb at master · openvinotoolkit/training_extensions · GitHub

但太小众了,后面主要还是用tensorflow、pytorch框架来进行训练,通过转换模型方式来使用。

剩余的就不梳理了,看多了就好了。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/883366.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-14
下一篇 2022-05-14

发表评论

登录后才能评论

评论列表(0条)

保存