yt8m_input.py 16 KB
Newer Older
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
1
# Copyright 2022 The TensorFlow Authors. All Rights Reserved.
Hye Yoon's avatar
Hye Yoon committed
2
3
4
5
6
7
8
9
10
11
12
13
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
14
15
16

"""class YT8MFrameFeatureReader(BaseReader).

Hye Yoon's avatar
Hye Yoon committed
17
18
19
20
21
22
23
  Reads TFRecords of SequenceExamples.

  The TFRecords must contain SequenceExamples with the sparse in64 'labels'
  context feature and a fixed length byte-quantized feature vector, obtained
  from the features in 'feature_names'. The quantized features will be mapped
  back into a range between min_quantized_value and max_quantized_value.
  link for details: https://research.google.com/youtube8m/download.html
24
"""
Hye Yoon's avatar
Hye Yoon committed
25
26
27
28

from typing import Dict

import tensorflow as tf
Yeqing Li's avatar
Yeqing Li committed
29
from official.projects.yt8m.dataloaders import utils
Yeqing Li's avatar
Yeqing Li committed
30
31
32
from official.vision.configs import video_classification as exp_cfg
from official.vision.dataloaders import decoder
from official.vision.dataloaders import parser
Hye Yoon's avatar
Hye Yoon committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61


def resize_axis(tensor, axis, new_size, fill_value=0):
  """Truncates or pads a tensor to new_size on on a given axis.

  Truncate or extend tensor such that tensor.shape[axis] == new_size. If the
  size increases, the padding will be performed at the end, using fill_value.

  Args:
    tensor: The tensor to be resized.
    axis: An integer representing the dimension to be sliced.
    new_size: An integer or 0d tensor representing the new value for
      tensor.shape[axis].
    fill_value: Value to use to fill any new entries in the tensor. Will be cast
      to the type of tensor.

  Returns:
    The resized tensor.
  """
  tensor = tf.convert_to_tensor(tensor)
  shape = tf.unstack(tf.shape(tensor))

  pad_shape = shape[:]
  pad_shape[axis] = tf.maximum(0, new_size - shape[axis])

  shape[axis] = tf.minimum(shape[axis], new_size)
  shape = tf.stack(shape)

  resized = tf.concat([
62
63
      tf.slice(tensor, tf.zeros_like(shape), shape),
      tf.fill(tf.stack(pad_shape), tf.cast(fill_value, tensor.dtype))
Hye Yoon's avatar
Hye Yoon committed
64
65
66
67
68
69
70
71
72
  ], axis)

  # Update shape.
  new_shape = tensor.shape.as_list()  # A copy is being made.
  new_shape[axis] = new_size
  resized = tf.ensure_shape(resized, new_shape)
  return resized


73
74
def _process_segment_and_label(video_matrix, num_frames, contexts,
                               segment_labels, segment_size,
Hye Yoon's avatar
Hye Yoon committed
75
76
                               num_classes) -> Dict[str, tf.Tensor]:
  """Processes a batched Tensor of frames.
77

Hye Yoon's avatar
Hye Yoon committed
78
79
80
81
82
83
  The same parameters used in process should be used here.
  Args:
    video_matrix: different features concatenated into one matrix
    num_frames: Number of frames per subclip.
    contexts: context information extracted from decoder
    segment_labels: if we read segment labels instead.
84
    segment_size: the segment_size used for reading segments. Segment length.
Hye Yoon's avatar
Hye Yoon committed
85
86
87
88
89
90
    num_classes: a positive integer for the number of classes.

  Returns:
    output: dictionary containing batch information
  """
  # Partition frame-level feature matrix to segment-level feature matrix.
91
  batch_video_ids = None
Hye Yoon's avatar
Hye Yoon committed
92
93
94
95
  if segment_labels:
    start_times = contexts["segment_start_times"].values
    # Here we assume all the segments that started at the same start time has
    # the same segment_size.
96
    uniq_start_times, seg_idxs = tf.unique(start_times, out_idx=tf.dtypes.int64)
