Movidius神經計算棒初體驗

Intel® Movidius™ 神經計算棒和U盤對比圖

Intel® Movidius™ 神經計算棒(NCS)是個使用USB接口的深度學習設備,比U盤略大,功耗1W,浮點性能可達100GFLOPs。php

100GFLOPs大概是什麼概念呢,i7-8700K有59.26GLOPs,Titan V FP16 有24576GLOPs……(僅供娛樂參考,對比是不一樣精度的)。html

安裝NCSDK

目前NCSDK官方安裝腳本只支持Ubuntu 16.04和Raspbian Stretch,折騰一下在其餘Linux系統運行也是沒問題的,例如我用ArchLinux,大概步驟以下:python

  1. 安裝python 三、opencv、tensorflow 1.4.0還有其餘依賴
  2. 編譯安裝caffe,須要用到這個沒合併的PR:Fix boost_python discovery for distros with different naming scheme
  3. 改官方的腳本,跳過系統檢查,跳過依賴安裝(在第一步就手工裝完了)

當我順利折騰完以外,才發現AUR是有了現成的ncsdklinux

安裝完成後改LD_LIBRARY_PATH和PYTHONPATH:git

export LD_LIBRARY_PATH=/opt/movidius/bvlc-caffe/build/install/lib64/:/usr/local/lib/
export PYTHONPATH="${PYTHONPATH}:/opt/movidius/caffe/python"

模型編譯

使用NCS須要把caffe或tensorflow訓練好的模型轉換成NCS支持的格式。由於Keras有TF後端,因此用Keras的模型也是能夠的。這裏以Keras自帶的VGG16爲例:github

from keras.applications import VGG16
from keras import backend as K
import tensorflow as tf

mn = VGG16()
saver = tf.train.Saver()
sess = K.get_session()
saver.save(sess, "./TF_Model/vgg16")

這裏直接用tf.train.Saver保存了一個tf模型,而後用mvNCCompile命令進行編譯,須要指定網絡的輸入和輸出節點,-s 12表示使用12個SHAVE處理器:後端

$ mvNCCompile TF_Model/vgg16.meta -in=input_1 -on=predictions/Softmax -s 12

順利的話就的到一個graph文件。api

(這裏用了我本身改的TensorFlowParser.py才能用)網絡

模型調優

mvNCProfile命令能夠查看模型中每一層使用的內存帶寬、算力,模型調優能夠以此爲參考。session

$ mvNCProfile TF_Model/vgg16.meta -in=input_1 -on=predictions/Softmax -s 12

Detailed Per Layer Profile
                                                       Bandwidth   time
#    Name                                        MFLOPs  (MB/s)    (ms)
=======================================================================
0    block1_conv1/Relu                            173.4   304.2   8.510
1    block1_conv2/Relu                           3699.4   662.6  83.297
2    block1_pool/MaxPool                            3.2   831.6   7.366
3    block2_conv1/Relu                           1849.7   419.9  33.158
4    block2_conv2/Relu                           3699.4   474.2  58.718
5    block2_pool/MaxPool                            1.6   923.4   3.317
6    block3_conv1/Relu                           1849.7   171.8  43.401
7    block3_conv2/Relu                           3699.4   180.6  82.579
8    block3_conv3/Relu                           3699.4   179.8  82.921
9    block3_pool/MaxPool                            0.8   919.2   1.666
10   block4_conv1/Relu                           1849.7   137.3  41.554
11   block4_conv2/Relu                           3699.4   169.0  67.442
12   block4_conv3/Relu                           3699.4   169.6  67.232
13   block4_pool/MaxPool                            0.4   929.7   0.825
14   block5_conv1/Relu                            924.8   308.9  20.176
15   block5_conv2/Relu                            924.8   318.0  19.594
16   block5_conv3/Relu                            924.8   314.9  19.788
17   block5_pool/MaxPool                            0.1   888.7   0.216
18   fc1/Relu                                     205.5  2155.9  90.937
19   fc2/Relu                                      33.6  2137.2  14.980
20   predictions/BiasAdd                            8.2  2645.0   2.957
21   predictions/Softmax                            0.0    19.0   0.201
-----------------------------------------------------------------------
                           Total inference time                  750.84
