训练和测试一个有效的机器学习模型最重要的一步是收集大量数据并使用这些数据对其进行有效训练。小批量(Mini-batches)训练是最有效的训练策略,在每次迭代中使用一小部分数据进行训练。
但是,随着大量的机器学习任务在视频数据集上执行,存在着对不等长视频进行有效批处理的问题。大多数解决方法依赖于将视频裁剪成相等的长度,以便在迭代期间提取相同数量的帧,但在我们需要从每一帧获取信息来有效地预测某些事情的场景中,这并不是特别有用的,特别是在自动驾驶汽车和动作识别的情景中。
本文我们通过创建一个可以处理不同长度视频的处理方法来解决该问题。
在Glenn Jocher的Yolov3中(https://github.com/ultralytics/yolov3),我用LoadStreams作为基础,创建了LoadStreamsBatch类。
类初始化
def __init__(self, sources='streams.txt', img_size=416, batch_size=2, subdir_search=False): self.mode = 'images' self.img_size = img_size self.def_img_size = None
videos = [] if os.path.isdir(sources): if subdir_search: for subdir, dirs, files in os.walk(sources): for file in files: if 'video' in magic.from_file(subdir + os.sep + file, mime=True): videos.append(subdir + os.sep + file) else: for elements in os.listdir(sources): if not os.path.isdir(elements) and 'video' in magic.from_file(sources + os.sep + elements, mime=True): videos.append(sources + os.sep + elements) else: with open(sources, 'r') as f: videos = [x.strip() for x in f.read().splitlines() if len(x.strip())]
n = len(videos) curr_batch = 0 self.data = [None] * batch_size self.cap = [None] * batch_size self.sources =