Movidius
1.神經(jīng)元計(jì)算棒
2.1什么是英特爾?Movidius?神經(jīng)計(jì)算棒?
英特爾?Movidius?神經(jīng)計(jì)算棒(NCS)是一款微型無(wú)風(fēng)扇深度學(xué)習(xí)設(shè)備橡娄,可用于學(xué)習(xí)邊緣的AI編程勺阐。NCS采用相同的低功耗高性能英特爾Movidius視覺(jué)處理單元(VPU),可在數(shù)百萬(wàn)智能安全攝像頭政恍,手勢(shì)控制無(wú)人機(jī),工業(yè)機(jī)器視覺(jué)設(shè)備等中找到达传。
特點(diǎn):高性能篙耗,低功耗。
英特爾?Movidius?神經(jīng)計(jì)算棒(英特爾?Movidius?NCS)是英特爾公司Movidius的嵌入式機(jī)器智能平臺(tái)宪赶。通過(guò)軟件和硬件工具宗弯,英特爾Movidius NCS將機(jī)器智能和AI從數(shù)據(jù)中心帶入最終用戶(hù)設(shè)備。英特爾Movidius NCS旨在幫助實(shí)現(xiàn)機(jī)器智能空間的微型化搂妻,并加速無(wú)處不在的智能設(shè)備和系統(tǒng)時(shí)代蒙保。
英特爾Movidius神經(jīng)計(jì)算棒具備強(qiáng)大的功能,可以在邊緣實(shí)現(xiàn)深度神經(jīng)網(wǎng)絡(luò)(DNN)推理應(yīng)用的快速原型設(shè)計(jì)欲主,驗(yàn)證和部署邓厕。其低功耗VPU架構(gòu)支持全新的AI應(yīng)用程序逝嚎,這些應(yīng)用程序不依賴(lài)于與云的連接。
NCS與英特爾Movidius神經(jīng)計(jì)算SDK相結(jié)合详恼,使深度學(xué)習(xí)開(kāi)發(fā)人員能夠在需要實(shí)時(shí)推理的低功耗應(yīng)用上分析补君,調(diào)整和部署卷積神經(jīng)網(wǎng)絡(luò)(CNN)。
2.2技術(shù)規(guī)格
處理器 英特爾?Movidius?VPU
支持的框架 TensorFlow?昧互,Caffe
連接 USB 3.0 Type-A
外形尺寸 USB stick (72.5mm X 27mm X 14mm)
工作溫度 0° - 40° C
最低系統(tǒng)要求 x86_64計(jì)算機(jī)運(yùn)行Ubuntu * 16.04或
Raspberry Pi 3 Model B運(yùn)行Stretch桌面或
Ubuntu 16.04 VirtualBox挽铁,
USB 2.0 Type-A端口(推薦USB 3.0),
1GB RAM,
4GB free storage space
2.3NCS環(huán)境安裝
2.3.1前提準(zhǔn)備:
1)Ubuntu 16.04系統(tǒng)敞掘,RPI3 Model B或Ubuntu VirtualBox叽掘。(實(shí)測(cè)ubuntu19,樹(shù)莓派zero W也可安裝渐逃。)
2)英特爾Movidius神經(jīng)計(jì)算棒(NCS)够掠。
3)確保Internet連接以下載和安裝NCSDK。
4)python3.5
備注:實(shí)際操作中需要使用自己本機(jī)自帶的python茄菊,不可使用ananconda的python環(huán)境疯潭,否則會(huì)報(bào)出caffe路徑的錯(cuò)誤或者其他錯(cuò)誤!
5)Keras版本:2.1
備注:如果不使用Keras2.1面殖,會(huì)出現(xiàn)符號(hào)無(wú)法解析的bug或者其他報(bào)錯(cuò)竖哩!
6)tensorflow版本:1.4.0
備注:如果高于1.6.0,會(huì)出現(xiàn)符號(hào)無(wú)法解析的bug或者其他報(bào)錯(cuò)脊僚,另外ncsdk安裝的過(guò)程中如果檢測(cè)不到tensorflow1.4.0相叁,會(huì)重新在線下載!
2.3.2開(kāi)始安裝:
1)打開(kāi)終端
ubuntu辽幌、ubuntu虛擬機(jī)和樹(shù)莓派操作系統(tǒng)都是Ctrl+Alt+T打開(kāi)終端增淹,或者點(diǎn)擊右鍵Open the terminal直接打開(kāi)命令行。ubuntu乌企、ubuntu虛擬機(jī)和樹(shù)莓派操作系統(tǒng)是相同的虑润,按以下步驟即可,不贅述加酵。
2)新建工作文件夾
mkdir -p~ / workspace
3)進(jìn)入新建的工作文件夾
cd ~/workspace
4)github克隆代碼
git clone https://github.com/movidius/ncsdk.git
5)進(jìn)入克隆的代碼文件夾
cd ~/workspace/ncsdk
6)編譯mvnc環(huán)境
sudo make install
備注:官網(wǎng)為make install 實(shí)際安裝中如果不加sudo 會(huì)提示權(quán)限的問(wèn)題拳喻。另外已經(jīng)安裝好的環(huán)境,也可以執(zhí)行此步驟猪腕,在線下載一些配置文件冗澈,并且刪除部分在線下載的文件,重新下載陋葡。一般按照經(jīng)驗(yàn)來(lái)說(shuō)亚亲,網(wǎng)絡(luò)良好的狀態(tài),執(zhí)行此步驟需要一個(gè)小時(shí)左右!這部分和電腦性能有關(guān)系捌归,電腦性能越好颊亮,需要的時(shí)間越少。
7)編譯Movidius自帶的例子
sudo make examples
備注:一般此步驟都會(huì)報(bào)錯(cuò)陨溅,因?yàn)闊o(wú)法在線下載一些模型文件和label標(biāo)簽文件這里面有一些是需要翻墻才能下載的或者是因?yàn)槲募蠖鴮?dǎo)致請(qǐng)求超時(shí)。但是此步驟執(zhí)行不通過(guò)绍在,不影響使用门扇,也可以使用開(kāi)發(fā)Movidius的第三方python庫(kù)—mvnc!這里安裝Movidius的python開(kāi)發(fā)環(huán)境偿渡,就是mvnc臼寄!特別要注意的是,此語(yǔ)句執(zhí)行完成之后溜宽,需要重開(kāi)命令行吉拳,mvnc環(huán)境才能生效。
8)卸載NCSDK
sudo make uninstall
備注:如果需要卸載后重裝适揉,推薦在此步驟之前留攒,手動(dòng)輸入
sudo make clean
9)驗(yàn)證是否安裝成功
命令行輸,如果不報(bào)錯(cuò)嫉嘀,則說(shuō)明安裝成功炼邀。
python3
import mvnc
2.3.3常見(jiàn)安裝異常處理:
1)執(zhí)行權(quán)限問(wèn)題報(bào)錯(cuò)。
參考方法:在執(zhí)行語(yǔ)句之前加上sudo(sudo是linux系統(tǒng)管理指令剪侮,是允許系統(tǒng)管理員讓普通用戶(hù)執(zhí)行一些或者全部的root命令的一個(gè)工具)拭宁。
2)opencv報(bào)錯(cuò)。
參考方法:重新構(gòu)建opencv
參考文章:https://blog.csdn.net/hzgloveyd/article/details/79031761
3)python路徑或者anaconda報(bào)錯(cuò)瓣俯。
參考方法:卸載anaconda或者注釋anaconda環(huán)境變量杰标。
參考文章:https://www.cnblogs.com/wiessharling/p/4272329.html
4)執(zhí)行make examples 時(shí)報(bào)錯(cuò):
sed: 1: "ncsdk.conf": extra characters at the end of n command
make: *** [prereqs] Error 1
參考文章:https://ncsforum.movidius.com/discussion/331/installing-nc-sdk-make-examples-error
5)caffe導(dǎo)入錯(cuò)誤
參考方法:輸入:
sudo apt-get instal -reinstall python * -decorator
參考文章:https://ncsforum.movidius.com/discussion/comment/1009/#Comment_1009
6)缺少其他模塊,例如scikit-image
參考方法:缺少什么模塊下載什么模塊彩匕。
sudo pip3 install scikit-image
其他故障參考:https://ncsforum.movidius.com/discussion/370/intel-ncs-troubleshooting-help-and-guidelines
7)使用NCSDK工具進(jìn)行格式轉(zhuǎn)換的時(shí)候發(fā)生錯(cuò)誤
修改/usr/local/bin/ncsdk/Controllers/TensorFlowParser.py 第1059行腔剂,完成后如下:
sudo nano /usr/local/bin/ncsdk/Controllers/TensorFlowParser.py
Ctrl+_,輸入1059快速跳轉(zhuǎn)到1059行
#desired_shape = node.inputs[1].eval()
desired_shape = node.inputs[1].eval(feed_dict={inputnode + ':0' : input_data})
備注:如果要在此平臺(tái)上訓(xùn)練自己的數(shù)據(jù)集推掸,這里必須要修改桶蝎。一般樹(shù)莓派上不需要更改,訓(xùn)練主機(jī)上需要更改谅畅,不然在后續(xù)的模型文件格式轉(zhuǎn)換過(guò)程中會(huì)報(bào)錯(cuò)登渣!
3NCSDK開(kāi)發(fā)入門(mén)
3.1NCSDK工作流程:
備注:網(wǎng)絡(luò)訓(xùn)練階段不使用NCSDK。
3.2NCSDK主要工具
英特爾?Movidius?神經(jīng)計(jì)算SDK提供了用于在開(kāi)發(fā)計(jì)算機(jī)(主機(jī)系統(tǒng))上分析毡泻,調(diào)整和編譯深度神經(jīng)網(wǎng)絡(luò)(DNN)模型的工具胜茧。
NCSDK包括一組軟件工具,用于編譯,配置和檢查(驗(yàn)證)運(yùn)行于Movidius計(jì)算棒上的模型圖文件(graph)呻顽。
NCSDK主要包括三個(gè)工具雹顺,分別為:
mvNCCompile:將caffe或者tensorflow的模型文件轉(zhuǎn)換生成Movidius識(shí)別的模型文件。
mvNCCheck:檢查caffe或者tensorflow的模型文件是否可用廊遍。
mvNCProfile:測(cè)試生成Movidius的模型文件的性能嬉愧。
注意:mvNCCompile mvNCCheck這兩個(gè)工具需要把Movidius插到電腦上,否則會(huì)報(bào)錯(cuò)喉前。
3.2.1mvNCCompile
mvNCCompile是一個(gè)命令行工具没酣,可將Caffe或TensorFlow *模型的網(wǎng)絡(luò)和權(quán)重文件編譯為英特爾?Movidius?圖形文件格式,該格式與英特爾?Movidius?神經(jīng)計(jì)算SDK(英特爾?Movidius?NCSDK)和神經(jīng)計(jì)算API兼容(NCAPI)卵迂。
Caffe的格式:
mvNCCompile network.prototxt [-w network.caffemodel] [-s max_number_of_shaves] [-in input_node_name] [-on output_node_name] [-is input_width input_height] [-o output_graph_filename] [-ec]
Tensorflow的格式:
mvNCCompile network.meta [-s max_number_of_shaves] [-in input_node_name] [-on output_node_name] [-is input_width input_height] [-o output_graph_filename] [-ec]
參數(shù)詳解:
參數(shù) 描述
Caffe:
network.prototxt
TensorFlow:
network.meta
network.pb 網(wǎng)絡(luò)文件的名稱(chēng)(必填)裕便。
[-h,-help] 顯示命令的幫助见咒。
[-w weights_file] 從訓(xùn)練中指定權(quán)重文件名偿衰。對(duì)于Caffe,這是.caffemodel文件改览。如果省略下翎,將使用零權(quán)重。
此選項(xiàng)不適用于TensorFlow網(wǎng)絡(luò)恃疯。
[-s max_number_of_shaves] 指定用于網(wǎng)絡(luò)層的最大SHAVE數(shù)(默認(rèn)值:1)漏设。
可用SHAVE的數(shù)量取決于您的神經(jīng)計(jì)算設(shè)備。對(duì)于某些層今妄,設(shè)備運(yùn)行時(shí)代碼可能使用較少的SHAVE郑口,其中測(cè)量通常顯示使用較少的SHAVE沒(méi)有推理性能下降(并因此顯示功率優(yōu)勢(shì))。
[-in input_node_name] 為網(wǎng)絡(luò)指定備用起點(diǎn)盾鳞。默認(rèn)情況下犬性,網(wǎng)絡(luò)的起點(diǎn)是輸入層。此選項(xiàng)啟用部分網(wǎng)絡(luò)處理腾仅。當(dāng)與-on選項(xiàng)一起使用時(shí)乒裆,用戶(hù)可以隔離網(wǎng)絡(luò)中的一個(gè)或多個(gè)層以進(jìn)行分析。
TensorFlow網(wǎng)絡(luò)需要此選項(xiàng)推励。您可以在創(chuàng)建網(wǎng)絡(luò)時(shí)使用name參數(shù)(適用于大多數(shù)圖層)鹤耍,并將該名稱(chēng)傳遞給此選項(xiàng)。
要添加不更改網(wǎng)絡(luò)的命名節(jié)點(diǎn)验辞,可以使用以下命令:
x = tensorflow.identity(prev_tensor, name='new_node')
[-on output_node_name] 為網(wǎng)絡(luò)指定備用端點(diǎn)稿黄。默認(rèn)情況下,網(wǎng)絡(luò)的終點(diǎn)是輸出層跌造。此選項(xiàng)啟用部分網(wǎng)絡(luò)處理杆怕。當(dāng)與-in選項(xiàng)一起使用時(shí)族购,用戶(hù)可以隔離網(wǎng)絡(luò)中的一個(gè)或多個(gè)層以進(jìn)行分析。
請(qǐng)注意陵珍,解析器將在此節(jié)點(diǎn)名稱(chēng)的第一個(gè)實(shí)例處停止(例如寝杖,如果Conv與共享名稱(chēng)相同,則不會(huì)處理它后面的Relu)互纯。
TensorFlow網(wǎng)絡(luò)需要此選項(xiàng)瑟幕。您可以在創(chuàng)建網(wǎng)絡(luò)時(shí)使用name參數(shù)(適用于大多數(shù)圖層),并將該名稱(chēng)傳遞給此選項(xiàng)留潦。
要添加不更改網(wǎng)絡(luò)的命名節(jié)點(diǎn)收苏,可以使用以下命令:
x = tensorflow.identity(prev_tensor, name='new_node')
[-is input_width input_height] 為輸入圖層上沒(méi)有尺寸限制的網(wǎng)絡(luò)指定輸入尺寸。
此選項(xiàng)假定批量大小為1愤兵,通道數(shù)為3。
[-o output_graph_filename] 指定輸出圖形文件名排吴。如果未提供秆乳,則“graph”將用于文件名。
[-ec] 跳過(guò)某些編譯器優(yōu)化以進(jìn)行連接; 這可能會(huì)糾正來(lái)自concat層或編譯失敗的無(wú)效結(jié)果的一些問(wèn)題钻哩。
例如:
Caffe:
mvNCCompile deploy.prototxt -w bvlc_googlenet.caffemodel -s 12 -in input -on prob -is 224 224 -o GoogLeNet.graph
Tensorflow:
mvNCCompile inception-v1.meta -s 12 -in=input -on=InceptionV1/Logits/Predictions/Reshape_1 -is 224 224 -o InceptionV1.graph
3.2.2mvNCCheck
https://movidius.github.io/blog/mvNCCheck/
mvNCCheck是一個(gè)命令行工具屹堰,用于檢查神經(jīng)計(jì)算設(shè)備上Caffe或TensorFlow *神經(jīng)網(wǎng)絡(luò)的有效性。
通過(guò)使用提供的網(wǎng)絡(luò)和適當(dāng)?shù)目蚣軒?kù)在設(shè)備和主機(jī)上的軟件中運(yùn)行推斷來(lái)完成檢查街氢。比較兩個(gè)推論的結(jié)果以確定網(wǎng)絡(luò)是否通過(guò)或失敗扯键。前5個(gè)推斷結(jié)果作為輸出提供。此工具最適用于圖像分類(lèi)網(wǎng)絡(luò)珊肃。
Caffe的格式:
mvNCCheck network.prototxt [-w network.caffemodel] [-s max_number_of_shaves] [-in input_node_name] [-on output_node_name] [-is input_width input_height] [-i input_filename] [-id top1_validation_ID] [-S scale_factor] [-M mean_subtraction_number/npy_filename] [-cs color_sequence] [-es]
Tensorflow的格式:
mvNCCheck network.meta [-s max_number_of_shaves] [-in input_node_name] [-on output_node_name] [-is input_width input_height] [-i input_filename] [-id top1_validation_ID] [-S scale_factor] [-M mean_subtraction_number/npy_filename] [-cs color_sequence] [-es]
參數(shù)詳解:
參數(shù) 描述
Caffe:
network.prototxt
TensorFlow:
network.meta
network.pb 網(wǎng)絡(luò)文件的名稱(chēng)(必填)荣刑。
[-h -help] 顯示命令的幫助。
[-w weights_file] 從訓(xùn)練中指定權(quán)重文件名伦乔。對(duì)于Caffe厉亏,這是.caffemodel文件。如果省略烈和,將使用零權(quán)重爱只。
此選項(xiàng)不適用于TensorFlow網(wǎng)絡(luò)。
[-s max_number_of_shaves] 指定用于網(wǎng)絡(luò)層的最大SHAVE數(shù)(默認(rèn)值:1)招刹。
可用SHAVE的數(shù)量取決于您的神經(jīng)計(jì)算設(shè)備恬试。對(duì)于某些層,設(shè)備運(yùn)行時(shí)代碼可能使用較少的SHAVE疯暑,其中測(cè)量通常顯示使用較少的SHAVE沒(méi)有推理性能下降(并因此顯示功率優(yōu)勢(shì))训柴。
[-in input_node_name] 為網(wǎng)絡(luò)指定備用起點(diǎn)。默認(rèn)情況下缰儿,網(wǎng)絡(luò)的起點(diǎn)是輸入層畦粮。此選項(xiàng)啟用部分網(wǎng)絡(luò)處理。當(dāng)與-on選項(xiàng)一起使用時(shí),用戶(hù)可以隔離網(wǎng)絡(luò)中的一個(gè)或多個(gè)層以進(jìn)行分析宣赔。
TensorFlow網(wǎng)絡(luò)需要此選項(xiàng)预麸。您可以在創(chuàng)建網(wǎng)絡(luò)時(shí)使用name參數(shù)(適用于大多數(shù)圖層),并將該名稱(chēng)傳遞給此選項(xiàng)儒将。
要添加不更改網(wǎng)絡(luò)的命名節(jié)點(diǎn)吏祸,可以使用以下命令:
x = tensorflow.identity(prev_tensor, name='new_node')
[-on output_node_name] 為網(wǎng)絡(luò)指定備用端點(diǎn)。默認(rèn)情況下钩蚊,網(wǎng)絡(luò)的終點(diǎn)是輸出層贡翘。此選項(xiàng)啟用部分網(wǎng)絡(luò)處理。當(dāng)與-in選項(xiàng)一起使用時(shí)砰逻,用戶(hù)可以隔離網(wǎng)絡(luò)中的一個(gè)或多個(gè)層以進(jìn)行分析鸣驱。
請(qǐng)注意,解析器將在此節(jié)點(diǎn)名稱(chēng)的第一個(gè)實(shí)例處停止(例如蝠咆,如果Conv與共享名稱(chēng)相同踊东,則不會(huì)處理它后面的Relu)。
TensorFlow網(wǎng)絡(luò)需要此選項(xiàng)刚操。您可以在創(chuàng)建網(wǎng)絡(luò)時(shí)使用name參數(shù)(適用于大多數(shù)圖層)闸翅,并將該名稱(chēng)傳遞給此選項(xiàng)。
要添加不更改網(wǎng)絡(luò)的命名節(jié)點(diǎn)菊霜,可以使用以下命令:
x = tensorflow.identity(prev_tensor, name='new_node')
[-is input_width input_height] 為輸入圖層上沒(méi)有尺寸限制的網(wǎng)絡(luò)指定輸入尺寸坚冀。
此選項(xiàng)假定批量大小為1,通道數(shù)為3鉴逞。
[-i input_filename] 指定用于驗(yàn)證的輸入張量记某。如果未設(shè)置,則將使用具有隨機(jī)生成數(shù)據(jù)的張量构捡。
[-S scale_factor] 使用除數(shù)來(lái)縮放輸入的每個(gè)值辙纬。
通常,圖像與每個(gè)輸入通道的數(shù)據(jù)一起存儲(chǔ)叭喜,范圍為0到255.如果神經(jīng)網(wǎng)絡(luò)期望輸入值在0.0到1.0范圍內(nèi)贺拣,則使用-S 255將數(shù)據(jù)縮放到網(wǎng)絡(luò)的預(yù)期范圍,因?yàn)槊總€(gè)值將除以255捂蕴。網(wǎng)絡(luò)期望值在-1.0到1.0范圍內(nèi)譬涡,使用-S 128并將-M 128數(shù)據(jù)擴(kuò)展到網(wǎng)絡(luò)的預(yù)期范圍。
[-M mean_subtraction_number]
[-M npy_filename] 在傳遞到神經(jīng)網(wǎng)絡(luò)之前從輸入數(shù)據(jù)中減去平均值啥辨。這可以是一個(gè)值或numpy平均文件文件名涡匀。
[-id top1_validation_ID] 提供Top-1驗(yàn)證的預(yù)期ID。
[-cs color_sequence] 指定神經(jīng)網(wǎng)絡(luò)期望的顏色序列:
2,1,0 = BGR (默認(rèn)值)
0,1,2 = RGB
[-ec] 跳過(guò)某些編譯器優(yōu)化以進(jìn)行連接; 這可能會(huì)糾正來(lái)自concat層或編譯失敗的無(wú)效結(jié)果的一些問(wèn)題溉知。
例如:
Caffe:
mvNCCheck deploy.prototxt -w bvlc_googlenet.caffemodel -s 12 -in input -on prob -is 224 224 -cs 2,1,0
Tensorflow:
mvNCCheck inception_v1.meta -s 12 -in=input -on=InceptionV1/Logits/Predictions/Reshape_1 -is 224 224 -cs 0,1,2
3.2.3mvNCProfile
mvNCProfile是一個(gè)命令行工具陨瘩,可編譯網(wǎng)絡(luò)以與英特爾?Movidius?神經(jīng)計(jì)算SDK(英特爾?Movidius?NCSDK)配合使用腕够,在連接的神經(jīng)計(jì)算設(shè)備上運(yùn)行網(wǎng)絡(luò),并輸出文本和HTML配置文件報(bào)告舌劳。
分析數(shù)據(jù)包含有關(guān)網(wǎng)絡(luò)性能的逐層統(tǒng)計(jì)信息帚湘。這有助于確定每層花費(fèi)多少時(shí)間來(lái)縮小網(wǎng)絡(luò)的潛在變化,從而縮短總推理時(shí)間甚淡。
Caffe格式:
mvNCProfile network.prototxt [-w network.caffemodel] [-s max_number_of_shaves] [-in input_node_name] [-on output_node_name] [-is input_width input_height] [-ec]
Tensorflow格式:
mvNCProfile network.meta [-s max_number_of_shaves] [-in input_node_name] [-on output_node_name] [-is input_width input_height] [-ec]
參數(shù)詳解:
參數(shù) 描述
Caffe:
network.prototxt
TensorFlow:
network.meta
network.pb 網(wǎng)絡(luò)文件的名稱(chēng)(必填)大诸。
[-w weights_file] 從訓(xùn)練中指定權(quán)重文件名。對(duì)于Caffe贯卦,這是.caffemodel文件资柔。如果省略,將使用零權(quán)重撵割。
此選項(xiàng)不適用于TensorFlow網(wǎng)絡(luò)贿堰。
[-s max_number_of_shaves] 指定用于網(wǎng)絡(luò)層的最大SHAVE數(shù)(默認(rèn)值:1)。
可用SHAVE的數(shù)量取決于您的神經(jīng)計(jì)算設(shè)備啡彬。對(duì)于某些層官边,設(shè)備運(yùn)行時(shí)代碼可能使用較少的SHAVE,其中測(cè)量通常顯示使用較少的SHAVE沒(méi)有推理性能下降(并因此顯示功率優(yōu)勢(shì))外遇。
[-in input_node_name] 為網(wǎng)絡(luò)指定備用起點(diǎn)。默認(rèn)情況下契吉,網(wǎng)絡(luò)的起點(diǎn)是輸入層跳仿。此選項(xiàng)啟用部分網(wǎng)絡(luò)處理。當(dāng)與-on選項(xiàng)一起使用時(shí)捐晶,用戶(hù)可以隔離網(wǎng)絡(luò)中的一個(gè)或多個(gè)層以進(jìn)行分析菲语。
TensorFlow網(wǎng)絡(luò)需要此選項(xiàng)。您可以在創(chuàng)建網(wǎng)絡(luò)時(shí)使用name參數(shù)(適用于大多數(shù)圖層)惑灵,并將該名稱(chēng)傳遞給此選項(xiàng)山上。
要添加不更改網(wǎng)絡(luò)的命名節(jié)點(diǎn),可以使用以下命令:
x=tensorflow.identity(prev_tensor, name='new_node')
[-on output_node_name] 為網(wǎng)絡(luò)指定備用端點(diǎn)英支。默認(rèn)情況下佩憾,網(wǎng)絡(luò)的終點(diǎn)是輸出層。此選項(xiàng)啟用部分網(wǎng)絡(luò)處理干花。當(dāng)與-in選項(xiàng)一起使用時(shí)妄帘,用戶(hù)可以隔離網(wǎng)絡(luò)中的一個(gè)或多個(gè)層以進(jìn)行分析。
請(qǐng)注意池凄,解析器將在此節(jié)點(diǎn)名稱(chēng)的第一個(gè)實(shí)例處停止(例如抡驼,如果Conv與共享名稱(chēng)相同,則不會(huì)處理它后面的Relu)肿仑。
TensorFlow網(wǎng)絡(luò)需要此選項(xiàng)致盟。您可以在創(chuàng)建網(wǎng)絡(luò)時(shí)使用name參數(shù)(適用于大多數(shù)圖層)碎税,并將該名稱(chēng)傳遞給此選項(xiàng)。
要添加不更改網(wǎng)絡(luò)的命名節(jié)點(diǎn)馏锡,可以使用以下命令:
x=tensorflow.identity(prev_tensor, name='new_node')
[-is input_width input_height] 為輸入圖層上沒(méi)有尺寸限制的網(wǎng)絡(luò)指定輸入尺寸雷蹂。
此選項(xiàng)假定批量大小為1,通道數(shù)為3眷篇。
[-ec] 跳過(guò)某些編譯器優(yōu)化以進(jìn)行連接; 這可能會(huì)糾正來(lái)自concat層或編譯失敗的無(wú)效結(jié)果的一些問(wèn)題萎河。
例如:
Caffe:
mvNCCompile deploy.prototxt -w bvlc_googlenet.caffemodel -s 12 -in input -on prob -is 224 224 -o GoogLeNet.graph
TensorFlow:
mvNCCompile inception-v1.meta -s 12 -in=input -on=InceptionV1/Logits/Predictions/Reshape_1 -is 224 224 -o InceptionV1.graph
3.3NCSDK API概述:
英特爾?Movidius?神經(jīng)計(jì)算SDK(英特爾?Movidius?NCSDK)提供了C / C ++或Python API,用于調(diào)用生成的模型圖(graph)文件進(jìn)行硬件加速的推理運(yùn)算蕉饼。
3.3.1NCAPI v1:
舊版NCSDK接口虐杯,原來(lái)的NCSDK使用了NCAPI v1。該版本已被NCAPI v2取代昧港,后者不向后兼容(即使用NCAPI v1編寫(xiě)的程序?qū)o(wú)法編譯或運(yùn)行NCAPI v2)擎椰。NCAPI v1不再更新。
將Python從NCAPI v1遷移到NCAPI v2:https://movidius.github.io/ncsdk/ncapi/python_api_migration.html
舊版python接口鏈接:https://movidius.github.io/ncsdk/ncapi/ncapi1/py_api/readme.html
3.3.2NCAPI v2:
創(chuàng)建NCAPI v2是為了在開(kāi)發(fā)神經(jīng)計(jì)算設(shè)備的應(yīng)用程序時(shí)為開(kāi)發(fā)人員提供更大的靈活性创肥。NCAPI v2的主要優(yōu)點(diǎn)是:
1)可以將多個(gè)網(wǎng)絡(luò)圖分配給單個(gè)神經(jīng)計(jì)算設(shè)備(每個(gè)設(shè)備最多10個(gè)圖达舒,受設(shè)備內(nèi)存限制)。
2)輸入/輸出由先進(jìn)先出(FIFO)隊(duì)列處理叹侄,后者支持排隊(duì)多個(gè)推理輸入/輸出巩搏。
3)支持32位浮點(diǎn)(FP32)輸入/輸出。
3.3.3NCAPI v2 Python:
英特爾?Movidius?神經(jīng)計(jì)算SDK(英特爾?Movidius?NCSDK)附帶一個(gè)Python語(yǔ)言API趾代,可通過(guò)神經(jīng)計(jì)算設(shè)備(如英特爾?Movidius?神經(jīng)計(jì)算棒)實(shí)現(xiàn)利用硬件加速深度神經(jīng)網(wǎng)絡(luò)的應(yīng)用程序贯底。
Python API作為單個(gè)Python模塊(mvncapi.py)提供,在安裝NCSDK時(shí)將其放置在開(kāi)發(fā)計(jì)算機(jī)上撒强。它已經(jīng)過(guò)Python 2.7和3.5的驗(yàn)證禽捆。
3.4Python API文檔:
安裝完成后會(huì)在ncsdk目錄下出現(xiàn)api文件夾,里面的python子文件夾就是python的api飘哨。
3.5Python API excel文檔:
4NCSDK開(kāi)發(fā)教程及案例
4.1開(kāi)發(fā)教程:
4.1.1導(dǎo)入NCAPI模塊
Python NCAPI位于mvnc包中的mvncapi模塊中胚想。
from mvnc import mvncapi
您可以使用global_get_option()和global_set_option()為GlobalOption枚舉中的選項(xiàng)獲取和設(shè)置應(yīng)??用程序級(jí)信息和選項(xiàng)。
4.1.2設(shè)置神經(jīng)計(jì)算設(shè)備
該設(shè)備類(lèi)表示神經(jīng)計(jì)算設(shè)備并提供方法與設(shè)備進(jìn)行通信芽隆。
全局函數(shù)enumerate_devices()用于獲取連接到主機(jī)系統(tǒng)的神經(jīng)計(jì)算設(shè)備列表浊服。
# Get a list of available device identifiers
device_list = mvncapi.enumerate_devices()
使用從enumerate_devices()調(diào)用獲得的設(shè)備標(biāo)識(shí)符之一初始化設(shè)備。
# Initialize a Device
device = mvncapi.Device(device_list[0])
初始化神經(jīng)計(jì)算設(shè)備并使用Device.open()打開(kāi)通信胚吁。
# Initialize the device and open communication
device.open()
您可以使用Device.get_option()獲取有關(guān)設(shè)備的信息臼闻,以獲取DeviceOption枚舉中的選項(xiàng)。
注意:如果您使用多個(gè)神經(jīng)計(jì)算設(shè)備囤采,則必須為每個(gè)神經(jīng)計(jì)算設(shè)備創(chuàng)建并打開(kāi)一個(gè)單獨(dú)的設(shè)備述呐。
4.1.3 為設(shè)備設(shè)置網(wǎng)絡(luò)圖和關(guān)聯(lián)的FIFO隊(duì)列
NCSDK需要使用mvNCCompile?NCSDK工具編譯的神經(jīng)網(wǎng)絡(luò)圖形文件。支持TensorFlow?*和Caffe的許多網(wǎng)絡(luò)模型蕉毯。有關(guān)準(zhǔn)備與NCSDK一起使用的網(wǎng)絡(luò)模型的詳細(xì)信息乓搬,請(qǐng)參閱為英特爾?Movidius?神經(jīng)計(jì)算SDK配置網(wǎng)絡(luò)思犁。
如果有已編譯的圖形,請(qǐng)將圖形文件數(shù)據(jù)加載到緩沖區(qū)进肯。
# Load graph file data
GRAPH_FILEPATH = './graph'
with open(GRAPH_FILEPATH, mode='rb') as f:
? ? graph_buffer = f.read()
該圖形類(lèi)提供了利用您的網(wǎng)絡(luò)圖的方法激蹲。
使用名稱(chēng)字符串初始化Graph。名稱(chēng)字符串可以是您最喜歡的mvncapi.MAX_NAME_SIZE字符江掩,也可以是空字符串学辱。
# Initialize a Graph object
graph = mvncapi.Graph('graph1')
圖形輸入和輸出由FIFO(先進(jìn)先出)隊(duì)列完成。該先進(jìn)先出類(lèi)表示這些隊(duì)列之一环形,并提供用于管理它的方法策泣。
為Graph創(chuàng)建輸入和輸出Fifo隊(duì)列,并使用Graph.allocate_with_fifos()將圖形分配給您的設(shè)備抬吟。您可以省略關(guān)鍵字參數(shù)以使用默認(rèn)的Fifo設(shè)置萨咕,也可以根據(jù)需要指定其他值。
# Allocate the graph to the device and create input and output Fifos with default arguments
input_fifo, output_fifo = graph.allocate_with_fifos(device, graph_file_buffer)
# Allocate the graph to the device and create input and output Fifos with keyword arguments (default values shown)
input_fifo, output_fifo = graph.allocate_with_fifos(device, graph_file_buffer,
? ? ? ? input_fifo_type=mvncapi.FifoType.HOST_WO, input_fifo_data_type=mvncapi.FifoDataType.FP32, input_fifo_num_elem=2,
? ? ? ? output_fifo_type=mvncapi.FifoType.HOST_RO, output_fifo_data_type=mvncapi.FifoDataType.FP32, output_fifo_num_elem=2)
可選參數(shù):
input_fifo_type / output_fifo_type:設(shè)置Fifo的讀/寫(xiě)訪問(wèn)權(quán)限火本。input_fifo將用于為您的網(wǎng)絡(luò)圖提供輸入危队,并且應(yīng)該是HOST_WO(只寫(xiě))FifoType,它允許API(“HOST”)寫(xiě)入Fifo钙畔。output_fifo將用于從網(wǎng)絡(luò)圖中獲取輸出茫陆,并且應(yīng)該是HOST_RO(只讀)FifoType,它允許API從Fifo讀取擎析。
input_fifo_data_type / output_fifo_data_type:設(shè)置Fifo將存儲(chǔ)的數(shù)據(jù)類(lèi)型簿盅。Fifos?的默認(rèn)數(shù)據(jù)類(lèi)型是32位浮點(diǎn)(32FP)。您還可以將數(shù)據(jù)類(lèi)型設(shè)置為16位浮點(diǎn)(16FP)叔锐。注意:無(wú)論配置輸入和輸出Fifos的數(shù)據(jù)類(lèi)型如何,API都會(huì)在執(zhí)行推斷時(shí)將張量轉(zhuǎn)換為16FP见秽。
input_fifo_num_elem / output_fifo / num_elem:設(shè)置Fifo隊(duì)列的大小愉烙,或每個(gè)Fifo將保留的最大元素?cái)?shù)。選擇一個(gè)對(duì)您的應(yīng)用程序流和內(nèi)存限制有意義的數(shù)字解取。另外請(qǐng)記住步责,如果輸入Fifo已滿(mǎn),寫(xiě)入輸入Fifo的方法將被阻止禀苦,如果輸出Fifo為空蔓肯,則從輸出Fifo讀取的方法將被阻止。
有關(guān)Fifo創(chuàng)建和分配的更多信息振乏,請(qǐng)參閱Fifo類(lèi)文檔蔗包,以便更好地控制Fifo設(shè)置。
對(duì)于GraphOption枚舉中的選項(xiàng)慧邮,您可以使用Graph.get_option()獲取有關(guān)Graph的信息调限。您可以使用Fifo.get_option()和Fifo.set_option()獲取有關(guān)Fifo的信息舟陆,以獲取FifoOption枚舉中的選項(xiàng)。
*注意:您必須為要使用的每個(gè)網(wǎng)絡(luò)圖形文件創(chuàng)建和分配圖形耻矮。一個(gè)設(shè)備可以有一個(gè)以上的圖形分配給它秦躯,但每個(gè)圖形只能被分配到一個(gè)單一的設(shè)備。
4.1.4獲得輸入張量
獲取和預(yù)處理輸入張量的方式取決于個(gè)人應(yīng)用程序裆装。如果使用的是Python3踱承,則cv2模塊提供了一種從文件或相機(jī)源加載圖像的簡(jiǎn)便方法。
下面是一個(gè)使用cv2模塊從文件中讀取圖像并調(diào)整其大小以滿(mǎn)足網(wǎng)絡(luò)要求的示例哨免。但是茎活,可能需要對(duì)正在使用的網(wǎng)絡(luò)模型以及要加載的圖像進(jìn)行額外的預(yù)處理。
import cv2
# Read an image from file
tensor = cv2.imread('img.jpg')
# Do pre-processing specific to this network model (resizing, subtracting network means, etc.)
你也可以使用numpy來(lái)操縱張量铁瞒。
import numpy
# Convert an input tensor to 32FP data type
tensor = tensor.astype(numpy.float32)
輸入張量數(shù)據(jù)必須是輸入Fifo的RW_DATA_TYPE選項(xiàng)指定的數(shù)據(jù)類(lèi)型妙色。默認(rèn)值為32位浮點(diǎn),但Fifos也可配置為存儲(chǔ)16位浮點(diǎn)數(shù)據(jù)慧耍。請(qǐng)參見(jiàn)FifoDataType枚舉身辨。張量數(shù)據(jù)應(yīng)存儲(chǔ)在numpy ndarray中。
4.1.5進(jìn)行推理
使用Graph.queue_inference_with_fifo_elem()將輸入張量寫(xiě)入輸入Fifo并將其排隊(duì)以進(jìn)行推理芍碧。推理完成后煌珊,輸入張量將從input_fifo隊(duì)列中刪除,結(jié)果張量將放在output_fifo隊(duì)列中泌豆。第三個(gè)參數(shù)必須為None定庵。第四個(gè)參數(shù)可以是您在讀取推理結(jié)果時(shí)希望與此特定張量關(guān)聯(lián)的任何對(duì)象,例如原始張量或窗口句柄踪危,或者無(wú)蔬浙。
# Write the tensor to the input_fifo and queue an inference
graph.queue_inference_with_fifo_elem(input_fifo, output_fifo, tensor, 'user object')
如果輸入的Fifo已滿(mǎn),則此方法調(diào)用將阻塞贞远,直到有空間寫(xiě)入Fifo畴博。您可以查看有多少元素的輸入和輸出的FIFO與Fifo.get_option()用于RO_WRITE_FILL_LEVEL和RO_READ_FILL_LEVEL,分別蓝仲。請(qǐng)注意俱病,推斷將需要一些時(shí)間才能完成,具體取決于網(wǎng)絡(luò)模型速度和設(shè)備通信延遲袱结,因此您可能需要等待以查看更新的級(jí)別亮隙。
推理完成后,您可以使用Fifo.read_elem()獲取推理結(jié)果垢夹。這也將返回您傳遞給Fifo.write_elem()的用戶(hù)對(duì)象溢吻。
# Get the results from the output queue
output, user_obj = output_fifo.read_elem()
然后,您可以根據(jù)特定網(wǎng)絡(luò)模型使用輸出結(jié)果果元。
4.1.6清理
在關(guān)閉與設(shè)備的通信之前煤裙,使用Graph.destroy()和Fifo.destroy()來(lái)銷(xiāo)毀Graph和Fifo對(duì)象并清理相關(guān)的內(nèi)存掩完。在被摧毀之前,F(xiàn)ifos必須是空的硼砰。然后使用Device.close()關(guān)閉設(shè)備且蓬,使用Device.destroy()來(lái)銷(xiāo)毀Device對(duì)象并清理相關(guān)內(nèi)存。
# Clean up
input_fifo.destroy()
output_fifo.destroy()
graph.destroy()
device.close()
device.destroy()
4.2開(kāi)發(fā)示例:
4.2.1示例1:從圖中獲取輸入張量描述符列表题翰。
import mvnc.mvncapi as mvnc
# Create and open a device and create and allocate a graph...
# Get a list of input TensorDescriptor objects from the graph
inputDescs = graph.get_option(mvnc.GraphOption.RO_INPUT_TENSOR_DESCRIPTORS)
…
4.2.2示例2:為輸入Fifo創(chuàng)建并設(shè)置通道次要主機(jī)張量描述符恶阴。
import mvnc.mvncapi as mvnc
# Create and open a device and create and allocate a graph with fifos...
# The n/c/w/h fields for the RW_HOST_TENSOR_DESCRIPTOR must match the dimensions of the RO_GRAPH_TENSOR_DESCRIPTOR, so start with that
host_tensor_descriptor = fifoIn.get_option(mvnc.FifoOption.RO_GRAPH_TENSOR_DESCRIPTOR)
# Set the tensor data type
host_tensor_descriptor.dataType = mvnc.FifoDataType.FP32.value
# For channel-minor data with FP32 data type, there are 4 bytes between each element of the channel
# The horizontal stride (wStride), vertical stride (hStride), and totalSize can be calculated from there
host_tensor_descriptor.cStride = 4
host_tensor_descriptor.wStride = host_tensor_descriptor.cStride * host_tensor_descriptor.c
host_tensor_descriptor.hStride = host_tensor_descriptor.wStride * host_tensor_descriptor.w
host_tensor_descriptor.totalSize = host_tensor_descriptor.hStride * host_tensor_descriptor.h
# Set the option
fifoIn.set_option(mvnc.FifoOption.RW_HOST_TENSOR_DESCRIPTOR, host_tensor_descriptor)
...
4.2.3示例三:列出設(shè)備序列
from mvnc import mvncapi
device_list = mvncapi.enumerate_devices()
if not device_list:
? ? print("Error - No neural compute devices detected.")
else:
? ? print(len(device_list), "neural compute devices found!")
4.2.4示例四:打印NCAPI版本
from mvnc import mvncapi
# Print the API version
print('NCAPI Version:', mvncapi.global_get_option(mvncapi.GlobalOption.RO_API_VERSION))
4.2.5案例五:全局設(shè)置
from mvnc import mvncapi
# Set the global logging level to debug (full verbosity)
mvncapi.global_set_option(mvncapi.GlobalOption.RW_LOG_LEVEL, mvncapi.LogLevel.DEBUG)
4.2.6案例六:Device類(lèi)的使用
from mvnc import mvncapi
# Get a list of valid device identifiers
device_list = mvncapi.enumerate_devices()
# Create a Device instance for the first device found
device = mvncapi.Device(device_list[0])
# Get the device name and output to console
print(device.get_option(mvncapi.DeviceOption.RO_DEVICE_NAME))
# Open communication with the device
device.open()
#
# Use the device...
#
# Clean up
device.close()
device.destroy()
from mvnc import mvncapi
# Get a list of valid device identifiers
device_list = mvncapi.enumerate_devices()
# Create a Device instance for the first device found
device = mvncapi.Device(device_list[0])
# Open communication with the device
device.open()
#
# Use the device...
#
# Close the device and destroy the device handle
device.close()
device.destroy()
from mvnc import mvncapi
# Get a list of valid device identifiers
device_list = mvncapi.enumerate_devices()
# Create a Device instance for the first device found
device = mvncapi.Device(device_list[0])
# Open communication with the device
device.open()
#
# Use the device...
#
# Close the device and destroy the device handle
device.close()
device.destroy()
from mvnc import mvncapi
# Get a list of valid device identifiers
device_list = mvncapi.enumerate_devices()
# Create a Device instance for the first device found
device = mvncapi.Device(device_list[0])
# Get the device name and output to console
print(device.get_option(mvncapi.DeviceOption.RO_DEVICE_NAME))
# Destroy the device handle
device.destroy()
from mvnc import mvncapi
# Get a list of valid device identifiers
device_list = mvncapi.enumerate_devices()
# Create a Device instance for the first device found
device = mvncapi.Device(device_list[0])
# Open communication with the device
device.open()
#
# Use the device...
#
# Close the device and destroy the device handle
device.close()
device.destroy()
4.2.7案例七:Fifo類(lèi)使用
"""Example Fifo usage with convenience functions."""
from mvnc import mvncapi
#
# Open a Device, create a Graph, and load graph data from file...
#
# Allocate the Graph and create and allocate two associate Fifos for input and output
input_fifo, output_fifo = graph.allocate_with_fifos(device, graph_buffer)
# Write an input tensor to the input Fifo and queue an inference
graph.queue_inference_with_fifo_elem(input_fifo, output_fifo, input_tensor, None, 'tensor1'
# Read the inference result tensor from the output Fifo
result_tensor, user_obj = output_fifo.read_elem()
#
# Do something with the result...
#
# Destroy the Fifos
input_fifo.destroy()
output_fifo.destroy()
#
# Perform other clean up...
#
"""Example Fifo usage without convenience functions."""
from mvnc import mvncapi
#
# Open a Device and allocate a Graph...
#
# Initialize two Fifos for input and output
input_fifo = mvncapi.Fifo('input1', mvncapi.FifoType.HOST_WO)
output_fifo = mvncapi.Fifo('output1', mvncapi.FifoType.HOST_RO)
# Get input and output TensorDescriptors from the graph
input_descriptor = graph.get_option(mvncapi.GraphOption.RO_INPUT_TENSOR_DESCRIPTORS)
output_descriptor = graph.get_option(mvncapi.GraphOption.RO_OUTPUT_TENSOR_DESCRIPTORS)
# Allocate the Fifo buffers
NUM_ELEMENTS = 2
input_fifo.allocate(device, input_descriptor[0], NUM_ELEMENTS)
output_fifo.allocate(device, output_descriptor[0], NUM_ELEMENTS)
# Write an input tensor to the input_fifo
input_fifo.write_elem(input_tensor, 'tensor1')
#
# Queue an inference with Graph.queue_inference()...
#
# Read the inference result tensor from the output Fifo
result_tensor, user_obj = output_fifo.read_elem()
#
# Do something with the result...
#
# Destroy the Fifos
input_fifo.destroy()
output_fifo.destroy()
#
# Perform other clean up...
#
from mvnc import mvncapi
#
# Open a Device and allocate a Graph...
#
# Create an input Fifo
input_fifo = mvncapi.Fifo('input1', mvncapi.FifoType.HOST_WO)
# Get an input TensorDescriptor from a graph
input_descs = graph.get_option(mvncapi.GraphOption.RO_INPUT_TENSOR_DESCRIPTORS)
# Allocate the Fifo buffer with the graph input TensorDescriptor to the device
NUM_ELEMENTS = 2
input_fifo.allocate(device, input_descs[0], NUM_ELEMENTS
#
# Write elements to the Fifo with Fifo.write_elem() and initiate inferences with Graph.queue_inference()
#
# Destroy the Fifo
input_fifo.destroy()
#
# Perform other clean up...
#
4.2.8案例八:Graph類(lèi)使用
from mvnc import mvncapi
#
# Create and open a Device...
# Create a Graph
graph = mvncapi.Graph('graph1')
# Read a compiled network graph from file (set the graph_filepath correctly for your graph file)
graph_filepath = './graph'
with open(graph_filepath, 'rb') as f:
? ? graph_buffer = f.read()
# Allocate the graph on the device and create input and output Fifos
input_fifo, output_fifo = graph.allocate_with_fifos(device, graph_buffer)
#
# Pre-procces your input tensor...
#
# Write the tensor to the input Fifo and queue an inference
graph.queue_inference_with_fifo_elem(input_fifo, output_fifo, input_tensor, None, 'object1')
#
# Read the output from the output_fifo and use it as needed...
#
# Clean up
input_fifo.destroy()
output_fifo.destroy()
graph.destroy()
device.close()
device.destroy()
from mvnc import mvncapi
# Get a list of valid device identifiers
device_list = mvncapi.enumerate_devices()
# Create a Device instance for the first device found
device = mvncapi.Device(device_list[0]
# Open communication with the device
device.open()
# Create a Graph
graph = mvncapi.Graph('graph1')
# Read a compiled network graph from file (set the graph_filepath correctly for your graph file)
graph_filepath = './graph'
with open(graph_filepath, 'rb') as f:
? ? graph_buffer = f.read()
# Allocate the graph on the device
graph.allocate(device, graph_buffer)
#
# Use the device...
#
# Deallocate and destroy the graph handle, close the device, and destroy the device handle
graph.destroy()
device.close()
device.destroy()
from mvnc import mvncapi
# Create a Graph
graph = mvncapi.Graph('graph1')
# Destroy the Graph
graph.destroy()