|
本帖最后由 xsky 于 2019-12-11 21:23 编辑
请问对于输入,rknn.config(batch_size=1, channel_mean_value='123 117 104 1', reorder_channel='0 1 2', epochs=100)
输入图片的数值没有归一化到0~1的时候,直接使用 rknn.build(do_quantization=True, dataset='dataset1.txt', rknn_batch_size=1)
而不是使用混合量化,是否可行,这样输入值的范围是 r: [-123, 132], g:[-117, 138], b:[-104, 151],这样如果不使用混合量化,是不是不可行? 现在测试输出的结果不对。
另外,使用这种方式,怎么看出模型是不是量化的模型,build过程中有这样的信息输出:
D Quantize @Conv_79_24:bias to asymmetric_quantized.
D Quantize @Conv_79_24:weight to asymmetric_quantized.
D Packing Initializer_12_95 ...
D Packing Initializer_13_90 ...
I Build config finished.
模型转换的代码:
- rknn = RKNN(verbose=True)
- rknn.config(batch_size=1, channel_mean_value='123 117 104 1', reorder_channel='0 1 2', epochs=100)
- model_file = 'detector.pth.tar_op-v9.onnx'
- rknn.load_onnx(model=model_file)
- print('done')
- is_quantization = True
- # 创建解析pb模型
- # do_quantization=False指定不进行量化
- # 量化会减小模型的体积和提升运算速度,但是会有精度的丢失
- print('--> Building model')
- rknn.build(do_quantization=True, dataset='dataset1.txt', rknn_batch_size=1)
- print('done')
- # 导出保存rknn模型文件
- if is_quantization:
- quant_suff = '_quant'
- else:
- quant_suff = '_un-quant'
- rknn_file = model_file + quant_suff + '.rknn'
- print('export:', rknn_file, '...')
- rknn.export_rknn(rknn_file)
- # Release RKNN Context
- rknn.release()
运行测试代码:
在预处理中先做了x-mean,通道顺序为RGB, 上一段build代码中开启量化能成功生成模型,但是推理的运行时间以下代码中使用onnx build 不量化应该是float16,和一段量化生成的.rknn模型运行差不多,甚至还多一点,这不应该啊, 在量化模型上传入的数据类型为float32,会不会有问题, ? 有问题的话,请问这个场景应该使用使用什么方式? 对于量化模型,如果输入的tensor是float32的,rknn runtime不会转换成需要的int8输入吗?
- from PIL import Image
- import numpy as np
- import torch
- import time
- import os
- from rknn.api import RKNN
- def pre_process(image):
- """
- :param image: PIL numpy instance
- :return:
- """
- # device = torch.device("cuda")pe
- w, h = image.size
- if w != 1920 and h != 1080:
- image = image.resize((1920, 1080), Image.ANTIALIAS)
- img = np.int16(image)
- img -= (123, 117, 104)
- img = img.transpose(2, 0, 1)
- img = np.array(img, dtype=np.float32, order='C')
- return img
- def load_images_tensor(path, suff='.jpg', max_num=0):
- print('load images @%s %s ...' % (path, suff))
- img_files = os.listdir(path)
- rm_files = []
- for fn in img_files:
- if os.path.splitext(fn)[1] != suff:
- rm_files.append(fn)
- for fn in rm_files:
- img_files.remove(fn)
- n = len(img_files)
- if max_num <= 0:
- max_num = n
- n = min(max_num, n)
- images = []
- for i in range(n):
- f_name = img_files[i]
- f = path + f_name
- im = Image.open(f)
- im = pre_process(im)
- images.append(im)
- print('load images:', len(images))
- return images
- if __name__ == '__main__':
- # 创建RKNN执行对象
- rknn = RKNN(verbose=True)
- #图像通道顺序不做调整
- rknn.config(batch_size=1, channel_mean_value='0 0 0 1', reorder_channel='0 1 2') # , channel_mean_value='123 117 104 1', reorder_channel='2 0 1'
- use_rknn = True
- print('is rknn:', use_rknn)
- if not use_rknn:
- model_file = 'detector.pth.tar_op-v9.onnx'
- #model_file = 'conv1_op-v9.onnx'
- print('--> Loading model', model_file)
- rknn.load_onnx(model=model_file)
- print('--> Building model')
- rknn.build(do_quantization=False, dataset='dataset.txt', rknn_batch_size=1)
- print('done')
- else:
- model_file = 'XMC2-Det_student_detector.pth.tar_op-v9.onnx_quant.rknn'
- rknn.load_rknn(model_file, load_model_in_npu=True) # , load_model_in_npu=True
- x_list = load_images_tensor('assets/images/', '.jpg', 20)
- num = len(x_list)
- run_cnt = num * 15
- x = x_list[0]
- t0 = time.time()
- for i in range(run_cnt):
- # idx = i % num
- # x = x_list[idx]
- outputs = rknn.inference(inputs=[x], data_type='float32', data_format='nchw')
- t1 = time.time()
- print(' times %d done: avg %.3f s' % (run_cnt, (t1 - t0) / run_cnt))
- # Release RKNN Context
- rknn.release()
|
|