inputs.py 53.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ==============================================================================
"""Model input function for tf-learn object detection model."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import functools

23
import tensorflow.compat.v1 as tf
24
from tensorflow.compat.v1 import estimator as tf_estimator
25
from object_detection.builders import dataset_builder
26
27
from object_detection.builders import image_resizer_builder
from object_detection.builders import model_builder
28
from object_detection.builders import preprocessor_builder
29
30
from object_detection.core import box_list
from object_detection.core import box_list_ops
31
from object_detection.core import densepose_ops
32
from object_detection.core import keypoint_ops
33
from object_detection.core import preprocessor
34
35
36
from object_detection.core import standard_fields as fields
from object_detection.data_decoders import tf_example_decoder
from object_detection.protos import eval_pb2
37
from object_detection.protos import image_resizer_pb2
38
from object_detection.protos import input_reader_pb2
39
from object_detection.protos import model_pb2
40
from object_detection.protos import train_pb2
41
from object_detection.utils import config_util
42
from object_detection.utils import ops as util_ops
43
from object_detection.utils import shape_utils
44

45
46
HASH_KEY = 'hash'
HASH_BINS = 1 << 31
47
SERVING_FED_EXAMPLE_KEY = 'serialized_example'
48
_LABEL_OFFSET = 1
49

50
51
52
# A map of names to methods that help build the input pipeline.
INPUT_BUILDER_UTIL_MAP = {
    'dataset_build': dataset_builder.build,
53
    'model_build': model_builder.build,
54
55
}

56

57
def _multiclass_scores_or_one_hot_labels(multiclass_scores, groundtruth_boxes,
pkulzc's avatar
pkulzc committed
58
59
                                         groundtruth_classes, num_classes):
  """Returns one-hot encoding of classes when multiclass_scores is empty."""
60

pkulzc's avatar
pkulzc committed
61
62
63
64
65
66
  # Replace groundtruth_classes tensor with multiclass_scores tensor when its
  # non-empty. If multiclass_scores is empty fall back on groundtruth_classes
  # tensor.
  def true_fn():
    return tf.reshape(multiclass_scores,
                      [tf.shape(groundtruth_boxes)[0], num_classes])
67

pkulzc's avatar
pkulzc committed
68
69
  def false_fn():
    return tf.one_hot(groundtruth_classes, num_classes)
70

pkulzc's avatar
pkulzc committed
71
72
73
  return tf.cond(tf.size(multiclass_scores) > 0, true_fn, false_fn)


Rich Munoz's avatar
Rich Munoz committed
74
75
76
def convert_labeled_classes_to_k_hot(groundtruth_labeled_classes,
                                     num_classes,
                                     map_empty_to_ones=False):
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  """Returns k-hot encoding of the labeled classes.

  If map_empty_to_ones is enabled and the input labeled_classes is empty,
  this function assumes all classes are exhaustively labeled, thus returning
  an all-one encoding.

  Args:
    groundtruth_labeled_classes: a Tensor holding a sparse representation of
      labeled classes.
    num_classes: an integer representing the number of classes
    map_empty_to_ones: boolean (default: False).  Set this to be True to default
    to an all-ones result if given an empty `groundtruth_labeled_classes`.
  Returns:
    A k-hot (and 0-indexed) tensor representation of
    `groundtruth_labeled_classes`.
  """
93
94
95
96
97
98
99
100
101
102
103
104

  # If the input labeled_classes is empty, it assumes all classes are
  # exhaustively labeled, thus returning an all-one encoding.
  def true_fn():
    return tf.sparse_to_dense(
        groundtruth_labeled_classes - _LABEL_OFFSET, [num_classes],
        tf.constant(1, dtype=tf.float32),
        validate_indices=False)

  def false_fn():
    return tf.ones(num_classes, dtype=tf.float32)

105
106
107
  if map_empty_to_ones:
    return tf.cond(tf.size(groundtruth_labeled_classes) > 0, true_fn, false_fn)
  return true_fn()
108
109
110
111
112


def _remove_unrecognized_classes(class_ids, unrecognized_label):
  """Returns class ids with unrecognized classes filtered out."""

113
114
  recognized_indices = tf.squeeze(
      tf.where(tf.greater(class_ids, unrecognized_label)), -1)
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  return tf.gather(class_ids, recognized_indices)


def assert_or_prune_invalid_boxes(boxes):
  """Makes sure boxes have valid sizes (ymax >= ymin, xmax >= xmin).

  When the hardware supports assertions, the function raises an error when
  boxes have an invalid size. If assertions are not supported (e.g. on TPU),
  boxes with invalid sizes are filtered out.

  Args:
    boxes: float tensor of shape [num_boxes, 4]

  Returns:
    boxes: float tensor of shape [num_valid_boxes, 4] with invalid boxes
      filtered out.

  Raises:
    tf.errors.InvalidArgumentError: When we detect boxes with invalid size.
      This is not supported on TPUs.
  """

137
  ymin, xmin, ymax, xmax = tf.split(boxes, num_or_size_splits=4, axis=1)
138
139
140
141
142
143
144
145
146
147
148
149
150

  height_check = tf.Assert(tf.reduce_all(ymax >= ymin), [ymin, ymax])
  width_check = tf.Assert(tf.reduce_all(xmax >= xmin), [xmin, xmax])

  with tf.control_dependencies([height_check, width_check]):
    boxes_tensor = tf.concat([ymin, xmin, ymax, xmax], axis=1)
    boxlist = box_list.BoxList(boxes_tensor)
    # TODO(b/149221748) Remove pruning when XLA supports assertions.
    boxlist = box_list_ops.prune_small_boxes(boxlist, 0)

  return boxlist.get()


151
152
153
154
155
156
def transform_input_data(tensor_dict,
                         model_preprocess_fn,
                         image_resizer_fn,
                         num_classes,
                         data_augmentation_fn=None,
                         merge_multiple_boxes=False,
157
                         retain_original_image=False,
158
                         use_multiclass_scores=False,
159
                         use_bfloat16=False,
160
                         retain_original_image_additional_channels=False,
161
162
                         keypoint_type_weight=None,
                         image_classes_field_map_empty_to_ones=True):
163
164
165
  """A single function that is responsible for all input data transformations.

  Data transformation functions are applied in the following order.
166
167
168
169
170
  1. If key fields.InputDataFields.image_additional_channels is present in
     tensor_dict, the additional channels will be merged into
     fields.InputDataFields.image.
  2. data_augmentation_fn (optional): applied on tensor_dict.
  3. model_preprocess_fn: applied only on image tensor in tensor_dict.
171
172
173
174
175
176
  4. keypoint_type_weight (optional): If groundtruth keypoints are in
     the tensor dictionary, per-keypoint weights are produced. These weights are
     initialized by `keypoint_type_weight` (or ones if left None).
     Then, for all keypoints that are not visible, the weights are set to 0 (to
     avoid penalizing the model in a loss function).
  5. image_resizer_fn: applied on original image and instance mask tensor in
177
     tensor_dict.
178
179
  6. one_hot_encoding: applied to classes tensor in tensor_dict.
  7. merge_multiple_boxes (optional): when groundtruth boxes are exactly the
180
181
182
183
184
185
186
187
188
     same they can be merged into a single box with an associated k-hot class
     label.

  Args:
    tensor_dict: dictionary containing input tensors keyed by
      fields.InputDataFields.
    model_preprocess_fn: model's preprocess function to apply on image tensor.
      This function must take in a 4-D float tensor and return a 4-D preprocess
      float tensor and a tensor containing the true image shape.