Hye Yoon's avatar
Hye Yoon committed
97
    # Range gather matrix, e.g., [[0,1,2],[1,2,3]] for segment_size == 3.
98
99
100
    range_mtx = tf.expand_dims(
        uniq_start_times, axis=-1) + tf.expand_dims(
            tf.range(0, segment_size, dtype=tf.int64), axis=0)
Hye Yoon's avatar
Hye Yoon committed
101
102
103
104
    # Shape: [num_segment, segment_size, feature_dim].
    batch_video_matrix = tf.gather_nd(video_matrix,
                                      tf.expand_dims(range_mtx, axis=-1))
    num_segment = tf.shape(batch_video_matrix)[0]
105
106
107
    if "id" in contexts:
      batch_video_ids = tf.reshape(
          tf.tile([contexts["id"]], [num_segment]), (num_segment,))
108
109
    batch_frames = tf.reshape(
        tf.tile([segment_size], [num_segment]), (num_segment,))
Hye Yoon's avatar
Hye Yoon committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    batch_frames = tf.cast(tf.expand_dims(batch_frames, 1), tf.float32)

    # For segment labels, all labels are not exhaustively rated. So we only
    # evaluate the rated labels.

    # Label indices for each segment, shape: [num_segment, 2].
    label_indices = tf.stack([seg_idxs, contexts["segment_labels"].values],
                             axis=-1)
    label_values = contexts["segment_scores"].values
    sparse_labels = tf.sparse.SparseTensor(label_indices, label_values,
                                           (num_segment, num_classes))
    batch_labels = tf.sparse.to_dense(sparse_labels, validate_indices=False)

    sparse_label_weights = tf.sparse.SparseTensor(
124
125
126
127
        label_indices, tf.ones_like(label_values, dtype=tf.float32),
        (num_segment, num_classes))
    batch_label_weights = tf.sparse.to_dense(
        sparse_label_weights, validate_indices=False)
Hye Yoon's avatar
Hye Yoon committed
128
129
130
131
132
    # output_dict = utils.get_segments(batch_video_matrix, batch_frames, 5)
  else:
    # Process video-level labels.
    label_indices = contexts["labels"].values
    sparse_labels = tf.sparse.SparseTensor(
133
134
135
136
        tf.expand_dims(label_indices, axis=-1),
        tf.ones_like(contexts["labels"].values, dtype=tf.bool), (num_classes,))
    labels = tf.sparse.to_dense(
        sparse_labels, default_value=False, validate_indices=False)
Hye Yoon's avatar
Hye Yoon committed
137
138

    # convert to batch format.
139
140
    if "id" in contexts:
      batch_video_ids = tf.expand_dims(contexts["id"], 0)
Hye Yoon's avatar
Hye Yoon committed
141
142
143
144
145
146
    batch_video_matrix = tf.expand_dims(video_matrix, 0)
    batch_labels = tf.expand_dims(labels, 0)
    batch_frames = tf.expand_dims(num_frames, 0)
    batch_label_weights = None

  output_dict = {
147
148
149
      "video_matrix": batch_video_matrix,
      "labels": batch_labels,
      "num_frames": batch_frames,
Hye Yoon's avatar
Hye Yoon committed
150
  }
151
152
  if batch_video_ids is not None:
    output_dict["video_ids"] = batch_video_ids
Hye Yoon's avatar
Hye Yoon committed
153
154
155
156
157
158
  if batch_label_weights is not None:
    output_dict["label_weights"] = batch_label_weights

  return output_dict


159
160
def _get_video_matrix(features, feature_size, max_frames, max_quantized_value,
                      min_quantized_value):
Hye Yoon's avatar
Hye Yoon committed
161
162
  """Decodes features from an input string and quantizes it.

163
164
165
166
167
168
  Args:
    features: raw feature values
    feature_size: length of each frame feature vector
    max_frames: number of frames (rows) in the output feature_matrix
    max_quantized_value: the maximum of the quantized value.
    min_quantized_value: the minimum of the quantized value.
Hye Yoon's avatar
Hye Yoon committed
169

170
171
172
173
  Returns:
    feature_matrix: matrix of all frame-features
    num_frames: number of frames in the sequence
  """
