1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/core/common_runtime/kernel_benchmark_testlib.h"
17 #include "tensorflow/core/framework/tensor.h"
18 #include "tensorflow/core/graph/node_builder.h"
19 #include "tensorflow/core/platform/test.h"
20 #include "tensorflow/core/platform/test_benchmark.h"
21
22 namespace tensorflow {
23
CombinedNonMaxSuppression(int batches,int box_num,int class_num,int q)24 static Graph* CombinedNonMaxSuppression(int batches, int box_num, int class_num,
25 int q) {
26 Graph* g = new Graph(OpRegistry::Global());
27 Tensor boxes(DT_FLOAT, TensorShape({batches, box_num, q, 4}));
28 boxes.flat<float>().setRandom();
29 Tensor scores(DT_FLOAT, TensorShape({batches, box_num, class_num}));
30 scores.flat<float>().setRandom();
31
32 Tensor max_output_size_per_class(100);
33 Tensor max_total_size(9000);
34 Tensor iou_threshold(float(0.3));
35 Tensor score_threshold(float(0.25));
36
37 Node* ret;
38 TF_CHECK_OK(NodeBuilder(g->NewName("n"), "CombinedNonMaxSuppression")
39 .Input(test::graph::Constant(g, boxes))
40 .Input(test::graph::Constant(g, scores))
41 .Input(test::graph::Constant(g, max_output_size_per_class))
42 .Input(test::graph::Constant(g, max_total_size))
43 .Input(test::graph::Constant(g, iou_threshold))
44 .Input(test::graph::Constant(g, score_threshold))
45 .Attr("pad_per_class", false)
46 .Attr("clip_boxes", true)
47 .Finalize(g, &ret));
48 return g;
49 }
50
51 #define BM_CombinedNonMaxSuppressionDev(DEVICE, B, BN, CN, Q) \
52 static void BM_CombinedNMS_##DEVICE##_##B##_##BN##_##CN##_##Q( \
53 ::testing::benchmark::State& state) { \
54 test::Benchmark(#DEVICE, CombinedNonMaxSuppression(B, BN, CN, Q), \
55 /*old_benchmark_api*/ false) \
56 .Run(state); \
57 state.SetItemsProcessed(state.iterations() * B); \
58 } \
59 BENCHMARK(BM_CombinedNMS_##DEVICE##_##B##_##BN##_##CN##_##Q);
60
61 #define BM_Batch(BN, CN, Q) \
62 BM_CombinedNonMaxSuppressionDev(cpu, 1, BN, CN, Q); \
63 BM_CombinedNonMaxSuppressionDev(cpu, 28, BN, CN, Q); \
64 BM_CombinedNonMaxSuppressionDev(cpu, 32, BN, CN, Q); \
65 BM_CombinedNonMaxSuppressionDev(cpu, 64, BN, CN, Q);
66
67 #define BN_Boxes_Number(CN, Q) \
68 BM_Batch(500, CN, Q); \
69 BM_Batch(1000, CN, Q); \
70 BM_Batch(1917, CN, Q); \
71 BM_Batch(2500, CN, Q);
72
73 BN_Boxes_Number(25, 1);
74 BN_Boxes_Number(25, 25);
75 BN_Boxes_Number(90, 1);
76 BN_Boxes_Number(90, 90);
77 BN_Boxes_Number(200, 1);
78 BN_Boxes_Number(200, 200);
79
80 } // namespace tensorflow
81