前言
Mobilenet SSD學習系列一(https://blog.csdn.net/ltshan139/article/details/101064590)說過,caffe_ssd架構不支援depthwise convolution的實作,隻能用convolution來近似代替,是以會大大增加Mobilenet-SSD模型的推理運作。
Depthwise Convolution的gpu實作
下面來講如何使得caffe-ssd架構支援Depthwise Convolution的cpu及gpu實作。
1)下載下傳代碼
git clone https://github.com/yonghenglh6/DepthwiseConvolution.git depthwise_conv
2)将上面這個開源項目的depthconv實作代碼(*.cpp, *.cu 以及*.hpp)拷貝到caffe-ssd對應目錄下
cd depthwise_conv
sudo cp -f caffe/src/caffe/layers/depthwise_conv_layer.c* /work/xxx/caffe_ssd/src/caffe/layers/
sudo cp -f caffe/include/caffe/layers/depthwise_conv_layer.hpp /work/xxx/caffe_ssd/include/caffe/layers/
3)重新對caffe-ssd架構代碼進行編譯:
sudo make clean
sudo make -j8
sudo make pycaffe -j8
使得caffe-ssd真正支援depthwise convolution layer的支援。
4)最後一步是 修改mobilenet-SSD下面的deploy.prototxt
sudo vi voc/MobileNetSSD_deploy.prototxt
a)把13個“engine: CAFFE” 重新都注釋掉
b)conv1/dw到conv13/dw 的type從”Convolution”替換成”DepthwiseConvolution”,總共也有13個地方。
下面以conv1/dw為例來看修改内容如紅框所示。

效果驗證
首先在demo.py的detect函數中添加time子產品如下圖所示。
在gpu模式下,使用原始的deploy.prototxt,即用convlution來近似代替depthwise convolution,其檢測測試圖檔(./images) 所花時間如下所示:
time=0.0463089942932s
time=0.0358200073242s
time=0.0369169712067s
time=0.0367810726166s
time=0.0381460189819s
time=0.037379026413s
time=0.0422718524933s
而基于update的deploy.prototxt,即真正使用depthwise convolution layer的實作後,同樣測試圖檔識别 所消耗時間如下所示。
time=0.036953878403s
time=0.00506377220154s
time=0.00571703910828s
time=0.0045220661163s
time=0.0079579162598s
time=0.0368349552155s
time=0.0831339359283s
可以看出 後者方式所化時間大為減少。