Hye Yoon's avatar
Hye Yoon committed
174
  decoded_features = tf.reshape(
175
176
      tf.cast(tf.io.decode_raw(features, tf.uint8), tf.float32),
      [-1, feature_size])
Hye Yoon's avatar
Hye Yoon committed
177
178
179
180
181
182
183
184

  num_frames = tf.math.minimum(tf.shape(decoded_features)[0], max_frames)
  feature_matrix = utils.Dequantize(decoded_features, max_quantized_value,
                                    min_quantized_value)
  feature_matrix = resize_axis(feature_matrix, 0, max_frames)
  return feature_matrix, num_frames


185
186
187
def _concat_features(features, feature_names, feature_sizes, max_frames,
                     max_quantized_value, min_quantized_value):
  """Loads (potentially) different types of features and concatenates them.
Hye Yoon's avatar
Hye Yoon committed
188

189
190
191
192
193
194
195
  Args:
      features: raw feature values
      feature_names: list of feature names
      feature_sizes: list of features sizes
      max_frames: number of frames in the sequence
      max_quantized_value: the maximum of the quantized value.
      min_quantized_value: the minimum of the quantized value.
Hye Yoon's avatar
Hye Yoon committed
196

197
198
199
200
  Returns:
      video_matrix: different features concatenated into one matrix
      num_frames: the number of frames in the video
  """
Hye Yoon's avatar
Hye Yoon committed
201
202
203
204
205

  num_features = len(feature_names)
  assert num_features > 0, "No feature selected: feature_names is empty!"

  assert len(feature_names) == len(feature_sizes), (
206
207
      "length of feature_names (={}) != length of feature_sizes (={})".format(
          len(feature_names), len(feature_sizes)))
Hye Yoon's avatar
Hye Yoon committed
208
209
210
211
212

  num_frames = -1  # the number of frames in the video
  feature_matrices = [None] * num_features  # an array of different features
  for feature_index in range(num_features):
    feature_matrix, num_frames_in_this_feature = _get_video_matrix(
213
214
        features[feature_names[feature_index]], feature_sizes[feature_index],
        max_frames, max_quantized_value, min_quantized_value)
Hye Yoon's avatar
Hye Yoon committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
    if num_frames == -1:
      num_frames = num_frames_in_this_feature

    feature_matrices[feature_index] = feature_matrix

  # cap the number of frames at self.max_frames
  num_frames = tf.minimum(num_frames, max_frames)

  # concatenate different features
  video_matrix = tf.concat(feature_matrices, 1)

  return video_matrix, num_frames


class Decoder(decoder.Decoder):
  """A tf.Example decoder for classification task."""

232
233
234
235
  def __init__(
      self,
      input_params: exp_cfg.DataConfig,
  ):
Hye Yoon's avatar
Hye Yoon committed
236
237
238
239

    self._segment_labels = input_params.segment_labels
    self._feature_names = input_params.feature_names
    self._context_features = {
240
        "id": tf.io.FixedLenFeature([], tf.string),
Hye Yoon's avatar
Hye Yoon committed
241
242
243
    }
    if self._segment_labels:
      self._context_features.update({
244
245
246
247
248
          # There is no need to read end-time given we always assume the segment
          # has the same size.
          "segment_labels": tf.io.VarLenFeature(tf.int64),
          "segment_start_times": tf.io.VarLenFeature(tf.int64),
          "segment_scores": tf.io.VarLenFeature(tf.float32)
Hye Yoon's avatar
Hye Yoon committed
249
250
251
252
253
      })
    else:
      self._context_features.update({"labels": tf.io.VarLenFeature(tf.int64)})

    self._sequence_features = {
254
255
        feature_name: tf.io.FixedLenSequenceFeature([], dtype=tf.string)
        for feature_name in self._feature_names
Hye Yoon's avatar
Hye Yoon committed
256
257
258
259
260
261
    }

  def decode(self, serialized_example):
    """Parses a single tf.Example into image and label tensors."""

    contexts, features = tf.io.parse_single_sequence_example(
262
263
264
        serialized_example,
        context_features=self._context_features,
        sequence_features=self._sequence_features)
