【问题标题】:How to connect convolution layer with lstm layer to in seq2seq tasks?如何将卷积层与 lstm 层连接到 seq2seq 任务中?
【发布时间】:2019-08-07 19:39:28
【问题描述】:

seq2seq 任务是从视频数据中识别句子(也称为纯视觉语音识别/唇读)。

该模型由卷积层和一个 lstm 层组成。但是卷积层的输出是[batch_size, height, width, channel_size]的形状;而lstm层的输入必须是[batch_size, n_steps, dimension]的形状。

工作流程是这样的:

  • 首先,数据组织为 [batch_size, n_steps, height, width, channel_size]。
  • 然后我将其重塑为 [batch_size*n_steps, height, width, channel_size] 并将其输入转换层。
  • conv 层的输出是[batch_size*n_steps, height', width', channel_size']
  • 当然可以reshape成[batch_size, n_steps, height', width', channel_size'],但是我要怎么输入到lstm层,需要[batch_size, n_steps, dimension]形状的数据

我不知道仅将轴 [height', width', channel_size'] 重塑为 [dimension] 的一个轴是否适合此仅视觉语音识别任务。

提示:

【问题讨论】:

    标签: tensorflow conv-neural-network lstm seq2seq


    【解决方案1】:

    RNN 期望输入是连续的。因此,输入的形状为[time, feature_size],或者如果您正在处理批处理[batch_size, time, feature_size]

    在您的情况下,输入的形状为[batch_size, number_of_frames, height, width, num_channels]。然后,您使用卷积层来学习每个视频帧中像素之间的空间依赖性。因此,对于每个视频帧,卷积层将为您提供一个形状为[activation_map_width, activation_map_height, number_of_filters] 的张量。然后,因为您想学习帧的上下文相关表示,所以您可以安全地重塑您为一维序列中的每个帧学习的所有内容。

    最后,你要提供的 RNN 是:[b_size, num_frames, am_width * am_height * num_filters]

    关于实现,如果我们假设您有 2 个视频,每个视频有 5 帧,其中每个帧的宽度和高度为 10 和 3 个通道,您应该这样做:

    # Batch of 2 videos with 7 frames of size [10, 10, 3]
    video = np.random.rand(2, 7, 10, 10, 3).astype(np.float32)
    # Flattening all the frames
    video_flat = tf.reshape(video, [14, 10, 10, 3])
    # Convolving each frame
    video_convolved = tf.layers.conv2d(video_flat, 5, [3,3])
    # Reshaping the frames back into the corresponding batches
    video_batch = tf.reshape(video_convolved, [2, 7, video_convolved.shape[1], video_convolved.shape[2], 5])
    # Combining all learned for each frame in 1D
    video_flat_frame = tf.reshape(video_batch, [2, 7, video_batch.shape[2] * video_batch.shape[3] * 5])
    # Passing the information for each frame through an RNN
    outputs, _ = tf.nn.dynamic_rnn(tf.nn.rnn_cell.LSTMCell(9), video_flat_frame, dtype=tf.float32)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        # Output where we have a context-dependent representation for each video frame
        print(sess.run(outputs).shape)
    

    请注意,为了简单起见,我在代码中对一些变量进行了硬编码。

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-13
      • 2015-05-07
      • 1970-01-01
      • 2018-12-22
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多