Skip to content

Commit b12f467

Browse files
committed
Added new code - almost final version
1 parent 0d7c93e commit b12f467

File tree

526 files changed

+186
-132
lines changed

Some content is hidden

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

526 files changed

+186
-132
lines changed

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
11
# HEYoT Neural Network Offloading Example
22
What about an Offloading Algorithm and some Split Computing?
33

4-
# Installation
4+
# Configurations
55
After giving us a star: You can download the repo and then start to configure all the infrastracture...
66

7-
Within the folder "environment" you can find the ".env.dev" file. This file sets the variables for the MQTT configurations used by the MQTT Client on the Edge, other than some parameters for the DJango App
7+
Within the folder "environment" you can find the ".env.dev" file. This file sets the variables for the MQTT Client running on the Edge and other parameters for the DJango Web App.
8+
9+
Another important file that needs to be configured is contained in the folder "esp32_board/nn_runner/src" and it's the "conf.h" file. This file contains the configurations of: WiFi, MQTT and NTP server for the device and the edge.
10+
11+
12+
# The Neural Network Model
13+
Inside the folder "heyot/neyral_networks/ai_models" you can find all the utils used for examples of nn model generation and conversion.
14+
The jupyter notebook "ConvertModel" can give you an idea on how to obtain a single header file for each layer of your neural network model - these header files are key components of the code that then runs on the ESP32 devices.
15+
16+
17+
Each model that you wat to run should be put in the models subdirectory with this structure:
18+
<model_name>/
19+
analytics_data/
20+
analytics.csv
21+
data/
22+
...
23+
layers/
24+
h/
25+
h5/
26+
tflite/
27+
pred_data/
28+
<moel_name>.h
29+
<model_name>.h5
30+
<model_name>.tflite
31+
832

environment/.env.dev

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ DOCKER_IMG_NAME="heyot"
1414
######################################################################################
1515
MQTT_BROKER="FLOWFACTORY-FABIO.local"
1616
MQTT_PORT="1883"
17-
MQTT_QOS="2"
17+
MQTT_QOS="2"
18+
######################################################################################
19+
NTP_SERVER="0.it.pool.ntp.org"

environment/.env.template

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,19 @@
11
######################################################################################
22
# Djagno Main Configuration
3-
DEBUG=""
4-
DJANGO_SECRET_KEY=""
5-
DJANGO_ALLOWED_HOSTS=""
6-
DJANGO_PROJECT_NAME=""
7-
DJANGO_ADMIN_EMAIL=""
8-
DJANGO_USR_PWD=""
9-
APP_USER=""
10-
WORK_DIR=""
11-
######################################################################################
12-
# Postgres Database
13-
SQL_ENGINE=""
14-
SQL_DATABASE=""
15-
SQL_HOST=""
16-
SQL_PORT=""
17-
SQL_USER=""
18-
SQL_PASSWORD=""
19-
DATABASE=""
20-
POSTGRES_DB=""
21-
POSTGRES_USER=""
22-
POSTGRES_PASSWORD=""
23-
######################################################################################
24-
# Nginx
25-
PROXY_NAME=""
26-
######################################################################################
27-
EMAIL_BACKEND="django_smtp_ssl.SSLEmailBackend"
28-
EMAIL_HOST=""
29-
EMAIL_HOST_USER=""
30-
EMAIL_HOST_PASSWORD=""
31-
EMAIL_PORT=""
32-
DEFAULT_FROM_EMAIL=""
3+
DEBUG="1"
4+
DJANGO_SECRET_KEY="django-insecure-h19zb6ksveuyc+$r1+qc0foxmz47#^2poboyw+sz@-r49gbw53"
5+
DJANGO_ALLOWED_HOSTS="*"
6+
DJANGO_PROJECT_NAME="heyot"
7+
DJANGO_ADMIN_EMAIL="[email protected]"
8+
DJANGO_USR_PWD="iot"
9+
APP_USER="heyot"
10+
WORK_DIR="heyot"
3311
######################################################################################
3412
# Docker Repository
35-
DOCKER_USERNAME=""
36-
DOCKER_IMG_NAME=""
37-
DOCKER_PASSWORD=""
38-
DOCKER_REPOSITORY=""
13+
DOCKER_IMG_NAME="heyot"
14+
######################################################################################
15+
MQTT_BROKER="FLOWFACTORY-FABIO.local"
16+
MQTT_PORT="1883"
17+
MQTT_QOS="2"
3918
######################################################################################
40-
MQTT_BROKER=""
41-
MQTT_PORT=""
42-
MQTT_QOS=""
19+
NTP_SERVER="0.it.pool.ntp.org"

esp32_board/nn_runner/src/main.cpp

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ float imageData[10][10] = {
3434
{255, 255, 0, 0, 255, 0, 0, 255, 255, 255},
3535
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255}
3636
};
37+
/*
38+
{0, 0, 255, 0, 0},
39+
{0, 0, 255, 0, 0},
40+
{0, 0, 255, 0, 0},
41+
{0, 0, 255, 0, 0},
42+
{0, 0, 255, 0, 0}*/
3743
const int imageHeight = 10;
3844
const int imageWidth = 10;
3945

@@ -60,29 +66,33 @@ const int imageWidth = 10;
6066
* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6167
*/
6268
// NN Variables
63-
tflite::MicroErrorReporter micro_error_reporter;
64-
tflite::ErrorReporter* error_reporter = &micro_error_reporter;
65-
const tflite::Model* model = nullptr;
66-
tflite::MicroInterpreter* interpreter = nullptr;
67-
TfLiteTensor* input;
68-
TfLiteTensor* output;
69-
constexpr int kTensorArenaSize = 12*1024;
70-
uint8_t tensor_arena[kTensorArenaSize];
71-
bool modelLoaded = false;
69+
tflite::MicroErrorReporter micro_error_reporter;
70+
tflite::ErrorReporter* error_reporter = &micro_error_reporter;
71+
const tflite::Model* model = nullptr;
72+
tflite::MicroInterpreter* interpreter = nullptr;
73+
TfLiteTensor* input;
74+
TfLiteTensor* output;
75+
constexpr int kTensorArenaSize = 12*1024;
76+
uint8_t tensor_arena[kTensorArenaSize];
77+
bool modelLoaded = false;
7278
// Communication & Offloading Variables
73-
WiFiClient espClient;
74-
PubSubClient client(espClient);
75-
int computedLayer = 0;
76-
struct tm timeinfo;
77-
UUID uuid;
78-
String MessageUUID = "";
79-
const int nonValidLayer = 999;
80-
int offloadingLayer = nonValidLayer;
81-
bool offloaded = false;
82-
bool analyticsPublished = false;
83-
bool modelDataLoaded = true;
79+
WiFiClient espClient;
80+
PubSubClient client(espClient);
81+
int computedLayer = 0;
82+
struct tm timeinfo;
83+
UUID uuid;
84+
String MessageUUID = "";
85+
const int nonValidLayer = 999;
86+
int offloadingLayer = nonValidLayer;
87+
bool offloaded = false;
88+
bool analyticsPublished = false;
89+
bool modelDataLoaded = true;
8490
StaticJsonDocument<512> jsonDoc;
8591

92+
93+
94+
95+
8696
/*
8797
* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
8898
* GENERATE MESSAGE UUID
@@ -136,17 +146,31 @@ void loadNNLayer(String layer_name){
136146
* INFERENCE FOR NN LAYER
137147
* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
138148
*/
139-
void runNNLayer(int offloading_layer_index){
149+
extern "C" void runNNLayer(int offloading_layer_index){
140150
// Generate the JSON message
151+
141152
for (int i = 0; i < offloading_layer_index; i++){
142153
String layer_name = "layer_" + String(i);
143154
float inizio = micros();
144155
loadNNLayer(layer_name);
145-
/*input = interpreter->input(0);
156+
input = interpreter->input(0);
146157
output = interpreter->output(0);
147-
interpreter->Invoke();*/
158+
interpreter->Invoke();
148159
jsonDoc["layer_inference_time"][i] = (micros()-inizio);
160+
161+
162+
// Extract relevant information from the output tensor
163+
TfLiteTensor* outputTensor = output;
164+
float* outputData = outputTensor->data.f;
165+
int numOutput = outputTensor->dims->data[1];
166+
167+
// Store output values in a C++ array
168+
/*String outputArray[numOutput - 1];
169+
for (int j = 0; j < numOutput; j++) {
170+
jsonDoc["layer_output"].add(String(outputData[j]));
171+
}*/
149172
jsonDoc["layer_output"][i] = "";
173+
150174
Serial.println("Computed layer: " + String(i)+" Inf Time: " + String(micros()-inizio) );
151175
}
152176
}

esp32_board/nn_runner/src/model_layers/layer_0.h

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

esp32_board/nn_runner/src/model_layers/layer_1.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
#include <cstdint>
44

55
const uint8_t layer_1[] = {
6-
28, 0, 0, 0, 84, 70, 76, 51, 20, 0, 32, 0, 28, 0, 24, 0, 20, 0, 16, 0, 12, 0, 0, 0, 8, 0, 4, 0, 20, 0, 0, 0, 28, 0, 0, 0, 148, 0, 0, 0, 236, 0, 0, 0, 144, 1, 0, 0, 160, 1, 0, 0, 64, 3, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 16, 0, 0, 0, 0, 0, 10, 0, 16, 0, 12, 0, 8, 0, 4, 0, 10, 0, 0, 0, 12, 0, 0, 0, 28, 0, 0, 0, 56, 0, 0, 0, 15, 0, 0, 0, 115, 101, 114, 118, 105, 110, 103, 95, 100, 101, 102, 97, 117, 108, 116, 0, 1, 0, 0, 0, 4, 0, 0, 0, 148, 255, 255, 255, 1, 0, 0, 0, 4, 0, 0, 0, 7, 0, 0, 0, 108, 97, 121, 101, 114, 95, 49, 0, 1, 0, 0, 0, 4, 0, 0, 0, 14, 255, 255, 255, 4, 0, 0, 0, 13, 0, 0, 0, 108, 97, 121, 101, 114, 95, 49, 95, 105, 110, 112, 117, 116, 0, 0, 0, 2, 0, 0, 0, 52, 0, 0, 0, 4, 0, 0, 0, 220, 255, 255, 255, 4, 0, 0, 0, 4, 0, 0, 0, 19, 0, 0, 0, 67, 79, 78, 86, 69, 82, 83, 73, 79, 78, 95, 77, 69, 84, 65, 68, 65, 84, 65, 0, 8, 0, 12, 0, 8, 0, 4, 0, 8, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 19, 0, 0, 0, 109, 105, 110, 95, 114, 117, 110, 116, 105, 109, 101, 95, 118, 101, 114, 115, 105, 111, 110, 0, 5, 0, 0, 0, 160, 0, 0, 0, 152, 0, 0, 0, 144, 0, 0, 0, 112, 0, 0, 0, 4, 0, 0, 0, 158, 255, 255, 255, 4, 0, 0, 0, 84, 0, 0, 0, 12, 0, 0, 0, 8, 0, 14, 0, 8, 0, 4, 0, 8, 0, 0, 0, 16, 0, 0, 0, 36, 0, 0, 0, 0, 0, 6, 0, 8, 0, 4, 0, 6, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 16, 0, 12, 0, 8, 0, 4, 0, 10, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0, 6, 0, 0, 0, 50, 46, 49, 51, 46, 48, 0, 0, 0, 0, 6, 0, 8, 0, 4, 0, 6, 0, 0, 0, 4, 0, 0, 0, 16, 0, 0, 0, 49, 46, 53, 46, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 124, 254, 255, 255, 128, 254, 255, 255, 132, 254, 255, 255, 15, 0, 0, 0, 77, 76, 73, 82, 32, 67, 111, 110, 118, 101, 114, 116, 101, 100, 46, 0, 1, 0, 0, 0, 20, 0, 0, 0, 0, 0, 14, 0, 24, 0, 20, 0, 16, 0, 12, 0, 8, 0, 4, 0, 14, 0, 0, 0, 20, 0, 0, 0, 28, 0, 0, 0, 124, 0, 0, 0, 128, 0, 0, 0, 132, 0, 0, 0, 4, 0, 0, 0, 109, 97, 105, 110, 0, 0, 0, 0, 1, 0, 0, 0, 20, 0, 0, 0, 0, 0, 14, 0, 22, 0, 0, 0, 16, 0, 12, 0, 11, 0, 4, 0, 14, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 5, 48, 0, 0, 0, 52, 0, 0, 0, 0, 0, 14, 0, 24, 0, 23, 0, 16, 0, 12, 0, 8, 0, 4, 0, 14, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 128, 0, 0, 0, 4, 0, 0, 0, 158, 255, 255, 255, 0, 0, 0, 1, 20, 0, 0, 0, 36, 0, 0, 0, 36, 0, 0, 0, 2, 0, 0, 0, 52, 0, 0, 0, 4, 0, 0, 0, 255, 255, 255, 255, 4, 0, 0, 0, 4, 0, 0, 0, 32, 0, 0, 0, 136, 255, 255, 255, 17, 0, 0, 0, 80, 97, 114, 116, 105, 116, 105, 111, 110, 101, 100, 67, 97, 108, 108, 58, 48, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 32, 0, 0, 0, 0, 0, 22, 0, 28, 0, 24, 0, 0, 0, 20, 0, 16, 0, 12, 0, 0, 0, 0, 0, 8, 0, 7, 0, 22, 0, 0, 0, 0, 0, 0, 1, 20, 0, 0, 0, 40, 0, 0, 0, 40, 0, 0, 0, 1, 0, 0, 0, 68, 0, 0, 0, 4, 0, 0, 0, 255, 255, 255, 255, 8, 0, 0, 0, 8, 0, 0, 0, 32, 0, 0, 0, 4, 0, 4, 0, 4, 0, 0, 0, 31, 0, 0, 0, 115, 101, 114, 118, 105, 110, 103, 95, 100, 101, 102, 97, 117, 108, 116, 95, 108, 97, 121, 101, 114, 95, 49, 95, 105, 110, 112, 117, 116, 58, 48, 0, 4, 0, 0, 0, 1, 0, 0, 0, 8, 0, 0, 0, 8, 0, 0, 0, 32, 0, 0, 0, 1, 0, 0, 0, 16, 0, 0, 0, 12, 0, 12, 0, 11, 0, 0, 0, 0, 0, 4, 0, 12, 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 17
6+
28, 0, 0, 0, 84, 70, 76, 51, 20, 0, 32, 0, 28, 0, 24, 0, 20, 0, 16, 0, 12, 0, 0, 0, 8, 0, 4, 0, 20, 0, 0, 0, 28, 0, 0, 0, 148, 0, 0, 0, 236, 0, 0, 0, 144, 1, 0, 0, 160, 1, 0, 0, 64, 3, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 16, 0, 0, 0, 0, 0, 10, 0, 16, 0, 12, 0, 8, 0, 4, 0, 10, 0, 0, 0, 12, 0, 0, 0, 28, 0, 0, 0, 56, 0, 0, 0, 15, 0, 0, 0, 115, 101, 114, 118, 105, 110, 103, 95, 100, 101, 102, 97, 117, 108, 116, 0, 1, 0, 0, 0, 4, 0, 0, 0, 148, 255, 255, 255, 1, 0, 0, 0, 4, 0, 0, 0, 7, 0, 0, 0, 108, 97, 121, 101, 114, 95, 49, 0, 1, 0, 0, 0, 4, 0, 0, 0, 14, 255, 255, 255, 4, 0, 0, 0, 13, 0, 0, 0, 108, 97, 121, 101, 114, 95, 49, 95, 105, 110, 112, 117, 116, 0, 0, 0, 2, 0, 0, 0, 52, 0, 0, 0, 4, 0, 0, 0, 220, 255, 255, 255, 4, 0, 0, 0, 4, 0, 0, 0, 19, 0, 0, 0, 67, 79, 78, 86, 69, 82, 83, 73, 79, 78, 95, 77, 69, 84, 65, 68, 65, 84, 65, 0, 8, 0, 12, 0, 8, 0, 4, 0, 8, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 19, 0, 0, 0, 109, 105, 110, 95, 114, 117, 110, 116, 105, 109, 101, 95, 118, 101, 114, 115, 105, 111, 110, 0, 5, 0, 0, 0, 160, 0, 0, 0, 152, 0, 0, 0, 144, 0, 0, 0, 112, 0, 0, 0, 4, 0, 0, 0, 158, 255, 255, 255, 4, 0, 0, 0, 84, 0, 0, 0, 12, 0, 0, 0, 8, 0, 14, 0, 8, 0, 4, 0, 8, 0, 0, 0, 16, 0, 0, 0, 36, 0, 0, 0, 0, 0, 6, 0, 8, 0, 4, 0, 6, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 16, 0, 12, 0, 8, 0, 4, 0, 10, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0, 6, 0, 0, 0, 50, 46, 49, 51, 46, 48, 0, 0, 0, 0, 6, 0, 8, 0, 4, 0, 6, 0, 0, 0, 4, 0, 0, 0, 16, 0, 0, 0, 49, 46, 53, 46, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 124, 254, 255, 255, 128, 254, 255, 255, 132, 254, 255, 255, 15, 0, 0, 0, 77, 76, 73, 82, 32, 67, 111, 110, 118, 101, 114, 116, 101, 100, 46, 0, 1, 0, 0, 0, 20, 0, 0, 0, 0, 0, 14, 0, 24, 0, 20, 0, 16, 0, 12, 0, 8, 0, 4, 0, 14, 0, 0, 0, 20, 0, 0, 0, 28, 0, 0, 0, 124, 0, 0, 0, 128, 0, 0, 0, 132, 0, 0, 0, 4, 0, 0, 0, 109, 97, 105, 110, 0, 0, 0, 0, 1, 0, 0, 0, 20, 0, 0, 0, 0, 0, 14, 0, 22, 0, 0, 0, 16, 0, 12, 0, 11, 0, 4, 0, 14, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 5, 48, 0, 0, 0, 52, 0, 0, 0, 0, 0, 14, 0, 24, 0, 23, 0, 16, 0, 12, 0, 8, 0, 4, 0, 14, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 128, 0, 0, 0, 4, 0, 0, 0, 158, 255, 255, 255, 0, 0, 0, 1, 20, 0, 0, 0, 36, 0, 0, 0, 36, 0, 0, 0, 2, 0, 0, 0, 52, 0, 0, 0, 4, 0, 0, 0, 255, 255, 255, 255, 1, 0, 0, 0, 1, 0, 0, 0, 32, 0, 0, 0, 136, 255, 255, 255, 17, 0, 0, 0, 80, 97, 114, 116, 105, 116, 105, 111, 110, 101, 100, 67, 97, 108, 108, 58, 48, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 32, 0, 0, 0, 0, 0, 22, 0, 28, 0, 24, 0, 0, 0, 20, 0, 16, 0, 12, 0, 0, 0, 0, 0, 8, 0, 7, 0, 22, 0, 0, 0, 0, 0, 0, 1, 20, 0, 0, 0, 40, 0, 0, 0, 40, 0, 0, 0, 1, 0, 0, 0, 68, 0, 0, 0, 4, 0, 0, 0, 255, 255, 255, 255, 3, 0, 0, 0, 3, 0, 0, 0, 32, 0, 0, 0, 4, 0, 4, 0, 4, 0, 0, 0, 31, 0, 0, 0, 115, 101, 114, 118, 105, 110, 103, 95, 100, 101, 102, 97, 117, 108, 116, 95, 108, 97, 121, 101, 114, 95, 49, 95, 105, 110, 112, 117, 116, 58, 48, 0, 4, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 32, 0, 0, 0, 1, 0, 0, 0, 16, 0, 0, 0, 12, 0, 12, 0, 11, 0, 0, 0, 0, 0, 4, 0, 12, 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 17
77
};

0 commit comments

Comments
 (0)