• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 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/lite/c/common.h"
17 #include "tensorflow/lite/micro/all_ops_resolver.h"
18 #include "tensorflow/lite/micro/benchmarks/micro_benchmark.h"
19 #include "tensorflow/lite/micro/examples/person_detection/model_settings.h"
20 #include "tensorflow/lite/micro/examples/person_detection/no_person_image_data.h"
21 #include "tensorflow/lite/micro/examples/person_detection/person_detect_model_data.h"
22 #include "tensorflow/lite/micro/examples/person_detection/person_image_data.h"
23 #include "tensorflow/lite/micro/micro_error_reporter.h"
24 #include "tensorflow/lite/micro/micro_interpreter.h"
25 #include "tensorflow/lite/micro/micro_utils.h"
26 #include "tensorflow/lite/micro/system_setup.h"
27 #include "tensorflow/lite/schema/schema_generated.h"
28 
29 /*
30  * Person Detection benchmark.  Evaluates runtime performance of the visual
31  * wakewords person detection model.  This is the same model found in
32  * exmaples/person_detection.
33  */
34 
35 namespace tflite {
36 
37 using PersonDetectionOpResolver = tflite::AllOpsResolver;
38 using PersonDetectionBenchmarkRunner = MicroBenchmarkRunner<int8_t>;
39 
40 // Create an area of memory to use for input, output, and intermediate arrays.
41 // Align arena to 16 bytes to avoid alignment warnings on certain platforms.
42 constexpr int kTensorArenaSize = 135 * 1024;
43 alignas(16) uint8_t tensor_arena[kTensorArenaSize];
44 
45 uint8_t op_resolver_buffer[sizeof(PersonDetectionOpResolver)];
46 uint8_t benchmark_runner_buffer[sizeof(PersonDetectionBenchmarkRunner)];
47 
48 // Initialize benchmark runner instance explicitly to avoid global init order
49 // issues on Sparkfun. Use new since static variables within a method
50 // are automatically surrounded by locking, which breaks bluepill and stm32f4.
CreateBenchmarkRunner(MicroProfiler * profiler)51 PersonDetectionBenchmarkRunner* CreateBenchmarkRunner(MicroProfiler* profiler) {
52   // We allocate PersonDetectionOpResolver from a global buffer
53   // because the object's lifetime must exceed that of the
54   // PersonDetectionBenchmarkRunner object.
55   return new (benchmark_runner_buffer) PersonDetectionBenchmarkRunner(
56       g_person_detect_model_data,
57       new (op_resolver_buffer) PersonDetectionOpResolver(), tensor_arena,
58       kTensorArenaSize, profiler);
59 }
60 
PersonDetectionNIerations(const int8_t * input,int iterations,const char * tag,PersonDetectionBenchmarkRunner & benchmark_runner,MicroProfiler & profiler)61 void PersonDetectionNIerations(const int8_t* input, int iterations,
62                                const char* tag,
63                                PersonDetectionBenchmarkRunner& benchmark_runner,
64                                MicroProfiler& profiler) {
65   benchmark_runner.SetInput(input);
66   int32_t ticks = 0;
67   for (int i = 0; i < iterations; ++i) {
68     profiler.ClearEvents();
69     benchmark_runner.RunSingleIteration();
70     ticks += profiler.GetTotalTicks();
71   }
72   MicroPrintf("%s took %d ticks (%d ms)", tag, ticks, TicksToMs(ticks));
73 }
74 
75 }  // namespace tflite
76 
main(int argc,char ** argv)77 int main(int argc, char** argv) {
78   tflite::InitializeTarget();
79 
80   tflite::MicroProfiler profiler;
81 
82   uint32_t event_handle = profiler.BeginEvent("InitializeBenchmarkRunner");
83   tflite::PersonDetectionBenchmarkRunner* benchmark_runner =
84       CreateBenchmarkRunner(&profiler);
85   profiler.EndEvent(event_handle);
86   profiler.Log();
87   MicroPrintf("");  // null MicroPrintf serves as a newline.
88 
89   tflite::PersonDetectionNIerations(
90       reinterpret_cast<const int8_t*>(g_person_data), 1,
91       "WithPersonDataIterations(1)", *benchmark_runner, profiler);
92   profiler.Log();
93   MicroPrintf("");  // null MicroPrintf serves as a newline.
94 
95   tflite::PersonDetectionNIerations(
96       reinterpret_cast<const int8_t*>(g_no_person_data), 1,
97       "NoPersonDataIterations(1)", *benchmark_runner, profiler);
98   profiler.Log();
99   MicroPrintf("");  // null MicroPrintf serves as a newline.
100 
101   tflite::PersonDetectionNIerations(
102       reinterpret_cast<const int8_t*>(g_person_data), 10,
103       "WithPersonDataIterations(10)", *benchmark_runner, profiler);
104   MicroPrintf("");  // null MicroPrintf serves as a newline.
105 
106   tflite::PersonDetectionNIerations(
107       reinterpret_cast<const int8_t*>(g_no_person_data), 10,
108       "NoPersonDataIterations(10)", *benchmark_runner, profiler);
109   MicroPrintf("");  // null MicroPrintf serves as a newline.
110 }
111