Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to choose convolution strides dynamically?

Tags:

tensorflow

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.

like image 801
MWB Avatar asked Nov 02 '17 00:11

MWB


1 Answers

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.

like image 120
asakryukin Avatar answered Nov 15 '22 05:11

asakryukin