1111namespace Eloquent {
1212 namespace TinyML {
1313
14+ enum TfLiteError {
15+ OK,
16+ VERSION_MISMATCH,
17+ CANNOT_ALLOCATE_TENSORS,
18+ NOT_INITIALIZED,
19+ INVOKE_ERROR
20+ };
21+
1422 /* *
1523 * Eloquent interface to Tensorflow Lite for Microcontrollers
1624 *
@@ -39,17 +47,15 @@ namespace Eloquent {
3947 static tflite::MicroErrorReporter microReporter;
4048 static tflite::ops::micro::AllOpsResolver resolver;
4149
42- Serial.println (" Start" );
43-
4450 reporter = µReporter;
4551 model = tflite::GetModel (modelData);
4652
47- Serial.println (" GetModel done" );
48-
4953 // assert model version and runtime version match
5054 if (model->version () != TFLITE_SCHEMA_VERSION) {
55+ Serial.println (" Version mismatch" ); delay (1000 );
5156 failed = true ;
52- Serial.println (" Version mismatch" );
57+ error = VERSION_MISMATCH;
58+
5359 reporter->Report (
5460 " Model provided is schema version %d not equal "
5561 " to supported version %d." ,
@@ -58,27 +64,23 @@ namespace Eloquent {
5864 return false ;
5965 }
6066
61- Serial.println (" Version check done" );
62-
6367 static tflite::MicroInterpreter interpreter (model, resolver, tensorArena, tensorArenaSize, reporter);
6468
69+ Serial.println (" Allocating tensors..." ); delay (1000 );
6570 if (interpreter.AllocateTensors () != kTfLiteOk ) {
71+ Serial.println (" Cannot allocate tensors" ); delay (1000 );
6672 failed = true ;
67- Serial.println (" Allocation failed" );
68- reporter->Report (" AllocateTensors() failed" );
73+ error = CANNOT_ALLOCATE_TENSORS;
6974
7075 return false ;
7176 }
7277
73- Serial.println (" AllocateTensors done" );
74-
7578 input = interpreter.input (0 );
7679 output = interpreter.output (0 );
80+ error = OK;
7781
7882 this ->interpreter = &interpreter;
7983
80- Serial.println (" Begin done" );
81-
8284 return true ;
8385 }
8486
@@ -100,7 +102,7 @@ namespace Eloquent {
100102 if (!initialized ())
101103 return sqrt (-1 );
102104
103- memcpy (input, this ->input ->data .uint8 , sizeof (uint8_t ) * inputSize);
105+ memcpy (this ->input ->data .uint8 , input , sizeof (uint8_t ) * inputSize);
104106
105107 if (interpreter->Invoke () != kTfLiteOk ) {
106108 reporter->Report (" Inference failed" );
@@ -123,25 +125,27 @@ namespace Eloquent {
123125 */
124126 float predict (float *input, float *output = NULL ) {
125127 // abort if initialization failed
126- if (!initialized ())
128+ if (!initialized ()) {
129+ error = NOT_INITIALIZED;
130+
127131 return sqrt (-1 );
132+ }
128133
129134 // copy input
130135 for (size_t i = 0 ; i < inputSize; i++)
131136 this ->input ->data .f [i] = input[i];
132137
133138 if (interpreter->Invoke () != kTfLiteOk ) {
139+ error = INVOKE_ERROR;
134140 reporter->Report (" Inference failed" );
135141
136142 return sqrt (-1 );
137143 }
138144
139145 // copy output
140146 if (output != NULL ) {
141- if (output != NULL ) {
142- for (uint16_t i = 0 ; i < outputSize; i++)
143- output[i] = this ->output ->data .f [i];
144- }
147+ for (uint16_t i = 0 ; i < outputSize; i++)
148+ output[i] = this ->output ->data .f [i];
145149 }
146150
147151 return this ->output ->data .f [0 ];
@@ -179,8 +183,30 @@ namespace Eloquent {
179183 return classIdx;
180184 }
181185
186+ /* *
187+ * Get error message
188+ * @return
189+ */
190+ const char * errorMessage () {
191+ switch (error) {
192+ case OK:
193+ return " No error" ;
194+ case VERSION_MISMATCH:
195+ return " Version mismatch" ;
196+ case CANNOT_ALLOCATE_TENSORS:
197+ return " Cannot allocate tensors" ;
198+ case NOT_INITIALIZED:
199+ return " Interpreter has not been initialized" ;
200+ case INVOKE_ERROR:
201+ return " Interpreter invoke() returned an error" ;
202+ default :
203+ return " Unknown error" ;
204+ }
205+ }
206+
182207 protected:
183208 bool failed;
209+ TfLiteError error;
184210 uint8_t tensorArena[tensorArenaSize];
185211 tflite::ErrorReporter *reporter;
186212 tflite::MicroInterpreter *interpreter;
0 commit comments