Skip to main content

RKNN-Toolkit2 Environment Setup

Note

The model conversion environment described in this chapter is deployed on an x86_64 Linux platform. It is recommended to complete model training and conversion in this environment. System version: WSL2 Ubuntu 22.04 / Virtual Machine Ubuntu 22.04 Tool version: RKNN-Toolkit2 2.3.2

1. Introduction to RKNPU

An NPU (Neural Processing Unit) is a specialized processor designed to accelerate neural network computations. To meet the growing demands of artificial intelligence, Rockchip has progressively integrated NPUs into its processors. These built-in NPUs are referred to as RKNPU.

The RV1126B, used in the LuckFox Aura series, integrates Rockchip’s third-generation RKNPU2 architecture, delivering up to 3.0 TOPS INT8 computing performance. It supports mixed-precision quantization including INT4, INT8, INT16, FP16, BF16, and TF32. Model conversion and deployment require the RKNPU2 SDK along with the RKNN-Toolkit2 toolchain.

2. Introduction to RKNN-Toolkit2

RKNN-Toolkit2 provides C and Python APIs on the PC platform to simplify model deployment and execution. With this toolkit, users can easily perform tasks such as model conversion, quantization, inference, performance and memory evaluation, quantization accuracy analysis, and model encryption.

The RKNN software stack enables developers to quickly deploy AI models onto Rockchip platforms. The overall framework is shown above.

To use the RKNPU, users must first run RKNN-Toolkit2 on a computer to convert a trained model into the RKNN format. The converted model can then be deployed on the development board using either the RKNN C API or Python API.

This section introduces how to quickly get started with RKNPU on the RV1126B series boards.

3 RKNN-Toolkit2 Installation (Ubuntu 22.04 X86_64 Platform)

3.1 Local Installation

  1. Environment Requirements.

    Operating System VersionUbuntu18.04(x64)Ubuntu20.04(x64)Ubuntu22.04(x64)
    Python Version3.6/3.73.8/3.93.10/3.11
  2. Download RKNN-Toolkit2.

    git clone https://github.com/airockchip/rknn-toolkit2.git
  3. Install Python Environment.

    sudo apt-get update
    sudo apt-get install python3 python3-dev python3-pip
    sudo apt-get install libxslt1-dev zlib1g zlib1g-dev libglib2.0-0 libsm6 libgl1-mesa-glx libprotobuf-dev gcc
  4. Install RKNN-Toolkit2 Dependencies.

    pip3 install -r rknn-toolkit2/packages/requirements_cpxx-2.2.0.txt

    # such as:
    pip3 install -r rknn-toolkit2/packages/requirements_cp310-2.2.0.txt

    Select the corresponding dependencies based on your Python version:

    Python VersionRKNN-Toolkit2 Dependencies
    3.6requirements_cp36-2.2.0.txt
    3.7requirements_cp37-2.2.0.txt
    3.8requirements_cp38-2.2.0.txt
    3.9requirements_cp39-2.2.0.txt
    3.10requirements_cp310-2.2.0.txt
    3.11requirements_cp311-2.2.0.txt
    3.12requirements_cp312-2.2.0.txt
  5. Install RKNN-Toolkit2.

    pip3 install rknn-toolkit2/packages/rknn_toolkit2-x.x.x+xxxxxxxx-cpxx-cpxx-linux_x86_64.whl

    # such as:
    pip3 install rknn-toolkit2/packages/rknn_toolkit2-2.2.0+81f21f4d-cp310-cp310-linux_x86_64.whl

    The package name format is: rknn_toolkit2-{version}+{commit}-cp{python_version}-cp{python_version}-linux_x86_64.whl. Choose the appropriate package based on the Python version:

    Python VersionRKNN-Toolkit2 Package
    3.6rknn_toolkit2-2.2.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
    3.7rknn_toolkit2-2.2.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
    3.8rknn_toolkit2-2.2.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
    3.9rknn_toolkit2-2.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
    3.10rknn_toolkit2-2.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
    3.11rknn_toolkit2-2.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
    3.12rknn_toolkit2-2.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

    If no errors occur after running the following command, the installation is successful:

    python3
    from rknn.api import RKNN

3.2 Conda Installation

It is recommended to use Conda to create a Python virtual environment, allowing flexible switching between different applications and avoiding version mismatches. For example, different Python virtual environments are required for AI model training and conversion.

3.2.1 Install Miniconda

  1. Check if Miniconda or another conda tool is installed by running:

    conda --version
  2. Download the installer:

    wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-4.6.14-Linux-x86_64.sh
  3. Install Miniconda:

    chmod 777 Miniconda3-4.6.14-Linux-x86_64.sh
    bash Miniconda3-4.6.14-Linux-x86_64.sh
    • Note: The miniconda installation package must be given execute permission using chmod 777.

    • Follow the installation steps, read and accept the license terms, and proceed with the installation. The miniconda folder will be created in the home directory.

  4. Enter the Conda base environment:

    source ~/miniconda3/bin/activate # Miniconda3 installation directory (customize based on your setup)
    # After activation, the command prompt should change to:
    # (base) xxx@xxx:~$
  5. To automatically activate the Miniconda environment each time you open a terminal, add the activation command to your shell configuration file:

    vim nano ~/.bashrc

    # Add the following line at the end:
    source ~/miniconda3/bin/activate

    # exit conda environment
    conda deactivate

3.2.2 Creating RKNN-Toolkit2 Conda Environment

  1. Create the RKNN-Toolkit2 development Conda environment, specifying Python version 3.8 (recommended version):

    conda create -n RKNN-Toolkit2 python=3.9
    • Type y to confirm installation of the default packages.
  2. Enter the RKNN-Toolkit2 Conda Environment.

    conda activate RKNN-Toolkit2
  3. Verify the Correct Python Version.

    python --version
    • Note: In some development environments, the Python version may not switch properly after creating the Conda environment. Restarting the terminal may resolve this issue.
  4. Clone the RKNN-Toolkit2 repository:

    git clone https://github.com/airockchip/rknn-toolkit2.git
  5. Enter the directory:

    cd rknn-toolkit2
  6. Install RKNN-Toolkit2 related dependency libraries. cp38 is the corresponding Conda environment python version. The version used in the experiment is 3.8, so use the dependency with the suffix cp38

    pip install tf-estimator-nightly==2.8.0.dev2021122109
    pip install -r rknn-toolkit2/packages/requirements_cp39-2.2.0.txt
  7. Install RKNN-Toolkit2:

    pip install rknn-toolkit2/packages/rknn_toolkit2-2.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
    • Select the installation package from the packages folder based on your Python version. For Python 3.8, use the package with the cp38 suffix. The 81f21f4d in the filename is a commit ID; choose the appropriate version for your setup.
  8. Verify Installation,If no errors occur, the installation is successful.

    python
    >>> from rknn.api import RKNN