• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015 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 
CropAndResize(int batches,int width,int height,int depth,int crop_height,int crop_width)24 static Graph* CropAndResize(int batches, int width, int height, int depth,
25                             int crop_height, int crop_width) {
26   Graph* g = new Graph(OpRegistry::Global());
27   Tensor in(DT_FLOAT, TensorShape({batches, height, width, depth}));
28   in.flat<float>().setRandom();
29   Tensor boxes(DT_FLOAT, TensorShape({batches, 4}));
30   auto boxes_tensor = boxes.matrix<float>();
31   Tensor box_ind(DT_INT32, TensorShape({batches}));
32   auto box_ind_flat = box_ind.flat<int32>();
33   for (int i = 0; i < batches; ++i) {
34     boxes_tensor(i, 0) = 0.2;
35     boxes_tensor(i, 1) = 0.2;
36     boxes_tensor(i, 2) = 0.8;
37     boxes_tensor(i, 3) = 0.7;
38     box_ind_flat(i) = i;
39   }
40   Tensor crop_size(DT_INT32, TensorShape({2}));
41   auto crop_size_flat = crop_size.flat<int32>();
42   crop_size_flat(0) = crop_height;
43   crop_size_flat(1) = crop_width;
44   Node* ret;
45   TF_CHECK_OK(NodeBuilder(g->NewName("n"), "CropAndResize")
46                   .Input(test::graph::Constant(g, in))
47                   .Input(test::graph::Constant(g, boxes))
48                   .Input(test::graph::Constant(g, box_ind))
49                   .Input(test::graph::Constant(g, crop_size))
50                   .Finalize(g, &ret));
51   return g;
52 }
53 
54 #define BM_CropAndResizeDev(DEVICE, B, W, H, D, CH, CW)                        \
55   static void BM_CropAndResize_##DEVICE##_##B##_##W##_##H##_##D##_##CH##_##CW( \
56       ::testing::benchmark::State& state) {                                    \
57     test::Benchmark(#DEVICE, CropAndResize(B, W, H, D, CH, CW),                \
58                     /*old_benchmark_api*/ false)                               \
59         .Run(state);                                                           \
60     state.SetItemsProcessed(state.iterations() * B * W * H * D);               \
61   }                                                                            \
62   BENCHMARK(BM_CropAndResize_##DEVICE##_##B##_##W##_##H##_##D##_##CH##_##CW);
63 
64 // Benchmark results using CPU:Intel Haswell with HyperThreading (6 cores)
65 // Benchmark                                Time(ns) CPU(ns)  Iterations
66 // BM_CropAndResize_cpu_1_640_640_3_512_512 7078765 7173520 100 163.361M items/s
67 // BM_CropAndResize_cpu_1_640_640_1_512_512 3801232 3914692 185  99.784M items/s
68 // BM_CropAndResize_cpu_1_80_80_512_7_7      182470  241767 2941  1.372G items/s
69 
70 BM_CropAndResizeDev(cpu, 1, 640, 640, 3, 512, 512);
71 BM_CropAndResizeDev(cpu, 1, 640, 640, 1, 512, 512);
72 BM_CropAndResizeDev(cpu, 1, 80, 80, 512, 7, 7);
73 
74 }  // namespace tensorflow
75