2

I want to perform a convolution over an image, in tensorflow. I want the kernel to be as tall as the image and very thin. For example:

kernel_size = [200, 24]
image_size = [200, 400]

If I use padding "SAME", instead of getting a vector out, I get a [200, 400] image back, since tensorflow pads the image at the top and bottom and convolves with the kernel over the padded image.

If, on the other hand, I use padding "VALID", the problem for the top and bottom disappears, but it also does not fully cover the horizontal direction of my image, such that, if the horizontal dimension of the image is not divisible by the kernel dimension, you lose a part of it.

Is there a way to perform "VALID" padding at the top and bottom and "SAME" padding left and right? Or is there another way of doing this?

2 Answers2

1

With the default padding options of tensorflow's convolution functions there is no way to do this, you will have to pad your tensor manually to get your horizontal dimension to be divisible by the kernel dimension.

After padding manually use VALID padding in order not to have the pixels of the manual padding be considered to be anything other than padding

To pad your tensor manualy you could use tf.concat with a constant tensor with shape and value you want. If your images are always the same size this will not be difficult to figure out.

Miguel Monteiro
  • 389
  • 1
  • 2
  • 16
1

For any non-standard padding, TF has a custom padding operation: tf.pad(). So just figure out what is the appropriate padding and call something like

# 't' is [[1, 2, 3], [4, 5, 6]].
# 'paddings' is [[1, 1,], [2, 2]].
# rank of 't' is 2.
pad(t, paddings, "CONSTANT") ==> [[0, 0, 0, 0, 0, 0, 0],
                                  [0, 0, 1, 2, 3, 0, 0],
                                  [0, 0, 4, 5, 6, 0, 0],
                                  [0, 0, 0, 0, 0, 0, 0]]
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753