9

TLDR; 我的问题是如何从 TFRecords 加载压缩视频帧。

我正在建立一个数据管道,用于在大型视频数据集 ( Kinetics ) 上训练深度学习模型。为此,我正在使用 TensorFlow,更具体地说是tf.data.DatasetTFRecordDataset结构。由于数据集包含约 30 万个 10 秒的视频,因此需要处理大量数据。在训练期间,我想从视频中随机采样 64 个连续帧,因此快速随机采样很重要。为了实现这一点,在训练期间可能会出现许多数据加载场景:

  1. 来自视频的样本。ffmpeg使用orOpenCV和示例帧加载视频。不理想,因为在视频中搜索很棘手,并且解码视频流比解码 JPG 慢得多。
  2. JPG 图片。通过将所有视频帧提取为 JPG 来预处理数据集。这会生成大量文件,由于随机访问,这可能不会很快。
  3. 数据容器。将数据集预处理为TFRecordsHDF5文件。需要做更多的工作来准备好管道,但很可能是这些选项中最快的。

我决定选择选项(3)并使用TFRecord文件来存储数据集的预处理版本。然而,这也并不像看起来那么简单,例如:

  1. 压缩。将视频帧作为未压缩字节数据存储在 TFRecords 中将需要大量磁盘空间。因此,我提取所有视频帧,应用 JPG 压缩并将压缩字节存储为 TFRecords。
  2. 视频数据。我们正在处理视频,因此 TFRecords 文件中的每个示例都将非常大,并且包含多个视频帧(通常 250-300 用于 10 秒的视频,具体取决于帧速率)。

我编写了以下代码来预处理视频数据集并将视频帧写入 TFRecord 文件(每个文件大小约为 5GB):

def _int64_feature(value):
    """Wrapper for inserting int64 features into Example proto."""
    if not isinstance(value, list):
        value = [value]
    return tf.train.Feature(int64_list=tf.train.Int64List(value=value))

def _bytes_feature(value):
    """Wrapper for inserting bytes features into Example proto."""
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))


with tf.python_io.TFRecordWriter(output_file) as writer:

  # Read and resize all video frames, np.uint8 of size [N,H,W,3]
  frames = ... 

  features = {}
  features['num_frames']  = _int64_feature(frames.shape[0])
  features['height']      = _int64_feature(frames.shape[1])
  features['width']       = _int64_feature(frames.shape[2])
  features['channels']    = _int64_feature(frames.shape[3])
  features['class_label'] = _int64_feature(example['class_id'])
  features['class_text']  = _bytes_feature(tf.compat.as_bytes(example['class_label']))
  features['filename']    = _bytes_feature(tf.compat.as_bytes(example['video_id']))

  # Compress the frames using JPG and store in as bytes in:
  # 'frames/000001', 'frames/000002', ...
  for i in range(len(frames)):
      ret, buffer = cv2.imencode(".jpg", frames[i])
      features["frames/{:04d}".format(i)] = _bytes_feature(tf.compat.as_bytes(buffer.tobytes()))

  tfrecord_example = tf.train.Example(features=tf.train.Features(feature=features))
  writer.write(tfrecord_example.SerializeToString())

这很好用;数据集很好地编写为 TFRecord 文件,帧为压缩的 JPG 字节。我的问题是,如何在训练期间读取 TFRecord 文件,从视频中随机采样 64 帧并解码 JPG 图像。

根据TensorFlow 的文档tf.Data我们需要执行以下操作:

filenames = tf.placeholder(tf.string, shape=[None])
dataset = tf.data.TFRecordDataset(filenames)
dataset = dataset.map(...)  # Parse the record into tensors.
dataset = dataset.repeat()  # Repeat the input indefinitely.
dataset = dataset.batch(32)
iterator = dataset.make_initializable_iterator()
training_filenames = ["/var/data/file1.tfrecord", "/var/data/file2.tfrecord"]
sess.run(iterator.initializer, feed_dict={filenames: training_filenames})

有很多关于如何使用图像执行此操作的示例,这非常简单。但是,对于视频和帧的随机采样,我被卡住了。该tf.train.Features对象将帧存储为frame/00001frame/000002。我的第一个问题是如何从dataset.map()函数内部随机采样一组连续帧?考虑因素是由于 JPG 压缩,每个帧都有可变数量的字节,需要使用tf.image.decode_jpeg.

任何帮助如何最好地设置从 TFRecord 文件读取视频样本将不胜感激!

4

2 回答 2

6

将每个帧编码为一个单独的特征使得动态选择帧变得困难,因为tf.parse_example()(and tf.parse_single_example()) 的签名要求解析的特征名称集在图构建时是固定的。但是,您可以尝试将帧编码为包含 JPEG 编码字符串列表的单个特征:

def _bytes_list_feature(values):
    """Wrapper for inserting bytes features into Example proto."""
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=values))

with tf.python_io.TFRecordWriter(output_file) as writer:

  # Read and resize all video frames, np.uint8 of size [N,H,W,3]
  frames = ... 

  features = {}
  features['num_frames']  = _int64_feature(frames.shape[0])
  features['height']      = _int64_feature(frames.shape[1])
  features['width']       = _int64_feature(frames.shape[2])
  features['channels']    = _int64_feature(frames.shape[3])
  features['class_label'] = _int64_feature(example['class_id'])
  features['class_text']  = _bytes_feature(tf.compat.as_bytes(example['class_label']))
  features['filename']    = _bytes_feature(tf.compat.as_bytes(example['video_id']))

  # Compress the frames using JPG and store in as a list of strings in 'frames'
  encoded_frames = [tf.compat.as_bytes(cv2.imencode(".jpg", frame)[1].tobytes())
                    for frame in frames]
  features['frames'] = _bytes_list_feature(encoded_frames)

  tfrecord_example = tf.train.Example(features=tf.train.Features(feature=features))
  writer.write(tfrecord_example.SerializeToString())

完成此操作后,就可以使用解析代码frames的修改版本动态地对功能进行切片:

def decode(serialized_example, sess):
  # Prepare feature list; read encoded JPG images as bytes
  features = dict()
  features["class_label"] = tf.FixedLenFeature((), tf.int64)
  features["frames"] = tf.VarLenFeature(tf.string)
  features["num_frames"] = tf.FixedLenFeature((), tf.int64)

  # Parse into tensors
  parsed_features = tf.parse_single_example(serialized_example, features)

  # Randomly sample offset from the valid range.
  random_offset = tf.random_uniform(
      shape=(), minval=0,
      maxval=parsed_features["num_frames"] - SEQ_NUM_FRAMES, dtype=tf.int64)

  offsets = tf.range(random_offset, random_offset + SEQ_NUM_FRAMES)

  # Decode the encoded JPG images
  images = tf.map_fn(lambda i: tf.image.decode_jpeg(parsed_features["frames"].values[i]),
                     offsets)

  label  = tf.cast(parsed_features["class_label"], tf.int64)

  return images, label

(请注意,我无法运行您的代码,因此可能会出现一些小错误,但希望这足以让您入门。)

于 2018-02-07T00:17:09.950 回答
4

由于您使用的是非常相似的依赖项,因此我建议您查看以下 Python 包,因为它解决了您的确切问题设置:

pip install video2tfrecord

或参考https://github.com/ferreirafabio/video2tfrecord。它还应该具有足够的适应性以使用tf.data.Dataset.

免责声明:我是该软件包的作者之一。

于 2018-02-12T21:45:57.787 回答