Create customized SNN code

Hi,

I’m interested in the neuromorphic hardware available, Akida. How can I create my own code to be launched in the platform? Thanks

Hi @rparada
Do you have any specific model in mind? If so, we can guide you to upload the model to our AI hub and run it on our hosted hardware.

Hi @shashi. Thank you for you fast response. My code is based on snntorch to classify various brain states on some functions. Is it possible to convert a snntorch code to Akida and then to be executed in your platform? Thanks!

Hi @rparada
Our expertise on such networks is limited. I will forward your request to Brainchip team and see if they can help. I will keep you posted. Thank you for your patience.

I really appreciate your effort to solve this request. Take your time. Thank you!

Hi @shashi, do you have any update regarding this request? thanks.

Hi @raul.parada.medina
We will check back with the brainchip team to see if they have any updates.

Hi @raul.parada.medina, unfortunately, we have not yet received an update from BrainChip regarding this request.

In the meantime, it may be worthwhile to reach out directly to the BrainChip team to express your interest and potentially get a quicker response. If we hear anything on our end, we’ll be sure to update this thread right away.

Hi @alex and @shashi for your reply, it looks there is no update from Brianchip in this sense. Please, could you tell me how to upload this model in the platform? Age estimation (regression) example — Akida Examples documentation. Thanks!

@stephan-degirum
Can you please help @raul.parada.medina ?

Hello @raul.parada.medina , conversion of a model from BrainChip’s model zoo into our format is straightforward:

Once you have an Akida model object, like Step 4 in the example:
model_akida = convert(model_quantized_keras)

You’ll need to map the model to your device and then convert it to a compatible binary:

from akida import devices

# Map model onto your Akida device
dev = devices()[0]
try:
    model_akida.map(dev, hw_only=True)
except RuntimeError:
    model_akida.map(dev, hw_only=False)

# Extract the C++-compatible program blob
blob = model_akida.sequences[0].program
with open("model_cxx.fbz", "wb") as f:
    f.write(blob)

print("C++-compatible model written to model_cxx.fbz")

Note: You want to be sure that the model is supported on your Akida device. There are many models on the BrainChip model zoo that are not compatible with their “version 1 IP” devices.

If your device is a v1 device, you’ll need to add a set_akida_version guard:

from cnn2snn import convert, set_akida_version, AkidaVersion
# Convert the model
with set_akida_version(AkidaVersion.v1):
    model_akida = convert(model_quantized_keras)
    model_akida.summary()

    from akida import devices
    # Map model onto your Akida device
    # ... (see above)

for more information on v1/v2 model compatibility please see their docs: Akida models zoo — Akida Examples documentation

Once you have a model binary blob created:

Create a model JSON file adjacent to the blob by following Model JSON Structure | PySDK | DeGirum Docs or by looking at existing BrainChip models on our AI Hub for reference: DeGirum AI Hub

ModelPath is your binary model file
RuntimeAgent is AKIDA
DeviceType is the middle output from akida devices in all caps.
For example for if akida devices shows: PCIe/NSoC_v2/0 you put: NSOC_V2

Your JSON + binary model blob are now compatible with PySDK. Try running the inference on your device locally by specifying the full path to the JSON as a zoo_url, see: PySDK Package | PySDK | DeGirum Docs
“For local AI hardware inferences you specify zoo_url parameter as either a path to a local model zoo directory, or a path to model’s .json configuration file.”

You can then zip them up and upload them to your model zoo in our AI Hub.

Let me know if this helped.

P.S. we currently have v1 hardware in our cloud farm, and this model is the face estimation model for NSoC_v2: DeGirum AI Hub

Hello @stephan-degirum, sorry for the late reply, since now, I couldn’t make this example work: Global Akida workflow — Akida Examples documentation

Please, can you tell me from the generated .fbz, how to make it work within this platform? If you can provide me a step a step solution could be great. I add both .fbz and .onnx. in this linkhttps://limewire.com/d/sXxGY#8AYrZHfe0B Thanks.

Hello @raul.parada.medina,

So this particular example seems to not be compatible with Version 1 Akida hardware, which is what we have access to currently. But, it should work with newer versions.

Could you please tell me first which hardware you are looking to use? Specifically, by giving me the output of akida devices if you have the Brainchip hardware on a local machine.
If you want to use the hardware we have on DeGirum AI Hub, it will be the NSOC_V2.

Either way, here’s what you can do to get the .fbz model file properly mapped:
Download the example as a jupyter notebook (at the end of the page) and try pasting this code in a new code block at the end of the notebook:

import akida
from cnn2snn import convert, set_akida_version, AkidaVersion

device = akida.devices()[0]
print(f"Found physical device: {device.desc}")

hw_version = device.version
if hw_version.product_id == 0xA2: # 0xA2 corresponds to the v2 IP product ID
    device_ip_version = akida.IpVersion.v2.name
else:
    device_ip_version = akida.IpVersion.v1.name
print(f"Device IP Version: {device_ip_version}")

if (device_ip_version == akida.IpVersion.v1.name):
    with set_akida_version(AkidaVersion.v1):
        model_akida = convert(model_quantized)
