Skip to content

Commit

Permalink
Code updated to better explain github issue: tensorflow/tflite-micro#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ShardulNalegave committed Feb 3, 2025
1 parent 021500c commit f5d2c46
Show file tree
Hide file tree
Showing 10 changed files with 12,571 additions and 12,514 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(esp-yolov8n-test)
project(esp-mbnetv2-test)
Binary file modified bus.raw
Binary file not shown.
20 changes: 10 additions & 10 deletions components/esp-tflite-micro/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ set(signal_srcs
"${signal_dir}/src/kiss_fft_wrappers/kiss_fft_int32.cc")

# remove sources which will be provided by esp_nn
list(REMOVE_ITEM srcs_kernels
"${tfmicro_kernels_dir}/add.cc"
"${tfmicro_kernels_dir}/conv.cc"
"${tfmicro_kernels_dir}/depthwise_conv.cc"
"${tfmicro_kernels_dir}/fully_connected.cc"
"${tfmicro_kernels_dir}/mul.cc"
"${tfmicro_kernels_dir}/pooling.cc"
"${tfmicro_kernels_dir}/softmax.cc")
# list(REMOVE_ITEM srcs_kernels
# "${tfmicro_kernels_dir}/add.cc"
# "${tfmicro_kernels_dir}/conv.cc"
# "${tfmicro_kernels_dir}/depthwise_conv.cc"
# "${tfmicro_kernels_dir}/fully_connected.cc"
# "${tfmicro_kernels_dir}/mul.cc"
# "${tfmicro_kernels_dir}/pooling.cc"
# "${tfmicro_kernels_dir}/softmax.cc")

FILE(GLOB esp_nn_kernels
"${tfmicro_kernels_dir}/esp_nn/*.cc")
Expand All @@ -73,7 +73,7 @@ set(lib_srcs
"${srcs_micro}"
"${srcs_kernels}"
"${srcs_tflite_bridge}"
"${esp_nn_kernels}"
# "${esp_nn_kernels}"
"${src_micro_frontend}"
"${signal_srcs}"
"${tflite_dir}/kernels/kernel_util.cc"
Expand Down Expand Up @@ -130,7 +130,7 @@ if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER_EQUAL "5.0")
endif()

# enable ESP-NN optimizations by Espressif
target_compile_options(${COMPONENT_LIB} PRIVATE -DESP_NN)
# target_compile_options(${COMPONENT_LIB} PRIVATE -DESP_NN)

