Export a MNIST model to a CPP standalone project#

[1]:
%pip install requests numpy ipywidgets ipycanvas
Requirement already satisfied: requests in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (2.32.3)
Requirement already satisfied: numpy in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (2.2.0)
Collecting ipywidgets
  Downloading ipywidgets-8.1.5-py3-none-any.whl.metadata (2.3 kB)
Collecting ipycanvas
  Downloading ipycanvas-0.13.3-py2.py3-none-any.whl.metadata (6.3 kB)
Requirement already satisfied: charset-normalizer<4,>=2 in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from requests) (3.4.0)
Requirement already satisfied: idna<4,>=2.5 in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from requests) (3.10)
Requirement already satisfied: urllib3<3,>=1.21.1 in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from requests) (2.2.3)
Requirement already satisfied: certifi>=2017.4.17 in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from requests) (2024.12.14)
Requirement already satisfied: comm>=0.1.3 in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from ipywidgets) (0.2.2)
Requirement already satisfied: ipython>=6.1.0 in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from ipywidgets) (8.30.0)
Requirement already satisfied: traitlets>=4.3.1 in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from ipywidgets) (5.14.3)
Collecting widgetsnbextension~=4.0.12 (from ipywidgets)
  Downloading widgetsnbextension-4.0.13-py3-none-any.whl.metadata (1.6 kB)
Collecting jupyterlab-widgets~=3.0.12 (from ipywidgets)
  Downloading jupyterlab_widgets-3.0.13-py3-none-any.whl.metadata (4.1 kB)
Requirement already satisfied: pillow>=6.0 in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from ipycanvas) (11.0.0)
Requirement already satisfied: decorator in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from ipython>=6.1.0->ipywidgets) (5.1.1)
Requirement already satisfied: exceptiongroup in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from ipython>=6.1.0->ipywidgets) (1.2.2)
Requirement already satisfied: jedi>=0.16 in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from ipython>=6.1.0->ipywidgets) (0.19.2)
Requirement already satisfied: matplotlib-inline in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from ipython>=6.1.0->ipywidgets) (0.1.7)
Requirement already satisfied: pexpect>4.3 in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from ipython>=6.1.0->ipywidgets) (4.9.0)
Requirement already satisfied: prompt_toolkit<3.1.0,>=3.0.41 in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from ipython>=6.1.0->ipywidgets) (3.0.48)
Requirement already satisfied: pygments>=2.4.0 in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from ipython>=6.1.0->ipywidgets) (2.18.0)
Requirement already satisfied: stack_data in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from ipython>=6.1.0->ipywidgets) (0.6.3)
Requirement already satisfied: typing_extensions>=4.6 in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from ipython>=6.1.0->ipywidgets) (4.12.2)
Requirement already satisfied: parso<0.9.0,>=0.8.4 in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from jedi>=0.16->ipython>=6.1.0->ipywidgets) (0.8.4)
Requirement already satisfied: ptyprocess>=0.5 in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from pexpect>4.3->ipython>=6.1.0->ipywidgets) (0.7.0)
Requirement already satisfied: wcwidth in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from prompt_toolkit<3.1.0,>=3.0.41->ipython>=6.1.0->ipywidgets) (0.2.13)
Requirement already satisfied: executing>=1.2.0 in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from stack_data->ipython>=6.1.0->ipywidgets) (2.1.0)
Requirement already satisfied: asttokens>=2.1.0 in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from stack_data->ipython>=6.1.0->ipywidgets) (3.0.0)
Requirement already satisfied: pure-eval in /builds/eclipse/aidge/aidge/venv/lib/python3.10/site-packages (from stack_data->ipython>=6.1.0->ipywidgets) (0.2.3)
Downloading ipywidgets-8.1.5-py3-none-any.whl (139 kB)
Downloading ipycanvas-0.13.3-py2.py3-none-any.whl (125 kB)
Downloading jupyterlab_widgets-3.0.13-py3-none-any.whl (214 kB)
Downloading widgetsnbextension-4.0.13-py3-none-any.whl (2.3 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.3/2.3 MB 43.7 MB/s eta 0:00:00
Installing collected packages: widgetsnbextension, jupyterlab-widgets, ipywidgets, ipycanvas
Successfully installed ipycanvas-0.13.3 ipywidgets-8.1.5 jupyterlab-widgets-3.0.13 widgetsnbextension-4.0.13
Note: you may need to restart the kernel to use updated packages.

Download the model#

[2]:
import os
import requests
[3]:
# Download onnx file if it has not been done before
if not os.path.isfile("./lenet_mnist.onnx"):
    response = requests.get("https://huggingface.co/vtemplier/LeNet_MNIST/resolve/main/lenet_mnist.onnx?download=true")
    if response.status_code == 200:
        with open("lenet_mnist.onnx", 'wb') as f:
            f.write(response.content)
        print("ONNX model downloaded successfully.")
    else:
        print("Failed to download ONNX model. Status code:", response.status_code)
ONNX model downloaded successfully.

Load the model in Aidge and manipulate it#

[4]:
import aidge_core
import aidge_backend_cpu
import aidge_onnx
import aidge_export_cpp
[5]:
model = aidge_onnx.load_onnx("lenet_mnist.onnx")
Warning: an error occured when trying to load node 'classifier_classifier_0_Flatten' of type flatten.
Loading node using a generic operator.
Please report this issue at https://gitlab.eclipse.org/eclipse/aidge/aidge_onnx
by providing your ONNX model and the following error:
ONNX_NODE_CONVERTER_ returned: module 'aidge_core' has no attribute 'Flatten'

- classifier_classifier_0_Flatten (Flatten | GenericOperator)
        - axis : 1
[6]:
# Remove Flatten node, useless in the CPP export
aidge_core.remove_flatten(model)

# Configuration for the model + forward dimensions
model.compile("cpu", aidge_core.dtype.float32, dims=[[1, 1, 28, 28]])
[7]:
!rm -rf lenet_export_fp32
[8]:
# Generate scheduling of the model
scheduler = aidge_core.SequentialScheduler(model)
scheduler.generate_scheduling()

Export the model#

[9]:
export_folder = "lenet_export_fp32"

aidge_core.export_utils.scheduler_export(
        scheduler,
        export_folder,
        aidge_export_cpp.ExportLibCpp,
        memory_manager=aidge_core.mem_info.generate_optimized_memory_info,
        memory_manager_args={"stats_folder": f"{export_folder}/stats", "wrapping": False }
)

aidge_core.export_utils.generate_main_cpp(export_folder, model)
gen : lenet_export_fp32/feature_feature_0_Conv_input_0.h

Draw your own number#

[10]:
from ipywidgets import HBox, VBox, Button, Layout
from ipycanvas import RoughCanvas, hold_canvas

img_name = "my_number.png"

canvas = RoughCanvas(width=28, height=28, sync_image_data=True)

button_gen = Button(description="Generate PNG")
button_clear = Button(description="Clear")

drawing = False
position = None
shape = []

def on_erase_button_clicked(b):
    canvas.clear()

def on_generate_button_clicked(b):
    try:
        canvas.to_file(img_name)
        print(f"Image generated to {img_name} !")
    except:
        print("Draw a number before generating the image.")

button_clear.on_click(on_erase_button_clicked)
button_gen.on_click(on_generate_button_clicked)

def on_mouse_down(x, y):
    global drawing
    global position
    global shape

    drawing = True
    position = (x, y)
    shape = [position]

def on_mouse_move(x, y):
    global drawing
    global position
    global shape

    if not drawing:
        return

    with hold_canvas():
        canvas.stroke_line(position[0], position[1], x, y)
        position = (x, y)

    shape.append(position)

def on_mouse_up(x, y):
    global drawing
    global position
    global shape

    drawing = False

    with hold_canvas():
        canvas.stroke_line(position[0], position[1], x, y)

    shape = []

canvas.on_mouse_down(on_mouse_down)
canvas.on_mouse_move(on_mouse_move)
canvas.on_mouse_up(on_mouse_up)

canvas.stroke_style = "#000000"

VBox((canvas, HBox((button_gen, button_clear))),
     layout=Layout(height='auto', width="300px"))
[10]:

Generate inputs for testing the model from your drawing#

[11]:
try:
    number_np = canvas.get_image_data()
    # We got a numpy array with the shape of (28,28,4)
    # Transform it to (28,28)
    x = number_np[:, :, 3].astype("float32")
    # Convert from [0, 255] to [0, 1] and export it
    aidge_core.export_utils.generate_input_file(export_folder="lenet_export_fp32", array_name="feature_feature_0_Conv_input_0", tensor=aidge_core.Tensor(x / 255))

except:
    print("Please draw a number in the previous cell before running this one.")
Please draw a number in the previous cell before running this one.

Compile the export and test it#

[12]:
!cd lenet_export_fp32 && make
make[1]: Entering directory '/builds/eclipse/aidge/aidge/docs/source/Tutorial/lenet_export_fp32'
g++  -O2 -Wall -Wextra -MMD -fopenmp -I. -I./dnn -I./dnn/include -I./dnn/layers -I./dnn/parameters -c dnn/src/forward.cpp -o build/./dnn/src/forward.o
g++  -O2 -Wall -Wextra -MMD -fopenmp -I. -I./dnn -I./dnn/include -I./dnn/layers -I./dnn/parameters -c main.cpp -o build/./main.o
g++  build/./dnn/src/forward.o  build/./main.o  -fopenmp -o bin/run_export
make[1]: Leaving directory '/builds/eclipse/aidge/aidge/docs/source/Tutorial/lenet_export_fp32'
[13]:
!./lenet_export_fp32/bin/run_export
classifier_classifier_5_Gemm_output_0:
-188706568407384341886269705051701248.000000 -434024629987304837895786320516612096.000000 -180765787170107798688365973927362560.000000 -294843922687277819062675711381733376.000000 -316934101082058087443579253368553472.000000 -279169700314024562438350553651085312.000000 -383271782134129731612400407826923520.000000 -1535563107650292829829821871685632.000000 185361515771950844420901482696540160.000000 -416314758820491330513501441298006016.000000