Caffe CPU convolution
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.
@SuperSoulHunter the sum the values of Filter Weight is -0.772, I don't think there is a bug.
I agree, I think -0.772 is the correct output for the above convolution (no bias).
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.
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.
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.
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.)