2016-07-08 54 views
1

所以我玩弄pycaffe的卷积函数作为基本卷积层的一部分。这是我的convolution.prototxt文件:Caffe的卷积真的如何工作?

name: "convolution" 
input: "data" 
input_dim: 1 
input_dim: 1 
input_dim: 227 
input_dim: 227 

layer { 
    name: "conv" 
    type: "Convolution" 
    bottom: "data" 
    top: "conv" 
    convolution_param { 
    num_output: 96 
    kernel_size: 11 
    stride: 1 
    } 
} 

这些参数都是相同的AlexNet的第一CONV层(除步幅,这实际上是4)。

我有一台配备NVIDIA GeForce GT 650M 1024 MB GPU的Macbook Pro。我不确定这意味着什么,但我的笔记本电脑也有一个Intel HD 4000作为内置GPU。

我在笔记本电脑上进行了一些测试,同时改变了跨度超参数,首先是在GPU模式下,然后是CPU。调用caffe.set_device(0); caffe.set_mode_gpu()

1)变的进步:

Stride 1: 27.26 ms 
Stride 2: 14.27 ms 
Stride 3: 10.57 ms 
Stride 4: 7.45 ms 

2)变的进步呼唤caffe.set_mode_cpu()后:

Stride 1: 49.77 ms # expected 
Stride 2: 9.92 ms # this and the results after this don't make sense 
Stride 3: 4.50 ms 
Stride 4: 1.96 ms 

(平均3)

我只是试图根据这些测试来了解Caffe的卷积是如何工作的。任何人都可以帮我解释这一点吗?为什么CPU模式比GPU模式执行得更快?


测试代码中,我使用的,如果你在看到自己感兴趣:

import numpy as np 
import caffe 
import time 

caffe.set_device(0) 
caffe.set_mode_gpu() # caffe.set_mode_cpu() 

net = caffe.Net('convolution.prototxt', caffe.TEST) 
total = 0.0 
for _ in range(3): 
    net.blobs['data'].data[...] = np.random.randn(1, 1, 227, 227) # there really is an ellipsis there 
    net.params['conv'][0].data[...] = np.random.randn(96, 1, 11, 11) 
    s = time.time() 
    r = net.forward() 
    e = time.time() 
    total += (e - s) 

print total/3 * 1000 

回答

1

所以,周围挖掘后,我发现,来自Caffe基本上使用额外的内存拉平局部地区,然后使用level3 BLAS例程(特别是cblas_sgemm)来执行矩阵乘法以得到结果。这导致以额外内存为代价的快速计算。

参考文献可以找到herehere

使用GPU的内存操作通常比CPU更昂贵。所有额外的内存使用可能是在GPU模式下运行时遇到的减速的可能解释。这也将取决于GPU的规格本身。