Skip to content

RKNN-Toolkit2 Model Conversion API Reference

1.API Details

1.1 RKNN Initialization and Release

This section describes 1.1 RKNN Initialization and Release. Related API or structure: firstRKNN(), release().

This section describes 1.1 RKNN Initialization and Release.

Example:

# Output detailed logs
from rknn.api import RKNN
rknn = RKNN(verbose=True)
# ...
rknn.release()

1.2 Model Configuration

This section describes 1.2 Model Configuration.

ItemContent
APIconfig
DescriptionmodelconversionParameters set。
ParametersDescription of Parameters.
Return ValueNone.

config Parameter Details

ParametersDescription
mean_valuesDescription of mean_values. Related values: None.
std_valuesDescription of std_values. Related values: None.
quant_img_RGB2BGRDescription of quant_img_RGB2BGR. Related values: True True False True.
quantized_dtypeDescription of quantized_dtype. Related values: w8a8 w4a16 w8a16 w4a8 w16a16 w16a16 w8a8 w8a8 w4a16 w8a16 w4a8 w16a16 w16a16.
quantized_algorithmDescription of quantized_algorithm. Related values: w4a16 w8a16 w4a16 w8a16.
quantized_methodDescription of quantized_method. Related values: w4a16.
float_dtypeDescription of float_dtype. Related values: float16 float16.
optimization_levelDescription of optimization_level.
target_platformDescription of target_platform. Related values: None.
custom_stringDescription of custom_string. Related values: None.
remove_weightDescription of remove_weight. Related values: False.
compress_weightDescription of compress_weight. Related values: False.
single_core_modeDescription of single_core_mode. Related values: False.
model_pruningDescription of model_pruning. Related values: False.
op_targetDescription of op_target. Related values: None.
dynamic_inputDescription of dynamic_input. Related values: None.
quantize_weightDescription of quantize_weight. Related values: False False.
remove_reshapeDescription of remove_reshape. Related values: False.
sparse_inferDescription of sparse_infer. Related values: False.
enable_flash_attentionDescription of enable_flash_attention. Related values: False.
auto_hybrid_cos_threshDescription of auto_hybrid_cos_thresh.
auto_hybrid_euc_threshDescription of auto_hybrid_euc_thresh. Related values: None.

Example:

# model config
rknn.config(mean_values=[[103.94, 116.78, 123.68]],
std_values=[[58.82, 58.82, 58.82]],
quant_img_RGB2BGR=True,
target_platform='rk3566')

1.3 Model Loading

This section describes 1.3 Model Loading.

1.3.1 Caffe Model Loading Interface

ItemContent
APIload_caffe
DescriptionDescription of Description.
ParametersDescription of Parameters. Related values: None.
Return Value0:importsuccess。
-1:importfailure。

Example:

# Load the mobilenet_v2 model from the current path
ret = rknn.load_caffe(model='./mobilenet_v2.prototxt',
blobs='./mobilenet_v2.caffemodel')

1.3.2 TensorFlow Model Loading Interface

ItemContent
APIload_tensorflow
DescriptionDescription of Description.
ParametersDescription of Parameters. Related values: NCHW False NHWC.
Return Value0:importsuccess。
-1:importfailure。

Example:

# Load the ssd_mobilenet_v1_coco_2017_11_17 model from the current directory
ret = rknn.load_tensorflow(tf_pb='./ssd_mobilenet_v1_coco_2017_11_17.pb',
inputs=['Preprocessor/sub'],
outputs=['concat', 'concat_1'],
input_size_list=[[300, 300, 3]])

1.3.3 TensorFlow Lite Model Loading Interface

ItemContent
APIload_tflite
DescriptionDescription of Description.
ParametersDescription of Parameters. Related values: NCHW False NHWC.
Return Value0:importsuccess。
-1:importfailure。

Example:

# Load the mobilenet_v1 model from the current directory
ret = rknn.load_tflite(model='./mobilenet_v1.tflite')

1.3.4 ONNX Model Loading

ItemContent
APIload_onnx
DescriptionONNXmodel load。
ParametersDescription of Parameters. Related values: None None None None None array( None.
Return Value0:importsuccess。
-1:importfailure。

Example:

# Load the arcface model from the current directory
ret = rknn.load_onnx(model='./arcface.onnx')

1.3.5 DarkNet Model Loading Interface

ItemContent
APIload_darknet
DescriptionDescription of Description.
ParametersDescription of Parameters.
Return Value0:importsuccess。
-1:importfailure。

Example:

# Load the yolov3-tiny model from the current directory
ret = rknn.load_darknet(model='./yolov3-tiny.cfg',
weight='./yolov3.weights')

1.3.6 PyTorch Model Loading Interface

ItemContent
APIload_pytorch
DescriptionDescription of Description.
ParametersDescription of Parameters.
Return Value0:importsuccess。
-1:importfailure。

Example:

# Load the resnet18 model from the current directory
ret = rknn.load_pytorch(model='./resnet18.pt',
input_size_list=[[1,3,224,224]])

1.4 Build RKNN Model

ItemContent
APIbuild
DescriptionRKNNmodel build。
ParametersDescription of Parameters. Related values: True None False w8a8.
Return Value0:buildsuccess。
-1:buildfailure。

Example:

# Build the RKNN model and perform quantization
ret = rknn.build(do_quantization=True, dataset='./dataset.txt')

1.5 Export RKNN Model

This section describes 1.5 Export RKNN Model.

ItemContent
APIexport_rknn
DescriptionRKNNmodel specifyfile(.rknnextension) in save。
ParametersDescription of Parameters.
Return Value0:exportsuccess。
-1:exportfailure。

Example:

# Save the built RKNN model as mobilenet_v1.rknn in the current path
ret = rknn.export_rknn(export_path='./mobilenet_v1.rknn')

1.6 Load RKNN Model

ItemContent
APIload_rknn
DescriptionDescription of Description.
Parameterspath:RKNNmodelfile of path。
Return Value0:loadsuccess。
-1:loadfailure。

Example:

# Load the mobilenet_v1.rknn model from the current path
ret = rknn.load_rknn(path='./mobilenet_v1.rknn')

1.7 Initialize Runtime Environment

This section describes 1.7 Initialize Runtime Environment.

ItemContent
APIinit_runtime
DescriptionDescription of Description.
ParametersDescription of Parameters. Related values: None None None False False False.
Return ValueDescription of Return Value.

Example:

# Initialize the runtime environment
ret = rknn.init_runtime(target='rk3566')

1.8 Model Inference

This section describes 1.8 Model Inference.

ItemContent
APIinference
DescriptionDescription of Description.
ParametersDescription of Parameters. Related values: None NHWC None.
Return ValueDescription of Return Value.

This section describes 1.8 Model Inference.

# Run image inference with the model and obtain the top-5 results
outputs = rknn.inference(inputs=[img])
show_outputs(outputs)

This section describes 1.8 Model Inference.

-----TOP 5-----
[ 156] score:0.928223 class:"Shih-Tzu"
[ 155] score:0.063171 class:"Pekinese, Pekingese, Peke"
[ 205] score:0.004299 class:"Lhasa, Lhasa apso"
[ 284] score:0.003096 class:"Persian cat"
[ 285] score:0.000171 class:"Siamese cat, Siamese

1.9 Model Performance Evaluation

ItemContent
APIeval_perf
DescriptionDescription of Description. Related values: False True.
ParametersDescription of Parameters. Related values: True True.
Return Valueperf_result:performanceinformation(string)。

Example:

# Evaluate model performance
perf_detail = rknn.eval_perf()

1.10 Get Memory Usage

ItemContent
APIeval_memory
DescriptionDescription of Description.
ParametersDescription of Parameters. Related values: True.
Return ValueDescription of Return Value.

Example:

# Evaluate model memory usage
memory_detail = rknn.eval_memory()

This section describes 1.10 Get Memory Usage.

======================================================
Memory Profile Info Dump
======================================================
NPU model memory detail(bytes):
Weight Memory: 3.53 MiB
Internal Tensor Memory: 1.67 MiB
Other Memory: 473.00 KiB
Total Memory: 5.66 MiB
INFO: When evaluating memory usage, we need consider
the size of model, current model size is: 4.09 MiB
======================================================

1.11 Query SDK Version

ItemContent
APIget_sdk_version
DescriptionDescription of Description.
ParametersNone.
Return Valuesdk_version:APIanddriver of versioninformation。type string。

Example:

# Get SDK version information
sdk_version = rknn.get_sdk_version()
print(sdk_version)

This section describes 1.11 Query SDK Version.

==============================================
RKNN VERSION:
API: 1.5.2 (8babfea build@2023-08-25T02:31:12)
DRV: rknn_server: 1.5.2 (8babfea build@2023-08-25T10:30:12)
DRV: rknnrt: 1.5.3b13 (42cbca6f5@2023-10-27T10:13:21)
==============================================

1.12 Hybrid Quantization

1.12.1 hybrid_quantization_step1

This section describes 1.12.1 hybrid_quantization_step1.

ItemContent
APIhybrid_quantization_step1
DescriptionDescription of Description.
ParametersDescription of Parameters. Related values: False None.
Return Value0:success。
-1:failure。

Example:

# Call hybrid_quantization_step1 to generate the quantization configuration file
ret = rknn.hybrid_quantization_step1(dataset='./dataset.txt')

1.12.2 hybrid_quantization_step2

This section describes 1.12.2 hybrid_quantization_step2.

ItemContent
APIhybrid_quantization_step2
DescriptionDescription of Description.
ParametersDescription of Parameters.
Return Value0:success。
-1:failure。

Example:

# Call hybrid_quantization_step2 to generate hybrid quantized RKNN model
ret = rknn.hybrid_quantization_step2(
model_input='./ssd_mobilenet_v2.model',
data_input='./ssd_mobilenet_v2.data',
model_quantization_cfg='./ssd_mobilenet_v2.quantization.cfg')

1.13 Quantization Accuracy Analysis

This section describes 1.13 Quantization Accuracy Analysis.

ItemContent
APIaccuracy_analysis
DescriptionDescription of Description.
ParametersDescription of Parameters. Related values: None None.
Return Value0:success。
-1:failure。

Example:

# Accuracy analysis
ret = rknn.accuracy_analysis(inputs=['./dog_224x224.jpg'])

1.14 Get Device List

ItemContent
APIlist_devices
DescriptionDescription of Description.
ParametersNone.
Return ValueDescription of Return Value.

Example:

rknn.list_devices()

This section describes 1.14 Get Device List.

*************************
all device(s) with adb mode:
VD46C3KM6N
*************************

This section describes 1.14 Get Device List.

1.15 Export Encrypted Model

This section describes 1.15 Export Encrypted Model.

ItemContent
APIexport_encrypted_rknn_model
DescriptionDescription of Description.
ParametersDescription of Parameters. Related values: None.
Return Value0:success。
-1:failure。

Example:

ret = rknn.export_encrypted_rknn_model('test.rknn')

1.16 Register Custom Operator

This section describes 1.16 Register Custom Operator.

ItemContent
APIreg_custom_op
DescriptionDescription of Description.
ParametersDescription of Parameters.
Return Value0:success。
-1:failure。

Example:

import numpy as np
from rknn.api.custom_op import get_node_attr
class cstSoftmax:
op_type = 'cstSoftmax'
def shape_infer(self, node, in_shapes, in_dtypes):
out_shapes = in_shapes.copy()
out_dtypes = in_dtypes.copy()
return out_shapes, out_dtypes
def compute(self, node, inputs):
x = inputs[0]
axis = get_node_attr(node, 'axis')
x_max = np.max(x, axis=axis, keepdims=True)
tmp = np.exp(x - x_max)
s = np.sum(tmp, axis=axis, keepdims=True)
outputs = [tmp / s]
return outputs
ret = rknn.reg_custom_op(cstSoftmax)

1.17 Generate C++ Deployment Example

ItemContent
APIcodegen
DescriptionDescription of Description.
ParametersDescription of Parameters. Related values: True False.
Return Value0:success。
-1:failure。

Example:

ret = rknn.codegen(output_path='./rknn_app_demo',
inputs=['./mobilenet_v2/dog_224x224.jpg'],
overwrite=True)