|
| 1 | + |
| 2 | +#include <freertos/FreeRTOS.h> |
| 3 | +#include <esp_log.h> |
| 4 | +#include <esp_timer.h> |
| 5 | + |
| 6 | +#include <tensorflow/lite/micro/micro_interpreter.h> |
| 7 | +#include <tensorflow/lite/micro/micro_log.h> |
| 8 | +#include <tensorflow/lite/micro/micro_profiler.h> |
| 9 | +#include <tensorflow/lite/micro/micro_mutable_op_resolver.h> |
| 10 | +#include <tensorflow/lite/schema/schema_generated.h> |
| 11 | + |
| 12 | +#include "model.h" |
| 13 | +#include "image.h" |
| 14 | + |
| 15 | +static const char* TAG = "mobilenet"; |
| 16 | + |
| 17 | +namespace { |
| 18 | + const tflite::Model* model = nullptr; |
| 19 | + tflite::MicroInterpreter* interpreter = nullptr; |
| 20 | + |
| 21 | + constexpr int kTensorArenaSize = 1.5 * 1024 * 1024; |
| 22 | + static uint8_t* tensor_arena; |
| 23 | + |
| 24 | + TfLiteTensor* input; |
| 25 | + TfLiteTensor* output; |
| 26 | +} |
| 27 | + |
| 28 | +int8_t quantize(float val) { |
| 29 | + auto zero_point = input->params.zero_point; |
| 30 | + auto scale = input->params.scale; |
| 31 | + return (val / scale) + zero_point; |
| 32 | +} |
| 33 | + |
| 34 | +float dequantize(int8_t val) { |
| 35 | + auto zero_point = output->params.zero_point; |
| 36 | + auto scale = output->params.scale; |
| 37 | + return (val - zero_point) * scale; |
| 38 | +} |
| 39 | + |
| 40 | +extern "C" void app_main(void) |
| 41 | +{ |
| 42 | + model = tflite::GetModel(esp_mobile_net_model); |
| 43 | + if (model->version() != TFLITE_SCHEMA_VERSION) { |
| 44 | + MicroPrintf("Model provided is schema version %d not equal to supported version %d.", model->version(), TFLITE_SCHEMA_VERSION); |
| 45 | + } |
| 46 | + |
| 47 | + if (tensor_arena == NULL) { |
| 48 | + tensor_arena = (uint8_t*)heap_caps_malloc(kTensorArenaSize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); |
| 49 | + } |
| 50 | + |
| 51 | + if (tensor_arena == NULL) { |
| 52 | + printf("Couldn't allocate memory of %d bytes\n", kTensorArenaSize); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + ESP_LOGI(TAG, "Allocated memory for Tensor Arena"); |
| 57 | + |
| 58 | + static tflite::MicroMutableOpResolver<7> micro_op_resolver; |
| 59 | + micro_op_resolver.AddRelu6(); |
| 60 | + micro_op_resolver.AddConv2D(); |
| 61 | + micro_op_resolver.AddDepthwiseConv2D(); |
| 62 | + micro_op_resolver.AddAdd(); |
| 63 | + micro_op_resolver.AddMean(); |
| 64 | + micro_op_resolver.AddFullyConnected(); |
| 65 | + micro_op_resolver.AddSoftmax(); |
| 66 | + |
| 67 | + static tflite::MicroInterpreter static_interpreter( |
| 68 | + model, micro_op_resolver, tensor_arena, kTensorArenaSize |
| 69 | + ); |
| 70 | + interpreter = &static_interpreter; |
| 71 | + |
| 72 | + if (interpreter->AllocateTensors() != kTfLiteOk) { |
| 73 | + MicroPrintf("AllocateTensors() failed"); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + ESP_LOGI(TAG, "Allocated Tensors"); |
| 78 | + |
| 79 | + input = interpreter->input(0); |
| 80 | + output = interpreter->output(0); |
| 81 | + |
| 82 | + for (int i = 0; i < image_raw_len; i++) { |
| 83 | + input->data.int8[i] = quantize(((uint8_t)image_raw[i] / 127.5) - 1); |
| 84 | + } |
| 85 | + |
| 86 | + long long start_time = esp_timer_get_time(); |
| 87 | + |
| 88 | + if (interpreter->Invoke() != kTfLiteOk) { |
| 89 | + MicroPrintf("Invoke() failed"); |
| 90 | + } |
| 91 | + |
| 92 | + long long total_time = esp_timer_get_time() - start_time; |
| 93 | + ESP_LOGI(TAG, "Invoke was successful"); |
| 94 | + printf("Invoke: Total time = %lld\n", total_time / 1000); |
| 95 | + |
| 96 | + int maxLabel = 0; |
| 97 | + float maxConf = 0.0; |
| 98 | + |
| 99 | + for (int i = 0; i < 1000; i++) { |
| 100 | + float conf = dequantize(output->data.int8[i]); |
| 101 | + if (conf > maxConf) { |
| 102 | + maxLabel = i; |
| 103 | + maxConf = conf; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + printf("\nLabel: %d, Confidence: %f\n", maxLabel, maxConf); |
| 108 | +} |
0 commit comments