set(common_flags -DTF_LITE_STATIC_MEMORY -DTF_LITE_DISABLE_X86_NEON -O3
-Wstrict-aliasing -Wno-unused-parameter -Wall -Wextra -Wvla
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ void SoftmaxQuantized(TfLiteContext* context, const TfLiteEvalTensor* input,
tflite::micro::GetTensorData<int16_t>(output));
} else {
#if ESP_NN
printf("\nSoftmax:-\n{ ");
for (int i = 0; i < 1000; i++) {
printf("%d, ", input->data.int8[i]);
}
printf("}\n");

const int32_t input_beta_multiplier = data->op_data.input_multiplier;
const int32_t input_beta_left_shift = data->op_data.input_left_shift;
const int diff_min = data->op_data.diff_min;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/lite/micro/kernels/kernel_util.h"

#include "tensorflow/lite/micro/micro_interpreter_graph.h"

#include "flatbuffers/flatbuffers.h" // from @flatbuffers
Expand Down Expand Up @@ -218,6 +220,16 @@ TfLiteStatus MicroInterpreterGraph::InvokeSubgraph(int subgraph_idx) {
TFLITE_DCHECK(registration->invoke);
TfLiteStatus invoke_status = registration->invoke(context_, node);

if (current_operator_index_ == 11 || current_operator_index_ == 62) {
TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context_, node, 0);
printf("\nNode %ld output:- { ", current_operator_index_);
for (int i = 0; i < 100; i++) {
if (i % 14 == 0) printf("\n");
printf("%d, ", output->data.int8[i]);
}
printf("}\n\n");
}

// All TfLiteTensor structs used in the kernel are allocated from temp
// memory in the allocator. This creates a chain of allocations in the
// temp section. The call below resets the chain of allocations to
Expand Down
24,979 changes: 12,489 additions & 12,490 deletions main/image.cc

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions main/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@

static const char* TAG = "esp_mbnetv2_test_app";

#include <stdlib.h>
int8_t random_int8() {
return (int8_t)(rand() % 256 - 128); // Generates a number between -128 and 127
}

namespace {
const tflite::Model* model = nullptr;
tflite::MicroInterpreter* interpreter = nullptr;

constexpr int kTensorArenaSize = 2 * 1024 * 1024;
constexpr int kTensorArenaSize = 1.5 * 1024 * 1024;
static uint8_t* tensor_arena;

TfLiteTensor* input;
Expand All @@ -44,7 +39,7 @@ float dequantize(int8_t val) {

extern "C" void app_main(void)
{
model = tflite::GetModel(esp_yolo_model);
model = tflite::GetModel(esp_mobile_net_model);
if (model->version() != TFLITE_SCHEMA_VERSION) {
MicroPrintf("Model provided is schema version %d not equal to supported version %d.", model->version(), TFLITE_SCHEMA_VERSION);
}
Expand Down Expand Up @@ -85,9 +80,14 @@ extern "C" void app_main(void)
output = interpreter->output(0);

for (int i = 0; i < image_raw_len; i++) {
// input->data.int8[i] = quantize((uint8_t)image_raw[i] / 255.0);
input->data.int8[i] = random_int8();
input->data.int8[i] = quantize(((uint8_t)image_raw[i] / 127.5) - 1);
}

printf("{ ");
for (int i = 0; i < 100; i++) {
printf("%d, ", input->data.int8[i]);
}
printf("}\n");

long long start_time = esp_timer_get_time();

Expand Down
4 changes: 2 additions & 2 deletions main/model.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "model.h"

alignas(8) const unsigned char esp_yolo_model[] = {
alignas(8) const unsigned char esp_mobile_net_model[] = {
0x1c, 0x00, 0x00, 0x00, 0x54, 0x46, 0x4c, 0x33, 0x14, 0x00, 0x20, 0x00,
0x1c, 0x00, 0x18, 0x00, 0x14, 0x00, 0x10, 0x00, 0x0c, 0x00, 0x00, 0x00,
0x08, 0x00, 0x04, 0x00, 0x14, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
Expand Down Expand Up @@ -160453,4 +160453,4 @@ alignas(8) const unsigned char esp_yolo_model[] = {
0x08, 0x00, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03
};
const unsigned int esp_yolo_model_len = 1925408;
const unsigned int esp_mobile_net_model_len = 1925408;
4 changes: 2 additions & 2 deletions main/model.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

#pragma once

extern const unsigned char esp_yolo_model[];
extern const unsigned int esp_yolo_model_len;
extern const unsigned char esp_mobile_net_model[];
extern const unsigned int esp_mobile_net_model_len;
40 changes: 40 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

import numpy as np
import tensorflow as tf
from tensorflow.keras.utils import load_img, img_to_array
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions

interpreter = tf.lite.Interpreter(model_path="mobilenet_v2_35_quantized.tflite", experimental_preserve_all_tensors=True)
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()[0]
output_details = interpreter.get_output_details()[0]

input_scale, input_zero = input_details["quantization"]
output_scale, output_zero = output_details["quantization"]

img = img_to_array(load_img("bus.jpg", target_size=(224, 224)))
# img.astype(np.uint8).tofile("bus.raw")

img = np.expand_dims(img, axis=0)
img = preprocess_input(img)
img = (img / input_scale) + input_zero
img = img.astype(np.int8)

print(img.flatten()[:100])
interpreter.set_tensor(input_details['index'], img)
interpreter.invoke()

output = interpreter.get_tensor(output_details['index']).astype(np.float32)
output = (output - output_zero) * output_scale
predictions = decode_predictions(output, top=5)
print(predictions)

tensor_details = interpreter.get_tensor_details()
tensor = None
for t in tensor_details:
if t['name'] == 'mobilenetv2_0.35_224_1/global_average_pooling2d_1/Mean':
tensor = t

print("\n\n")
print(interpreter.get_tensor(tensor['index']).flatten()[:100])

0 comments on commit f5d2c46

Please sign in to comment.