Hye Yoon's avatar
Hye Yoon committed
265

266
    return {"contexts": contexts, "features": features}
Hye Yoon's avatar
Hye Yoon committed
267
268
269
270


class Parser(parser.Parser):
  """Parses a video and label dataset.
271

Hye Yoon's avatar
Hye Yoon committed
272
273
274
275
276
277
    takes the decoded raw tensors dict
    and parse them into a dictionary of tensors
    that can be consumed by the model.
    It will be executed after decoder.
  """

278
279
280
281
282
283
  def __init__(
      self,
      input_params: exp_cfg.DataConfig,
      max_quantized_value=2,
      min_quantized_value=-2,
  ):
Hye Yoon's avatar
Hye Yoon committed
284
285
286
    self._num_classes = input_params.num_classes
    self._segment_size = input_params.segment_size
    self._segment_labels = input_params.segment_labels
287
    self._include_video_id = input_params.include_video_id
Hye Yoon's avatar
Hye Yoon committed
288
289
290
291
292
293
294
295
296
297
    self._feature_names = input_params.feature_names
    self._feature_sizes = input_params.feature_sizes
    self._max_frames = input_params.max_frames
    self._max_quantized_value = max_quantized_value
    self._min_quantized_value = min_quantized_value

  def _parse_train_data(self, decoded_tensors):
    """Parses data for training."""
    # loads (potentially) different types of features and concatenates them
    self.video_matrix, self.num_frames = _concat_features(
298
299
        decoded_tensors["features"], self._feature_names, self._feature_sizes,
        self._max_frames, self._max_quantized_value, self._min_quantized_value)
300
301
    if not self._include_video_id:
      del decoded_tensors["contexts"]["id"]
302
303
304
305
306
    output_dict = _process_segment_and_label(self.video_matrix, self.num_frames,
                                             decoded_tensors["contexts"],
                                             self._segment_labels,
                                             self._segment_size,
                                             self._num_classes)
Hye Yoon's avatar
Hye Yoon committed
307
308
309
310
311
312
    return output_dict

  def _parse_eval_data(self, decoded_tensors):
    """Parses data for evaluation."""
    # loads (potentially) different types of features and concatenates them
    self.video_matrix, self.num_frames = _concat_features(
313
314
        decoded_tensors["features"], self._feature_names, self._feature_sizes,
        self._max_frames, self._max_quantized_value, self._min_quantized_value)
315
316
    if not self._include_video_id:
      del decoded_tensors["contexts"]["id"]
317
318
319
320
321
    output_dict = _process_segment_and_label(self.video_matrix, self.num_frames,
                                             decoded_tensors["contexts"],
                                             self._segment_labels,
                                             self._segment_size,
                                             self._num_classes)
Hye Yoon's avatar
Hye Yoon committed
322
323
324
325
326
327
328
329
330
331
332
333
334
    return output_dict  # batched

  def parse_fn(self, is_training):
    """Returns a parse fn that reads and parses raw tensors from the decoder.

    Args:
      is_training: a `bool` to indicate whether it is in training mode.

    Returns:
      parse: a `callable` that takes the serialized example and generate the
        images, labels tuple where labels is a dict of Tensors that contains
        labels.
    """
335

Hye Yoon's avatar
Hye Yoon committed
336
337
338
339
340
341
342
343
344
    def parse(decoded_tensors):
      """Parses the serialized example data."""
      if is_training:
        return self._parse_train_data(decoded_tensors)
      else:
        return self._parse_eval_data(decoded_tensors)

    return parse

345

