I have an existing classification model that was trained using theano's conv2d under theano.tensor.nnet. Now I have to use this model to do some sort of prediction in Java.
I implement a simple convolution in Python(In the end, I will code it in Java) as per some documentation(https://developer.apple.com/Library/ios/documentation/Performance/Conceptual/vImage/ConvolutionOperations/ConvolutionOperations.html). For example, for a 2*2 kernel (k11,k12,k21,k22), one of the areas under the kernel is (a11,a12,a21,a22). The convolution is performed by a11*k11 + a12*k12 + a21*k21 + a22*k22.
Unfortunately, as I test my convolution code and theano's conv code with some dummy matrix and kernels, they give different results. Only in some rare cases, they give same results.
It seems to me that there are many variants of convolution algorithm and I have to implement the exact same convolution algorithm as used by theano's ConvOp. However, I can't find any material describing theano's Conv2d algorithm.
Could you explain a little bit about theano's conv2d algorithm?
The following is my python code for convolution:
def convolution(img_arr, kernel):
h, w = img_arr.shape
k = len(kernel)
k_flat = kernel.flatten()
neww = w - len(kernel) + 1
newh = h - len(kernel) + 1
after = np.zeros((newh, neww))
for i in range(neww):
for j in range(newh):
after[j,i] = (img_arr[j:j+k,i:i+k].flatten()*k_flat).sum()
return after
Theano convolution does exactly the same thing as
scipy.signal.convolve2d
. This is exploited/tested e.g. here. For self-containedness, try copy+pasting:It outputs
Since scipy implements a standard convolution, so does theano.
Actually you and the (Theano, Scripy) are all right. the reason is that: your are using the different convolution2D. the Theano and Script using the convolution2D defined in Math that should rotate the kernel. but you did not (ref: http://www.songho.ca/dsp/convolution/convolution.html#convolution_2d). SO if your kernel is like this:
than change it to (center symmetry):
So using your method will get the same answer as Theano/Scripy