Skip to content

Commit 1331698

Browse files
committed
Added python_ops_resolver from latest tflite-micro
- all_ops_resolver is removed from latest tflite-micro sources. - Created `openmv-libtf-updated.cpp` from `openmv-libtf.cpp` and used python_ops_resolver instead of all_ops_resolver Signed-off-by: Vikram <[email protected]>
1 parent 2712c39 commit 1331698

File tree

3 files changed

+275
-0
lines changed

3 files changed

+275
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/* This file is part of the OpenMV project.
2+
* Copyright (c) 2013-2019 Ibrahim Abdelkader <[email protected]> & Kwabena W. Agyeman <[email protected]>
3+
* This work is licensed under the MIT license, see the file LICENSE for details.
4+
*/
5+
6+
// Copied and modified for using with newer tflite-micro sources
7+
8+
#include "python_ops_resolver.h"
9+
#include "tensorflow/lite/micro/tflite_bridge/micro_error_reporter.h"
10+
#include "tensorflow/lite/micro/micro_interpreter.h"
11+
#include "tensorflow/lite/schema/schema_generated.h"
12+
13+
#include "tensorflow-microlite.h"
14+
#include "openmv-libtf.h"
15+
#include "micropython-error-reporter.h"
16+
#include <stdio.h>
17+
18+
extern "C" {
19+
STATIC microlite::MicropythonErrorReporter micro_error_reporter;
20+
/*
21+
Return the index'th tensor
22+
*/
23+
TfLiteTensor *libtf_interpreter_get_input_tensor(microlite_interpreter_obj_t *microlite_interpreter, mp_uint_t index) {
24+
25+
tflite::MicroInterpreter *interpreter = (tflite::MicroInterpreter *)microlite_interpreter->tf_interpreter;
26+
27+
return interpreter->input((size_t)index);
28+
}
29+
30+
TfLiteTensor *libtf_interpreter_get_output_tensor(microlite_interpreter_obj_t *microlite_interpreter, mp_uint_t index) {
31+
32+
tflite::MicroInterpreter *interpreter = (tflite::MicroInterpreter *)microlite_interpreter->tf_interpreter;
33+
34+
return interpreter->output((size_t)index);
35+
}
36+
37+
// static int libtf_align_tensor_arena(uint8_t **tensor_arena, size_t *tensor_arena_size)
38+
// {
39+
// tflite::ErrorReporter *error_reporter = &micro_error_reporter;
40+
41+
// error_reporter->Report("Performing Alignment");
42+
// uint8_t alignment = ((uint8_t) (*tensor_arena)) % 16;
43+
44+
// if (alignment) {
45+
46+
// unsigned int fix = 16 - alignment;
47+
48+
// if ((*tensor_arena_size) < fix) {
49+
// return 1;
50+
// }
51+
52+
// (*tensor_arena) += fix;
53+
// (*tensor_arena_size) -= fix;
54+
// }
55+
56+
// return 0;
57+
// }
58+
59+
60+
int libtf_interpreter_init(microlite_interpreter_obj_t *microlite_interpreter) {
61+
62+
tflite::ErrorReporter *error_reporter = &micro_error_reporter;
63+
64+
const tflite::Model *model = tflite::GetModel(microlite_interpreter->model_data->items);
65+
66+
// if (model->version() != TFLITE_SCHEMA_VERSION) {
67+
// error_reporter->Report("Model provided is schema version is not equal to supported version!");
68+
// return 1;
69+
// }
70+
71+
// if (libtf_align_tensor_arena((uint8_t **)microlite_interpreter->tensor_area->items, &microlite_interpreter->tensor_area->len)) {
72+
// error_reporter->Report("Align failed!");
73+
// return 1;
74+
// }
75+
76+
77+
microlite_interpreter->tf_error_reporter = (mp_obj_t)error_reporter;
78+
microlite_interpreter->tf_model = (mp_obj_t)model;
79+
80+
81+
// tflite::MicroAllocator *allocator = tflite::MicroAllocator::Create(
82+
// (uint8_t*)microlite_interpreter->tensor_area->items,
83+
// microlite_interpreter->tensor_area->len, error_reporter);
84+
85+
86+
tflite::PythonOpsResolver op_resolver;
87+
tflite::MicroInterpreter *interpreter = new tflite::MicroInterpreter(model,
88+
op_resolver,
89+
(uint8_t*)microlite_interpreter->tensor_area->items,
90+
microlite_interpreter->tensor_area->len);
91+
92+
if (interpreter->AllocateTensors() != kTfLiteOk) {
93+
MicroPrintf("AllocateTensors() failed!");
94+
return 1;
95+
}
96+
97+
microlite_interpreter->tf_interpreter = (mp_obj_t)interpreter;
98+
99+
return 0;
100+
}
101+
102+
int libtf_interpreter_invoke(microlite_interpreter_obj_t *microlite_interpreter)
103+
{
104+
tflite::MicroInterpreter *interpreter = (tflite::MicroInterpreter *)microlite_interpreter->tf_interpreter;
105+
106+
mp_call_function_1(microlite_interpreter->input_callback, microlite_interpreter);
107+
108+
if (interpreter->Invoke() != kTfLiteOk) {
109+
MicroPrintf("Invoke() failed!");
110+
return 1;
111+
}
112+
113+
mp_call_function_1(microlite_interpreter->output_callback, microlite_interpreter);
114+
return 0;
115+
}
116+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/* Copyright 2022 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 "python_ops_resolver.h"
17+
18+
#include "tensorflow/lite/micro/kernels/micro_ops.h"
19+
20+
namespace tflite {
21+
22+
PythonOpsResolver::PythonOpsResolver() {
23+
// Please keep this list of Builtin Operators in alphabetical order.
24+
AddAbs();
25+
AddAdd();
26+
AddAddN();
27+
AddArgMax();
28+
AddArgMin();
29+
AddAssignVariable();
30+
AddAveragePool2D();
31+
AddBatchToSpaceNd();
32+
AddBroadcastArgs();
33+
AddBroadcastTo();
34+
AddCallOnce();
35+
AddCast();
36+
AddCeil();
37+
AddCircularBuffer();
38+
AddConcatenation();
39+
AddConv2D();
40+
AddCos();
41+
AddCumSum();
42+
AddDepthToSpace();
43+
AddDepthwiseConv2D();
44+
AddDequantize();
45+
AddDetectionPostprocess();
46+
AddDiv();
47+
AddElu();
48+
AddEqual();
49+
AddEthosU();
50+
AddExp();
51+
AddExpandDims();
52+
AddFill();
53+
AddFloor();
54+
AddFloorDiv();
55+
AddFloorMod();
56+
AddFullyConnected();
57+
AddGather();
58+
AddGatherNd();
59+
AddGreater();
60+
AddGreaterEqual();
61+
AddHardSwish();
62+
AddIf();
63+
AddL2Normalization();
64+
AddL2Pool2D();
65+
AddLeakyRelu();
66+
AddLess();
67+
AddLessEqual();
68+
AddLog();
69+
AddLogicalAnd();
70+
AddLogicalNot();
71+
AddLogicalOr();
72+
AddLogistic();
73+
AddLogSoftmax();
74+
AddMaxPool2D();
75+
AddMaximum();
76+
AddMean();
77+
AddMinimum();
78+
AddMirrorPad();
79+
AddMul();
80+
AddNeg();
81+
AddNotEqual();
82+
AddPack();
83+
AddPad();
84+
AddPadV2();
85+
AddPrelu();
86+
AddQuantize();
87+
AddReadVariable();
88+
AddReduceMax();
89+
AddRelu();
90+
AddRelu6();
91+
AddReshape();
92+
AddResizeBilinear();
93+
AddResizeNearestNeighbor();
94+
AddRound();
95+
AddRsqrt();
96+
AddSelectV2();
97+
AddShape();
98+
AddSin();
99+
AddSlice();
100+
AddSoftmax();
101+
AddSpaceToBatchNd();
102+
AddSpaceToDepth();
103+
AddSplit();
104+
AddSplitV();
105+
AddSqrt();
106+
AddSquare();
107+
AddSquaredDifference();
108+
AddSqueeze();
109+
AddStridedSlice();
110+
AddSub();
111+
AddSum();
112+
AddSvdf();
113+
AddTanh();
114+
AddTranspose();
115+
AddTransposeConv();
116+
AddUnidirectionalSequenceLSTM();
117+
AddUnpack();
118+
AddVarHandle();
119+
AddWhile();
120+
AddZerosLike();
121+
}
122+
123+
} // namespace tflite
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* Copyright 2018 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+
#ifndef TENSORFLOW_LITE_MICRO_PYTHON_OPS_RESOLVER_H_
16+
#define TENSORFLOW_LITE_MICRO_PYTHON_OPS_RESOLVER_H_
17+
18+
#include "tensorflow/lite/micro/compatibility.h"
19+
#include "tensorflow/lite/micro/micro_mutable_op_resolver.h"
20+
21+
namespace tflite {
22+
23+
// PythonOpsResolver is used to register all the Ops for the TFLM Python
24+
// interpreter. This is ok since code size is not a concern from Python and
25+
// the goal is to be able to run any model supported by TFLM in a flexible way
26+
class PythonOpsResolver : public MicroMutableOpResolver<200> {
27+
public:
28+
PythonOpsResolver();
29+
30+
private:
31+
TF_LITE_REMOVE_VIRTUAL_DELETE
32+
};
33+
34+
} // namespace tflite
35+
36+
#endif // TENSORFLOW_LITE_MICRO_PYTHON_OPS_RESOLVER_H_

0 commit comments

Comments
 (0)