-
Notifications
You must be signed in to change notification settings - Fork 74k
/
draw_bounding_box_op.cc
247 lines (222 loc) · 10.4 KB
/
draw_bounding_box_op.cc
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* Copyright 2015 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.
==============================================================================*/
// See ../ops/image_ops.cc for details.
#define EIGEN_USE_THREADS
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/register_types.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/tensor_shape.h"
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/platform/logging.h"
namespace tensorflow {
namespace {
std::vector<std::vector<float>> DefaultColorTable(int depth) {
std::vector<std::vector<float>> color_table;
color_table.emplace_back(std::vector<float>({1, 1, 0, 1})); // 0: yellow
color_table.emplace_back(std::vector<float>({0, 0, 1, 1})); // 1: blue
color_table.emplace_back(std::vector<float>({1, 0, 0, 1})); // 2: red
color_table.emplace_back(std::vector<float>({0, 1, 0, 1})); // 3: lime
color_table.emplace_back(std::vector<float>({0.5, 0, 0.5, 1})); // 4: purple
color_table.emplace_back(std::vector<float>({0.5, 0.5, 0, 1})); // 5: olive
color_table.emplace_back(std::vector<float>({0.5, 0, 0, 1})); // 6: maroon
color_table.emplace_back(std::vector<float>({0, 0, 0.5, 1})); // 7: navy blue
color_table.emplace_back(std::vector<float>({0, 1, 1, 1})); // 8: aqua
color_table.emplace_back(std::vector<float>({1, 0, 1, 1})); // 9: fuchsia
if (depth == 1) {
for (int64_t i = 0; i < color_table.size(); i++) {
color_table[i][0] = 1;
}
}
return color_table;
}
} // namespace
template <class T>
class DrawBoundingBoxesOp : public OpKernel {
public:
explicit DrawBoundingBoxesOp(OpKernelConstruction* context)
: OpKernel(context) {}
void Compute(OpKernelContext* context) override {
const Tensor& images = context->input(0);
const Tensor& boxes = context->input(1);
const int64_t depth = images.dim_size(3);
OP_REQUIRES(context, images.dims() == 4,
errors::InvalidArgument("The rank of the images should be 4"));
OP_REQUIRES(
context, boxes.dims() == 3,
errors::InvalidArgument("The rank of the boxes tensor should be 3"));
OP_REQUIRES(context, images.dim_size(0) == boxes.dim_size(0),
errors::InvalidArgument("The batch sizes should be the same"));
OP_REQUIRES(
context, depth == 4 || depth == 1 || depth == 3,
errors::InvalidArgument("Channel depth should be either 1 (GRY), "
"3 (RGB), or 4 (RGBA)"));
OP_REQUIRES(
context, boxes.dim_size(2) == 4,
errors::InvalidArgument(
"The size of the third dimension of the box must be 4. Received: ",
boxes.dim_size(2)));
const int64_t batch_size = images.dim_size(0);
const int64_t height = images.dim_size(1);
const int64_t width = images.dim_size(2);
std::vector<std::vector<float>> color_table;
if (context->num_inputs() == 3) {
const Tensor& colors_tensor = context->input(2);
OP_REQUIRES(context, colors_tensor.shape().dims() == 2,
errors::InvalidArgument("colors must be a 2-D matrix",
colors_tensor.shape().DebugString()));
OP_REQUIRES(context, colors_tensor.shape().dim_size(1) >= depth,
errors::InvalidArgument("colors must have equal or more ",
"channels than the image provided: ",
colors_tensor.shape().DebugString()));
if (colors_tensor.NumElements() != 0) {
color_table.clear();
auto colors = colors_tensor.matrix<float>();
for (int64_t i = 0; i < colors.dimension(0); i++) {
std::vector<float> color_value(depth);
for (int64_t j = 0; j < depth; j++) {
color_value[j] = colors(i, j);
}
color_table.emplace_back(color_value);
}
}
}
if (color_table.empty()) {
color_table = DefaultColorTable(depth);
}
Tensor* output;
OP_REQUIRES_OK(
context,
context->allocate_output(
0, TensorShape({batch_size, height, width, depth}), &output));
output->tensor<T, 4>() = images.tensor<T, 4>();
auto canvas = output->tensor<T, 4>();
for (int64_t b = 0; b < batch_size; ++b) {
const int64_t num_boxes = boxes.dim_size(1);
const auto tboxes = boxes.tensor<float, 3>();
for (int64_t bb = 0; bb < num_boxes; ++bb) {
int64_t color_index = bb % color_table.size();
const int64_t min_box_row =
static_cast<float>(tboxes(b, bb, 0)) * (height - 1);
const int64_t min_box_row_clamp =
std::max<int64_t>(min_box_row, int64_t{0});
const int64_t max_box_row =
static_cast<float>(tboxes(b, bb, 2)) * (height - 1);
const int64_t max_box_row_clamp =
std::min<int64_t>(max_box_row, height - 1);
const int64_t min_box_col =
static_cast<float>(tboxes(b, bb, 1)) * (width - 1);
const int64_t min_box_col_clamp =
std::max<int64_t>(min_box_col, int64_t{0});
const int64_t max_box_col =
static_cast<float>(tboxes(b, bb, 3)) * (width - 1);
const int64_t max_box_col_clamp =
std::min<int64_t>(max_box_col, width - 1);
if (min_box_row > max_box_row || min_box_col > max_box_col) {
LOG(WARNING) << "Bounding box (" << min_box_row << "," << min_box_col
<< "," << max_box_row << "," << max_box_col
<< ") is inverted and will not be drawn.";
continue;
}
if (min_box_row >= height || max_box_row < 0 || min_box_col >= width ||
max_box_col < 0) {
LOG(WARNING) << "Bounding box (" << min_box_row << "," << min_box_col
<< "," << max_box_row << "," << max_box_col
<< ") is completely outside the image"
<< " and will not be drawn.";
continue;
}
// At this point, {min,max}_box_{row,col}_clamp are inside the
// image.
OP_REQUIRES(
context, min_box_row_clamp >= 0,
errors::InvalidArgument("Min box row clamp is less than 0."));
OP_REQUIRES(
context, max_box_row_clamp >= 0,
errors::InvalidArgument("Max box row clamp is less than 0."));
OP_REQUIRES(context, min_box_row_clamp <= height,
errors::InvalidArgument(
"Min box row clamp is greater than height."));
OP_REQUIRES(context, max_box_row_clamp <= height,
errors::InvalidArgument(
"Max box row clamp is greater than height."));
OP_REQUIRES(
context, min_box_col_clamp >= 0,
errors::InvalidArgument("Min box col clamp is less than 0."));
OP_REQUIRES(
context, max_box_col_clamp >= 0,
errors::InvalidArgument("Max box col clamp is less than 0."));
OP_REQUIRES(context, min_box_col_clamp <= width,
errors::InvalidArgument(
"Min box col clamp is greater than width."));
OP_REQUIRES(context, max_box_col_clamp <= width,
errors::InvalidArgument(
"Max box col clamp is greater than width."));
// At this point, the min_box_row and min_box_col are either
// in the image or above/left of it, and max_box_row and
// max_box_col are either in the image or below/right or it.
OP_REQUIRES(
context, min_box_row <= height,
errors::InvalidArgument("Min box row is greater than height."));
OP_REQUIRES(context, max_box_row >= 0,
errors::InvalidArgument("Max box row is less than 0."));
OP_REQUIRES(
context, min_box_col <= width,
errors::InvalidArgument("Min box col is greater than width."));
OP_REQUIRES(context, max_box_col >= 0,
errors::InvalidArgument("Max box col is less than 0."));
// Draw top line.
if (min_box_row >= 0) {
for (int64_t j = min_box_col_clamp; j <= max_box_col_clamp; ++j)
for (int64_t c = 0; c < depth; c++) {
canvas(b, min_box_row, j, c) =
static_cast<T>(color_table[color_index][c]);
}
}
// Draw bottom line.
if (max_box_row < height) {
for (int64_t j = min_box_col_clamp; j <= max_box_col_clamp; ++j)
for (int64_t c = 0; c < depth; c++) {
canvas(b, max_box_row, j, c) =
static_cast<T>(color_table[color_index][c]);
}
}
// Draw left line.
if (min_box_col >= 0) {
for (int64_t i = min_box_row_clamp; i <= max_box_row_clamp; ++i)
for (int64_t c = 0; c < depth; c++) {
canvas(b, i, min_box_col, c) =
static_cast<T>(color_table[color_index][c]);
}
}
// Draw right line.
if (max_box_col < width) {
for (int64_t i = min_box_row_clamp; i <= max_box_row_clamp; ++i)
for (int64_t c = 0; c < depth; c++) {
canvas(b, i, max_box_col, c) =
static_cast<T>(color_table[color_index][c]);
}
}
}
}
}
};
#define REGISTER_CPU_KERNEL(T) \
REGISTER_KERNEL_BUILDER( \
Name("DrawBoundingBoxes").Device(DEVICE_CPU).TypeConstraint<T>("T"), \
DrawBoundingBoxesOp<T>); \
REGISTER_KERNEL_BUILDER( \
Name("DrawBoundingBoxesV2").Device(DEVICE_CPU).TypeConstraint<T>("T"), \
DrawBoundingBoxesOp<T>);
TF_CALL_half(REGISTER_CPU_KERNEL);
TF_CALL_float(REGISTER_CPU_KERNEL);
} // namespace tensorflow