代码之家  ›  专栏  ›  技术社区  ›  Rylan Schaeffer

如何通过tensorflow conv2d提供成批图像序列

  •  4
  • Rylan Schaeffer  · 技术社区  · 6 年前

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

    我有成批的形状图像序列:

    [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_fn 过轴=1,但我发现 this function does not propagate gradients

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

    1 回复  |  直到 6 年前
        1
  •  4
  •   Vijay Mariappan    6 年前

    因为所有的图像( 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])
    

    x_整形输出大小将为 (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])
    

    输出大小将与输入相同: (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)