caffe icon indicating copy to clipboard operation
caffe copied to clipboard

Caffe CPU convolution

Open linnanwang opened this issue 11 years ago • 6 comments

Hi there,

I'm trying the LeNet example and I modified a few places to understand the inner working of Caffe. It comes to my attention that the CPU convolution layer behaves different from valid conv2 in matlab.

Here is an example, (I populate the bottom blob with all the 1s) and convolve with the kernel:

Input Blob: 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000

Filter Weight -0.038 -0.231 -0.077 0.259 0.079 -0.332 -0.153 0.216 -0.169 -0.019 0.345 -0.240 -0.023 -0.026 -0.311 0.106 -0.195 -0.254 0.263 0.081 0.074 0.257 0.136 -0.287 -0.233

Result Feature Map: -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773 -0.773

I'm aware of that there is a biased term to be added after the gemm convolution, however, all the information is printed out immediately after forward_cpu_gemm by a print function added by me in the blob.hpp

The results is weird and the correct output should be around 0.5030 (produced by matlab).

Can anyone kindly explain the inner working of cpu_gemm_conv? Or is it potentially a bug? Thanks.

linnanwang avatar May 26 '15 20:05 linnanwang

@SuperSoulHunter the sum the values of Filter Weight is -0.772, I don't think there is a bug.

Hsinfu avatar May 26 '15 21:05 Hsinfu

I agree, I think -0.772 is the correct output for the above convolution (no bias).

seanbell avatar May 27 '15 01:05 seanbell

Thank you for the reply, after sum up the kernel, it indeed = -0.772~ It probably I made some mistakes at the MATLAB side, I'm sorry for asking this dumb question.

linnanwang avatar May 27 '15 01:05 linnanwang

To those who are doing the same validation process, just be aware that we need flip the filter kernel horizontally and vertically before calling the conv2(valid). The results should conform to the caffe convolution.

linnanwang avatar May 27 '15 01:05 linnanwang

Yes, I think caffe is technically doing a cross-correlation (in the sense that the filter is not flipped with respect to the direction it moves along the image). But alternatively you could just consider the weights to be "pre-flipped" in memory for efficiency.

seanbell avatar May 27 '15 04:05 seanbell

Closing as this seems resolved. Yes, Caffe convolution is really correlation, which is a fairly standard if inconsistent usage in this domain. (Perhaps it should be noted more carefully somewhere.)

longjon avatar Jun 07 '15 07:06 longjon