In the previous blog, we updated the example code to be compatible with a general ESP32-S3 dev kit module.
One issue with the ESP-DL digit recognition example is that they do not provide the original PyTorch model or the output files generated after the esp-ppq process. Only the final .espdl file is provided.
Therefore, we need to build our own model based on their dataset.
Fortunately, Espressif provides the code for training, testing, and quantizing the model.
In this post, I will walk through the entire process: building the .espdl model using PyTorch and the esp-ppq library, and finally deploying it to the ESP32-S3 to verify it works.
How This Blog May Help You
Learn how to build a neural network using PyTorch.
Understand the complete workflow from dataset preparation to deployment on the ESP32-S3.
import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import DataLoader, random_split from torchvision import datasets, transforms
defdisplay_random_images(dataset: torch.utils.data.dataset.Dataset, classes: list[str] = None, n: int = 10, display_shape: bool = True, seed: int = None):
if n > 10: n = 10 display_shape = False print(f"For display purposes, n shouldn't be larger than 10, setting to 10 and removing shape display.")
# 3. Set random seed if seed: random.seed(seed)
# 4. Get random sample indexes random_samples_idx = random.sample(range(len(dataset)), k=n)
# 5. Setup plot plt.figure(figsize=(16, 8))
# 6. Loop through samples and display random samples for i, targ_sample inenumerate(random_samples_idx): targ_image, targ_label = dataset[targ_sample][0], dataset[targ_sample][1]
# Plot adjusted samples plt.subplot(1, n, i+1) plt.imshow(targ_image_adjust) plt.axis("off") if classes: title = f"class: {classes[targ_label]}" if display_shape: title = title + f"\nshape: {targ_image_adjust.shape}" plt.title(title)
# Display random images from ImageFolder created Dataset display_random_images(train_dataset, n=5, classes=class_names, seed=42)
6. Define Loss Function and Optimizer
Use standard loss and optimizer for image classification:
9. Run the Quantization Script in the Python 3.10 Environment
1
!konda run "python espdl_model_quant.py"
10. After Quantization
You will get the .onnx model, which you can inspect using Netron.
11. Copy the .espdl File to the ESP32-S3 Working Directory
12. Update CMakeLists.txt to Use the New Model
13. Build and Flash the Firmware Using Your Own Model
14. Success!
Issue Summary
As of this writing, the esp-dl package requires Python version <=3.10. It does not work with Python >3.12.
When importing the package, use esp_ppq (with an underscore), not esp-ppq.
Most of the time was spent figuring out how to change the Python version in Google Colab. Using konda to create a virtual environment solved this issue.
In the next post in this series, I will review the model and highlight all the important points about the neural network architecture used in digit recognition.