else:
    with set_akida_version(AkidaVersion.v2):
        model_akida = convert(model_quantized)

print("Model successfully converted...")
model_ip_version = model_akida.ip_version.name
print(f"Model IP Version: {model_ip_version}")

if model_ip_version != device_ip_version:
    print("\nERROR: Mismatch detected! The 'convert' step did not produce a v2 model.")
else:
    print("\nSUCCESS: Model and device IP versions match. Proceeding with mapping...")
    
    try:
        model_akida.map(device, hw_only=True)
        print("Mapping completed successfully.")

        print("\nModel Summary after mapping:")
        model_akida.summary()

        # --- 5. Extract and save the program ---
        if model_akida.sequences and model_akida.sequences[0].program:
            akida_program = model_akida.sequences[0].program
            print(f"\nExtracted program of size: {len(akida_program)} bytes")

            output_filename = "model_program.fbz"
            with open(output_filename, "wb") as f:
                f.write(akida_program)
            print(f"Successfully saved the C++ compatible program to '{output_filename}'")
        else:
            print("\nCould not find a hardware program after mapping.")

    except RuntimeError as e:
        print(f"\nAn error occurred during mapping: {e}")

Running this code block will attempt to create a .fbz model file for the final model depending on the hardware on your system. For me, it gave an error saying that the model has incompatible layers for version 1 Brainchip hardware, but it should work on version 2.

If it works, then you can take the .fbz file, and follow the example JSON structure of a model such as:
https://hub.degirum.com/degirum/brainchip/akidanet--160alpha50_quant_akida_NSoC_v2_1

You would create a similar JSON file right next to the .fbz file, but with updated fields for the model input tensor dimensions and the model path. Then PySDK will see your model.

Let me know if you get stuck or have any other questions

Hi @stephan-degirum, I would like to run this example on the current device model in degyrum, you’ve told me it is v1. From the example (notebook from brainchip) I’ve provided you what should I change to get the v1 instead of v2? Also, it is still not clear how to create the json once I got the .fbz file, if you can explain me in more detail it would be nice.
Thank you very much!

Hi @stephan-degirum could be this the right v1 version? Download mnist_cnn_v1.fbz | LimeWire

Hello @raul.parada.medina

Here’s some more information regarding model preparation for PySDK on BrainChip hardware to get us on the same page:

To create a model compatible with PySDK you will need to

  1. Ensure it was created / converted for Akida v1 hardware.
  2. Extract the C++ -compatible binary file for the model. See the script from my previous reply for model conversion / preparation steps.
  3. Create the accompanying JSON files.

Apart from trying to run the notebook for the MNIST workflow, I also tried your latest .fbz model file. It looks like that this model file was compiled with Akida version 2.14.0, whereas PySDK currently supports Akida version 2.11.0:

Cannot deserialize the model: A model created with Akida 2.14.0 is not supported by Akida 2.11.0.

If downgrading Akida version is an option for you, please do so using
pip install akida==2.11.0
and then recreate the model file.

After you have a working C++ -compatible model binary file, to make it work with PySDK you need to create two files next to the binary:

  1. Model Parameters JSON file (<model_name>.json)

You can reuse the following example model JSON file with some minor changes to get it to work:

{
  "ConfigVersion": 10,
  "MODEL_PARAMETERS": [
    {
      "ModelPath": "<model_name>.bin"
    }
  ],
  "PRE_PROCESS": [
    {
      "InputQuantEn": true,
      "InputN": 1,
      "InputH": 224,
      "InputW": 224,
      "InputC": 3
    }
  ],
  "POST_PROCESS": [
    {
      "OutputPostprocessType": "Classification",
      "LabelsPath": "labels.json",
      "OutputTopK": 5,
      "OutputNumClasses": 1000,
      "OutputSoftmaxEn": true
    }
  ],
  "DEVICE": [
    {
      "RuntimeAgent": "AKIDA",
      "DeviceType": "NSOC_V2"
    }
  ],
  "Checksum": "79a7c1fc18f34ae5cd9d1a02537aaee5a8fb355ab59f3bc14163deb5c7e71841"
}

You need to adapt the following:

  • Replace InputH, InputW, InputC, and OutputNumClasses with the correct values matching your model.

2. Labels JSON (labels.json)

Create this file alongside your model parameters JSON. It maps output indices to class labels:


{
"0": "class_0_label",
"1": "class_1_label",
"2": "class_2_label",
"3": "class_3_label..etc"
}

Your directory should now look like this:

model_name.json
model_name.bin
labels.json

Run Your Model

You can now test inference using a simple PySDK script:

import degirum as dg

model = dg.load_model("model", dg.LOCAL, zoo_url="/path/to/your/model_name.json")

result = model.predict("some-image.jpg")

print(result)

result.image_overlay.save("result_overlay.jpg")

Now, once again this is assuming that you have BrainChip hardware on your system locally. I still don’t quite understand if you want to run this inference locally or in our cloud, but hopefully this gives you some more relevant information on the workflow for Akida model preparation.

Please let me know some more information on exactly what your application usecase is so that I can give you some more tailored information.

If you get stuck at any step let me know.