-----------------------------------------------------------------------
Generating Profile Report 'output_report.html'...

VGG16

能夠看到執行1此VGG16推斷須要750ms,主要時間花在了幾個卷積層,因此這個模型用在實時的視頻分析是不合適的,這時能夠試試其餘的網絡,例如SqueezeNet只要48ms:

Detailed Per Layer Profile
                                                       Bandwidth   time
#    Name                                        MFLOPs  (MB/s)    (ms)
=======================================================================
0    data                                           0.0 78350.0   0.004
1    conv1                                        347.7  1622.7   8.926
2    pool1                                          2.6  1440.0   1.567
3    fire2/squeeze1x1                               9.3  1214.8   0.458
4    fire2/expand1x1                                6.2   155.2   0.608
5    fire2/expand3x3                               55.8   476.3   1.783
6    fire3/squeeze1x1                              12.4  1457.4   0.509
7    fire3/expand1x1                                6.2   152.6   0.618
8    fire3/expand3x3                               55.8   478.3   1.776
9    fire4/squeeze1x1                              24.8  1022.0   0.730
10   fire4/expand1x1                               24.8   176.2   1.093
11   fire4/expand3x3                              223.0   389.7   4.450
12   pool4                                          1.7  1257.7   1.174
13   fire5/squeeze1x1                              11.9   780.3   0.476
14   fire5/expand1x1                                6.0   154.0   0.341
15   fire5/expand3x3                               53.7   359.8   1.314
16   fire6/squeeze1x1                              17.9   639.4   0.593
17   fire6/expand1x1                               13.4   159.5   0.531
18   fire6/expand3x3                              120.9   259.7   2.935
19   fire7/squeeze1x1                              26.9   826.1   0.689
20   fire7/expand1x1                               13.4   159.7   0.530
21   fire7/expand3x3                              120.9   255.2   2.987
22   fire8/squeeze1x1                              35.8   727.0   0.799
23   fire8/expand1x1                               23.9   164.0   0.736
24   fire8/expand3x3                              215.0   191.4   5.677
25   pool8                                          0.8  1263.8   0.563
26   fire9/squeeze1x1                              11.1   585.9   0.388
27   fire9/expand1x1                                5.5   154.1   0.340
28   fire9/expand3x3                               49.8   283.2   1.664
29   conv10                                       173.1   335.8   3.400
30   pool10                                         0.3   676.1   0.477
31   prob                                           0.0     9.5   0.200
-----------------------------------------------------------------------
                           Total inference time                   48.34
-----------------------------------------------------------------------

推斷

有了圖文件,咱們就能夠把它加載到NCS,而後進行推斷:

from mvnc import mvncapi as mvnc

## 枚舉設備
devices = mvnc.EnumerateDevices()

## 打開第一個NCS
device = mvnc.Device(devices[0])
device.OpenDevice()

## 讀取圖文件
with open("graph", mode='rb') as f:
    graphfile = f.read()

## 加載圖
graph = device.AllocateGraph(graphfile)

圖加載加載完成後,就能夠graph.LoadTensor給它一個輸入,graph.GetResult獲得結果。

# 從攝像頭獲取圖像
ret, frame = cap.read()

# 預處理
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (224, 224))
img = preprocess_input(img.astype('float32'))

# 輸入
graph.LoadTensor(img.astype(numpy.float16), 'user object')

# 獲取結果
output, userobj = graph.GetResult()

result = decode_predictions(output.reshape(1, 1000))

識別出鼠標:

鼠標

Kindle和iPod還算類似吧:)

圖片描述

完整的代碼在oraoto/learn_ml/ncs,有MNIST和VGG16的例子。

相關文章
相關標籤/搜索