Hye Yoon's avatar
Hye Yoon committed
346
class PostBatchProcessor():
347
348
  """Processes a video and label dataset which is batched."""

Hye Yoon's avatar
Hye Yoon committed
349
350
351
352
353
354
355
  def __init__(self, input_params: exp_cfg.DataConfig):
    self.segment_labels = input_params.segment_labels
    self.num_classes = input_params.num_classes
    self.segment_size = input_params.segment_size

  def post_fn(self, batched_tensors):
    """Processes batched Tensors."""
356
    video_ids = batched_tensors.get("video_ids", None)
357
358
359
    video_matrix = batched_tensors["video_matrix"]
    labels = batched_tensors["labels"]
    num_frames = batched_tensors["num_frames"]
Hye Yoon's avatar
Hye Yoon committed
360
361
362
363
364
    label_weights = None

    if self.segment_labels:
      # [batch x num_segment x segment_size x num_features]
      # -> [batch * num_segment x segment_size x num_features]
365
366
      if video_ids is not None:
        video_ids = tf.reshape(video_ids, [-1])
Hye Yoon's avatar
Hye Yoon committed
367
368
369
370
      video_matrix = tf.reshape(video_matrix, [-1, self.segment_size, 1152])
      labels = tf.reshape(labels, [-1, self.num_classes])
      num_frames = tf.reshape(num_frames, [-1, 1])

371
372
      label_weights = tf.reshape(batched_tensors["label_weights"],
                                 [-1, self.num_classes])
Hye Yoon's avatar
Hye Yoon committed
373
374
375
376
377
378

    else:
      video_matrix = tf.squeeze(video_matrix)
      labels = tf.squeeze(labels)

    batched_tensors = {
379
380
381
        "video_matrix": video_matrix,
        "labels": labels,
        "num_frames": num_frames,
Hye Yoon's avatar
Hye Yoon committed
382
    }
383
384
    if video_ids is not None:
      batched_tensors["video_ids"] = video_ids
Hye Yoon's avatar
Hye Yoon committed
385
386
387
388
389
390

    if label_weights is not None:
      batched_tensors["label_weights"] = label_weights

    return batched_tensors

391

Hye Yoon's avatar
Hye Yoon committed
392
class TransformBatcher():
393
394
395
  """Performs manual batching on input dataset."""

  def __init__(self, input_params: exp_cfg.DataConfig):
Hye Yoon's avatar
Hye Yoon committed
396
397
398
    self._segment_labels = input_params.segment_labels
    self._global_batch_size = input_params.global_batch_size
    self._is_training = input_params.is_training
399
    self._include_video_id = input_params.include_video_id
Hye Yoon's avatar
Hye Yoon committed
400
401

  def batch_fn(self, dataset, input_context):
402
    """Add padding when segment_labels is true."""
Hye Yoon's avatar
Hye Yoon committed
403
404
405
    per_replica_batch_size = input_context.get_per_replica_batch_size(
        self._global_batch_size) if input_context else self._global_batch_size
    if not self._segment_labels:
406
      dataset = dataset.batch(per_replica_batch_size, drop_remainder=True)
Hye Yoon's avatar
Hye Yoon committed
407
408
    else:
      # add padding
409
410
411
412
413
414
415
416
417
418
419
420
      pad_shapes = {
          "video_matrix": [None, None, None],
          "labels": [None, None],
          "num_frames": [None, None],
          "label_weights": [None, None]
      }
      pad_values = {
          "video_matrix": 0.0,
          "labels": -1.0,
          "num_frames": 0.0,
          "label_weights": 0.0
      }
421
422
423
      if self._include_video_id:
        pad_shapes["video_ids"] = [None]
        pad_values["video_ids"] = None
Hye Yoon's avatar
Hye Yoon committed
424
      dataset = dataset.padded_batch(
425
426
427
428
          per_replica_batch_size,
          padded_shapes=pad_shapes,
          drop_remainder=True,
          padding_values=pad_values)
Hye Yoon's avatar
Hye Yoon committed
429
    return dataset