我目前正在制作视频字幕(自然语言的帧序列)。
我最近开始在Tensorflow中使用tf.data.Dataset类而不是feed_dict参数。
我的目标是将这些帧馈送到预先训练的CNN(inceptionv3),提取特征向量,然后将其馈送到我的RNN seq2seq网络。
在将数据集与初始模型映射后,我遇到了张量流类型的问题:然后,无论是通过dataset.batch()还是dataset.take(),数据集都是完全不可用的。我什至不能做一个单发的迭代器!
这是我继续构建数据集的方法:
步骤1:我首先为每个视频提取相同数量的帧。我将所有这些存储到numpy数组中。其形状为(nb_videos,nb_frames,宽度,高度,通道)
请注意,在此数据集中,每个视频具有相同的大小并具有3个颜色通道。
步骤2:然后,我使用这个大的numpy数组创建一个tf.data.Dataset对象
请注意,通过python打印此数据集可得到:
使用n_videos = 2;宽度= 240;高度= 320;频道= 3
我已经不明白“ DataAdapter”代表什么
在此刻;我可以创建一个一次性迭代器,但是使用dataset.batch(1)返回:
我不明白为什么“?”而不是“ 1”形。
步骤3:我使用数据集上的map函数将所有视频的所有帧调整为299 * 299 * 3(必须使用InceptionV3)
在这一点上,我可以使用数据集中的数据并进行一次迭代。
步骤4:我再次使用map函数,使用InceptionV3预训练模型提取每个特征。
此时发生问题:
打印数据集将得出:
还行吧
但是,现在不可能为此数据集创建一个一次性迭代器
步骤1 :
X_train_slice, Y_train = build_dataset(number_of_samples)
第2步:
X_train = tf.data.Dataset.from_tensor_slices(X_train_slice)
第三步:
def format_video(video):
frames = tf.image.resize_images(video, (299,299))
frames = tf.keras.applications.inception_v3.preprocess_input(frames)
return frames
X_train = X_train.map(lambda video: format_video(video))
步骤4:
初始模型:
image_model = tf.keras.applications.InceptionV3(include_top=False,
weights='imagenet')
new_input = image_model.input
hidden_layer = image_model.layers[-1].output
image_features_extract_model = tf.keras.Model(new_input, hidden_layer)
对于tf.reduce_mean;参见how-to-get-pool3-features-of-inception-v3-model-using-keras(SO)
def extract_video_features(video):
batch_features = image_features_extract_model(video)
batch_features = tf.reduce_mean(batch_features, axis=(1, 2))
return batch_features
X_train = X_train.map(lambda video: extract_video_features(video))
创建迭代器:
iterator = X_train.make_one_shot_iterator()
这是输出:
ValueError: Failed to create a one-shot iterator for a dataset.
`Dataset.make_one_shot_iterator()` does not support datasets that capture
stateful objects, such as a `Variable` or `LookupTable`. In these cases, use
`Dataset.make_initializable_iterator()`. (Original error: Cannot capture a
stateful node (name:conv2d/kernel, type:VarHandleOp) by value.)
我不太了解:它要求我使用Initializable_iterator,但是这种迭代器专用于占位符。在这里,我有原始数据!
最佳答案
您使用的管道错误。tf.data
的想法是为模型提供输入管道,而不是包含模型本身。您正在尝试做的事情适合模型作为流水线的一个步骤(您的步骤4),但是,如错误所示,这是行不通的。
相反,您应该做的是按照自己的方式构建模型,然后在输入数据上调用model.predict
,以获得所需的功能(作为计算值)。如果要添加进一步的计算,请将其添加到模型中,因为predict
调用将运行模型并返回输出层的值。
旁注:image_features_extract_model = tf.keras.Model(new_input, hidden_layer)
完全无关紧要,因为您已选择输入和输出张量:输入是image_model
的输入,而输出是image_model
的输出,因此image_features_extract_model
等同于。
最终代码应为:
X_train_slice, Y_train = build_dataset(number_of_samples)
X_train = tf.data.Dataset.from_tensor_slices(X_train_slice)
def format_video(video):
frames = tf.image.resize_images(video, (299,299))
frames = tf.keras.applications.inception_v3.preprocess_input(frames)
return frames
X_train = X_train.map(lambda video: format_video(video))
image_model = tf.keras.applications.InceptionV3(include_top=False,
weights='imagenet')
bottlenecks = image_model.predict(X_train)
# Do something with your bottlenecks
关于tensorflow - 为什么在使用InceptionV3之后不能再使用数据集?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57304811/