如何通过 Tensorflow conv2d 提供批量图像序列

Ryl*_*fer 4 tensorflow recurrent-neural-network convolutional-neural-network

这似乎是一个微不足道的问题,但我一直无法找到答案。

我已经批量处理了一系列形状的图像:

[batch_size, number_of_frames, frame_height, frame_width, number_of_channels]

我想通过几个卷积层和池化层来传递每一帧。然而,TensorFlow 的conv2d层接受形状的 4D 输入:

[batch_size, frame_height, frame_width, number_of_channels]

我的第一次尝试是使用tf.map_fnoveraxis=1,但我发现这个函数不会传播梯度

我的第二次尝试是tf.unstack在第一个维度上使用,然后使用tf.while_loop. 但是, mybatch_sizenumber_of_frames是动态确定的(即两者都是None),如果未指定则tf.unstack引发。我尝试指定,但这会引发{ValueError} Cannot infer num from shape (?, ?, 30, 30, 3)numnum=tf.shape(self.observations)[1]{TypeError} Expected int for argument 'num' not <tf.Tensor 'A2C/infer/strided_slice:0' shape=() dtype=int32>.

vij*_*y m 5

由于所有图像 ( num_of_frames) 都传递到同一个卷积模型,因此您可以将批次和帧堆叠在一起并进行普通卷积。只需使用tf.resize如下图即可实现:


# input with size [batch_size, frame_height, frame_width, number_of_channels
x = tf.placeholder(tf.float32,[None, None,32,32,3])

# reshape for the conv input
x_reshapped = tf.reshape(x,[-1, 32, 32, 3])
Run Code Online (Sandbox Code Playgroud)

x_reshaped 输出大小将是 (50, 32, 32, 3)

# define your conv network
y = tf.layers.conv2d(x_reshapped,5,kernel_size=(3,3),padding='SAME')
#(50, 32, 32, 3)

#Get back the input shape
out = tf.reshape(x,[-1, tf.shape(x)[1], 32, 32, 3])
Run Code Online (Sandbox Code Playgroud)

输出大小将与输入相同: (10, 5, 32, 32, 3

with tf.Session() as sess:
   sess.run(tf.global_variables_initializer())

   print(sess.run(out, {x:np.random.normal(size=(10,5,32,32,3))}).shape)
   #(10, 5, 32, 32, 3) 
Run Code Online (Sandbox Code Playgroud)