diff --git a/aidge_export_arm_cortexm/templates/memory/mem_info.jinja b/aidge_export_arm_cortexm/templates/memory/mem_info.jinja deleted file mode 100644 index f835d9649a599c9339256c59d5941fcfc8f1b545..0000000000000000000000000000000000000000 --- a/aidge_export_arm_cortexm/templates/memory/mem_info.jinja +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef MEM_INFO_H -#define MEM_INFO_H - -#define MEMORY_SIZE {{ mem_size }} -#define MEMORY_ALIGNMENT {{ mem_alignment }} - -{% for i in range(mem_info|length) -%} -{%- set layer_name = mem_info[i][0] %} -/* {{layer_name}} memory */ -{% for j in range(1, mem_info[i]|length) %} -#define {{ layer_name|upper }}_MEM_{{ mem_info_legends[j]|upper }} {{ mem_info[i][j] }} -{%- endfor %} -{% endfor %} - - -#endif /* MEM_INFO_H */ diff --git a/aidge_export_arm_cortexm/templates/network/dnn_header.jinja b/aidge_export_arm_cortexm/templates/network/dnn_header.jinja deleted file mode 100644 index 7b238c167b9849cb41bad7b61ef0c596f8d29abd..0000000000000000000000000000000000000000 --- a/aidge_export_arm_cortexm/templates/network/dnn_header.jinja +++ /dev/null @@ -1,22 +0,0 @@ -{#- For name header -#} -#ifndef DNN_H -#define DNN_H - -#ifdef __cplusplus -extern "C" { -#endif - -{#- For libraries #} -{% for lib in libraries %} -#include <{{ lib }}> -{%- endfor %} - -{% for func in functions %} -{{ func }} -{% endfor %} - -#ifdef __cplusplus -} -#endif - -#endif /* DNN_H */ \ No newline at end of file diff --git a/aidge_export_arm_cortexm/templates/network/network_forward.jinja b/aidge_export_arm_cortexm/templates/network/network_forward.jinja deleted file mode 100644 index bde5553020d1a36f225a1402172715a7446c4496..0000000000000000000000000000000000000000 --- a/aidge_export_arm_cortexm/templates/network/network_forward.jinja +++ /dev/null @@ -1,28 +0,0 @@ -{#- For libraries -#} - -#include <stdint.h> - -#include "dnn.h" -#include "network_functions.h" - -// Layer & memory configurations -{%- for header in headers %} -#include "{{ header }}" -{%- endfor %} - -{# mem has the datatype of the firt input #} -{#- Change here to improve it -#} -{% if inputs[0][0] %} -static {{inputs[0][0]}} mem[MEMORY_SIZE]; -{% else %} -static float mem[MEMORY_SIZE]; -{% endif %} - -{# Forward function #} -{#- Support multiple inputs with different datatypes and multiple outputs with different datatypes -#} -void model_forward({% for inp in inputs %}const {{inp[0]}}* {{inp[1]}}, {% endfor %}{% for out in outputs %}{{out[0]}}* {{out[1]}}{{ ", " if not loop.last else "" }}{% endfor %}) -{ - {%- for action in actions %} - {{ action }} - {%- endfor %} -} diff --git a/aidge_export_arm_cortexm/templates/network/network_prototypes.jinja b/aidge_export_arm_cortexm/templates/network/network_prototypes.jinja deleted file mode 100644 index 4d2f3452f1c2434c1d767ba654a0ca26ac2bae2a..0000000000000000000000000000000000000000 --- a/aidge_export_arm_cortexm/templates/network/network_prototypes.jinja +++ /dev/null @@ -1,19 +0,0 @@ -{#- For name header -#} -#ifndef NETWORK_FUNCTIONS_HPP -#define NETWORK_FUNCTIONS_HPP - -{#- For libraries #} -{% for lib in libraries %} -#include <{{ lib }}> -{%- endfor %} - -{% for file in files %} -#include "{{ file }}" -{%- endfor %} - -{% for func in functions %} -{{ func }} -{% endfor %} - - -#endif /* NETWORK_FUNCTIONS_HPP */ \ No newline at end of file diff --git a/aidge_export_arm_cortexm/utils/__init__.py b/aidge_export_arm_cortexm/utils/__init__.py deleted file mode 100644 index bd48bd6a7b39cccf74f0f723124b6af2e7db478d..0000000000000000000000000000000000000000 --- a/aidge_export_arm_cortexm/utils/__init__.py +++ /dev/null @@ -1,33 +0,0 @@ -from pathlib import Path - -# Constants -FILE = Path(__file__).resolve() -ROOT = FILE.parents[1] - - -def get_all_available_boards(): - boards = {} - - directory_path = Path(str(ROOT / "boards")) - - for subfolder in directory_path.rglob('*'): - if subfolder.is_dir() and \ - subfolder.name != "__pycache__" and \ - (subfolder.parent / '__init__.py').exists() and \ - not (subfolder / '__init__.py').exists(): - - # Get relative path to boards directory - relpath = str(subfolder.relative_to(directory_path)) - - # Get board name - board_name = relpath.replace('/', '').replace('\\', '') - - boards[board_name.lower()] = str(subfolder) - - return boards - -AVAILABLE_BOARDS = get_all_available_boards() - - -def has_board(board_name: str) -> bool: - return board_name.lower() in AVAILABLE_BOARDS.keys() diff --git a/aidge_export_arm_cortexm/utils/converter.py b/aidge_export_arm_cortexm/utils/converter.py deleted file mode 100644 index 3bc2f392b9f48b96972f3ff744bbba3bf945ca13..0000000000000000000000000000000000000000 --- a/aidge_export_arm_cortexm/utils/converter.py +++ /dev/null @@ -1,55 +0,0 @@ -import numpy as np -import aidge_core - -def numpy_dtype2ctype(dtype): - if dtype == np.int8: - return "int8_t" - elif dtype == np.uint8: - return "uint8_t" - elif dtype == np.int16: - return "int16_t" - elif dtype == np.int32: - return "int32_t" - elif dtype == np.int64: - return "int64_t" - elif dtype == np.float32: - return "float" - elif dtype == np.float64: - return "double" - # Add more dtype mappings as needed - else: - raise ValueError(f"Unsupported {dtype} dtype") - - -def aidge_datatype2ctype(datatype): - if datatype == aidge_core.dtype.int8: - return "int8_t" - elif datatype == aidge_core.dtype.uint8: - return "uint8_t" - elif datatype == aidge_core.dtype.int32: - return "int32_t" - elif datatype == aidge_core.dtype.int64: - return "int64_t" - elif datatype == aidge_core.dtype.float32: - return "float" - elif datatype == aidge_core.dtype.float64: - return "double" - # Add more dtype mappings as needed - else: - raise ValueError(f"Unsupported {datatype} aidge dtype") - - -def aidge_datatype2dataformat(datatype): - if datatype == aidge_core.dtype.int8: - return "int8" - elif datatype == aidge_core.dtype.int32: - return "int32" - elif datatype == aidge_core.dtype.int64: - return "int64" - elif datatype == aidge_core.dtype.float32: - return "float32" - elif datatype == aidge_core.dtype.float64: - return "float64" - # Add more dtype mappings as needed - else: - raise ValueError(f"Unsupported {datatype} aidge dtype") diff --git a/examples/README.md b/examples/README.md deleted file mode 100644 index 643f196717043d908c7e0342f61883abcb3806f6..0000000000000000000000000000000000000000 --- a/examples/README.md +++ /dev/null @@ -1,6 +0,0 @@ -# Examples on how to use this module Aidge ARM CortexM Export - -This folder contains some examples on how to use the `Aidge ARM CortexM Export` module in your projects. -- [LeNet export for MNIST dataset](./export_LeNet/) - -Feel free to propose your own contributions with this module ! \ No newline at end of file diff --git a/examples/export_LeNet/export_lenet_fp32.ipynb b/examples/export_LeNet/export_lenet_fp32.ipynb deleted file mode 100644 index 08b64318ec66e55754aa1e2302d0576854557e05..0000000000000000000000000000000000000000 --- a/examples/export_LeNet/export_lenet_fp32.ipynb +++ /dev/null @@ -1,281 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Export a MNIST model to a CPP standalone project" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "%pip install requests numpy ipywidgets ipycanvas" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Download the model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "import requests" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Download onnx file if it has not been done before\n", - "if not os.path.isfile(\"./lenet_mnist.onnx\"):\n", - " response = requests.get(\"https://huggingface.co/vtemplier/LeNet_MNIST/resolve/main/lenet_mnist.onnx?download=true\")\n", - " if response.status_code == 200:\n", - " with open(\"lenet_mnist.onnx\", 'wb') as f:\n", - " f.write(response.content)\n", - " print(\"ONNX model downloaded successfully.\")\n", - " else:\n", - " print(\"Failed to download ONNX model. Status code:\", response.status_code)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Load the model in Aidge and manipulate it" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import aidge_core\n", - "import aidge_backend_cpu\n", - "import aidge_onnx\n", - "import aidge_export_cpp\n", - "import aidge_export_arm_cortexm" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "model = aidge_onnx.load_onnx(\"lenet_mnist.onnx\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Remove Flatten node, useless in the CPP export\n", - "aidge_core.remove_flatten(model)\n", - "\n", - "# Freeze the model by setting constant to parameters producers\n", - "for node in model.get_nodes():\n", - " if node.type() == \"Producer\":\n", - " node.get_operator().set_attr(\"Constant\", True)\n", - "\n", - "# Create Producer Node for the Graph\n", - "input_node = aidge_core.Producer([1, 1, 28, 28], \"input\")\n", - "input_node.add_child(model)\n", - "model.add(input_node)\n", - "\n", - "# Configuration for the model + forward dimensions\n", - "model.compile(\"cpu\", aidge_core.DataType.Float32)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Generate scheduling of the model\n", - "scheduler = aidge_core.SequentialScheduler(model)\n", - "scheduler.generate_scheduling()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Export the model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "aidge_export_arm_cortexm.export(\"lenet_export_fp32\", model, scheduler, board=\"stm32h7\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Draw your own number" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from ipywidgets import HBox, VBox, Button, Layout\n", - "from ipycanvas import RoughCanvas, hold_canvas\n", - "\n", - "img_name = \"my_number.png\"\n", - "\n", - "canvas = RoughCanvas(width=28, height=28, sync_image_data=True)\n", - "\n", - "button_gen = Button(description=\"Generate PNG\")\n", - "button_clear = Button(description=\"Clear\")\n", - "\n", - "drawing = False\n", - "position = None\n", - "shape = []\n", - "\n", - "def on_erase_button_clicked(b):\n", - " canvas.clear()\n", - "\n", - "def on_generate_button_clicked(b):\n", - " try:\n", - " canvas.to_file(img_name)\n", - " print(f\"Image generated to {img_name} !\")\n", - " except:\n", - " print(\"Draw a number before generating the image.\")\n", - "\n", - "button_clear.on_click(on_erase_button_clicked)\n", - "button_gen.on_click(on_generate_button_clicked)\n", - "\n", - "def on_mouse_down(x, y):\n", - " global drawing\n", - " global position\n", - " global shape\n", - "\n", - " drawing = True\n", - " position = (x, y)\n", - " shape = [position]\n", - "\n", - "def on_mouse_move(x, y):\n", - " global drawing\n", - " global position\n", - " global shape\n", - "\n", - " if not drawing:\n", - " return\n", - "\n", - " with hold_canvas():\n", - " canvas.stroke_line(position[0], position[1], x, y)\n", - " position = (x, y)\n", - "\n", - " shape.append(position)\n", - "\n", - "def on_mouse_up(x, y):\n", - " global drawing\n", - " global position\n", - " global shape\n", - "\n", - " drawing = False\n", - "\n", - " with hold_canvas():\n", - " canvas.stroke_line(position[0], position[1], x, y)\n", - "\n", - " shape = []\n", - "\n", - "canvas.on_mouse_down(on_mouse_down)\n", - "canvas.on_mouse_move(on_mouse_move)\n", - "canvas.on_mouse_up(on_mouse_up)\n", - "\n", - "canvas.stroke_style = \"#000000\"\n", - "\n", - "VBox((canvas, HBox((button_gen, button_clear))),\n", - " layout=Layout(height='auto', width=\"300px\"))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Generate inputs for testing the model from your drawing" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "try:\n", - " number_np = canvas.get_image_data()\n", - " # We got a numpy array with the shape of (28,28,4)\n", - " # Transform it to (28,28)\n", - " x = number_np[:, :, 3].astype(\"float32\")\n", - " # Convert from [0, 255] to [0, 1] and export it\n", - " aidge_export_cpp.generate_input_file(export_folder=\"lenet_export_fp32\",\n", - " array_name=\"inputs\",\n", - " array=x / 255)\n", - "except:\n", - " print(\"Please draw a number in the previous cell before running this one.\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Compile the export and test it" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "!cd lenet_export_fp32 && make build_image_docker && make build_docker" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "env", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.16" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -}