如何将图像序列输入LSTM网络进行视频分类
How to input the sequence of the images in to LSTM network for video classification
我正在使用 LSTM 来 class 化视频。我正在使用 Keras python 库来创建长短期记忆 LSTM 网络。我知道 LSTM 采用(样本、时间戳、特征)中数据的输入形状。我有三个 class 视频,每个 class 都有 10 个视频文件。这意味着我有 10*3=30 个样本。
我为每个视频文件创建了一个帧序列。这些序列中的每一个都包含 32 帧视频文件。我使用经过训练的模型来提取特征,因此我将这些帧中的每一个都输入到 VGG16 预训练模型中,它为单个帧生成 512 个特征。所以一个视频文件应该有一个 (32,512) 维度的数组。然后,我将所有 30 个样本的每个数组附加到一个数组中,并将其保存为 numpy 数组。数组的最终维度是 (960,512)。
现在我的问题是我应该如何将这个数组重塑为(样本、时间戳、特征)=(20,32,512)。这是我使用的代码片段:请注意 x_generator 有 640,512,我希望将其转换为 (30,32,512)。
如果能解决我的问题,我将不胜感激。
x_generator=None
if x_generator is None:
imgx = image.img_to_array(img)
imgx = np.expand_dims(imgx, axis=0)
imgx = preprocess_input(imgx)
x_generator = base_model.predict(imgx)
else
imgx = image.img_to_array(img)
imgx = np.expand_dims(imgx, axis=0)
imgx = preprocess_input(imgx)
x_generator = np.append(x_generator,base_model.predict(imgx),axis=0)
如果您通过追加维度 (32, 512) 的 30 个样本得到 960 个值,您可以只使用 np.reshape 以预期维度重塑数组。
x_generator = np.reshape(x_generator, [30, 32, 512])
我正在使用 LSTM 来 class 化视频。我正在使用 Keras python 库来创建长短期记忆 LSTM 网络。我知道 LSTM 采用(样本、时间戳、特征)中数据的输入形状。我有三个 class 视频,每个 class 都有 10 个视频文件。这意味着我有 10*3=30 个样本。 我为每个视频文件创建了一个帧序列。这些序列中的每一个都包含 32 帧视频文件。我使用经过训练的模型来提取特征,因此我将这些帧中的每一个都输入到 VGG16 预训练模型中,它为单个帧生成 512 个特征。所以一个视频文件应该有一个 (32,512) 维度的数组。然后,我将所有 30 个样本的每个数组附加到一个数组中,并将其保存为 numpy 数组。数组的最终维度是 (960,512)。 现在我的问题是我应该如何将这个数组重塑为(样本、时间戳、特征)=(20,32,512)。这是我使用的代码片段:请注意 x_generator 有 640,512,我希望将其转换为 (30,32,512)。 如果能解决我的问题,我将不胜感激。
x_generator=None
if x_generator is None:
imgx = image.img_to_array(img)
imgx = np.expand_dims(imgx, axis=0)
imgx = preprocess_input(imgx)
x_generator = base_model.predict(imgx)
else
imgx = image.img_to_array(img)
imgx = np.expand_dims(imgx, axis=0)
imgx = preprocess_input(imgx)
x_generator = np.append(x_generator,base_model.predict(imgx),axis=0)
如果您通过追加维度 (32, 512) 的 30 个样本得到 960 个值,您可以只使用 np.reshape 以预期维度重塑数组。
x_generator = np.reshape(x_generator, [30, 32, 512])