<jupyter_start><jupyter_text>Test inference on ESP32 microcontroller<jupyter_code>from __future__ import absolute_import, division, print_function, unicode_literals

import glob
import serial
from serial import Serial
import time

import numpy as np
import tensorflow as tf

from PIL import Image
import matplotlib.pyplot as plt
try:
  %tensorflow_version 2.x
except:
  pass
INPUT_IMAGE_SIZE = 28

# Path to TensorFlow Lite model file
tflite_model_file = 'models/mnist_quant.tflite'

# Path to images folder
path = 'test_images_14x14/'
file_format = ".jpg"



EXPECTED_PRECISION = 0.000001
# Function to open serial port
def init_serial(port_name):
    # Configure and open serial port
    port = serial.Serial(
        port=port_name,
        baudrate=115200,
        bytesize=serial.EIGHTBITS,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        xonxoff=False,
        rtscts=False,
        dsrdtr=False
    )
    
    port.reset_input_buffer()
    port.reset_output_buffer()
    return port
def send_to_mcu(port, data):
    bytes_written = port.write(data)
    return bytes_written
def read_result_from_mcu(port):
    line = port.read_until()   # read a '\n' terminated line
    return line
def infer_with_TF_lite(interpreter, input_details, output_details, raw_image):
    # Get input size
    img = raw_image

    # Normalize image
    #img = img / 255.
    input_details = interpreter.get_input_details()[0]
    output_details = interpreter.get_output_details()[0]
    # Add a batch dimension and a dimension because we use grayscale format
    # Reshape from (INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE) to (1, INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE, 1)
    test_image=raw_image
    #print('Data Type ',test_image.dtype)
    
    input_scale, input_zero_point = input_details["quantization"]
    test_image = test_image / input_scale + input_zero_point
    test_image = np.expand_dims(test_image, axis=0).astype(input_details["dtype"])
    # Point the data to be used for testing
    interpreter.set_tensor(input_details['index'], test_image)

    # Run the interpreter
    interpreter.invoke()

    # Obtain results
    #predictions = interpreter.get_tensor(output_details['index'])[0]
    output = interpreter.get_tensor(output_details['index'])[0]
    predictions = output.argmax()
    
    return predictions
def infer_with_MCU(raw_image):
    img = np.array(raw_image, dtype=np.uint8)
    bytes_sent = send_to_mcu(serial_port, img)
    
    response_str = read_result_from_mcu(serial_port)
    response_str = response_str.decode("utf-8")
    print(response_str)
    predictions = np.fromstring(response_str, dtype=np.float32, sep=',')
    predictions = predictions.argmax()
    return predictions
def compare_results(result_tfl, result_mcu):
    for i in range(len(result_tfl)):
        if abs(result_tfl[i] - result_mcu[i]) > EXPECTED_PRECISION :
            return False
        else:
            pass
    return True
# Create Interpreter (Load TFLite model).
interpreter = tf.lite.Interpreter(model_path=tflite_model_file)
# Allocate tensors
interpreter.allocate_tensors()
# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()<jupyter_output><empty_output><jupyter_text>Be sure your ESP32 board connected to development computer.<jupyter_code># Get list of present serial ports
!dmesg | grep -e tty
# Configure and open serial port
SERIAL_PORT_NAME = '/dev/ttyUSB0'
serial_port = init_serial(SERIAL_PORT_NAME)

# Check which port was really used
print("Opened serial port : {0}".format(serial_port.name))        
files = [files for files in glob.glob(path + "*" + file_format, recursive=False)]
print("Found {0} {1} files".format(len(files), file_format))<jupyter_output>Found 0 .jpg files<jupyter_text>Run inference on TensorFlow Lite model and on MCU. Compare results.<jupyter_code># Test on random mnist database numbers
# Load MNIST dataset
mnist = tf.keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# Filter intermediate values to scale for use in uC
train_images[train_images>200]=255
train_images[train_images<=200]=0

test_images[test_images>200]=255
test_images[test_images<=200]=0


for f in range(1):
    
    # load random mnist number
    index = np.random.randint(0,59999)
    
    # Read image
    raw_image = train_images[index]
    plt.imshow(raw_image)
    
    #result_tfl = infer_with_TF_lite(interpreter, input_details, output_details, raw_image)
    
    start_time = time.time(); 
    result_mcu = infer_with_MCU(raw_image)
    end_time = time.time(); 

    #status = compare_results(result_tfl, result_mcu)
    #status_str = "Ok" if status else "Reults NOT eqal!"
    #print("For file: {0} - {1}".format(f, status_str))
    print("\nResult_tfl = ", result_tfl)
    print("Result_mcu =", result_mcu)
    print("Run time: %.5f seconds\n" %(end_time-start_time))
raw_image.dtype
serial_port.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0950479233226837, 'fraction_numerical': 0.01457667731629393, 'mean_word_length': 4.328723404255319, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '278674', 'license': 'Apache-2.0', 'n_tokens_mistral': 1715, 'n_tokens_neox': 1659, 'n_words': 386, 'path': 'python/Test_TFLite_Micro.ipynb', 'repo_name': 'GanManCan/trackpad_card', 'size': '5008'}