|
本帖最后由 xsky 于 2020-2-22 22:30 编辑
在多输入时,各个输入shape不一样,为何从onnx转换为rknn后输入的顺序会乱, 输出的顺序是正常的,
另外C API 可以支持多输入, Python接口怎么构选多输入? user guide 文档并没有叙述.
谢谢!
Pytorch 1.2.0
RKNN 1.3.0tensorflow 1.14.0
onnx 1.4.1
onnx-tf 1.2.1
RKNN API 1.3.0, DRIVE: 1.3.0 Debain 10
如下代码:
- import platform
- import os
- import torch
- import numpy as np
-
- from rknn.api import RKNN
-
- import onnx
- from onnx_tf.backend import prepare
-
- class LSTM(torch.nn.Module):
-
- def __init__(self, input_size, hidden_size):
- super(LSTM, self).__init__()
-
- self._input_size = input_size
- self._hidden_size = hidden_size
- self._fc = torch.nn.Linear(input_size, input_size)
- self._tanh = torch.nn.Tanh()
- self._sigmoid = torch.nn.Sigmoid()
-
- def forward(self, x, h0, c0):
-
- return self._fc(x), self._sigmoid(h0), self._sigmoid(c0)
- #ox, (h1, c1) = self._lstm(x, (h0, c0))
- return ox, h1, c1
-
- if __name__ == '__main__':
-
- system = platform.system()
-
- isize = 128
- hsize = 64
- #m = torch.nn.Linear(isize, hsize)
- m = LSTM(isize, hsize)
- m.eval()
- m.float()
-
- h0 = torch.full((1, 1, hsize), fill_value=0.15, dtype=torch.float)
- c0 = torch.full((1, 1, hsize//2), fill_value=0.07, dtype=torch.float)
- x = torch.full((1, isize), fill_value=0.1, dtype=torch.float)
- #x1 = torch.full((1, hsize), fill_value=0.2, dtype=torch.float)
- pt_file = "lstm{%dx%d}.pt" % (isize, hsize)
-
- rknn = RKNN(verbose=True)
- rknn.config(batch_size=1,
- epochs=1) # asymmetric_quantized-u8, quantized_dtype='dynamic_fixed_point-16' , channel_mean_value='0 0 0 1', reorder_channel='0 1 2',
-
- #pt = torch.jit.trace(m, x, h0, c0)
- #pt.save(pt_file)
- #rknn.load_pytorch(model=pt_file, input_size_list=[[1, isize]])
-
- onnx_file = pt_file + '.onnx'
- torch.onnx.export(m, (x, h0, c0), onnx_file,
- export_params=True, # store the trained parameter weights inside the model file
- # do_constant_folding=True, # whether to execute constant folding for optimization
- opset_version=9, # the ONNX version to export the model to
- input_names=['input', 'h0', 'c0'], # the model's input names
- output_names=['output', 'h1', 'c1'] # the model's output names
- # dynamic_axes={'input' : {0 : 'batch_size'}, # variable lenght axes
- # 'output' : {0 : 'batch_size'}}
- )
- rknn.load_onnx(model=onnx_file)
-
- rknn_file = pt_file + '.rknn'
- ret = rknn.build(do_quantization=False, dataset='./dataset.txt')
- rknn.export_rknn(rknn_file)
-
- print('--> Init runtime environment')
-
- if system == 'Windows':
- print("rknn run target rk1808")
- ret = rknn.init_runtime(target='rk1808', target_sub_class='AICS')
- else:
- print("rknn run target self")
- ret = rknn.init_runtime()
- if ret != 0:
- print('Init runtime environment failed')
- exit(ret)
- print('done')
-
- ix = x.numpy()
- tf_model = onnx.load(onnx_file)
- tf_rep = prepare(tf_model)
- tf_out, tf_h1, tf_c1 = tf_rep.run((ix, h0, c0))
- tf_out = np.array(tf_out)
- print("onnx out:", ix.shape, ', ', tf_out.shape)
- print(tf_out)
-
- torch_out, th_h1, th_c1 = m.forward(x, h0, c0)
- print("torch [out]:", x.shape, ', ', torch_out.shape)
- print(torch_out.detach().unsqueeze(0).numpy())
-
- ih0 = h0.numpy()
- ic0 = c0.numpy()
- rknn_out = rknn.inference(inputs=[(ix, ih0, ic0)], data_type='float32', data_format='nchw') # , inputs_pass_through=[1]
- rknn_out = np.array(rknn_out)
- print("rknn out:", ix.shape, ', ', rknn_out.shape)
- print(rknn_out)
-
- rknn.release()
输出的模型形状,输入输出均已命名,且onnx模型可正常运行:
查询RKNN输入相关的代码, 在RK3399Pro上运行:- ret = rknn_query(ctx, RKNN_QUERY_IN_OUT_NUM, &in_out_num, sizeof(in_out_num));
- if (ret < 0) {
- printf(_LINE_MARK_ " rknn_query fail: query in out num, ret=%d\n", ret);
- goto Error;
- }
- assert(_NInChs == in_out_num.n_input && _NOutChs == in_out_num.n_output);
- for (int i = 0; i < _NInChs; i++) {
- inputs_attr[i].index = i;
- ret = rknn_query(ctx, RKNN_QUERY_INPUT_ATTR, &(inputs_attr[i]), sizeof(inputs_attr[i]));
- if (ret < 0) {
- printf(_LINE_MARK_ " rknn_query fail: query inputs attrs, ret=%d\n", ret);
- goto Error;
- }
- }
- for (int i = 0; i < _NOutChs; i++) {
- outputs_attr[i].index = i;
- ret = rknn_query(ctx, RKNN_QUERY_OUTPUT_ATTR, &(outputs_attr[i]), sizeof(outputs_attr[i]));
- if (ret < 0) {
- printf(_LINE_MARK_ " rknn_query fail: query outputs attrs ret=%d\n", ret);
- goto Error;
- }
- }
输入为 (x, h0, c0)按顺序对应的shape 分另为:(1, 128), (1, 1, 64), (1, 1, 32),但在RKNN C API中获取到的输入属性为 (1, 1, 32), (1, 128), (1, 1, 64):
输出的顺序确是正确的(1, 128), (1, 1, 64), (1, 1, 32):, scale = 1}}
而且, 不同次导出的onnx, RKNN编译查询得到的输入属性都会不同
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|