{"id":13705085,"url":"https://github.com/nxp-imx/pyarmnn-release","last_synced_at":"2026-02-07T23:10:32.059Z","repository":{"id":50306345,"uuid":"243318449","full_name":"nxp-imx/pyarmnn-release","owner":"nxp-imx","description":"PyArmNN is a python extension for Arm NN SDK. PyArmNN provides interface similar to Arm NN C++ Api.","archived":false,"fork":false,"pushed_at":"2020-12-22T19:23:02.000Z","size":33835,"stargazers_count":14,"open_issues_count":5,"forks_count":3,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-08-02T22:14:20.578Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nxp-imx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-26T17:05:09.000Z","updated_at":"2024-08-02T22:14:20.579Z","dependencies_parsed_at":"2022-09-24T11:21:38.708Z","dependency_job_id":null,"html_url":"https://github.com/nxp-imx/pyarmnn-release","commit_stats":null,"previous_names":["nxpmicro/pyarmnn-release"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nxp-imx%2Fpyarmnn-release","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nxp-imx%2Fpyarmnn-release/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nxp-imx%2Fpyarmnn-release/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nxp-imx%2Fpyarmnn-release/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nxp-imx","download_url":"https://codeload.github.com/nxp-imx/pyarmnn-release/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224448858,"owners_count":17313132,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T22:00:31.365Z","updated_at":"2026-02-07T23:10:32.029Z","avatar_url":"https://github.com/nxp-imx.png","language":"Python","funding_links":[],"categories":["微处理器 MPU 端"],"sub_categories":["PyArmNN"],"readme":"# About PyArmNN\n\nPyArmNN is a python extension for [Arm NN SDK](https://developer.arm.com/ip-products/processors/machine-learning/arm-nn) and provides an interface similar to Arm NN C++ API.\n\nThis repository provides resources to create legacy Python packages for Arm NN 19.08, 19.11, and 20.02. Since 20.05 PyArmNN is integrated into Arm NN itself. It is also targetted for the [NXP i.MX8 series](https://www.nxp.com/products/processors-and-microcontrollers/arm-processors/i-mx-applications-processors/i-mx-8-processors:IMX8-SERIES), which runs [Yocto Linux](https://www.yoctoproject.org/) on aarch64, other platforms are not tested.\n\nBefore you proceed with building or installing, you will need to checkout and build a corresponding [Arm NN version](https://source.codeaurora.org/external/imx/armnn-imx/). You may also find the libraries and header files in `/usr/lib` and `/usr/include` on your Yocto image.\n\nPyArmNN is built around public Arm NN headers. PyArmNN does not implement any computation kernels itself, all operations are\ndelegated to the Arm NN C++ library.\n\nThe [SWIG](http://www.swig.org/) project is used to generate the Arm NN python shadow classes and C wrapper.\n\n# Setup development environment\n\nTo build Python packages or develop PyArmNN, you will need to set up a working environment. You do not need to do this to install a Python package, run examples, or unit tests. To set up your environment, make sure that:\n\n1. You have Python 3.6+ installed system-side. The package is not compatible with older Python versions.\n2. You have python3.6-dev installed system-side. This contains header files needed to build PyArmNN extension module.\n3. In case you build Python from sources manually, make sure that the following libraries are installed and available in your system:\n``python3.6-dev build-essential checkinstall libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev``\n4. Install SWIG 4.x. Only 3.x version is typically available in Linux package managers, so you will have to build it and install it from sources. It can be downloaded from the [SWIG project website](http://www.swig.org/download.html) or [SWIG GitHub](https://github.com/swig/swig). To install it follow the guide on [SWIG GitHub](https://github.com/swig/swig/wiki/Getting-Started).\n\n## Setup virtual environment\n\nAfter setting up the development environment, it is recommended to create a Python virtual environment, so you do not pollute your working folder:\n```bash\npython3 -m venv env\nsource env/bin/activate\n```\n\nYou may run into missing python modules such as *wheel*. Make sure to install those using pip:\n```bash\npip install wheel\n```\n\n## Building Python packages\n\nPython supports source and binary distribution packages.\n\nThe source package contains `setup.py` script that is executed on the user's machine during package installation, thus it is platform-independent.\n\nWhen preparing the binary package (wheel), `setup.py` is executed on the build machine, the resulting package contains only the result\nof the build (generated files and resources, test results, etc.) and so it is platform dependent.\n\nThere are 2 ways to build PyArmNN Python packages. Either using individual scripts or using CMake, which automates the process.\n\n### CMake build\n\nThe recommended aproach is to build PyArmNN using CMake. First run the cmake command from the build directory and then run make:\n```bash\nmkdir build\ncd build\ncmake .. \\\n-DBUILD_PYTHON_SRC=\u003c0 or 1\u003e \\\n-DBUILD_PYTHON_WHL=\u003c0 or 1\u003e \\ \n-DARMNN_LIB=\u003cpath_to_armnn_libs\u003e \\\n-DARMNN_INCLUDE=\u003cpath_to_armnn_headers\u003e \\\n-DSWIG_EXECUTABLE=\u003cpath_to_swig_executable\u003e\nmake -j\u003cno_of_threads\u003e\n```\n`BUILD_PYTHON_SRC` can be set to 0 or 1 depending on if you want to build the python source package. \n\n`BUILD_PYTHON_WHL` can be set to 0 or 1 depending on if you want to build the python wheel (binary package).\n\n`ARMNN_LIB` should point to the directory where Arm NN libraries can be found.\n\n`ARMNN_INCLUDE` should point to the directory where Arm NN headers can be found.\n\n`SWIG_EXECUTABLE` is optional. Run `swig -version` to see what version of swig is called. If it's 4.x, you are good to go, otherwise, you should set this variable to point to the swig executable (not the directory).\n\nAfter the build finishes, you will find the python packages in `\u003cbuild_dir\u003e/python/pyarmnn/dist`.\n\n### Build using individual scripts\n\nPyArmNN can also be built using the provided python scripts only. The advantage of that is that you may use prebuilt Arm NN libraries, and generally have more control over the build.\n\nFirst, navigate to the `\u003crepo_dir\u003e/python/pyarmnn` directory.\n```bash\ncd \u003crepo_dir\u003e/python/pyarmnn\n```\n\n#### 1. Set environment:\n`ARMNN_INCLUDE` and `ARMNN_LIB` are mandatory and should point to Arm NN headers and libraries against which you will be generating the wrappers. `SWIG_EXECUTABLE` should only be set if you have multiple versions of SWIG installed or you used a custom location for your installation. You should also run the commands from the `\u003crepo_dir\u003e/python/pyarmnn` directory:\n```bash\nexport SWIG_EXECUTABLE=\u003cpath_to_swig_exec\u003e\nexport ARMNN_INCLUDE=\u003cpath_to_armnn_include\u003e\nexport ARMNN_LIB=\u003cpath_to_armnn_libraries\u003e\n``` \n\n#### 2. Clean and build SWIG wrappers:\n\n```bash\npython setup.py clean --all\npython swig_generate.py -v\npython setup.py build_ext --inplace\n```\nThis step will put all generated files under `src/pyarmnn/_generated` folder and can be used repeatedly to re-generate the wrappers.\n\n#### 4. Build the source package\n\n```bash\npython setup.py sdist\n```\nAs the result you will get `\u003cbuild_dir\u003e/python/pyarmnn/dist/pyarmnn-20.2.0.tar.gz` file. As you can see it is platform-independent.\n\n#### 5. Build the binary package\n\n```bash\npython setup.py bdist_wheel\n```\nAs the result you will get something like `dist/pyarmnn-20.2.0-cp37-cp37m-linux_aarch64.whl` file. As you can see it is platform-dependent.\n\n# PyArmNN installation\n\nPyArmNN can be distributed as a source package or a binary package (wheel).\n\nBinary package is platform dependent, the name of the package will indicate the platform it was built for, e.g.:\n\n* Linux x86 64bit machine: `pyarmnn-20.2.0-cp37-cp37m-linux_x86_64.whl`\n* Linux Aarch 64 bit machine: `pyarmnn-20.2.0-cp37-cp37m-linux_aarch64.whl`\n\nThe source package is platform-independent but installation involves the compilation of Arm NN Python extension. You will need to have g++ compatible with C++ 14 standard and a python development library installed on the build machine.\n\nBoth of them, source and binary package, require the Arm NN library to be present on the target/build machine.\n\nIt is strongly suggested to work within a python virtual environment. The further steps assume that the virtual environment was created and activated before running PyArmNN installation commands.\n\nPyArmNN also depends on the NumPy python library. It will be automatically downloaded and installed alongside PyArmNN. If your machine does not have access to Python pip repositories you might need to install NumPy in advance by following public instructions: https://scipy.org/install.html\n\n## Installing from the wheel\n\nMake sure that Arm NN binaries and Arm NN dependencies are installed and can be found in one of the system default library locations. You can check default locations by executing the following command:\n```bash\ngcc --print-search-dirs\n```\nInstall PyArmNN from binary by pointing to the wheel file:\n```bash\npip install dist/pyarmnn-20.2.0-cp37-cp37m-linux_aarch64.whl\n```\n\n## Installing from the source package\n\nAlternatively, you can install it from the source package. This is the more reliable way but requires a little more effort on the users part.\n\nWhile installing from sources, you have the freedom of choosing Arm NN libraries' location. Set environment variables `ARMNN_LIB` and `ARMNN_INCLUDE` to point to Arm NN libraries and headers.\nIf you want to use system default locations, just set `ARMNN_INCLUDE` to point to Arm NN headers.\n\n```bash\nexport ARMNN_INCLUDE=\u003cpath_to_armnn_include\u003e\nexport ARMNN_LIB=\u003cpath_to_armnn_libraries\u003e\n```\n\nInstall PyArmNN as follows:\n```bash\npip install dist/pyarmnn-20.2.0.tar.gz\n```\n\nIf PyArmNN installation script fails to find Arm NN libraries it will raise an error like this\n\n`RuntimeError: ArmNN library was not found in ('/usr/lib/gcc/aarch64-linux-gnu/8/', \u003c...\u003e ,'/lib/', '/usr/lib/'). Please install ArmNN to one of the standard locations or set correct ARMNN_INCLUDE and ARMNN_LIB env variables.`\n\nYou can now verify that PyArmNN library is installed and check PyArmNN version using:\n```bash\npip show pyarmnn\n```\nYou can also verify it by running the following and getting output similar to below:\n```bash\npython -c \"import pyarmnn as ann;print(ann.GetVersion())\"\n20200200\n```\n\n# PyArmNN API overview\n\n### Getting started\nThe easiest way to begin using PyArmNN is by using Parsers. We will demonstrate how to use them below:\n\nCreate a parser object and load your model file.\n```python\nimport pyarmnn as ann\nimport imageio\n\n# ONNX, Caffe and TF parsers also exist.\nparser = ann.ITfLiteParser()\nnetwork = parser.CreateNetworkFromBinaryFile('./model.tflite')\n```\n\nGet the input binding information by using the name of the input layer.\n```python\ninput_binding_info = parser.GetNetworkInputBindingInfo(0, 'model/input')\n\n# Create a runtime object that will perform inference.\noptions = ann.CreationOptions()\nruntime = ann.IRuntime(options)\n```\nChoose preferred backends for execution and optimize the network.\n```python\n# Backend choices earlier in the list have higher preference.\npreferredBackends = [ann.BackendId('CpuAcc'), ann.BackendId('CpuRef')]\nopt_network, messages = ann.Optimize(network, preferredBackends, runtime.GetDeviceSpec(), ann.OptimizerOptions())\n\n# Load the optimized network into the runtime.\nnet_id, _ = runtime.LoadNetwork(opt_network)\n```\nMake workload tensors using input and output binding information.\n```python\n# Load an image and create an inputTensor for inference.\nimg = imageio.imread('./image.png')\ninput_tensors = ann.make_input_tensors([input_binding_info], [img])\n\n# Get output binding information for an output layer by using the layer name.\noutput_binding_info = parser.GetNetworkOutputBindingInfo(0, 'model/output')\noutput_tensors = ann.make_output_tensors([outputs_binding_info])\n```\n\nPerform inference and get the results back into a NumPy array.\n```python\nruntime.EnqueueWorkload(0, input_tensors, output_tensors)\n\nresults = ann.workload_tensors_to_ndarray(output_tensors)\nprint(results)\n```\n\n### Examples\n\nTo further explore PyArmNN API there are several examples provided in the examples folder running classification on an image. To run them first install the dependencies:\n```bash\ncd python/pyarmnn\npip install -r examples/requirements.txt\n```\n\nAfterward simply execute the example scripts, e.g.:\n```bash\npython tflite_mobilenetv1_quantized.py\n```\nAll resources are downloaded during execution, so if you do not have access to the internet, you may need to download these manually. `example_utils.py` contains code shared between the examples.\n\n# Tox for automation\n\nTo make things easier *tox* is available for automating individual tasks or running multiple commands at once such as generating wrappers, running unit tests using multiple python versions, or generating documentation. To run it use:\n\n```bash\ntox \u003ctask_name\u003e\n```\n\nSee `tox.ini` for the list of tasks. You may also modify it for your own purposes. To dive deeper into tox read through https://tox.readthedocs.io/en/latest/.\n\nTox is intended for easier automation, so you may have to modify the script slightly to work with your environment. We do not guarantee, that it works out of the box.\n\n# Running unit-tests\n\nDownload resources required to run unit tests by executing the script from the `python/pyarmnn` directory. You will also need to install the required python modules.\n\n```bash\ncd python/pyarmnn\npip install -r test/requirements.txt\npython ./scripts/download_test_resources.py\n```\n\nThe script will download an archive from the Linaro server and extract it. A folder `test/testdata` will be created. Execute `pytest` from the project root dir:\n```bash\npython -m pytest test/ -v\n```\nor run tox which will do both:\n```bash\ntox\n```\n\n# Prebuilt wheels\n\nPrebuilt wheel packages for aarch64 used in Yocto Linux images can be found in the `whl` directory. There are many architectures, versions of Python, etc., so if the package is not there, you will need to build it yourself.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnxp-imx%2Fpyarmnn-release","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnxp-imx%2Fpyarmnn-release","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnxp-imx%2Fpyarmnn-release/lists"}