Skip to content

Commit da6d821

Browse files
committed
Added examples
1 parent 0378362 commit da6d821

File tree

196 files changed

+37974
-165
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

196 files changed

+37974
-165
lines changed

CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ target_compile_definitions(
2929
COMPILE_DEFINITIONS TF_LITE_STATIC_MEMORY=1
3030
)
3131

32+
set_target_properties(
33+
pico-tflmicro
34+
PROPERTIES
35+
COMPILE_FLAGS -fno-rtti
36+
COMPILE_FLAGS -fno-exceptions
37+
COMPILE_FLAGS -fno-threadsafe-statics
38+
COMPILE_FLAGS -nostdlib
39+
)
40+
3241
target_link_libraries(
3342
pico-tflmicro
3443
pico_stdlib
@@ -234,6 +243,15 @@ target_compile_definitions(
234243
COMPILE_DEFINITIONS TF_LITE_STATIC_MEMORY=1
235244
)
236245

246+
set_target_properties(
247+
pico-tflmicro_test
248+
PROPERTIES
249+
COMPILE_FLAGS -fno-rtti
250+
COMPILE_FLAGS -fno-exceptions
251+
COMPILE_FLAGS -fno-threadsafe-statics
252+
COMPILE_FLAGS -nostdlib
253+
)
254+
237255
target_link_libraries(
238256
pico-tflmicro_test
239257
pico_stdlib
@@ -246,6 +264,10 @@ target_sources(pico-tflmicro_test
246264
${CMAKE_CURRENT_LIST_DIR}/src/tensorflow/lite/micro/testing/test_conv_model.h
247265
)
248266

267+
add_subdirectory("examples/hello_world")
268+
add_subdirectory("examples/person_detection")
269+
add_subdirectory("examples/magic_wand")
270+
add_subdirectory("examples/micro_speech")
249271
add_subdirectory("tests/greedy_memory_planner_test")
250272
add_subdirectory("tests/kernel_activations_test")
251273
add_subdirectory("tests/kernel_add_test")

examples/hello_world/CMakeLists.txt

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
2+
cmake_minimum_required(VERSION 3.12)
3+
4+
project(hello_world C CXX ASM)
5+
set(CMAKE_C_STANDARD 11)
6+
set(CMAKE_CXX_STANDARD 11)
7+
8+
9+
add_executable(hello_world_test "")
10+
11+
target_include_directories(hello_world_test
12+
PRIVATE
13+
${CMAKE_CURRENT_LIST_DIR}/.
14+
)
15+
16+
set_target_properties(
17+
hello_world_test
18+
PROPERTIES
19+
COMPILE_FLAGS -fno-rtti
20+
COMPILE_FLAGS -fno-exceptions
21+
COMPILE_FLAGS -fno-threadsafe-statics
22+
COMPILE_FLAGS -nostdlib
23+
)
24+
25+
target_sources(hello_world_test
26+
PRIVATE
27+
${CMAKE_CURRENT_LIST_DIR}/hello_world_test.cpp
28+
${CMAKE_CURRENT_LIST_DIR}/model.cpp
29+
${CMAKE_CURRENT_LIST_DIR}/model.h
30+
)
31+
32+
target_link_libraries(
33+
hello_world_test
34+
pico-tflmicro
35+
pico-tflmicro_test
36+
)
37+
38+
pico_add_extra_outputs(hello_world_test)
39+
40+
41+
add_executable(hello_world "")
42+
43+
target_include_directories(hello_world
44+
PRIVATE
45+
${CMAKE_CURRENT_LIST_DIR}/.
46+
)
47+
48+
set_target_properties(
49+
hello_world
50+
PROPERTIES
51+
COMPILE_FLAGS -fno-rtti
52+
COMPILE_FLAGS -fno-exceptions
53+
COMPILE_FLAGS -fno-threadsafe-statics
54+
COMPILE_FLAGS -nostdlib
55+
)
56+
57+
target_sources(hello_world
58+
PRIVATE
59+
${CMAKE_CURRENT_LIST_DIR}/constants.cpp
60+
${CMAKE_CURRENT_LIST_DIR}/main.cpp
61+
${CMAKE_CURRENT_LIST_DIR}/main_functions.cpp
62+
${CMAKE_CURRENT_LIST_DIR}/model.cpp
63+
${CMAKE_CURRENT_LIST_DIR}/output_handler.cpp
64+
${CMAKE_CURRENT_LIST_DIR}/constants.h
65+
${CMAKE_CURRENT_LIST_DIR}/main_functions.h
66+
${CMAKE_CURRENT_LIST_DIR}/model.h
67+
${CMAKE_CURRENT_LIST_DIR}/output_handler.h
68+
)
69+
70+
target_link_libraries(
71+
hello_world
72+
pico-tflmicro
73+
)
74+
75+
pico_add_extra_outputs(hello_world)
76+
77+
78+
add_executable(output_handler_test "")
79+
80+
target_include_directories(output_handler_test
81+
PRIVATE
82+
${CMAKE_CURRENT_LIST_DIR}/.
83+
)
84+
85+
set_target_properties(
86+
output_handler_test
87+
PROPERTIES
88+
COMPILE_FLAGS -fno-rtti
89+
COMPILE_FLAGS -fno-exceptions
90+
COMPILE_FLAGS -fno-threadsafe-statics
91+
COMPILE_FLAGS -nostdlib
92+
)
93+
94+
target_sources(output_handler_test
95+
PRIVATE
96+
${CMAKE_CURRENT_LIST_DIR}/output_handler.cpp
97+
${CMAKE_CURRENT_LIST_DIR}/output_handler_test.cpp
98+
${CMAKE_CURRENT_LIST_DIR}/constants.h
99+
${CMAKE_CURRENT_LIST_DIR}/output_handler.h
100+
)
101+
102+
target_link_libraries(
103+
output_handler_test
104+
pico-tflmicro
105+
pico-tflmicro_test
106+
)
107+
108+
pico_add_extra_outputs(output_handler_test)
109+

examples/hello_world/constants.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 "constants.h"
17+
18+
// This is a small number so that it's easy to read the logs
19+
const int kInferencesPerCycle = 20;

examples/hello_world/constants.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
#ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_HELLO_WORLD_CONSTANTS_H_
17+
#define TENSORFLOW_LITE_MICRO_EXAMPLES_HELLO_WORLD_CONSTANTS_H_
18+
19+
// This constant represents the range of x values our model was trained on,
20+
// which is from 0 to (2 * Pi). We approximate Pi to avoid requiring additional
21+
// libraries.
22+
const float kXrange = 2.f * 3.14159265359f;
23+
24+
// This constant determines the number of inferences to perform across the range
25+
// of x values defined above. Since each inference takes time, the higher this
26+
// number, the more time it will take to run through the entire range. The value
27+
// of this constant can be tuned so that one full cycle takes a desired amount
28+
// of time. Since different devices take different amounts of time to perform
29+
// inference, this value should be defined per-device.
30+
extern const int kInferencesPerCycle;
31+
32+
#endif // TENSORFLOW_LITE_MICRO_EXAMPLES_HELLO_WORLD_CONSTANTS_H_
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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 <math.h>
17+
18+
#include "tensorflow/lite/micro/all_ops_resolver.h"
19+
#include "model.h"
20+
#include "tensorflow/lite/micro/micro_error_reporter.h"
21+
#include "tensorflow/lite/micro/micro_interpreter.h"
22+
#include "tensorflow/lite/micro/testing/micro_test.h"
23+
#include "tensorflow/lite/schema/schema_generated.h"
24+
#include "tensorflow/lite/version.h"
25+
26+
TF_LITE_MICRO_TESTS_BEGIN
27+
28+
TF_LITE_MICRO_TEST(LoadModelAndPerformInference) {
29+
// Define the input and the expected output
30+
float x = 0.0f;
31+
float y_true = sin(x);
32+
33+
// Set up logging
34+
tflite::MicroErrorReporter micro_error_reporter;
35+
36+
// Map the model into a usable data structure. This doesn't involve any
37+
// copying or parsing, it's a very lightweight operation.
38+
const tflite::Model* model = ::tflite::GetModel(g_model);
39+
if (model->version() != TFLITE_SCHEMA_VERSION) {
40+
TF_LITE_REPORT_ERROR(&micro_error_reporter,
41+
"Model provided is schema version %d not equal "
42+
"to supported version %d.\n",
43+
model->version(), TFLITE_SCHEMA_VERSION);
44+
}
45+
46+
// This pulls in all the operation implementations we need
47+
tflite::AllOpsResolver resolver;
48+
49+
// Create an area of memory to use for input, output, and intermediate arrays.
50+
51+
// Minimum arena size, at the time of writing. After allocating tensors
52+
// you can retrieve this value by invoking interpreter.arena_used_bytes().
53+
const int model_arena_size = 754;
54+
/* Extra headroom for model + alignment + future interpreter changes */
55+
const int extra_arena_size = 554 + 16 + 100;
56+
const int tensor_arena_size = model_arena_size + extra_arena_size;
57+
uint8_t tensor_arena[tensor_arena_size];
58+
59+
// Build an interpreter to run the model with
60+
tflite::MicroInterpreter interpreter(
61+
model, resolver, tensor_arena, tensor_arena_size, &micro_error_reporter);
62+
// Allocate memory from the tensor_arena for the model's tensors
63+
TF_LITE_MICRO_EXPECT_EQ(interpreter.AllocateTensors(), kTfLiteOk);
64+
65+
// Alert for substantial increase in arena size usage.
66+
TF_LITE_MICRO_EXPECT_LE(interpreter.arena_used_bytes(),
67+
model_arena_size + 100);
68+
// Obtain a pointer to the model's input tensor
69+
TfLiteTensor* input = interpreter.input(0);
70+
71+
// Make sure the input has the properties we expect
72+
TF_LITE_MICRO_EXPECT_NE(nullptr, input);
73+
// The property "dims" tells us the tensor's shape. It has one element for
74+
// each dimension. Our input is a 2D tensor containing 1 element, so "dims"
75+
// should have size 2.
76+
TF_LITE_MICRO_EXPECT_EQ(2, input->dims->size);
77+
// The value of each element gives the length of the corresponding tensor.
78+
// We should expect two single element tensors (one is contained within the
79+
// other).
80+
TF_LITE_MICRO_EXPECT_EQ(1, input->dims->data[0]);
81+
TF_LITE_MICRO_EXPECT_EQ(1, input->dims->data[1]);
82+
// The input is an 8 bit integer value
83+
TF_LITE_MICRO_EXPECT_EQ(kTfLiteInt8, input->type);
84+
85+
// Get the input quantization parameters
86+
float input_scale = input->params.scale;
87+
int input_zero_point = input->params.zero_point;
88+
89+
// Quantize the input from floating-point to integer
90+
int8_t x_quantized = x / input_scale + input_zero_point;
91+
// Place the quantized input in the model's input tensor
92+
input->data.int8[0] = x_quantized;
93+
94+
// Run the model and check that it succeeds
95+
TfLiteStatus invoke_status = interpreter.Invoke();
96+
TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, invoke_status);
97+
98+
// Obtain a pointer to the output tensor and make sure it has the
99+
// properties we expect. It should be the same as the input tensor.
100+
TfLiteTensor* output = interpreter.output(0);
101+
TF_LITE_MICRO_EXPECT_EQ(2, output->dims->size);
102+
TF_LITE_MICRO_EXPECT_EQ(1, output->dims->data[0]);
103+
TF_LITE_MICRO_EXPECT_EQ(1, output->dims->data[1]);
104+
TF_LITE_MICRO_EXPECT_EQ(kTfLiteInt8, output->type);
105+
106+
// Get the output quantization parameters
107+
float output_scale = output->params.scale;
108+
int output_zero_point = output->params.zero_point;
109+
110+
// Obtain the quantized output from model's output tensor
111+
int8_t y_pred_quantized = output->data.int8[0];
112+
// Dequantize the output from integer to floating-point
113+
float y_pred = (y_pred_quantized - output_zero_point) * output_scale;
114+
115+
// Check if the output is within a small range of the expected output
116+
float epsilon = 0.05f;
117+
TF_LITE_MICRO_EXPECT_NEAR(y_true, y_pred, epsilon);
118+
119+
// Run inference on several more values and confirm the expected outputs
120+
x = 1.f;
121+
y_true = sin(x);
122+
input->data.int8[0] = x / input_scale + input_zero_point;
123+
interpreter.Invoke();
124+
y_pred = (output->data.int8[0] - output_zero_point) * output_scale;
125+
TF_LITE_MICRO_EXPECT_NEAR(y_true, y_pred, epsilon);
126+
127+
x = 3.f;
128+
y_true = sin(x);
129+
input->data.int8[0] = x / input_scale + input_zero_point;
130+
interpreter.Invoke();
131+
y_pred = (output->data.int8[0] - output_zero_point) * output_scale;
132+
TF_LITE_MICRO_EXPECT_NEAR(y_true, y_pred, epsilon);
133+
134+
x = 5.f;
135+
y_true = sin(x);
136+
input->data.int8[0] = x / input_scale + input_zero_point;
137+
interpreter.Invoke();
138+
y_pred = (output->data.int8[0] - output_zero_point) * output_scale;
139+
TF_LITE_MICRO_EXPECT_NEAR(y_true, y_pred, epsilon);
140+
}
141+
142+
TF_LITE_MICRO_TESTS_END

examples/hello_world/main.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 "main_functions.h"
17+
18+
// This is the default main used on systems that have the standard C entry
19+
// point. Other devices (for example FreeRTOS or ESP32) that have different
20+
// requirements for entry code (like an app_main function) should specialize
21+
// this main.cc file in a target-specific subfolder.
22+
int main(int argc, char* argv[]) {
23+
setup();
24+
while (true) {
25+
loop();
26+
}
27+
}

0 commit comments

Comments
 (0)