How to choose convolution strides dynamically?
Using placeholders doesn't seem to work:
s = tf.placeholder(np.int32)
image = tf.placeholder(np.float32, [None, 3, 32, 32])
tf.layers.conv2d(image,
filters=32,
kernel_size=[3, 3],
strides=[s, s],
padding='same',
data_format='channels_first')
This gives a TypeError
.
Similar difficulties arise with pool_size
and strides
when doing pooling.
Unfortunately Tensorflow doesn't allow passing Tensors to the definition of conv2d
. The approach I used was basically run the conv2d
with strides of 1 and then slice the result with required strides. Not the optimal approach probably, but it works and tf.strided_slice
can use tensors. So in your case it would be something like:
s = tf.placeholder(np.int32,[4])
image = tf.placeholder(np.float32, [None, 3, 32, 32])
convoluted = tf.layers.conv2d(image,
filters=32,
kernel_size=[3, 3],
strides=[1,1],
padding='same',
data_format='channels_first')
result = tf.strided_slice(convoluted,
[0,0,0,0],
tf.shape(convoluted),
s)
Then you can pass 4 stride sizes to s during the run, where each entry corresponds to the stride in respective dimension of the convoluted input.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With