tflite_micro_person_detection_init

This commit is contained in:
yangqingsheng
2020-12-08 17:16:20 +08:00
parent 55168d954d
commit 200c0ff460
310 changed files with 121982 additions and 208 deletions

View File

@@ -0,0 +1,22 @@
/* Copyright 2020 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.
==============================================================================*/
#ifndef TENSORFLOW_LITE_MICRO_BENCHMARKS_KEYWORD_SCRAMBLED_MODEL_DATA_H_
#define TENSORFLOW_LITE_MICRO_BENCHMARKS_KEYWORD_SCRAMBLED_MODEL_DATA_H_
extern const unsigned char g_keyword_scrambled_model_data[];
extern const unsigned int g_keyword_scrambled_model_data_length;
#endif // TENSORFLOW_LITE_MICRO_BENCHMARKS_KEYWORD_SCRAMBLED_MODEL_DATA_H_

View File

@@ -0,0 +1,121 @@
/* Copyright 2020 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.
==============================================================================*/
#ifndef TENSORFLOW_LITE_MICRO_BENCHMARKS_MICRO_BENCHMARK_H_
#define TENSORFLOW_LITE_MICRO_BENCHMARKS_MICRO_BENCHMARK_H_
#include <climits>
#include "tensorflow/lite/micro/micro_error_reporter.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/micro/micro_op_resolver.h"
#include "tensorflow/lite/micro/micro_time.h"
namespace micro_benchmark {
extern tflite::ErrorReporter* reporter;
} // namespace micro_benchmark
#define TF_LITE_MICRO_BENCHMARKS_BEGIN \
namespace micro_benchmark { \
tflite::ErrorReporter* reporter; \
} \
\
int main(int argc, char** argv) { \
tflite::MicroErrorReporter error_reporter; \
micro_benchmark::reporter = &error_reporter; \
int32_t start_ticks; \
int32_t duration_ticks; \
int32_t duration_ms; \
HAL_Init();\
SystemClock_Config();\
board_init();\
#define TF_LITE_MICRO_BENCHMARKS_END \
return 0; \
}
#define TF_LITE_MICRO_BENCHMARK(func) \
if (tflite::ticks_per_second() == 0) { \
TF_LITE_REPORT_ERROR(micro_benchmark::reporter, \
"no timer implementation found"); \
return 0; \
} \
start_ticks = tflite::GetCurrentTimeTicks(); \
func; \
duration_ticks = tflite::GetCurrentTimeTicks() - start_ticks; \
if (duration_ticks > INT_MAX / 1000) { \
duration_ms = duration_ticks / (tflite::ticks_per_second() / 1000); \
} else { \
duration_ms = (duration_ticks * 1000) / tflite::ticks_per_second(); \
} \
micro_benchmark::reporter->Report("%s took %d ticks (%d ms)", #func, \
duration_ticks, duration_ms);
template <typename inputT>
class MicroBenchmarkRunner {
public:
// The lifetimes of model, op_resolver and tensor_arena must exceed that of
// the created MicroBenchmarkRunner object.
MicroBenchmarkRunner(const uint8_t* model,
const tflite::MicroOpResolver* op_resolver,
uint8_t* tensor_arena, int tensor_arena_size)
: model_(tflite::GetModel(model)),
reporter_(&micro_reporter_),
interpreter_(model_, *op_resolver, tensor_arena, tensor_arena_size,
reporter_) {
interpreter_.AllocateTensors();
}
void RunSingleIteration() {
// Run the model on this input and make sure it succeeds.
TfLiteStatus invoke_status = interpreter_.Invoke();
if (invoke_status != kTfLiteOk) {
TF_LITE_REPORT_ERROR(reporter_, "Invoke failed.");
}
}
void SetRandomInput(const int random_seed) {
// The pseudo-random number generator is initialized to a constant seed
std::srand(random_seed);
TfLiteTensor* input = interpreter_.input(0);
// Pre-populate input tensor with random values.
int input_length = input->bytes / sizeof(inputT);
inputT* input_values = tflite::GetTensorData<inputT>(input);
for (int i = 0; i < input_length; i++) {
// Pre-populate input tensor with a random value based on a constant seed.
input_values[i] = static_cast<inputT>(
std::rand() % (std::numeric_limits<inputT>::max() -
std::numeric_limits<inputT>::min() + 1));
}
}
void SetInput(const inputT* custom_input) {
TfLiteTensor* input = interpreter_.input(0);
inputT* input_buffer = tflite::GetTensorData<inputT>(input);
int input_length = input->bytes / sizeof(inputT);
for (int i = 0; i < input_length; i++) {
input_buffer[i] = custom_input[i];
}
}
private:
const tflite::Model* model_;
tflite::MicroErrorReporter micro_reporter_;
tflite::ErrorReporter* reporter_;
tflite::MicroInterpreter interpreter_;
};
#endif // TENSORFLOW_LITE_MICRO_BENCHMARKS_MICRO_BENCHMARK_H_

View File

@@ -0,0 +1,92 @@
/* Copyright 2019 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.
==============================================================================*/
#include "tensorflow/lite/c/common.h"
#include "tensorflow/lite/micro/all_ops_resolver.h"
#include "tensorflow/lite/micro/benchmarks/micro_benchmark.h"
#include "tensorflow/lite/micro/examples/person_detection_experimental/model_settings.h"
#include "tensorflow/lite/micro/examples/person_detection_experimental/no_person_image_data.h"
#include "tensorflow/lite/micro/examples/person_detection_experimental/person_detect_model_data.h"
#include "tensorflow/lite/micro/examples/person_detection_experimental/person_image_data.h"
#include "tensorflow/lite/micro/micro_error_reporter.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/micro/micro_utils.h"
#include "tensorflow/lite/schema/schema_generated.h"
#include "tensorflow/lite/version.h"
/*
* Person Detection benchmark. Evaluates runtime performance of the visual
* wakewords person detection model. This is the same model found in
* exmaples/person_detection.
*/
namespace {
using PersonDetectionExperimentalOpResolver = tflite::AllOpsResolver;
using PersonDetectionExperimentalBenchmarkRunner = MicroBenchmarkRunner<int8_t>;
// Create an area of memory to use for input, output, and intermediate arrays.
// Align arena to 16 bytes to avoid alignment warnings on certain platforms.
constexpr int kTensorArenaSize = 135 * 1024;
alignas(16) uint8_t tensor_arena[kTensorArenaSize];
uint8_t op_resolver_buffer[sizeof(PersonDetectionExperimentalOpResolver)];
uint8_t
benchmark_runner_buffer[sizeof(PersonDetectionExperimentalBenchmarkRunner)];
PersonDetectionExperimentalBenchmarkRunner* benchmark_runner = nullptr;
// Initialize benchmark runner instance explicitly to avoid global init order
// issues on Sparkfun. Use new since static variables within a method
// are automatically surrounded by locking, which breaks bluepill and stm32f4.
void CreateBenchmarkRunner() {
// We allocate PersonDetectionExperimentalOpResolver from a global buffer
// because the object's lifetime must exceed that of the
// PersonDetectionBenchmarkRunner object.
benchmark_runner =
new (benchmark_runner_buffer) PersonDetectionExperimentalBenchmarkRunner(
g_person_detect_model_data,
new (op_resolver_buffer) PersonDetectionExperimentalOpResolver(),
tensor_arena, kTensorArenaSize);
}
void InitializeBenchmarkRunner() {
CreateBenchmarkRunner();
benchmark_runner->SetInput(reinterpret_cast<const int8_t*>(g_person_data));
}
void PersonDetectionTenIerationsWithPerson() {
benchmark_runner->SetInput(reinterpret_cast<const int8_t*>(g_person_data));
for (int i = 0; i < 10; i++) {
benchmark_runner->RunSingleIteration();
}
}
void PersonDetectionTenIerationsWithoutPerson() {
benchmark_runner->SetInput(reinterpret_cast<const int8_t*>(g_no_person_data));
for (int i = 0; i < 10; i++) {
benchmark_runner->RunSingleIteration();
}
}
} // namespace
TF_LITE_MICRO_BENCHMARKS_BEGIN
TF_LITE_MICRO_BENCHMARK(InitializeBenchmarkRunner());
TF_LITE_MICRO_BENCHMARK(benchmark_runner->RunSingleIteration());
TF_LITE_MICRO_BENCHMARK(PersonDetectionTenIerationsWithPerson());
TF_LITE_MICRO_BENCHMARK(PersonDetectionTenIerationsWithoutPerson());
TF_LITE_MICRO_BENCHMARKS_END