cancel
Showing results for 
Search instead for 
Did you mean: 

STM32N6 + YOLOv8n ONNX Model. Issues with PNG Input and Code Generation

TerZer
Associate III

Hi everyone,

I'm working with an STM32N6 board and trying to deploy a custom YOLOv8n model in ONNX format using CubeMX and STM32 Edge AI. I’ve run into a few issues and was hoping someone could help clarify the process.

Here’s what I’ve done so far:

I successfully imported my yolov8n.onnx model into CubeMX + Edge AI.

When I try to test the model using a .PNG image, I get an error saying PNG format is not supported. When I generate the C code using the model, I see a line in network.c where -inf is not defined, causing compilation errors.

My Questions:

How can I test my model using a PNG image, either on the STM32N6 device or on a desktop simulation?

Do I need to convert the PNG to raw input data manually?

What’s the correct way to prepare a PNG image as an input buffer for use with STM32 inference code?

How do I resolve the -inf not defined issue in the generated network.c?

Any help, code snippets, or documentation pointers would be greatly appreciated!

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions

Hello @TerZer,

 

You can find the tutorial here:

How to build an AI application from scratch on the... - STMicroelectronics Community

 

Have a good day,

Julian


In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.

View solution in original post

13 REPLIES 13
Julian E.
ST Employee

Hello @TerZer,

 

You can find all the documentation regarding the st edge ai core, X Cube AI and the N6 here:

https://stedgeai-dc.st.com/assets/embedded-docs/index.html

 

Concerning the use of PNG file for validation, you need to convert them to npy or npz file (numpy file, which is a python library). 

Here is an example of code to do that in python:

import numpy as np
from PIL import Image
import os

# --- Settings ---
image_dir = "images"
class_map = {
    "cat": 0,
    "dog": 1
}

# --- Storage ---
images = []
labels = []

# --- Process each PNG ---
for filename in os.listdir(image_dir):
    if filename.endswith(".png"):
        filepath = os.path.join(image_dir, filename)
        
        # Open image
        image = Image.open(filepath).convert("RGB")
        image_array = np.array(image)

        # Get numeric label
        if "cat" in filename:
            label = class_map["cat"]
        elif "dog" in filename:
            label = class_map["dog"]
        else:
            continue  # skip unknowns

        images.append(image_array)
        labels.append(label)

# --- Convert to arrays ---
X = np.stack(images)   # shape: (N, H, W, C)
y = np.array(labels)   # shape: (N,)

print("X shape:", X.shape)
print("y shape:", y.shape)
print("Labels:", y)

# --- Save as .npz ---
np.savez("dataset.npz", X=X, y=y)

 

Then you can use this to perform validation on target or on desktop using X Cube AI in cubeMX or the n6_scripts.

https://stedgeai-dc.st.com/assets/embedded-docs/stneuralart_getting_started.html#ref_with_representative_data 

 

Concerning this: "How do I resolve the -inf not defined issue in the generated network.c?

Could you give more details please.

 

Have a good day,

Julian

 

 

 

 


In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.
TerZer
Associate III

Hi @Julian E. ,

I've updated the model and generated a new dataset.npz file (included in model.zip). When I add this to CubeMX, I’m able to validate the model on my desktop successfully. However, I encountered an issue when trying to validate on the target device STM32N6570-DK board.

Here’s a screenshot of my current CubeMX configuration:

TerZer_0-1752750518571.png

After pressing Generate Code and attempting to build the project, I get the following build errors:

../X-CUBE-AI/App/network.c:7652:41: error: 'inf' undeclared (first use in this function); did you mean 'jnf'?
 7652 |   const float pad_const_value_native = -inf;
../X-CUBE-AI/App/network.c:7805:41: error: 'inf' undeclared (first use in this function); did you mean 'jnf'?
 7805 |   const float pad_const_value_native = -inf;
../X-CUBE-AI/App/network.c:7958:41: error: 'inf' undeclared (first use in this function); did you mean 'jnf'?
 7958 |   const float pad_const_value_native = -inf;

 

Any ideas on what might be causing this? Let me know if you need any additional details.

Thanks in advance!

Hello @TerZer,

 

The inf error is pretty strange, we are look for the cause, but it can be solved if you quantize the model.

The hardware acceleration on NPU is possible only with int8 layers, meaning that you need to quantize the model to use the n6 to its full potential.

 

You can quantize the model manually or via the dev cloud (upload the model, select the n6, quantize and download the output model).

https://stedgeai-dc.st.com/session 

 

Then you can do the validation on target manually follow this:

https://stedgeai-dc.st.com/assets/embedded-docs/stneuralart_getting_started.html

(basically, use the st edge ai core CLI to do a generate, then the n6_loader.py then a validate command)

 

Or via X Cube AI that does the same automatically:

(Don't forget to change the baudrate)

JulianE_0-1752841876721.png

 

I attached your model quantized with the dev cloud.

 

Have a good day,

Julian

 


In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.
TerZer
Associate III

Hello @Julian E. ,

When I tried to quantize the model using cloud with dataset I get error: quantization failed and this is the terminal output:

Executing with:  {'model': '/tmp/quantization-service/63d3f705-6fcc-4b69-bcb4-4b8da0990171/yolov8n.onnx', 'data': '/tmp/quantization-service/63d3f705-6fcc-4b69-bcb4-4b8da0990171/dataset.npz', 'disable_per_channel': False}
Preprocess the model to infer shapes of each tensor
list index out of range

2025-07-21 05:59:33.170260: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2025-07-21 05:59:33.205418: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.

 
I have now changed my model to the .tflite file format and converted it from float to int8 types. Using ST Edge AI Developer Cloud I was able to run a benchmark on my board. One thing that I would like to know is how to launch it on my local board from CubeMX and CubeIDE? I want to generate code, run it on an image and using TouchGFX show results on the display.

Hello @TerZer,

 

We have different getting started application. You can find them by searching AI getting started STM32n6 on google.

In the case of a yolo model, this object detection is interesting for you:

GitHub - STMicroelectronics/STM32N6-GettingStarted-ObjectDetection: An AI software application package demonstrating simple implementation of object detection use case on STM32N6 product.​

 

It contains an application code example for both the Nucleo and DK N6 boards with the camera pipeline, pre/post processing, model inference and display on screen (in the case of the DK board, otherwise, you can display it on the pc). For the touchgfx part, you will have to adapt the code by yourself.

 

In the github repository I linked, you can find document (in /Doc) on how to use your model instead of the one available by default. By careful not to forget to change the postprocessing as explained here:

STM32N6-GettingStarted-ObjectDetection/Middlewares/ai-postprocessing-wrapper/README.md at main · STMicroelectronics/STM32N6-GettingStarted-ObjectDetection · GitHub

 

You also can find documentation about the st edge ai core here:

https://stedgeai-dc.st.com/assets/embedded-docs/index.html

 

Have a good day,

Julian

 

Have a good day,

Julian


In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.
Matthieu
ST Employee

Hello,

You have a specific "how to" to deploy a yolov8n thanks to STM32 model zoo: https://github.com/STMicroelectronics/stm32ai-modelzoo-services/blob/main/object_detection/deployment/doc/tuto/How_to_deploy_yolov8_yolov5_object_detection.md

Best Regards,

Matthieu

TerZer
Associate III

Hello @Julian E. and @Matthieu ,

Thank you for previously sharing documentation and resources on loading AI models using scripts and the STEdgeAI-Core command line tool.

I'm now looking to integrate an AI model (attached) into a CubeMX project that includes TouchGFX. My goal is to create a simple UI that displays a bitmap image and, when a button is clicked, runs model on that image and overlays bounding boxes.

However, I'm encountering difficulties using X-CUBE-AI within CubeMX:

  1. In "Software Packs -> Select Components", I can only select X-CUBE-AI Device Temp in the First Stage Boot Loader (FSBL) context, not in the Application context.
  2. If I include it in the FSBL, I get the following linker error:
    STM32N6570-DK_FSBL.elf section .rodata will not fit in region ROM
  3. Additionally, CubeMX displays the warning:
    "Clock and Peripherals parameters not set for best performance."
    If I choose to let CubeMX set the clock automatically, it disrupts the entire clock configuration. After fixing the clocks manually, the same warning still appears.

Could you advise whether it's possible to run an AI model using CubeMX with TouchGFX integration? If so, how can I correctly configure X-CUBE-AI and the project structure to support this workflow?

Thank you in advance for your help!

Hello @TerZer,

 

I am writing a knowledge-based article (one of the community sections) about how to setup a project with X Cube AI from scratch on STM32n6. First one article on the DK board, then one on the nucleo board.

 

I think you can then start from that and add touch gfx

 

I added you to my list of persons to notify when it will be ready (it is a draft as of now).

 

Have a good day,

Julian


In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.
TerZer
Associate III

Hello @Julian E. ,

Thanks for the update and for adding me to the notification list.

Do you have an estimated timeline for when the documentation will be ready or when the draft might be available for public?

Looking forward to it!