189
190
191
192
    image_resizer_fn: image resizer function to apply on groundtruth instance
      `masks. This function must take a 3-D float tensor of an image and a 3-D
      tensor of instance masks and return a resized version of these along with
      the true shapes.
193
194
195
196
197
198
199
200
    num_classes: number of max classes to one-hot (or k-hot) encode the class
      labels.
    data_augmentation_fn: (optional) data augmentation function to apply on
      input `tensor_dict`.
    merge_multiple_boxes: (optional) whether to merge multiple groundtruth boxes
      and classes for a given image if the boxes are exactly the same.
    retain_original_image: (optional) whether to retain original image in the
      output dictionary.
pkulzc's avatar
pkulzc committed
201
202
203
204
    use_multiclass_scores: whether to use multiclass scores as class targets
      instead of one-hot encoding of `groundtruth_classes`. When
      this is True and multiclass_scores is empty, one-hot encoding of
      `groundtruth_classes` is used as a fallback.
205
    use_bfloat16: (optional) a bool, whether to use bfloat16 in training.
206
207
    retain_original_image_additional_channels: (optional) Whether to retain
      original image additional channels in the output dictionary.
208
209
210
    keypoint_type_weight: A list (of length num_keypoints) containing
      groundtruth loss weights to use for each keypoint. If None, will use a
      weight of 1.
211
212
213
    image_classes_field_map_empty_to_ones: A boolean flag indicating if empty
      image classes field indicates that all classes have been labeled on this
      image [true] or none [false].
214
215
216
217

  Returns:
    A dictionary keyed by fields.InputDataFields containing the tensors obtained
    after applying all the transformations.
218
219
220
221
222

  Raises:
    KeyError: If both groundtruth_labeled_classes and groundtruth_image_classes
      are provided by the decoder in tensor_dict since both fields are
      considered to contain the same information.
223
  """
pkulzc's avatar
pkulzc committed
224
  out_tensor_dict = tensor_dict.copy()
225

226
227
228
229
230
231
  input_fields = fields.InputDataFields
  labeled_classes_field = input_fields.groundtruth_labeled_classes
  image_classes_field = input_fields.groundtruth_image_classes
  verified_neg_classes_field = input_fields.groundtruth_verified_neg_classes
  not_exhaustive_field = input_fields.groundtruth_not_exhaustive_classes

232
233
234
235
236
  if (labeled_classes_field in out_tensor_dict and
      image_classes_field in out_tensor_dict):
    raise KeyError('groundtruth_labeled_classes and groundtruth_image_classes'
                   'are provided by the decoder, but only one should be set.')

237
238
239
240
241
  for field, map_empty_to_ones in [(labeled_classes_field, True),
                                   (image_classes_field,
                                    image_classes_field_map_empty_to_ones),
                                   (verified_neg_classes_field, False),
                                   (not_exhaustive_field, False)]:
242
243
244
    if field in out_tensor_dict:
      out_tensor_dict[field] = _remove_unrecognized_classes(
          out_tensor_dict[field], unrecognized_label=-1)
Rich Munoz's avatar
Rich Munoz committed
245
      out_tensor_dict[field] = convert_labeled_classes_to_k_hot(
246
          out_tensor_dict[field], num_classes, map_empty_to_ones)
247
248

  if input_fields.multiclass_scores in out_tensor_dict:
pkulzc's avatar
pkulzc committed
249
    out_tensor_dict[
250
        input_fields
pkulzc's avatar
pkulzc committed
251
        .multiclass_scores] = _multiclass_scores_or_one_hot_labels(
252
253
254
            out_tensor_dict[input_fields.multiclass_scores],
            out_tensor_dict[input_fields.groundtruth_boxes],
            out_tensor_dict[input_fields.groundtruth_classes],
pkulzc's avatar
pkulzc committed
255
256
            num_classes)

257
  if input_fields.groundtruth_boxes in out_tensor_dict:
pkulzc's avatar
pkulzc committed
258
259
260
    out_tensor_dict = util_ops.filter_groundtruth_with_nan_box_coordinates(
        out_tensor_dict)
    out_tensor_dict = util_ops.filter_unrecognized_classes(out_tensor_dict)
261

262
  if retain_original_image:
263
264
    out_tensor_dict[input_fields.original_image] = tf.cast(
        image_resizer_fn(out_tensor_dict[input_fields.image],
pkulzc's avatar
pkulzc committed
265
                         None)[0], tf.uint8)
266

267
268
269
270
  if input_fields.image_additional_channels in out_tensor_dict:
    channels = out_tensor_dict[input_fields.image_additional_channels]
    out_tensor_dict[input_fields.image] = tf.concat(
        [out_tensor_dict[input_fields.image], channels], axis=2)
271
272
    if retain_original_image_additional_channels:
      out_tensor_dict[
273
          input_fields.image_additional_channels] = tf.cast(
274
              image_resizer_fn(channels, None)[0], tf.uint8)
275

276
277
  # Apply data augmentation ops.
  if data_augmentation_fn is not None:
pkulzc's avatar
pkulzc committed
278
    out_tensor_dict = data_augmentation_fn(out_tensor_dict)
279
280

  # Apply model preprocessing ops and resize instance masks.
281
  image = out_tensor_dict[input_fields.image]
282
  preprocessed_resized_image, true_image_shape = model_preprocess_fn(
283
      tf.expand_dims(tf.cast(image, dtype=tf.float32), axis=0))
284
285
286
287
288
289
290
291
292
293

  preprocessed_shape = tf.shape(preprocessed_resized_image)
  new_height, new_width = preprocessed_shape[1], preprocessed_shape[2]

  im_box = tf.stack([
      0.0, 0.0,
      tf.to_float(new_height) / tf.to_float(true_image_shape[0, 0]),
      tf.to_float(new_width) / tf.to_float(true_image_shape[0, 1])
  ])

294
295
  if input_fields.groundtruth_boxes in tensor_dict:
    bboxes = out_tensor_dict[input_fields.groundtruth_boxes]
296
297
    boxlist = box_list.BoxList(bboxes)
    realigned_bboxes = box_list_ops.change_coordinate_frame(boxlist, im_box)
298
299
300

    realigned_boxes_tensor = realigned_bboxes.get()
    valid_boxes_tensor = assert_or_prune_invalid_boxes(realigned_boxes_tensor)
301
    out_tensor_dict[
302
        input_fields.groundtruth_boxes] = valid_boxes_tensor
303

304
305
  if input_fields.groundtruth_keypoints in tensor_dict:
    keypoints = out_tensor_dict[input_fields.groundtruth_keypoints]
306
307
308
    realigned_keypoints = keypoint_ops.change_coordinate_frame(keypoints,
                                                               im_box)
    out_tensor_dict[
309
310
311
312
        input_fields.groundtruth_keypoints] = realigned_keypoints
    flds_gt_kpt = input_fields.groundtruth_keypoints
    flds_gt_kpt_vis = input_fields.groundtruth_keypoint_visibilities
    flds_gt_kpt_weights = input_fields.groundtruth_keypoint_weights
313
314
315
316
    if flds_gt_kpt_vis not in out_tensor_dict:
      out_tensor_dict[flds_gt_kpt_vis] = tf.ones_like(
          out_tensor_dict[flds_gt_kpt][:, :, 0],
          dtype=tf.bool)
317
318
319
320
321
322
323
324
    flds_gt_kpt_depth = fields.InputDataFields.groundtruth_keypoint_depths
    flds_gt_kpt_depth_weight = (
        fields.InputDataFields.groundtruth_keypoint_depth_weights)
    if flds_gt_kpt_depth in out_tensor_dict:
      out_tensor_dict[flds_gt_kpt_depth] = out_tensor_dict[flds_gt_kpt_depth]
      out_tensor_dict[flds_gt_kpt_depth_weight] = out_tensor_dict[
          flds_gt_kpt_depth_weight]

325
326
327
328
    out_tensor_dict[flds_gt_kpt_weights] = (
        keypoint_ops.keypoint_weights_from_visibilities(
            out_tensor_dict[flds_gt_kpt_vis],
            keypoint_type_weight))
329

330
  dp_surface_coords_fld = input_fields.groundtruth_dp_surface_coords
331
332
333
334
335
336
  if dp_surface_coords_fld in tensor_dict:
    dp_surface_coords = out_tensor_dict[dp_surface_coords_fld]
    realigned_dp_surface_coords = densepose_ops.change_coordinate_frame(
        dp_surface_coords, im_box)
    out_tensor_dict[dp_surface_coords_fld] = realigned_dp_surface_coords

337
338
339
  if use_bfloat16:
    preprocessed_resized_image = tf.cast(
        preprocessed_resized_image, tf.bfloat16)
340
341
342
343
    if input_fields.context_features in out_tensor_dict:
      out_tensor_dict[input_fields.context_features] = tf.cast(
          out_tensor_dict[input_fields.context_features], tf.bfloat16)
  out_tensor_dict[input_fields.image] = tf.squeeze(
344
      preprocessed_resized_image, axis=0)
345
  out_tensor_dict[input_fields.true_image_shape] = tf.squeeze(
346
      true_image_shape, axis=0)
347
348
  if input_fields.groundtruth_instance_masks in out_tensor_dict:
    masks = out_tensor_dict[input_fields.groundtruth_instance_masks]
349
    _, resized_masks, _ = image_resizer_fn(image, masks)
350
351
    if use_bfloat16:
      resized_masks = tf.cast(resized_masks, tf.bfloat16)
pkulzc's avatar
pkulzc committed
352
    out_tensor_dict[
353
        input_fields.groundtruth_instance_masks] = resized_masks
354

pkulzc's avatar
pkulzc committed
355
  zero_indexed_groundtruth_classes = out_tensor_dict[
356
      input_fields.groundtruth_classes] - _LABEL_OFFSET
357
  if use_multiclass_scores:
pkulzc's avatar
pkulzc committed
358
    out_tensor_dict[
359
360
        input_fields.groundtruth_classes] = out_tensor_dict[
            input_fields.multiclass_scores]
pkulzc's avatar
pkulzc committed
361
  else:
362
    out_tensor_dict[input_fields.groundtruth_classes] = tf.one_hot(
pkulzc's avatar
pkulzc committed
363
        zero_indexed_groundtruth_classes, num_classes)
364
  out_tensor_dict.pop(input_fields.multiclass_scores, None)
365

366
  if input_fields.groundtruth_confidences in out_tensor_dict:
pkulzc's avatar
pkulzc committed
367
    groundtruth_confidences = out_tensor_dict[
368
        input_fields.groundtruth_confidences]
369
    # Map the confidences to the one-hot encoding of classes
370
    out_tensor_dict[input_fields.groundtruth_confidences] = (
371
        tf.reshape(groundtruth_confidences, [-1, 1]) *
372
        out_tensor_dict[input_fields.groundtruth_classes])
373
374
375
  else:
    groundtruth_confidences = tf.ones_like(
        zero_indexed_groundtruth_classes, dtype=tf.float32)
376
377
    out_tensor_dict[input_fields.groundtruth_confidences] = (
        out_tensor_dict[input_fields.groundtruth_classes])
378

379
  if merge_multiple_boxes:
380
381
    merged_boxes, merged_classes, merged_confidences, _ = (
        util_ops.merge_boxes_with_multiple_labels(
382
            out_tensor_dict[input_fields.groundtruth_boxes],
383
384
385
            zero_indexed_groundtruth_classes,
            groundtruth_confidences,
            num_classes))
386
    merged_classes = tf.cast(merged_classes, tf.float32)
387
388
389
    out_tensor_dict[input_fields.groundtruth_boxes] = merged_boxes
    out_tensor_dict[input_fields.groundtruth_classes] = merged_classes
    out_tensor_dict[input_fields.groundtruth_confidences] = (
390
        merged_confidences)
391
392
393
  if input_fields.groundtruth_boxes in out_tensor_dict:
    out_tensor_dict[input_fields.num_groundtruth_boxes] = tf.shape(
        out_tensor_dict[input_fields.groundtruth_boxes])[0]
394

pkulzc's avatar
pkulzc committed
395
  return out_tensor_dict
396
397


398
399
400
401
402
def pad_input_data_to_static_shapes(tensor_dict,
                                    max_num_boxes,
                                    num_classes,
                                    spatial_image_shape=None,
                                    max_num_context_features=None,
403
404
                                    context_feature_length=None,
                                    max_dp_points=336):
405
406
  """Pads input tensors to static shapes.

407
408
409
  In case num_additional_channels > 0, we assume that the additional channels
  have already been concatenated to the base image.

410
411
412
413
414
415
416
417
  Args:
    tensor_dict: Tensor dictionary of input data
    max_num_boxes: Max number of groundtruth boxes needed to compute shapes for
      padding.
    num_classes: Number of classes in the dataset needed to compute shapes for
      padding.
    spatial_image_shape: A list of two integers of the form [height, width]
      containing expected spatial shape of the image.
418
419
420
    max_num_context_features (optional): The maximum number of context
      features needed to compute shapes padding.
    context_feature_length (optional): The length of the context feature.
421
422
423
424
425
    max_dp_points (optional): The maximum number of DensePose sampled points per
      instance. The default (336) is selected since the original DensePose paper
      (https://arxiv.org/pdf/1802.00434.pdf) indicates that the maximum number
      of samples per part is 14, and therefore 24 * 14 = 336 is the maximum
      sampler per instance.
426
427
428
429
430
431

  Returns:
    A dictionary keyed by fields.InputDataFields containing padding shapes for
    tensors in the dataset.

  Raises:
432
    ValueError: If groundtruth classes is neither rank 1 nor rank 2, or if we
433
434
435
      detect that additional channels have not been concatenated yet, or if
      max_num_context_features is not specified and context_features is in the
      tensor dict.
436
437
438
439
440
441
  """
  if not spatial_image_shape or spatial_image_shape == [-1, -1]:
    height, width = None, None
  else:
    height, width = spatial_image_shape  # pylint: disable=unpacking-non-sequence

442
  input_fields = fields.InputDataFields
443
  num_additional_channels = 0
444
  if input_fields.image_additional_channels in tensor_dict:
445
    num_additional_channels = shape_utils.get_dim_as_int(tensor_dict[
446
        input_fields.image_additional_channels].shape[2])
447
448
449
450

  # We assume that if num_additional_channels > 0, then it has already been
  # concatenated to the base image (but not the ground truth).
  num_channels = 3
451
  if input_fields.image in tensor_dict:
452
    num_channels = shape_utils.get_dim_as_int(
453
        tensor_dict[input_fields.image].shape[2])
454
455
456
457
458
459

  if num_additional_channels:
    if num_additional_channels >= num_channels:
      raise ValueError(
          'Image must be already concatenated with additional channels.')

460
    if (input_fields.original_image in tensor_dict and
461
        shape_utils.get_dim_as_int(
462
            tensor_dict[input_fields.original_image].shape[2]) ==
463
464
465
466
        num_channels):
      raise ValueError(
          'Image must be already concatenated with additional channels.')

467
  if input_fields.context_features in tensor_dict and (
468
469
470
471
472
      max_num_context_features is None):
    raise ValueError('max_num_context_features must be specified in the model '
                     'config if include_context is specified in the input '
                     'config')

473
  padding_shapes = {
474
475
476
      input_fields.image: [height, width, num_channels],
      input_fields.original_image_spatial_shape: [2],
      input_fields.image_additional_channels: [
477
478
          height, width, num_additional_channels
      ],
479
480
481
482
483
484
485
      input_fields.source_id: [],
      input_fields.filename: [],
      input_fields.key: [],
      input_fields.groundtruth_difficult: [max_num_boxes],
      input_fields.groundtruth_boxes: [max_num_boxes, 4],
      input_fields.groundtruth_classes: [max_num_boxes, num_classes],
      input_fields.groundtruth_instance_masks: [
486
487
          max_num_boxes, height, width
      ],
488
      input_fields.groundtruth_instance_mask_weights: [max_num_boxes],
489
490
491
492
493
      input_fields.groundtruth_is_crowd: [max_num_boxes],
      input_fields.groundtruth_group_of: [max_num_boxes],
      input_fields.groundtruth_area: [max_num_boxes],
      input_fields.groundtruth_weights: [max_num_boxes],
      input_fields.groundtruth_confidences: [
494
495
          max_num_boxes, num_classes
      ],
496
497
498
499
500
501
502
      input_fields.num_groundtruth_boxes: [],
      input_fields.groundtruth_label_types: [max_num_boxes],
      input_fields.groundtruth_label_weights: [max_num_boxes],
      input_fields.true_image_shape: [3],
      input_fields.groundtruth_image_classes: [num_classes],
      input_fields.groundtruth_image_confidences: [num_classes],
      input_fields.groundtruth_labeled_classes: [num_classes],
503
504
  }

505
506
  if input_fields.original_image in tensor_dict:
    padding_shapes[input_fields.original_image] = [
507
        height, width,
508
        shape_utils.get_dim_as_int(tensor_dict[input_fields.
509
                                               original_image].shape[2])
510
    ]
511
  if input_fields.groundtruth_keypoints in tensor_dict:
512
    tensor_shape = (
513
        tensor_dict[input_fields.groundtruth_keypoints].shape)
514
515
516
    padding_shape = [max_num_boxes,
                     shape_utils.get_dim_as_int(tensor_shape[1]),
                     shape_utils.get_dim_as_int(tensor_shape[2])]
517
518
519
    padding_shapes[input_fields.groundtruth_keypoints] = padding_shape
  if input_fields.groundtruth_keypoint_visibilities in tensor_dict:
    tensor_shape = tensor_dict[input_fields.
520
                               groundtruth_keypoint_visibilities].shape
521
    padding_shape = [max_num_boxes, shape_utils.get_dim_as_int(tensor_shape[1])]
522
    padding_shapes[input_fields.
523
524
                   groundtruth_keypoint_visibilities] = padding_shape

525
526
527
528
529
530
531
532
533
  if fields.InputDataFields.groundtruth_keypoint_depths in tensor_dict:
    tensor_shape = tensor_dict[fields.InputDataFields.
                               groundtruth_keypoint_depths].shape
    padding_shape = [max_num_boxes, shape_utils.get_dim_as_int(tensor_shape[1])]
    padding_shapes[fields.InputDataFields.
                   groundtruth_keypoint_depths] = padding_shape
    padding_shapes[fields.InputDataFields.
                   groundtruth_keypoint_depth_weights] = padding_shape

534
  if input_fields.groundtruth_keypoint_weights in tensor_dict:
535
    tensor_shape = (
536
        tensor_dict[input_fields.groundtruth_keypoint_weights].shape)
537
    padding_shape = [max_num_boxes, shape_utils.get_dim_as_int(tensor_shape[1])]
538
    padding_shapes[input_fields.
539
                   groundtruth_keypoint_weights] = padding_shape
540
  if input_fields.groundtruth_dp_num_points in tensor_dict:
541
    padding_shapes[
542
        input_fields.groundtruth_dp_num_points] = [max_num_boxes]
543
    padding_shapes[
544
        input_fields.groundtruth_dp_part_ids] = [
545
546
            max_num_boxes, max_dp_points]
    padding_shapes[
547
        input_fields.groundtruth_dp_surface_coords] = [
548
            max_num_boxes, max_dp_points, 4]
549
550
551
552
553
554
555
556
  if input_fields.groundtruth_track_ids in tensor_dict:
    padding_shapes[
        input_fields.groundtruth_track_ids] = [max_num_boxes]

  if input_fields.groundtruth_verified_neg_classes in tensor_dict:
    padding_shapes[
        input_fields.groundtruth_verified_neg_classes] = [num_classes]
  if input_fields.groundtruth_not_exhaustive_classes in tensor_dict:
557
    padding_shapes[
558
        input_fields.groundtruth_not_exhaustive_classes] = [num_classes]
559
560

  # Prepare for ContextRCNN related fields.
561
  if input_fields.context_features in tensor_dict:
562
    padding_shape = [max_num_context_features, context_feature_length]
563
    padding_shapes[input_fields.context_features] = padding_shape
564
565

    tensor_shape = tf.shape(
566
567
568
569
570
571
572
573
        tensor_dict[fields.InputDataFields.context_features])
    tensor_dict[fields.InputDataFields.valid_context_size] = tensor_shape[0]
    padding_shapes[fields.InputDataFields.valid_context_size] = []
  if fields.InputDataFields.context_feature_length in tensor_dict:
    padding_shapes[fields.InputDataFields.context_feature_length] = []
  if fields.InputDataFields.context_features_image_id_list in tensor_dict:
    padding_shapes[fields.InputDataFields.context_features_image_id_list] = [
        max_num_context_features]
574

575
576
  if input_fields.is_annotated in tensor_dict:
    padding_shapes[input_fields.is_annotated] = []
577

578
579
  padded_tensor_dict = {}
  for tensor_name in tensor_dict:
580
581
    padded_tensor_dict[tensor_name] = shape_utils.pad_or_clip_nd(
        tensor_dict[tensor_name], padding_shapes[tensor_name])
582
583
584

  # Make sure that the number of groundtruth boxes now reflects the
  # padded/clipped tensors.
585
586
  if input_fields.num_groundtruth_boxes in padded_tensor_dict:
    padded_tensor_dict[input_fields.num_groundtruth_boxes] = (
587
        tf.minimum(
588
            padded_tensor_dict[input_fields.num_groundtruth_boxes],
589
            max_num_boxes))
590
591
592
  return padded_tensor_dict


593
594
595
596
597
598
599
600
601
602
603
604
605
606
def augment_input_data(tensor_dict, data_augmentation_options):
  """Applies data augmentation ops to input tensors.

  Args:
    tensor_dict: A dictionary of input tensors keyed by fields.InputDataFields.
    data_augmentation_options: A list of tuples, where each tuple contains a
      function and a dictionary that contains arguments and their values.
      Usually, this is the output of core/preprocessor.build.

  Returns:
    A dictionary of tensors obtained by applying data augmentation ops to the
    input tensor dictionary.
  """
  tensor_dict[fields.InputDataFields.image] = tf.expand_dims(
607
      tf.cast(tensor_dict[fields.InputDataFields.image], dtype=tf.float32), 0)
608
609
610

  include_instance_masks = (fields.InputDataFields.groundtruth_instance_masks
                            in tensor_dict)
611
612
  include_instance_mask_weights = (
      fields.InputDataFields.groundtruth_instance_mask_weights in tensor_dict)
613
614
  include_keypoints = (fields.InputDataFields.groundtruth_keypoints
                       in tensor_dict)
615
616
  include_keypoint_visibilities = (
      fields.InputDataFields.groundtruth_keypoint_visibilities in tensor_dict)
617
618
  include_keypoint_depths = (
      fields.InputDataFields.groundtruth_keypoint_depths in tensor_dict)
619
620
621
622
  include_label_weights = (fields.InputDataFields.groundtruth_weights
                           in tensor_dict)
  include_label_confidences = (fields.InputDataFields.groundtruth_confidences
                               in tensor_dict)
623
624
  include_multiclass_scores = (fields.InputDataFields.multiclass_scores in
                               tensor_dict)
625
626
627
628
  dense_pose_fields = [fields.InputDataFields.groundtruth_dp_num_points,
                       fields.InputDataFields.groundtruth_dp_part_ids,
                       fields.InputDataFields.groundtruth_dp_surface_coords]
  include_dense_pose = all(field in tensor_dict for field in dense_pose_fields)
629
630
631
  tensor_dict = preprocessor.preprocess(
      tensor_dict, data_augmentation_options,
      func_arg_map=preprocessor.get_default_func_arg_map(
632
633
          include_label_weights=include_label_weights,
          include_label_confidences=include_label_confidences,
634
          include_multiclass_scores=include_multiclass_scores,
635
          include_instance_masks=include_instance_masks,
636
          include_instance_mask_weights=include_instance_mask_weights,
637
          include_keypoints=include_keypoints,
638
          include_keypoint_visibilities=include_keypoint_visibilities,
639
640
          include_dense_pose=include_dense_pose,
          include_keypoint_depths=include_keypoint_depths))
641
642
643
644
645
  tensor_dict[fields.InputDataFields.image] = tf.squeeze(
      tensor_dict[fields.InputDataFields.image], axis=0)
  return tensor_dict


646
647
648
649
650
651
def _get_labels_dict(input_dict):
  """Extracts labels dict from input dict."""
  required_label_keys = [
      fields.InputDataFields.num_groundtruth_boxes,
      fields.InputDataFields.groundtruth_boxes,
      fields.InputDataFields.groundtruth_classes,
652
      fields.InputDataFields.groundtruth_weights,
653
654
655
656
657
658
  ]
  labels_dict = {}
  for key in required_label_keys:
    labels_dict[key] = input_dict[key]

  optional_label_keys = [
659
      fields.InputDataFields.groundtruth_confidences,
660
      fields.InputDataFields.groundtruth_labeled_classes,
661
      fields.InputDataFields.groundtruth_keypoints,
662
663
      fields.InputDataFields.groundtruth_keypoint_depths,
      fields.InputDataFields.groundtruth_keypoint_depth_weights,
664
      fields.InputDataFields.groundtruth_instance_masks,
665
      fields.InputDataFields.groundtruth_instance_mask_weights,
666
667
      fields.InputDataFields.groundtruth_area,
      fields.InputDataFields.groundtruth_is_crowd,
668
      fields.InputDataFields.groundtruth_group_of,
669
670
671
      fields.InputDataFields.groundtruth_difficult,
      fields.InputDataFields.groundtruth_keypoint_visibilities,
      fields.InputDataFields.groundtruth_keypoint_weights,
672
673
      fields.InputDataFields.groundtruth_dp_num_points,
      fields.InputDataFields.groundtruth_dp_part_ids,
674
      fields.InputDataFields.groundtruth_dp_surface_coords,
675
676
      fields.InputDataFields.groundtruth_track_ids,
      fields.InputDataFields.groundtruth_verified_neg_classes,
677
678
      fields.InputDataFields.groundtruth_not_exhaustive_classes,
      fields.InputDataFields.groundtruth_image_classes,
679
680
681
682
683
684
685
686
687
688
689
  ]

  for key in optional_label_keys:
    if key in input_dict:
      labels_dict[key] = input_dict[key]
  if fields.InputDataFields.groundtruth_difficult in labels_dict:
    labels_dict[fields.InputDataFields.groundtruth_difficult] = tf.cast(
        labels_dict[fields.InputDataFields.groundtruth_difficult], tf.int32)
  return labels_dict


690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
def _replace_empty_string_with_random_number(string_tensor):
  """Returns string unchanged if non-empty, and random string tensor otherwise.

  The random string is an integer 0 and 2**63 - 1, casted as string.


  Args:
    string_tensor: A tf.tensor of dtype string.

  Returns:
    out_string: A tf.tensor of dtype string. If string_tensor contains the empty
      string, out_string will contain a random integer casted to a string.
      Otherwise string_tensor is returned unchanged.

  """

  empty_string = tf.constant('', dtype=tf.string, name='EmptyString')

  random_source_id = tf.as_string(
      tf.random_uniform(shape=[], maxval=2**63 - 1, dtype=tf.int64))

  out_string = tf.cond(
      tf.equal(string_tensor, empty_string),
      true_fn=lambda: random_source_id,
      false_fn=lambda: string_tensor)

  return out_string


719
def _get_features_dict(input_dict, include_source_id=False):
720
  """Extracts features dict from input dict."""
721
722
723
724
725

  source_id = _replace_empty_string_with_random_number(
      input_dict[fields.InputDataFields.source_id])

  hash_from_source_id = tf.string_to_hash_bucket_fast(source_id, HASH_BINS)
726
727
728
729
730
  features = {
      fields.InputDataFields.image:
          input_dict[fields.InputDataFields.image],
      HASH_KEY: tf.cast(hash_from_source_id, tf.int32),
      fields.InputDataFields.true_image_shape:
pkulzc's avatar
pkulzc committed
731
732
733
          input_dict[fields.InputDataFields.true_image_shape],
      fields.InputDataFields.original_image_spatial_shape:
          input_dict[fields.InputDataFields.original_image_spatial_shape]
734
  }
735
736
  if include_source_id:
    features[fields.InputDataFields.source_id] = source_id
737
738
739
  if fields.InputDataFields.original_image in input_dict:
    features[fields.InputDataFields.original_image] = input_dict[
        fields.InputDataFields.original_image]
740
741
742
  if fields.InputDataFields.image_additional_channels in input_dict:
    features[fields.InputDataFields.image_additional_channels] = input_dict[
        fields.InputDataFields.image_additional_channels]
743
744
745
746
747
748
  if fields.InputDataFields.context_features in input_dict:
    features[fields.InputDataFields.context_features] = input_dict[
        fields.InputDataFields.context_features]
  if fields.InputDataFields.valid_context_size in input_dict:
    features[fields.InputDataFields.valid_context_size] = input_dict[
        fields.InputDataFields.valid_context_size]
749
750
751
  if fields.InputDataFields.context_features_image_id_list in input_dict:
    features[fields.InputDataFields.context_features_image_id_list] = (
        input_dict[fields.InputDataFields.context_features_image_id_list])
752
753
754
  return features


755
756
def create_train_input_fn(train_config, train_input_config,
                          model_config):
757
758
759
760
761
  """Creates a train `input` function for `Estimator`.

  Args:
    train_config: A train_pb2.TrainConfig.
    train_input_config: An input_reader_pb2.InputReader.
762
    model_config: A model_pb2.DetectionModel.
763
764
765
766
767

  Returns:
    `input_fn` for `Estimator` in TRAIN mode.
  """

768
  def _train_input_fn(params=None):
769
770
    return train_input(train_config, train_input_config, model_config,
                       params=params)
771

772
  return _train_input_fn
773

774

775
def train_input(train_config, train_input_config,
776
                model_config, model=None, params=None, input_context=None):
777
778
779
780
781
782
783
784
785
  """Returns `features` and `labels` tensor dictionaries for training.

  Args:
    train_config: A train_pb2.TrainConfig.
    train_input_config: An input_reader_pb2.InputReader.
    model_config: A model_pb2.DetectionModel.
    model: A pre-constructed Detection Model.
      If None, one will be created from the config.
    params: Parameter dictionary passed from the estimator.
786
787
788
    input_context: optional, A tf.distribute.InputContext object used to
      shard filenames and compute per-replica batch_size when this function
      is being called per-replica.
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818

  Returns:
    A tf.data.Dataset that holds (features, labels) tuple.

    features: Dictionary of feature tensors.
      features[fields.InputDataFields.image] is a [batch_size, H, W, C]
        float32 tensor with preprocessed images.
      features[HASH_KEY] is a [batch_size] int32 tensor representing unique
        identifiers for the images.
      features[fields.InputDataFields.true_image_shape] is a [batch_size, 3]
        int32 tensor representing the true image shapes, as preprocessed
        images could be padded.
      features[fields.InputDataFields.original_image] (optional) is a
        [batch_size, H, W, C] float32 tensor with original images.
    labels: Dictionary of groundtruth tensors.
      labels[fields.InputDataFields.num_groundtruth_boxes] is a [batch_size]
        int32 tensor indicating the number of groundtruth boxes.
      labels[fields.InputDataFields.groundtruth_boxes] is a
        [batch_size, num_boxes, 4] float32 tensor containing the corners of
        the groundtruth boxes.
      labels[fields.InputDataFields.groundtruth_classes] is a
        [batch_size, num_boxes, num_classes] float32 one-hot tensor of
        classes.
      labels[fields.InputDataFields.groundtruth_weights] is a
        [batch_size, num_boxes] float32 tensor containing groundtruth weights
        for the boxes.
      -- Optional --
      labels[fields.InputDataFields.groundtruth_instance_masks] is a
        [batch_size, num_boxes, H, W] float32 tensor containing only binary
        values, which represent instance masks for objects.
819
820
821
      labels[fields.InputDataFields.groundtruth_instance_mask_weights] is a
        [batch_size, num_boxes] float32 tensor containing groundtruth weights
        for each instance mask.
822
823
824
      labels[fields.InputDataFields.groundtruth_keypoints] is a
        [batch_size, num_boxes, num_keypoints, 2] float32 tensor containing
        keypoints for each box.
825
826
827
828
829
830
      labels[fields.InputDataFields.groundtruth_weights] is a
        [batch_size, num_boxes, num_keypoints] float32 tensor containing
        groundtruth weights for the keypoints.
      labels[fields.InputDataFields.groundtruth_visibilities] is a
        [batch_size, num_boxes, num_keypoints] bool tensor containing
        groundtruth visibilities for each keypoint.
831
832
      labels[fields.InputDataFields.groundtruth_labeled_classes] is a
        [batch_size, num_classes] float32 k-hot tensor of classes.
833
834
835
836
837
838
839
840
841
842
843
      labels[fields.InputDataFields.groundtruth_dp_num_points] is a
        [batch_size, num_boxes] int32 tensor with the number of sampled
        DensePose points per object.
      labels[fields.InputDataFields.groundtruth_dp_part_ids] is a
        [batch_size, num_boxes, max_sampled_points] int32 tensor with the
        DensePose part ids (0-indexed) per object.
      labels[fields.InputDataFields.groundtruth_dp_surface_coords] is a
        [batch_size, num_boxes, max_sampled_points, 4] float32 tensor with the
        DensePose surface coordinates. The format is (y, x, v, u), where (y, x)
        are normalized image coordinates and (v, u) are normalized surface part
        coordinates.
844
845
      labels[fields.InputDataFields.groundtruth_track_ids] is a
        [batch_size, num_boxes] int32 tensor with the track ID for each object.
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866

  Raises:
    TypeError: if the `train_config`, `train_input_config` or `model_config`
      are not of the correct type.
  """
  if not isinstance(train_config, train_pb2.TrainConfig):
    raise TypeError('For training mode, the `train_config` must be a '
                    'train_pb2.TrainConfig.')
  if not isinstance(train_input_config, input_reader_pb2.InputReader):
    raise TypeError('The `train_input_config` must be a '
                    'input_reader_pb2.InputReader.')
  if not isinstance(model_config, model_pb2.DetectionModel):
    raise TypeError('The `model_config` must be a '
                    'model_pb2.DetectionModel.')

  if model is None:
    model_preprocess_fn = INPUT_BUILDER_UTIL_MAP['model_build'](
        model_config, is_training=True).preprocess
  else:
    model_preprocess_fn = model.preprocess

867
868
  num_classes = config_util.get_number_of_classes(model_config)

869
870
871
872
873
874
875
876
877
878
879
880
  def transform_and_pad_input_data_fn(tensor_dict):
    """Combines transform and pad operation."""
    data_augmentation_options = [
        preprocessor_builder.build(step)
        for step in train_config.data_augmentation_options
    ]
    data_augmentation_fn = functools.partial(
        augment_input_data,
        data_augmentation_options=data_augmentation_options)

    image_resizer_config = config_util.get_image_resizer_config(model_config)
    image_resizer_fn = image_resizer_builder.build(image_resizer_config)
881
    keypoint_type_weight = train_input_config.keypoint_type_weight or None
882
883
884
    transform_data_fn = functools.partial(
        transform_input_data, model_preprocess_fn=model_preprocess_fn,
        image_resizer_fn=image_resizer_fn,
885
        num_classes=num_classes,
886
887
888
889
        data_augmentation_fn=data_augmentation_fn,
        merge_multiple_boxes=train_config.merge_multiple_label_boxes,
        retain_original_image=train_config.retain_original_images,
        use_multiclass_scores=train_config.use_multiclass_scores,
890
891
        use_bfloat16=train_config.use_bfloat16,
        keypoint_type_weight=keypoint_type_weight)
892
893
894
895

    tensor_dict = pad_input_data_to_static_shapes(
        tensor_dict=transform_data_fn(tensor_dict),
        max_num_boxes=train_input_config.max_number_of_boxes,
896
        num_classes=num_classes,
897
        spatial_image_shape=config_util.get_spatial_image_size(
898
899
900
901
902
903
904
905
            image_resizer_config),
        max_num_context_features=config_util.get_max_num_context_features(
            model_config),
        context_feature_length=config_util.get_context_feature_length(
            model_config))
    include_source_id = train_input_config.include_source_id
    return (_get_features_dict(tensor_dict, include_source_id),
            _get_labels_dict(tensor_dict))
906
  reduce_to_frame_fn = get_reduce_to_frame_fn(train_input_config, True)
907
908
909
910

  dataset = INPUT_BUILDER_UTIL_MAP['dataset_build'](
      train_input_config,
      transform_input_data_fn=transform_and_pad_input_data_fn,
911
      batch_size=params['batch_size'] if params else train_config.batch_size,
912
913
      input_context=input_context,
      reduce_to_frame_fn=reduce_to_frame_fn)
914
  return dataset
915
916


917
def create_eval_input_fn(eval_config, eval_input_config, model_config):
918
919
920
921
922
  """Creates an eval `input` function for `Estimator`.

  Args:
    eval_config: An eval_pb2.EvalConfig.
    eval_input_config: An input_reader_pb2.InputReader.
923
    model_config: A model_pb2.DetectionModel.
924
925
926
927
928

  Returns:
    `input_fn` for `Estimator` in EVAL mode.
  """

929
  def _eval_input_fn(params=None):
930
931
    return eval_input(eval_config, eval_input_config, model_config,
                      params=params)
932

933
  return _eval_input_fn
934

935

936
def eval_input(eval_config, eval_input_config, model_config,
937
               model=None, params=None, input_context=None):
938
939
940
941
942
943
944
945
946
  """Returns `features` and `labels` tensor dictionaries for evaluation.

  Args:
    eval_config: An eval_pb2.EvalConfig.
    eval_input_config: An input_reader_pb2.InputReader.
    model_config: A model_pb2.DetectionModel.
    model: A pre-constructed Detection Model.
      If None, one will be created from the config.
    params: Parameter dictionary passed from the estimator.
947
948
949
    input_context: optional, A tf.distribute.InputContext object used to
      shard filenames and compute per-replica batch_size when this function
      is being called per-replica.
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978

  Returns:
    A tf.data.Dataset that holds (features, labels) tuple.

    features: Dictionary of feature tensors.
      features[fields.InputDataFields.image] is a [1, H, W, C] float32 tensor
        with preprocessed images.
      features[HASH_KEY] is a [1] int32 tensor representing unique
        identifiers for the images.
      features[fields.InputDataFields.true_image_shape] is a [1, 3]
        int32 tensor representing the true image shapes, as preprocessed
        images could be padded.
      features[fields.InputDataFields.original_image] is a [1, H', W', C]
        float32 tensor with the original image.
    labels: Dictionary of groundtruth tensors.
      labels[fields.InputDataFields.groundtruth_boxes] is a [1, num_boxes, 4]
        float32 tensor containing the corners of the groundtruth boxes.
      labels[fields.InputDataFields.groundtruth_classes] is a
        [num_boxes, num_classes] float32 one-hot tensor of classes.
      labels[fields.InputDataFields.groundtruth_area] is a [1, num_boxes]
        float32 tensor containing object areas.
      labels[fields.InputDataFields.groundtruth_is_crowd] is a [1, num_boxes]
        bool tensor indicating if the boxes enclose a crowd.
      labels[fields.InputDataFields.groundtruth_difficult] is a [1, num_boxes]
        int32 tensor indicating if the boxes represent difficult instances.
      -- Optional --
      labels[fields.InputDataFields.groundtruth_instance_masks] is a
        [1, num_boxes, H, W] float32 tensor containing only binary values,
        which represent instance masks for objects.
979
980
981
      labels[fields.InputDataFields.groundtruth_instance_mask_weights] is a
        [1, num_boxes] float32 tensor containing groundtruth weights for each
        instance mask.
982
983
984
985
986
987
      labels[fields.InputDataFields.groundtruth_weights] is a
        [batch_size, num_boxes, num_keypoints] float32 tensor containing
        groundtruth weights for the keypoints.
      labels[fields.InputDataFields.groundtruth_visibilities] is a
        [batch_size, num_boxes, num_keypoints] bool tensor containing
        groundtruth visibilities for each keypoint.
988
989
990
991
992
      labels[fields.InputDataFields.groundtruth_group_of] is a [1, num_boxes]
        bool tensor indicating if the box covers more than 5 instances of the
        same class which heavily occlude each other.
      labels[fields.InputDataFields.groundtruth_labeled_classes] is a
        [num_boxes, num_classes] float32 k-hot tensor of classes.
993
994
995
996
997
998
999
1000
1001
1002
1003
      labels[fields.InputDataFields.groundtruth_dp_num_points] is a
        [batch_size, num_boxes] int32 tensor with the number of sampled
        DensePose points per object.
      labels[fields.InputDataFields.groundtruth_dp_part_ids] is a
        [batch_size, num_boxes, max_sampled_points] int32 tensor with the
        DensePose part ids (0-indexed) per object.
      labels[fields.InputDataFields.groundtruth_dp_surface_coords] is a
        [batch_size, num_boxes, max_sampled_points, 4] float32 tensor with the
        DensePose surface coordinates. The format is (y, x, v, u), where (y, x)
        are normalized image coordinates and (v, u) are normalized surface part
        coordinates.
1004
1005
      labels[fields.InputDataFields.groundtruth_track_ids] is a
        [batch_size, num_boxes] int32 tensor with the track ID for each object.
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021

  Raises:
    TypeError: if the `eval_config`, `eval_input_config` or `model_config`
      are not of the correct type.
  """
  params = params or {}
  if not isinstance(eval_config, eval_pb2.EvalConfig):
    raise TypeError('For eval mode, the `eval_config` must be a '
                    'train_pb2.EvalConfig.')
  if not isinstance(eval_input_config, input_reader_pb2.InputReader):
    raise TypeError('The `eval_input_config` must be a '
                    'input_reader_pb2.InputReader.')
  if not isinstance(model_config, model_pb2.DetectionModel):
    raise TypeError('The `model_config` must be a '
                    'model_pb2.DetectionModel.')

1022
1023
1024
1025
1026
1027
1028
1029
  if eval_config.force_no_resize:
    arch = model_config.WhichOneof('model')
    arch_config = getattr(model_config, arch)
    image_resizer_proto = image_resizer_pb2.ImageResizer()
    image_resizer_proto.identity_resizer.CopyFrom(
        image_resizer_pb2.IdentityResizer())
    arch_config.image_resizer.CopyFrom(image_resizer_proto)

1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
  if model is None:
    model_preprocess_fn = INPUT_BUILDER_UTIL_MAP['model_build'](
        model_config, is_training=False).preprocess
  else:
    model_preprocess_fn = model.preprocess

  def transform_and_pad_input_data_fn(tensor_dict):
    """Combines transform and pad operation."""
    num_classes = config_util.get_number_of_classes(model_config)

    image_resizer_config = config_util.get_image_resizer_config(model_config)
    image_resizer_fn = image_resizer_builder.build(image_resizer_config)
1042
    keypoint_type_weight = eval_input_config.keypoint_type_weight or None
1043
1044
1045
1046
1047
1048

    transform_data_fn = functools.partial(
        transform_input_data, model_preprocess_fn=model_preprocess_fn,
        image_resizer_fn=image_resizer_fn,
        num_classes=num_classes,
        data_augmentation_fn=None,
1049
1050
        retain_original_image=eval_config.retain_original_images,
        retain_original_image_additional_channels=
1051
        eval_config.retain_original_image_additional_channels,
1052
1053
1054
        keypoint_type_weight=keypoint_type_weight,
        image_classes_field_map_empty_to_ones=eval_config
        .image_classes_field_map_empty_to_ones)
1055
1056
1057
1058
1059
    tensor_dict = pad_input_data_to_static_shapes(
        tensor_dict=transform_data_fn(tensor_dict),
        max_num_boxes=eval_input_config.max_number_of_boxes,
        num_classes=config_util.get_number_of_classes(model_config),
        spatial_image_shape=config_util.get_spatial_image_size(
1060
1061
1062
1063
1064
1065
1066
1067
            image_resizer_config),
        max_num_context_features=config_util.get_max_num_context_features(
            model_config),
        context_feature_length=config_util.get_context_feature_length(
            model_config))
    include_source_id = eval_input_config.include_source_id
    return (_get_features_dict(tensor_dict, include_source_id),
            _get_labels_dict(tensor_dict))
1068
1069
1070

  reduce_to_frame_fn = get_reduce_to_frame_fn(eval_input_config, False)

1071
1072
1073
  dataset = INPUT_BUILDER_UTIL_MAP['dataset_build'](
      eval_input_config,
      batch_size=params['batch_size'] if params else eval_config.batch_size,
1074
      transform_input_data_fn=transform_and_pad_input_data_fn,
1075
      input_context=input_context,
1076
      reduce_to_frame_fn=reduce_to_frame_fn)
1077
  return dataset
1078
1079


1080
def create_predict_input_fn(model_config, predict_input_config):
1081
1082
  """Creates a predict `input` function for `Estimator`.

1083
1084
  Args:
    model_config: A model_pb2.DetectionModel.
1085
    predict_input_config: An input_reader_pb2.InputReader.
1086

1087
1088
1089
1090
  Returns:
    `input_fn` for `Estimator` in PREDICT mode.
  """

1091
  def _predict_input_fn(params=None):
1092
1093
    """Decodes serialized tf.Examples and returns `ServingInputReceiver`.

1094
1095
1096
    Args:
      params: Parameter dictionary passed from the estimator.

1097
1098
1099
    Returns:
      `ServingInputReceiver`.
    """
1100
    del params
1101
    example = tf.placeholder(dtype=tf.string, shape=[], name='tf_example')
1102

1103
    num_classes = config_util.get_number_of_classes(model_config)
1104
1105
1106
    model_preprocess_fn = INPUT_BUILDER_UTIL_MAP['model_build'](
        model_config, is_training=False).preprocess

1107
1108
    image_resizer_config = config_util.get_image_resizer_config(model_config)
    image_resizer_fn = image_resizer_builder.build(image_resizer_config)
1109

1110
    transform_fn = functools.partial(
1111
        transform_input_data, model_preprocess_fn=model_preprocess_fn,
1112
1113
1114
1115
        image_resizer_fn=image_resizer_fn,
        num_classes=num_classes,
        data_augmentation_fn=None)

1116
1117
1118
    decoder = tf_example_decoder.TfExampleDecoder(
        load_instance_masks=False,
        num_additional_channels=predict_input_config.num_additional_channels)
1119
    input_dict = transform_fn(decoder.decode(example))
1120
    images = tf.cast(input_dict[fields.InputDataFields.image], dtype=tf.float32)
1121
    images = tf.expand_dims(images, axis=0)
1122
1123
    true_image_shape = tf.expand_dims(
        input_dict[fields.InputDataFields.true_image_shape], axis=0)
1124

1125
    return tf_estimator.export.ServingInputReceiver(
1126
1127
1128
        features={
            fields.InputDataFields.image: images,
            fields.InputDataFields.true_image_shape: true_image_shape},
1129
1130
1131
        receiver_tensors={SERVING_FED_EXAMPLE_KEY: example})

  return _predict_input_fn
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152


def get_reduce_to_frame_fn(input_reader_config, is_training):
  """Returns a function reducing sequence tensors to single frame tensors.

  If the input type is not TF_SEQUENCE_EXAMPLE, the tensors are passed through
  this function unchanged. Otherwise, when in training mode, a single frame is
  selected at random from the sequence example, and the tensors for that frame
  are converted to single frame tensors, with all associated context features.
  In evaluation mode all frames are converted to single frame tensors with
  copied context tensors. After the sequence example tensors are converted into
  one or many single frame tensors, the images from each frame are decoded.

  Args:
    input_reader_config: An input_reader_pb2.InputReader.
    is_training: Whether we are in training mode.

  Returns:
    `reduce_to_frame_fn` for the dataset builder
  """
  if input_reader_config.input_type != (
1153
1154
      input_reader_pb2.InputType.Value('TF_SEQUENCE_EXAMPLE')):
    return lambda dataset, dataset_map_fn, batch_size, config: dataset
1155
  else:
1156
1157
    def reduce_to_frame(dataset, dataset_map_fn, batch_size,
                        input_reader_config):
1158
1159
1160
1161
      """Returns a function reducing sequence tensors to single frame tensors.

      Args:
        dataset: A tf dataset containing sequence tensors.
1162
1163
1164
1165
1166
1167
        dataset_map_fn: A function that handles whether to
          map_with_legacy_function for this dataset
        batch_size: used if map_with_legacy_function is true to determine
          num_parallel_calls
        input_reader_config: used if map_with_legacy_function is true to
          determine num_parallel_calls
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188

      Returns:
        A tf dataset containing single frame tensors.
      """
      if is_training:
        def get_single_frame(tensor_dict):
          """Returns a random frame from a sequence.

          Picks a random frame and returns slices of sequence tensors
          corresponding to the random frame. Returns non-sequence tensors
          unchanged.

          Args:
            tensor_dict: A dictionary containing sequence tensors.

          Returns:
            Tensors for a single random frame within the sequence.
          """
          num_frames = tf.cast(
              tf.shape(tensor_dict[fields.InputDataFields.source_id])[0],
              dtype=tf.int32)
1189
1190
1191
1192
1193
1194
          if input_reader_config.frame_index == -1:
            frame_index = tf.random.uniform((), minval=0, maxval=num_frames,
                                            dtype=tf.int32)
          else:
            frame_index = tf.constant(input_reader_config.frame_index,
                                      dtype=tf.int32)
1195
1196
1197
1198
1199
1200
1201
1202
1203
          out_tensor_dict = {}
          for key in tensor_dict:
            if key in fields.SEQUENCE_FIELDS:
              # Slice random frame from sequence tensors
              out_tensor_dict[key] = tensor_dict[key][frame_index]
            else:
              # Copy all context tensors.
              out_tensor_dict[key] = tensor_dict[key]
          return out_tensor_dict
1204
1205
        dataset = dataset_map_fn(dataset, get_single_frame, batch_size,
                                 input_reader_config)
1206
      else:
1207
1208
        dataset = dataset_map_fn(dataset, util_ops.tile_context_tensors,
                                 batch_size, input_reader_config)
1209
1210
        dataset = dataset.unbatch()
      # Decode frame here as SequenceExample tensors contain encoded images.
1211
1212
      dataset = dataset_map_fn(dataset, util_ops.decode_image, batch_size,
                               input_reader_config)
1213
1214
      return dataset
    return reduce_to_frame