Toybrick

4M 的 onnx 转换出 rknn 居然有 500M 是怎么回事?

sawyer_z

新手上路

积分
17
楼主
发表于 2024-9-23 10:44:56    查看: 812|回复: 0 | [复制链接]    打印 | 只看该作者
本帖最后由 sawyer_z 于 2024-9-23 10:47 编辑

一个支持 dynamic shape 的 onnx,本来是 4M 大小,已验证可以正常运行。
通过下面的代码转换后,生成的 rknn 模型居然有 500多 M。可能会是什么原因,该怎么处理?源模型见附件。



import numpy as np
import torch
from rknn.api import RKNN

# Create RKNN object
rknn = RKNN()

dynamic_input = [
    [[1, 100, 13], [1, 25, 112, 112]],
    # [[1, 200, 13], [1, 50, 112, 112]],
    # [[1, 300, 13], [1, 75, 112, 112]],
    # [[1, 400, 13], [1, 100, 112, 112]],
    # [[1, 500, 13], [1, 125, 112, 112]],
    # [[1, 600, 13], [1, 150, 112, 112]]
]

# Pre-process config
print('--> Config model')
rknn.config(target_platform='rk3588', optimization_level=3, dynamic_input=dynamic_input)
print('done')

print('--> Loading ONNX model')
ret = rknn.load_onnx(model='model.onnx')
if ret != 0:
    print('Load ONNX model failed!')
    exit(ret)
print('done')
# print('done')

# Build model
print('--> Building model')
ret = rknn.build(do_quantization=False)
if ret != 0:
    print('Build RKNN model failed!')
    exit(ret)
print('done')

# Export RKNN model
print('--> Export RKNN model')
ret = rknn.export_rknn('./model.rknn')
if ret != 0:
    print('Export RKNN model failed!')
    exit(ret)
print('done')

# Release RKNN object for next use
rknn.release()



python3 onnx2rknn.py
I rknn-toolkit2 version: 2.2.1
--> Config model
W config: Please make sure the model can be dynamic when enable 'config.dynamic_input'!
I The 'dynamic_input' function has been enabled, the MaxShape is dynamic_input[0] = [[1, 100, 13], [1, 25, 112, 112]]!
          The following functions are subject to the MaxShape:
            1. The quantified dataset needs to be configured according to MaxShape
            2. The eval_perf or eval_memory return the results of MaxShape
done
--> Loading ONNX model
I Loading : 100%|████████████████████████████████████████████████| 60/60 [00:00<00:00, 33442.96it/s]
W load_onnx: The config.mean_values is None, zeros will be set for input 0!
W load_onnx: The config.std_values is None, ones will be set for input 0!
W load_onnx: The config.mean_values is None, zeros will be set for input 1!
W load_onnx: The config.std_values is None, ones will be set for input 1!
done
--> Building model
I OpFusing 0: 100%|██████████████████████████████████████████████| 100/100 [00:00<00:00, 384.21it/s]
I OpFusing 1 :  29%|█████████████▋                                 | 29/100 [00:00<00:01, 45.54it/s]W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
I OpFusing 1 :  51%|███████████████████████▉                       | 51/100 [00:02<00:02, 22.76it/s]W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
I OpFusing 1 :  67%|███████████████████████████████▍               | 67/100 [00:08<00:04,  7.81it/s]W build: The 'dynamic_input' function is enabled, disable tiling_global_maxpool2!
I OpFusing 1 :  71%|█████████████████████████████████▎             | 71/100 [00:08<00:03,  8.28it/s]W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
I OpFusing 1 :   1%|▍                                               | 1/100 [00:08<14:19,  8.68s/it]W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
I OpFusing 1 :  23%|██████████▊                                    | 23/100 [00:08<00:29,  2.65it/s]W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
I OpFusing 1 :  53%|████████████████████████▉                      | 53/100 [00:08<00:07,  6.09it/s]W build: The 'dynamic_input' function is enabled, disable tiling_global_maxpool2!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
I OpFusing 1 :  96%|█████████████████████████████████████████████  | 96/100 [00:08<00:00, 10.98it/s]W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable tiling_global_maxpool2!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
I OpFusing 1 :  85%|███████████████████████████████████████▉       | 85/100 [00:08<00:01,  9.60it/s]W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable tiling_global_maxpool2!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
I OpFusing 1 : 100%|██████████████████████████████████████████████| 100/100 [00:08<00:00, 11.15it/s]
I OpFusing 0 : 100%|██████████████████████████████████████████████| 100/100 [00:09<00:00, 10.32it/s]
I OpFusing 1 :   0%|                                                        | 0/100 [00:09<?, ?it/s]W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable tiling_global_maxpool2!
I OpFusing 1 :  53%|████████████████████████▉                      | 53/100 [00:09<00:08,  5.46it/s]W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable tiling_global_maxpool2!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
I OpFusing 1 : 100%|██████████████████████████████████████████████| 100/100 [00:09<00:00, 10.23it/s]
I OpFusing 0 : 100%|██████████████████████████████████████████████| 100/100 [00:09<00:00, 10.14it/s]
I OpFusing 1 :   0%|                                                        | 0/100 [00:09<?, ?it/s]W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable tiling_global_maxpool2!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
I OpFusing 1 : 100%|██████████████████████████████████████████████| 100/100 [00:09<00:00, 10.11it/s]
I OpFusing 2 : 100%|██████████████████████████████████████████████| 100/100 [00:09<00:00, 10.07it/s]
I OpFusing 0 : 100%|██████████████████████████████████████████████| 100/100 [00:10<00:00,  9.98it/s]
I OpFusing 1 :   0%|                                                        | 0/100 [00:10<?, ?it/s]W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable tiling_global_maxpool2!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
I OpFusing 1 : 100%|██████████████████████████████████████████████| 100/100 [00:10<00:00,  9.95it/s]
I OpFusing 2 : 100%|██████████████████████████████████████████████| 100/100 [00:10<00:00,  9.95it/s]
I OpFusing 0: 100%|██████████████████████████████████████████████| 100/100 [00:00<00:00, 544.68it/s]
I OpFusing 1 : 100%|█████████████████████████████████████████████| 100/100 [00:00<00:00, 451.09it/s]
I OpFusing 2 : 100%|█████████████████████████████████████████████| 100/100 [00:00<00:00, 424.94it/s]
I rknn building ...
I rknn buiding done.
done
--> Export RKNN model
done

ls -al

-rw-r--r--  1 root root   4129926 Sep 21 11:58 model.onnx
-rw-r--r--  1 root root 524161785 Sep 22 13:56 model.rknn

另外,如果更改 opset 为14,会出现下面的错误:
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable _p_convert_maxpool_to_maxpool_tile!
W build: The 'dynamic_input' function is enabled, disable tiling_global_maxpool2!
I rknn building ...
terminate called after throwing an instance of 'std:ut_of_range'
  what():  vector::_M_range_check: __n (which is 2) >= this->size() (which is 3)
Aborted

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

产品中心 购买渠道 开源社区 Wiki教程 资料下载 关于Toybrick


快速回复 返回顶部 返回列表