utils.py 2.31 KB
Newer Older
Yeqing Li's avatar
Yeqing Li committed
1
# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
Abdullah Rashwan's avatar
Abdullah Rashwan 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.
Yeqing Li's avatar
Yeqing Li committed
14

Abdullah Rashwan's avatar
Abdullah Rashwan committed
15
"""Data loader utils."""
Fan Yang's avatar
Fan Yang committed
16
from typing import Dict
Abdullah Rashwan's avatar
Abdullah Rashwan committed
17
18
19
20
21
22
23

# Import libraries
import tensorflow as tf

from official.vision.beta.ops import preprocess_ops


Fan Yang's avatar
Fan Yang committed
24
25
26
27
28
29
30
31
32
def process_source_id(source_id: tf.Tensor) -> tf.Tensor:
  """Processes source_id to the right format.

  Args:
    source_id: A `tf.Tensor` that contains the source ID. It can be empty.

  Returns:
    A formatted source ID.
  """
Abdullah Rashwan's avatar
Abdullah Rashwan committed
33
  if source_id.dtype == tf.string:
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
34
    source_id = tf.cast(tf.strings.to_number(source_id), tf.int64)
Abdullah Rashwan's avatar
Abdullah Rashwan committed
35
36
37
  with tf.control_dependencies([source_id]):
    source_id = tf.cond(
        pred=tf.equal(tf.size(input=source_id), 0),
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
38
        true_fn=lambda: tf.cast(tf.constant(-1), tf.int64),
Abdullah Rashwan's avatar
Abdullah Rashwan committed
39
40
41
42
        false_fn=lambda: tf.identity(source_id))
  return source_id


Fan Yang's avatar
Fan Yang committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def pad_groundtruths_to_fixed_size(groundtruths: Dict[str, tf.Tensor],
                                   size: int) -> Dict[str, tf.Tensor]:
  """Pads the first dimension of groundtruths labels to the fixed size.

  Args:
    groundtruths: A dictionary of {`str`: `tf.Tensor`} that contains groundtruth
      annotations of `boxes`, `is_crowds`, `areas` and `classes`.
    size: An `int` that specifies the expected size of the first dimension of
      padded tensors.

  Returns:
    A dictionary of the same keys as input and padded tensors as values.

  """
Abdullah Rashwan's avatar
Abdullah Rashwan committed
57
58
59
60
61
62
63
64
65
  groundtruths['boxes'] = preprocess_ops.clip_or_pad_to_fixed_size(
      groundtruths['boxes'], size, -1)
  groundtruths['is_crowds'] = preprocess_ops.clip_or_pad_to_fixed_size(
      groundtruths['is_crowds'], size, 0)
  groundtruths['areas'] = preprocess_ops.clip_or_pad_to_fixed_size(
      groundtruths['areas'], size, -1)
  groundtruths['classes'] = preprocess_ops.clip_or_pad_to_fixed_size(
      groundtruths['classes'], size, -1)
  return groundtruths