我正在尝试构建一个数据管道来推断某些视频文件。视频数量很多,因此,我使用 tf.data.dataset 管道和 from_generator 方法来创建可管理的批次。但数据集不断再次产生相同的输出。
这是我的代码:
enteclass FrameGenerator:
def __init__(self, video_paths, n_frames, training=False):
"""
Returns a set of frames from a video in the video_paths list
Args:
video_paths: a list of path to videos
n_frames: Number of frames
training: A boolean to determine if a training dataset should be created
"""
self.video_paths = video_paths
self.n_frames = n_frames
self.training = training
def __call__(self):
"""
gets called and yields a set of frames
each time the instance of the class is called
"""
video_paths = self.video_paths
# print(type(video_paths))
if self.training:
random.shuffle(video_paths)
for path in video_paths:
video_frames = frames_from_video_file(path, self.n_frames)
file_name = path.split('/')[-1]
# print(file_name, "filfile")
yield video_frames, file_namer code here
代码在这里实例化:
output_signature = (tf.TensorSpec(shape = (None, None, None, 3), dtype=tf.float32), tf.TensorSpec(shape = None, dtype=tf.string))
dataset = tf.data.Dataset.from_generator(FrameGenerator(video_paths, 20, training=False), output_signature=output_signature)
我的批量大小为 10 以及数据集的一个实例:
inference_datasets = dataset.batch(BATCH_SIZE)
但是当我这样称呼时:
sample_inference_dataset = next(iter(inference_datasets))
还有这个:
enter codesample_inference_dataset[1]
它会生成相同的文件集。
我看到的第一个问题是如何一起使用iter和next。您正在创建迭代器的新实例,然后使用 next() 来获取第一个元素,因此您将始终获得第一个元素。您需要将迭代器存储在变量上,然后在不同的行上使用 next() 来返回迭代器上的下一个项目