简体   繁体   中英

Where is the center Pixel of an even filter (6x6) in Matlab (imfilter) and TensorFlow (tf.nn.conv2d)?

For my master thesis i am currently porting a convolution operation from a Matlab Code to TensorFlow (Python API). The exception is that in Matlab an even filter_size was used (6x6).

By debugging the Output of the Matlab and my TensorFlow Code, i recognized that the function imfilter from Matlab acts way different than the tf.nn.conv2d operation in TensorFlow. I get way different mean and std values, same for the min and max values.

I can confirm that both, the values of my weights on Matlab and TF are the same and correct reshaped. The Bias is only a Scalar, so this isn't the problem either.

My presumption is that both functions imfilter (Matlab) and tf.nn.conv2d (TF) don't use the same center pixel for convolution, because the output of an odd kernel size is similar. So for an odd kernel size both functions operate similar.

Here is a snippet of the Matlab Code:

imfilter(input_data(:,:,j), conv_subfilter, 'same', 0, 'conv');

conv_subfilter is a 6x6x48 Filter Kernel and is convolving a Width x Heigth x 48 Image - the result is a Image with depth 1, for both, TF and Matlab

Here a snippet of my TF Code:

h_conv3_ip = convolution2d(max_pool_conv2, W_conv3_ip, b_conv3_ip, [1, 1, 1, 1], 'SAME', "h_conv3_ip")

def convolution2d(x, W, b, strides, padding, name):
    conv2d = tf.nn.conv2d(x, W, strides=strides, padding=padding, name=name) 
    conv2d = tf.add(conv2d, b, name=name) 
    return conv2d

Can someone tell me if the Kernel center is the problem or sth. else? Thanks for help in advance!

I found out that the correct formula for calculating the center pixel is: floor(([mn] +1) /2). I was testing it on a 10x10 patch, which was initialized with ones. The Filter 6x6 was initialized with ones too. In the upper left boarder i got a value of 16, both for the TF and Matlab convolution. So there is no difference regarding the index of the center pixel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM