Commit 3b9137be by taishi

Merge branch 'feature/adding_lab7' into 'master'

Adding lab 7 exercise.

See merge request !2
parents 87451071 80783b6b
......@@ -2,3 +2,5 @@
build
.vscode
__pycache__
venv
NN.*
\ No newline at end of file
[submodule "lab_7/lib/pico-tflmicro"]
path = lab_7/lib/pico-tflmicro
url = https://github.com/raspberrypi/pico-tflmicro.git
cmake_minimum_required(VERSION 3.12)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
include(pico_sdk_import.cmake)
# project(pico-tflite-inference-test)
project(pico_tflite_inference_test C CXX ASM)
# initialize the Pico SDK
pico_sdk_init()
add_executable(main main.cpp) # main function to run.
add_executable(main_arena_size_test arena_size_test.cpp) # to check arena size for a given model.
add_executable(main_inference_test inference_test.cpp) # to test hand written digit recognition with static test data.
target_include_directories(main
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/.
)
target_include_directories(main_arena_size_test
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/.
)
target_include_directories(main_inference_test
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/.
)
set_target_properties(
main
PROPERTIES
COMPILE_FLAGS -fno-rtti
COMPILE_FLAGS -fno-exceptions
COMPILE_FLAGS -fno-threadsafe-statics
COMPILE_FLAGS -nostdlib
)
set_target_properties(
main_arena_size_test
PROPERTIES
COMPILE_FLAGS -fno-rtti
COMPILE_FLAGS -fno-exceptions
COMPILE_FLAGS -fno-threadsafe-statics
COMPILE_FLAGS -nostdlib
)
set_target_properties(
main_inference_test
PROPERTIES
COMPILE_FLAGS -fno-rtti
COMPILE_FLAGS -fno-exceptions
COMPILE_FLAGS -fno-threadsafe-statics
COMPILE_FLAGS -nostdlib
)
add_subdirectory(src)
add_subdirectory(models)
add_subdirectory(lib/pico-tflmicro)
add_subdirectory(lib/config)
add_subdirectory(lib/lcd)
add_subdirectory(lib/font)
include_directories(./src)
include_directories(./models)
include_directories(./lib/pico-tflmicro)
include_directories(./lib/config)
include_directories(./lib/lcd)
include_directories(./lib/font)
target_link_libraries(
main
src
lcd
font
config
models
pico_stdlib
hardware_spi
hardware_pwm
pico_multicore
hardware_adc
pico-tflmicro
)
target_link_libraries(
main_arena_size_test
src
config
models
pico_stdlib
hardware_spi
hardware_pwm
pico_multicore
hardware_adc
pico-tflmicro
)
target_link_libraries(
main_inference_test
src
config
models
pico_stdlib
hardware_spi
hardware_pwm
pico_multicore
hardware_adc
pico-tflmicro
)
# enable usb and uart output
pico_enable_stdio_usb(main 1)
pico_enable_stdio_uart(main 1)
pico_enable_stdio_usb(main_arena_size_test 1)
pico_enable_stdio_uart(main_arena_size_test 1)
pico_enable_stdio_usb(main_inference_test 1)
pico_enable_stdio_uart(main_inference_test 1)
# create map/bin/hex/uf2 file etc.
pico_add_extra_outputs(main)
pico_add_extra_outputs(main_arena_size_test)
pico_add_extra_outputs(main_inference_test)
#include <cmath>
#include<iostream>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "pico/sync.h"
#include "hardware/watchdog.h"
#include "hardware/sync.h"
#include "DEV_Config.h"
#include "model.h"
#include "model_settings.h"
#include "mnist_model_data.h"
using namespace std;
Model ml_model;
int main() {
System_Init();
sleep_ms(1000);
// initialize ML model
if (!ml_model.setup()) {
printf("Failed to initialize ML model!\n");
return -1;
}
printf("Model initialized\n");
uint8_t* test_image_input = ml_model.input_data();
if (test_image_input == nullptr) {
printf("Cannot set input\n");
return -1;
}
int byte_size = ml_model.byte_size();
if (!byte_size) {
printf("Byte size not found\n");
return -1;
}
while(1) {
printf("The tensor arena size: %d\n", ml_model.interpreter->arena_used_bytes());
sleep_ms(1000);
}
return 0;
}
\ No newline at end of file
#include <cmath>
#include<iostream>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "pico/sync.h"
#include "hardware/watchdog.h"
#include "hardware/sync.h"
#include "DEV_Config.h"
#include "inference.h"
using namespace std;
int main(void) {
System_Init();
sleep_ms(5000);
inference_test();
return 0;
}
\ No newline at end of file
# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_Config_SRCS 变量
aux_source_directory(. DIR_CONFIG_SRCS)
# 生成链接库
add_library(config ${DIR_CONFIG_SRCS})
target_link_libraries(config PUBLIC pico_stdlib hardware_spi hardware_pwm)
/*****************************************************************************
* | File : DEV_Config.c
* | Author : Waveshare team
* | Function : Show SDcard BMP picto LCD
* | Info :
* Provide the hardware underlying interface
*----------------
* | This version: V1.0
* | Date : 2018-01-11
* | Info : Basic version
*
******************************************************************************/
#include "DEV_Config.h"
#include "pico/stdlib.h"
void DEV_Digital_Write(UWORD Pin, UBYTE Value)
{
gpio_put(Pin, Value);
}
UBYTE DEV_Digital_Read(UWORD Pin)
{
return gpio_get(Pin);
}
/**
* GPIO Mode
**/
void DEV_GPIO_Mode(UWORD Pin, UWORD Mode)
{
gpio_init(Pin);
if(Mode == 0 || Mode == GPIO_IN) {
gpio_set_dir(Pin, GPIO_IN);
} else {
gpio_set_dir(Pin, GPIO_OUT);
}
}
void DEV_GPIO_Init(void)
{
DEV_GPIO_Mode(LCD_RST_PIN,GPIO_OUT);
DEV_GPIO_Mode(LCD_DC_PIN, GPIO_OUT);
//DEV_GPIO_Mode(LCD_BKL_PIN, GPIO_OUT);
DEV_GPIO_Mode(LCD_CS_PIN, GPIO_OUT);
DEV_GPIO_Mode(TP_CS_PIN,GPIO_OUT);
DEV_GPIO_Mode(TP_IRQ_PIN,GPIO_IN);
DEV_GPIO_Mode(SD_CS_PIN,GPIO_OUT);
//gpio_set_pulls(TP_IRQ_PIN,true,false);
DEV_Digital_Write(TP_CS_PIN, 1);
DEV_Digital_Write(LCD_CS_PIN, 1);
//DEV_Digital_Write(LCD_BKL_PIN, 0);
DEV_Digital_Write(SD_CS_PIN, 1);
gpio_set_function(LCD_BKL_PIN, GPIO_FUNC_PWM);
}
/********************************************************************************
function: System Init
note:
Initialize the communication method
********************************************************************************/
uint8_t System_Init(void)
{
stdio_init_all();
DEV_GPIO_Init();
spi_init(SPI_PORT,5000000);
gpio_set_function(LCD_CLK_PIN,GPIO_FUNC_SPI);
gpio_set_function(LCD_MOSI_PIN,GPIO_FUNC_SPI);
gpio_set_function(LCD_MISO_PIN,GPIO_FUNC_SPI);
return 0;
}
void System_Exit(void)
{
}
/*********************************************
function: Hardware interface
note:
SPI4W_Write_Byte(value) :
Register hardware SPI
*********************************************/
uint8_t SPI4W_Write_Byte(uint8_t value)
{
uint8_t rxDat;
spi_write_read_blocking(spi1,&value,&rxDat,1);
return rxDat;
}
uint8_t SPI4W_Read_Byte(uint8_t value)
{
return SPI4W_Write_Byte(value);
}
/********************************************************************************
function: Delay function
note:
Driver_Delay_ms(xms) : Delay x ms
Driver_Delay_us(xus) : Delay x us
********************************************************************************/
void Driver_Delay_ms(uint32_t xms)
{
sleep_ms(xms);
}
void Driver_Delay_us(uint32_t xus)
{
int j;
for(j=xus; j > 0; j--);
}
/*****************************************************************************
* | File : DEV_Config.c
* | Author : Waveshare team
* | Function : GPIO Function
* | Info :
* Provide the hardware underlying interface
*----------------
* | This version: V1.0
* | Date : 2018-01-11
* | Info : Basic version
*
******************************************************************************/
#ifndef TFLITE_INFERENCE_TEST_DEV_CONFIG_H_
#define TFLITE_INFERENCE_TEST_DEV_CONFIG_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "pico/stdlib.h"
#include "hardware/spi.h"
#include "hardware/pwm.h"
#include "stdio.h"
#define UBYTE uint8_t
#define UWORD uint16_t
#define UDOUBLE uint32_t
#define LCD_RST_PIN 15
#define LCD_DC_PIN 8
#define LCD_CS_PIN 9
#define LCD_CLK_PIN 10
#define LCD_BKL_PIN 13
#define LCD_MOSI_PIN 11
#define LCD_MISO_PIN 12
#define TP_CS_PIN 16
#define TP_IRQ_PIN 17
#define SD_CS_PIN 22
#define INPUT_IMAGE_SIZE 28
#define MULTICORE_RUN_INFERENCE_FLAG 123
#define UNKNOWN_PREDICTION 100
#define SPI_PORT spi1
#define MAX_BMP_FILES 25
/*------------------------------------------------------------------------------------------------------*/
void DEV_Digital_Write(UWORD Pin, UBYTE Value);
UBYTE DEV_Digital_Read(UWORD Pin);
void DEV_GPIO_Mode(UWORD Pin, UWORD Mode);
void DEV_GPIO_Init(void);
uint8_t System_Init(void);
void System_Exit(void);
uint8_t SPI4W_Write_Byte(uint8_t value);
uint8_t SPI4W_Read_Byte(uint8_t value);
void Driver_Delay_ms(uint32_t xms);
void Driver_Delay_us(uint32_t xus);
#ifdef __cplusplus
}
#endif
#endif // TFLITE_INFERENCE_TEST_DEV_CONFIG_H_
\ No newline at end of file
aux_source_directory(. DIR_font_SRCS)
add_library(font ${DIR_font_SRCS})
target_link_libraries(font PUBLIC)
/**
******************************************************************************
* @file fonts.h
* @author MCD Application Team
* @version V1.0.0
* @date 18-February-2014
* @brief Header for fonts.c file
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef TFLITE_INFERENCE_TEST_FONT_H_
#define TFLITE_INFERENCE_TEST_FONT_H_
/* Max size of bitmap will based on a font24 (17x24) */
#define MAX_HEIGHT_FONT 24
#define MAX_WIDTH_FONT 17
#define OFFSET_BITMAP 54
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
typedef struct _tFont
{
const uint8_t *table;
uint16_t Width;
uint16_t Height;
} sFONT;
extern sFONT Font24;
extern sFONT Font20;
extern sFONT Font16;
extern sFONT Font12;
extern sFONT Font8;
#ifdef __cplusplus
}
#endif
#endif /* __FONTS_H */ // TFLITE_INFERENCE_TEST_FONT_H_
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
aux_source_directory(. DIR_LCD_SRCS)
include_directories(../config)
include_directories(../font)
include_directories(../../src)
add_library(lcd ${DIR_LCD_SRCS})
target_link_libraries(
lcd
PUBLIC
config
font
src
pico_stdlib
pico_multicore
)
/*****************************************************************************
* | File : LCD_Driver.h
* | Author : Waveshare team
* | Function : ILI9486 Drive function
* | Info :
* Image scanning:
* Please use progressive scanning to generate images or fonts
*----------------
* | This version: V1.0
* | Date : 2018-01-11
* | Info : Basic version
*
******************************************************************************/
/**************************Intermediate driver layer**************************/
#ifndef TFLITE_INFERENCE_TEST_LCD_DRIVER_H_
#define TFLITE_INFERENCE_TEST_LCD_DRIVER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "DEV_Config.h"
#define LCD_2_8 0x52
#define LCD_3_5 0x00
#define COLOR uint16_t //The variable type of the color (unsigned short)
#define POINT uint16_t //The type of coordinate (unsigned short)
#define LENGTH uint16_t //The type of coordinate (unsigned short)
/********************************************************************************
function:
Define the full screen height length of the display
********************************************************************************/
#define LCD_X_MAXPIXEL 480 //LCD width maximum memory
#define LCD_Y_MAXPIXEL 320 //LCD height maximum memory
#define LCD_X 0
#define LCD_Y 0
#define LCD_3_5_WIDTH (LCD_X_MAXPIXEL - 2 * LCD_X) //LCD width
#define LCD_3_5_HEIGHT LCD_Y_MAXPIXEL //LCD height
#define LCD_2_8_WIDTH 240 //LCD width
#define LCD_2_8_HEIGHT 320
/********************************************************************************
function:
scanning method
********************************************************************************/
typedef enum {
L2R_U2D = 0, //The display interface is displayed , left to right, up to down
L2R_D2U ,
R2L_U2D ,
R2L_D2U ,
U2D_L2R ,
U2D_R2L ,
D2U_L2R ,
D2U_R2L ,
} LCD_SCAN_DIR;
#define SCAN_DIR_DFT D2U_L2R //Default scan direction = L2R_U2D
/********************************************************************************
function:
Defines the total number of rows in the display area
********************************************************************************/
typedef struct {
LENGTH LCD_Dis_Column; //COLUMN
LENGTH LCD_Dis_Page; //PAGE
LCD_SCAN_DIR LCD_Scan_Dir;
POINT LCD_X_Adjust; //LCD x actual display position calibration
POINT LCD_Y_Adjust; //LCD y actual display position calibration
} LCD_DIS;
/********************************************************************************
function:
Macro definition variable name
********************************************************************************/
void LCD_Init(LCD_SCAN_DIR LCD_ScanDir, uint16_t LCD_BLval);
void LCD_SetGramScanWay(LCD_SCAN_DIR Scan_dir);
void LCD_WriteReg(uint8_t Reg);
void LCD_WriteData(uint16_t Data);
void LCD_SetWindow(POINT Xstart, POINT Ystart, POINT Xend, POINT Yend);
void LCD_SetCursor(POINT Xpoint, POINT Ypoint);
void LCD_SetColor(COLOR Color ,POINT Xpoint, POINT Ypoint);
void LCD_SetPointlColor(POINT Xpoint, POINT Ypoint, COLOR Color);
void LCD_SetArealColor(POINT Xstart, POINT Ystart, POINT Xend, POINT Yend,COLOR Color);
void LCD_Clear(COLOR Color);
uint8_t LCD_Read_Id(void);
void LCD_SetBackLight(uint16_t value);
#ifdef __cplusplus
}
#endif
#endif // TFLITE_INFERENCE_TEST_LCD_DRIVER_H_
/*****************************************************************************
* | File : LCD_GUI.h
* | Author : Waveshare team
* | Function : Achieve drawing: draw points, lines, boxes, circles and
* their size, solid dotted line, solid rectangle hollow
* rectangle, solid circle hollow circle.
* | Info :
* Achieve display characters: Display a single character, string, number
* Achieve time display: adaptive size display time minutes and seconds
*----------------
* | This version: V1.0
* | Date : 2017-08-16
* | Info : Basic version
*
******************************************************************************/
/****************************Upper application layer**************************/
#ifndef TFLITE_INFERENCE_TEST_LCD_GUI_H_
#define TFLITE_INFERENCE_TEST_LCD_GUI_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "LCD_Driver.h"
#include "fonts.h"
#define LOW_Speed_Show 0
#define HIGH_Speed_Show 1
/********************************************************************************
function:
dot pixel
********************************************************************************/
typedef enum {
DOT_PIXEL_1X1 = 1, // dot pixel 1 x 1
DOT_PIXEL_2X2 , // dot pixel 2 X 2
DOT_PIXEL_3X3 , // dot pixel 3 X 3
DOT_PIXEL_4X4 , // dot pixel 4 X 4
DOT_PIXEL_5X5 , // dot pixel 5 X 5
DOT_PIXEL_6X6 , // dot pixel 6 X 6
DOT_PIXEL_7X7 , // dot pixel 7 X 7
DOT_PIXEL_8X8 , // dot pixel 8 X 8
} DOT_PIXEL;
#define DOT_PIXEL_DFT DOT_PIXEL_1X1 //Default dot pilex
/********************************************************************************
function:
dot Fill style
********************************************************************************/
typedef enum {
DOT_FILL_AROUND = 1, // dot pixel 1 x 1
DOT_FILL_RIGHTUP , // dot pixel 2 X 2
} DOT_STYLE;
#define DOT_STYLE_DFT DOT_FILL_AROUND //Default dot pilex
/********************************************************************************
function:
solid line and dotted line
********************************************************************************/
typedef enum {
LINE_SOLID = 0,
LINE_DOTTED,
} LINE_STYLE;
/********************************************************************************
function:
DRAW Internal fill
********************************************************************************/
typedef enum {
DRAW_EMPTY = 0,
DRAW_FULL,
} DRAW_FILL;
/********************************************************************************
function:
time
********************************************************************************/
typedef struct {
uint16_t Year; //0000
uint8_t Month; //1 - 12
uint8_t Day; //1 - 30
uint8_t Hour; //0 - 23
uint8_t Min; //0 - 59
uint8_t Sec; //0 - 59
} DEV_TIME;
extern DEV_TIME sDev_time;
/********************************************************************************
function:
Defines commonly used colors for the display
********************************************************************************/
#define LCD_BACKGROUND WHITE //Default background color
#define FONT_BACKGROUND WHITE //Default font background color
#define FONT_FOREGROUND GRED //Default font foreground color
#define WHITE 0xFFFF
#define BLACK 0x0000
#define BLUE 0x001F
#define BRED 0XF81F
#define GRED 0XFFE0
#define GBLUE 0X07FF
#define RED 0xF800
#define MAGENTA 0xF81F
#define GREEN 0x07E0
#define CYAN 0x7FFF
#define YELLOW 0xFFE0
#define BROWN 0XBC40
#define BRRED 0XFC07
#define GRAY 0X8430
/********************************************************************************
function:
Macro definition variable name
********************************************************************************/
//Clear
void GUI_Clear(COLOR Color);
//Drawing
void GUI_DrawPoint(POINT Xpoint, POINT Ypoint, COLOR Color, DOT_PIXEL Dot_Pixel, DOT_STYLE Dot_FillWay);
void GUI_DrawLine(POINT Xstart, POINT Ystart, POINT Xend, POINT Yend, COLOR Color, LINE_STYLE Line_Style, DOT_PIXEL Dot_Pixel);
void GUI_DrawRectangle(POINT Xstart, POINT Ystart, POINT Xend, POINT Yend, COLOR Color, DRAW_FILL Filled , DOT_PIXEL Dot_Pixel );
void GUI_DrawCircle(POINT X_Center, POINT Y_Center, LENGTH Radius, COLOR Color, DRAW_FILL Draw_Fill , DOT_PIXEL Dot_Pixel );
//pic
void GUI_Disbitmap(POINT Xpoint, POINT Ypoint, const unsigned char *pMap, POINT Width, POINT Height);
void GUI_DisGrayMap(POINT Xpoint, POINT Ypoint, const unsigned char *pBmp);
//Display string
void GUI_DisChar(POINT Xstart, POINT Ystart, const char Acsii_Char, sFONT* Font, COLOR Color_Background, COLOR Color_Foreground);
void GUI_DisString_EN(POINT Xstart, POINT Ystart, const char * pString, sFONT* Font, COLOR Color_Background, COLOR Color_Foreground );
void GUI_DisNum(POINT Xpoint, POINT Ypoint, int32_t Nummber, sFONT* Font, COLOR Color_Background, COLOR Color_Foreground );
void GUI_Showtime(POINT Xstart, POINT Ystart, POINT Xend, POINT Yend, DEV_TIME *pTime, COLOR Color);
//show
void GUI_Show(void);
#ifdef __cplusplus
}
#endif
#endif // TFLITE_INFERENCE_TEST_LCD_GUI_H_
\ No newline at end of file
/*****************************************************************************
* | File : LCD_Touch.h
* | Author : Waveshare team
* | Function : LCD Touch Pad Driver and Draw
* | Info :
* Image scanning
* Please use progressive scanning to generate images or fonts
*----------------
* | This version: V1.0
* | Date : 2017-08-16
* | Info : Basic version
*
******************************************************************************/
#ifndef TFLITE_INFERENCE_TEST_LCD_TOUCH_H_
#define TFLITE_INFERENCE_TEST_LCD_TOUCH_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "DEV_Config.h"
#include "LCD_Driver.h"
#include "LCD_GUI.h"
#include <math.h>
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/float.h"
#include "pico/multicore.h"
#include "pico/sync.h"
#define TP_PRESS_DOWN 0x80
#define TP_PRESSED 0x40
#define IGNORE_INTERVAL_MS 200
#define DIGIT_INPUT_COUNT 4
#define BOX_SIZE 84
#define BOX_START_Y 120 // Starting Y position for the boxes
#define BORDER_THICKNESS 2
#define BOX_PADDING 4
#define POINT_SPACE 2
#define WINDOW_SIZE 3
#define SPACE_BETWEEN_BOXES 10
//Touch screen structure
typedef struct {
POINT Xpoint0;
POINT Ypoint0;
POINT Xpoint;
POINT Ypoint;
uint8_t chStatus;
uint8_t chType;
int16_t iXoff;
int16_t iYoff;
float fXfac;
float fYfac;
//Select the coordinates of the XPT2046 touch \
screen relative to what scan direction
LCD_SCAN_DIR TP_Scan_Dir;
}TP_DEV;
//Brush structure
typedef struct{
POINT Xpoint;
POINT Ypoint;
COLOR Color;
DOT_PIXEL DotPixel;
}TP_DRAW;
typedef struct{
uint8_t InputData[INPUT_IMAGE_SIZE * INPUT_IMAGE_SIZE]; // 784 uint8_t array
int8_t PredictedDigit;
} USER_INPUT;
typedef struct{
// semaphore_t Semaphore;
bool IsProcessing;
USER_INPUT UserInputs[DIGIT_INPUT_COUNT]; // 4 inputs
} INFERENCE;
typedef struct{
int start_x;
int start_y;
int end_x;
int end_y;
uint8_t content[BOX_SIZE][BOX_SIZE];
} BOX_REFERENCE;
void TP_GetAdFac(void);
void TP_Adjust(void);
void TP_Dialog(void);
void TP_DrawBoard(void);
void TP_Init( LCD_SCAN_DIR Lcd_ScanDir );
void init_gui(void);
void reset_inference(INFERENCE* _inference);
int find_box_by_point(void);
void clear_drawing();
void draw_inference_result();
#ifdef __cplusplus
}
#endif
#endif // TFLITE_INFERENCE_TEST_LCD_TOUCH_H_
Subproject commit 03cbb1e7b89792aef2c59e2dbe8cb2c81c049bbd
#include <cmath>
#include<iostream>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "pico/sync.h"
#include "hardware/watchdog.h"
#include "hardware/sync.h"
#include "LCD_Driver.h"
#include "LCD_Touch.h"
#include "LCD_GUI.h"
#include "DEV_Config.h"
#include "model.h"
#include "model_settings.h"
#include "mnist_model_data.h"
using namespace std;
#define HALT_CORE_1() while (1) { tight_loop_contents(); }
INFERENCE inference;
Model ml_model;
mutex_t mutex; // Declare a mutex
int count_digits(int number) {
if (number == 0) {
return 1; // Special case for 0, which has 1 digit
}
number = abs(number); // Handle negative numbers
return std::floor(std::log10(number)) + 1;
}
// run core0 loop that displays UI and handle user interaction
void core1_entry() {
uint16_t cnt=0;
while(true) {
for(cnt=1000;cnt>2;cnt--)
{
LCD_SetBackLight(1000);
// pass the ML inputs, output, and semaphore
TP_DrawBoard();
}
}
}
int main(void)
{
System_Init();
mutex_init(&mutex); // Initialize the mutex
sleep_ms(5000);
// initialize LCD display
LCD_SCAN_DIR lcd_scan_dir = SCAN_DIR_DFT;
LCD_Init(lcd_scan_dir,1000);
TP_Init(lcd_scan_dir);
LCD_SCAN_DIR bmp_scan_dir = D2U_R2L;
TP_GetAdFac();
reset_inference(&inference);
init_gui();
// run core1 loop that handles user interface
multicore_launch_core1(core1_entry);
// initialize ML model
if (!ml_model.setup()) {
printf("Failed to initialize ML model!\n");
HALT_CORE_1();
}
printf("Model initialized\n");
uint8_t* test_image_input = ml_model.input_data();
if (test_image_input == nullptr) {
printf("Cannot set input\n");
HALT_CORE_1();
}
int byte_size = ml_model.byte_size();
if (!byte_size) {
printf("Byte size not found\n");
HALT_CORE_1();
}
while (true) {
// Block the process until data being filled
uint32_t g = multicore_fifo_pop_blocking();
// Acquire the mutex (blocking)
mutex_enter_blocking(&mutex);
inference.IsProcessing = true;
// Run inference and set predicted result
// This for loop is used for debugging purpose.
for (int index=0; index<DIGIT_INPUT_COUNT; index++) {
for (int i=0; i<INPUT_IMAGE_SIZE; i++) {
for (int j=0; j<INPUT_IMAGE_SIZE; j++) {
uint8_t num = inference.UserInputs[index].InputData[i*INPUT_IMAGE_SIZE + j];
int space = 3 - count_digits(num);
printf("%d", num);
for (int i = 0; i < space; ++i) {
printf(" ");
}
}
printf("\n");
}
memcpy(test_image_input, inference.UserInputs[index].InputData, byte_size);
int result = ml_model.predict();
if (result == -1) {
printf("Failed to run inference\n");
inference.UserInputs[index].PredictedDigit = UNKNOWN_PREDICTION;
} else {
printf("Predicted: %d\n", result);
inference.UserInputs[index].PredictedDigit = result;
}
sleep_ms(200);
}
printf("Login process finished.\n");
inference.IsProcessing = false;
//Return the resource
mutex_exit(&mutex);
}
return 0;
}
aux_source_directory(. DIR_MODELS_SRCS)
include_directories(../src)
add_library(models ${DIR_MODELS_SRCS})
target_include_directories(models PUBLIC src)
\ No newline at end of file
#include "mnist_model_data.h"
alignas(8) const unsigned char mnist_model_data[] = {
// TODO: 1. Copy your tflite C array and paste it here.
};
// TODO: 2. Change model data length
const int mnist_model_data_len = -1;
\ No newline at end of file
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake
# This can be dropped into an external project to help locate this SDK
# It should be include()ed prior to project()
if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
endif ()
set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the PICO SDK")
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of PICO SDK from git if not otherwise locatable")
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")
if (NOT PICO_SDK_PATH)
if (PICO_SDK_FETCH_FROM_GIT)
include(FetchContent)
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
if (PICO_SDK_FETCH_FROM_GIT_PATH)
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
endif ()
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG master
)
if (NOT pico_sdk)
message("Downloading PICO SDK")
FetchContent_Populate(pico_sdk)
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
endif ()
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
else ()
message(FATAL_ERROR
"PICO SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
)
endif ()
endif ()
get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
if (NOT EXISTS ${PICO_SDK_PATH})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
endif ()
set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the PICO SDK")
endif ()
set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the PICO SDK" FORCE)
include(${PICO_SDK_INIT_CMAKE_FILE})
aux_source_directory(. DIR_SRC_SRCS)
include_directories(../lib/pico-tflmicro)
add_library(src ${DIR_SRC_SRCS})
target_link_libraries(
src
PUBLIC
pico_stdlib
pico-tflmicro
)
#include <cmath>
#include<iostream>
#include <cstdlib>
#include <iostream>
#include "pico/stdlib.h"
#include "model.h"
#include "inference.h"
#include "model_settings.h"
#include "mnist_model_data.h"
#include "mnist_image_data.h"
using namespace std;
#define HALT_CORE_1() while (1) { tight_loop_contents(); }
const uint8_t* test_dataset[] = {
mnist_image_data_0,
mnist_image_data_1,
mnist_image_data_2,
mnist_image_data_3,
mnist_image_data_4,
mnist_image_data_5,
mnist_image_data_6,
mnist_image_data_7,
mnist_image_data_8,
mnist_image_data_9
};
int count_digits(int number) {
if (number == 0) {
return 1; // Special case for 0, which has 1 digit
}
number = abs(number); // Handle negative numbers
return std::floor(std::log10(number)) + 1;
}
void inference_test(void)
{
Model ml_model;
if (!ml_model.setup()) {
printf("Failed to initialize ML model!\n");
HALT_CORE_1();
}
printf("Model initialized\n");
uint8_t* test_image_input = ml_model.input_data();
if (test_image_input == nullptr) {
printf("Cannot set input\n");
HALT_CORE_1();
}
int byte_size = ml_model.byte_size();
if (!byte_size) {
printf("Byte size not found\n");
HALT_CORE_1();
}
while (true) {
int random = rand() % 10;
const uint8_t* sample_data = test_dataset[random];
for (int i=0; i<image_row_size; i++) {
for (int j=0; j<image_col_size; j++) {
int num = sample_data[image_col_size*i + j];
int space = 3 - count_digits(num);
printf("%d", num);
for (int i = 0; i < space; ++i) {
printf(" ");
}
}
printf("\n");
}
memcpy(test_image_input, sample_data, byte_size);
int result = ml_model.predict();
if (result == NAN) {
printf("Failed to run inference\n");
} else {
printf("Actual: %d, Predicted: %d\n", random, result);
}
sleep_ms(10000);
}
}
\ No newline at end of file
#ifndef TFLITE_INFERENCE_TEST_INFERENCE_H_
#define TFLITE_INFERENCE_TEST_INFERENCE_H_
#include <stdio.h>
#include <cstdint>
#include "pico/stdlib.h"
#include "pico/multicore.h"
// #include "hardware/irq.h"
#define DIGIT_SIZE 28
#define NUM_BOX 4
void inference_test(void);
#endif // TFLITE_INFERENCE_TEST_INFERENCE_H_
\ No newline at end of file
#ifndef TFLITE_INFERENCE_TEST_MNIST_IMAGE_DATA_H_
#define TFLITE_INFERENCE_TEST_MNIST_IMAGE_DATA_H_
#include <cstdint>
extern const uint8_t mnist_image_data_0[];
extern const uint8_t mnist_image_data_1[];
extern const uint8_t mnist_image_data_2[];
extern const uint8_t mnist_image_data_3[];
extern const uint8_t mnist_image_data_4[];
extern const uint8_t mnist_image_data_5[];
extern const uint8_t mnist_image_data_6[];
extern const uint8_t mnist_image_data_7[];
extern const uint8_t mnist_image_data_8[];
extern const uint8_t mnist_image_data_9[];
#endif // TFLITE_INFERENCE_TEST_MNIST_IMAGE_DATA_H_
#ifndef TFLITE_INFERENCE_TEST_MNIST_MODEL_DATA_H_
#define TFLITE_INFERENCE_TEST_MNIST_MODEL_DATA_H_
#include <cstdint>
extern const unsigned char mnist_model_data[];
extern const int mnist_model_data_len;
#endif // TFLITE_INFERENCE_TEST_MNIST_MODEL_DATA_H_
#include "tensorflow/lite/micro/tflite_bridge/micro_error_reporter.h"
#include "tensorflow/lite/schema/schema_generated.h"
#include "tensorflow/lite/micro/micro_mutable_op_resolver.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "model.h"
#include "model_settings.h"
// TODO: 4. Import your model data
Model::Model() :
model(nullptr),
interpreter(nullptr),
input(nullptr),
error_reporter(nullptr)
{
}
Model::~Model()
{
if (interpreter != NULL) {
delete interpreter;
interpreter = NULL;
}
if (model != NULL) {
delete model;
model = NULL;
}
if (input != NULL) {
delete input;
input = NULL;
}
if (error_reporter != NULL) {
delete error_reporter;
error_reporter = NULL;
}
}
int Model::setup()
{
static tflite::MicroErrorReporter micro_error_reporter;
error_reporter = &micro_error_reporter;
model = tflite::GetModel(/* TODO: 5. Load your model. */);
if (model->version() != TFLITE_SCHEMA_VERSION) {
TF_LITE_REPORT_ERROR(error_reporter,
"Model provided is schema version %d not equal "
"to supported version %d.",
model->version(), TFLITE_SCHEMA_VERSION);
return 0;
}
static tflite::MicroMutableOpResolver</* TODO: 6. Change operations resolver size. */> micro_op_resolver;
// TODO: 7. Add operations according to your model.
static uint8_t tensor_arena[arena_size];
// Build an interpreter to run the model with.
// NOLINTNEXTLINE(runtime-global-variables)
static tflite::MicroInterpreter static_interpreter(
model, micro_op_resolver, tensor_arena, arena_size);
interpreter = &static_interpreter;
// TODO: 8. Allocate tensor
// Get information about the memory area to use for the model's input.
input = interpreter->input(0);
return 1;
}
uint8_t* Model::input_data() {
if (input == nullptr) {
return nullptr;
}
return input->data.uint8;
}
int Model::byte_size() {
if (input == nullptr) {
return 0;
}
return input->bytes;
}
int Model::predict()
{
printf("Invocation started\n");
// TODO: 9. Run invoke inference, if error, return -1
printf("Invocation finished\n");
TfLiteTensor* output = interpreter->output(0);
int result = -1;
// TODO: 10. Return an index of the output neuron, which has maximum probability.
return result;
}
\ No newline at end of file
#ifndef TFLITE_INFERENCE_TEST_MODEL_H_
#define TFLITE_INFERENCE_TEST_MODEL_H_
#include "tensorflow/lite/micro/tflite_bridge/micro_error_reporter.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/micro/micro_mutable_op_resolver.h"
#include "tensorflow/lite/schema/schema_generated.h"
class Model {
public:
Model();
virtual ~Model();
int setup();
int predict();
uint8_t* input_data();
int byte_size();
const tflite::Model* model = nullptr;
TfLiteTensor* input = nullptr;
tflite::MicroInterpreter* interpreter = nullptr;
tflite::ErrorReporter* error_reporter = nullptr;
private:
};
#endif // TFLITE_INFERENCE_TEST_MODEL_H_
\ No newline at end of file
#ifndef TFLITE_INFERENCE_TEST_MODEL_SETTINGS_H_
#define TFLITE_INFERENCE_TEST_MODEL_SETTINGS_H_
constexpr int kNumCols = 96;
constexpr int kNumRows = 96;
constexpr int kNumChannels = 1;
constexpr int kMaxImageSize = kNumCols * kNumRows * kNumChannels;
constexpr int kCategoryCount = 2;
constexpr int kPersonIndex = 1;
constexpr int kNotAPersonIndex = 0;
extern const char* kCategoryLabels[kCategoryCount];
constexpr int image_col_size = 28;
constexpr int image_row_size = 28;
constexpr int arena_size = -1; // TODO: 3. Edit this for your own model.
#endif // TFLITE_INFERENCE_TEST_MODEL_SETTINGS_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment