This repository was archived by the owner on Mar 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 327
Expand file tree
/
Copy pathto_ragged.py
More file actions
86 lines (70 loc) · 2.96 KB
/
to_ragged.py
File metadata and controls
86 lines (70 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright 2022 The KerasCV Authors
#
# 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
#
# https://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.
import tensorflow as tf
import keras_cv.bounding_box.validate_format as validate_format
def to_ragged(bounding_boxes, sentinel=-1, dtype=tf.float32):
"""converts a Dense padded bounding box `tf.Tensor` to a `tf.RaggedTensor`.
Bounding boxes are ragged tensors in most use cases. Converting them to a
dense tensor makes it easier to work with Tensorflow ecosystem.
This function can be used to filter out the masked out bounding boxes by
checking for padded sentinel value of the class_id axis of the
bounding_boxes.
Usage:
```python
bounding_boxes = {
"boxes": tf.constant([[2, 3, 4, 5], [0, 1, 2, 3]]),
"classes": tf.constant([[-1, 1]]),
}
bounding_boxes = bounding_box.to_ragged(bounding_boxes)
print(bounding_boxes)
# {
# "boxes": [[0, 1, 2, 3]],
# "classes": [[1]]
# }
```
Args:
bounding_boxes: a Tensor of bounding boxes. May be batched, or
unbatched.
sentinel: The value indicating that a bounding box does not exist at the
current index, and the corresponding box is padding.
Defaults to `-1`.
dtype: the data type to use for the underlying Tensors.
Returns:
dictionary of `tf.RaggedTensor` or 'tf.Tensor' containing the filtered
bounding boxes.
"""
info = validate_format.validate_format(bounding_boxes)
if info["ragged"]:
return bounding_boxes
boxes = bounding_boxes.get("boxes")
classes = bounding_boxes.get("classes")
confidence = bounding_boxes.get("confidence", None)
mask = classes != sentinel
boxes = tf.ragged.boolean_mask(boxes, mask)
classes = tf.ragged.boolean_mask(classes, mask)
if confidence is not None:
confidence = tf.ragged.boolean_mask(confidence, mask)
if isinstance(boxes, tf.Tensor):
boxes = tf.RaggedTensor.from_tensor(boxes)
if isinstance(classes, tf.Tensor) and len(classes.shape) > 1:
classes = tf.RaggedTensor.from_tensor(classes)
if confidence is not None:
if isinstance(confidence, tf.Tensor) and len(confidence.shape) > 1:
confidence = tf.RaggedTensor.from_tensor(confidence)
result = bounding_boxes.copy()
result["boxes"] = tf.cast(boxes, dtype)
result["classes"] = tf.cast(classes, dtype)
if confidence is not None:
result["confidence"] = tf.cast(confidence, dtype)
return result