我正在使用 Mydia从视频中提取随机帧。因为我有很多视频,所以我想在保持可重复性的同时并行执行此工作流程。 mydia.Videos
接受随机种子,这对于确保可重复性很重要。现在我需要处理并行化部分。
给定 n
个视频和一个随机种子 r
,无论工作人员的数量如何,我如何确保每个视频提取的帧都是相同的?我对算法组件特别感兴趣,不一定是代码。
我最初的想法是使用multiprocessing.Pool
。但是,如果进程的完成时间不确定,那么在对帧进行采样时就会出现竞争条件;即,如果 proc 1 花费的时间比 proc 0 长,则来自 Videos
类的采样帧将与 proc 0 花费的时间长于 proc 1 时不同。
最佳答案
我的解决方案有点非正统,因为它是特定于库的。 Mydia 允许传递要提取的帧,而不是强制 Videos
客户端直接采样。这使我有机会预先计算要在父进程中采样的帧。通过这样做,我可以通过用这些帧实例化一个新的 Videos
来“模拟”子进程中的随机性。例如:
class MySampler:
def __init__(self, input_directory: Path, total_frames: int, num_frames: int, fps: int):
self.input_directory = Path(input_directory)
self.frames_per_video = [
self.__get_frame_numbers_for_each_video(total_frames, num_frames, fps)
for _ in self.input_directory.glob("*.mp4")
]
@staticmethod
def get_reader(num_frames: int, frames: List[int]):
# ignores the inputs and returns samples the frames that its constructed with
return Videos(target_size=(512, 512), num_frames=num_frames, mode=lambda *_: frames)
然后我可以简单地将其并行化:
def sample_frames(self, number_of_workers: int):
pool = Pool(processes=number_of_workers)
videos = list(self.input_directory.glob("*.mp4"))
pool.starmap_async(self.read_video, zip(self.frames_per_video, videos))
pool.close()
pool.join()
其中 read_video
是调用 get_reader
并进行读取的方法。
关于python-3.x - 如何在并行化随机种子实验时确保可重复性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52709049/