|
版本:rknn-toolkit 1.3.2
在转换一个目标检测模型时遇到问题(问题详见帖子:http://t.rock-chips.com/forum.ph ... tid=1625&extra=)。
通过删减层、尝试不同输出,定位到问题首先出现在 nn.ReLU. 为了让问题更清晰,写了一个小模型如下:
- class Net(torch.nn.Module):
- def __init__(self):
- super(Net, self).__init__()
- self.conv1 = torch.nn.Conv2d(3,24,3)
- self.nonlinear = torch.nn.ReLU() # successful op: Sigmoid, LeakyReLU
- def forward(self, x):
- x1 = self.conv1(x)
- y = self.nonlinear(x1)
- return x1, y
问题描述:
比较 pytorch 和 rknn 模型的输出
- 对于 y: 绝对距离和 cos 距离都很小,认为几乎一致
- 对于 x1: rknn 输出张量的所有分量严格等于0.0, cos 距离为 nan
另外,Net 中的self.nonlinear 还尝试了 Sigmoid, LeakyReLU, y 和 x1 的 rknn 输出都正常,与 torch 输出几乎一致
附上一个可以完整运行的代码:
- # -*- coding: utf-8 -*-
- # create time: 2020-05-15 15:29
- import torch
- from rknn.api import RKNN
- import numpy as np
- class Net(torch.nn.Module):
- def __init__(self):
- super(Net, self).__init__()
- self.conv1 = torch.nn.Conv2d(3,24,3)
- self.nonlinear = torch.nn.ReLU() # successful op: Sigmoid, LeakyReLU
- def forward(self, x):
- x1 = self.conv1(x)
- y = self.nonlinear(x1)
- return x1, y
- def E_D(vector1, vector2):
- print("vector1 norm: ", np.linalg.norm(vector1))
- print("vector2 norm: ", np.linalg.norm(vector2))
- print("distance: ", np.linalg.norm(vector1 - vector2))
- def cos_d(vector1, vector2):
- d = 1 - np.dot(vector1, vector2) / (np.linalg.norm(vector1) * (np.linalg.norm(vector2)))
- print(d)
- if __name__ == '__main__':
- net = Net()
- i1 = torch.rand(1,3,5,5)
- # i2 = torch.rand(1,2,7,7)
- # i3 = torch.rand(1,3,9,9)
- trace_model = torch.jit.trace(net, (i1))
- trace_model.save('test.pt')
- rknn = RKNN(verbose=True)
- rknn.config(batch_size=1,
- channel_mean_value='0 0 0 1',
- reorder_channel='0 1 2',
- epochs=1)
- ret = rknn.load_pytorch(model='test.pt', input_size_list=[[3,5,5]])
- # ret = rknn.load_onnx(model='lstm{128x64}.pt.onnx')
- if ret != 0:
- print('Load pytorch model failed!')
- exit(ret)
- ret = rknn.build(do_quantization=False, dataset='./dataset.txt')
- if ret != 0:
- print('Build pytorch failed!')
- exit(ret)
- ret = rknn.init_runtime()
- if ret != 0:
- print('Init runtime environment failed')
- exit(ret)
- rknn_r = rknn.inference(inputs=[i1.numpy()],
- data_type='float32',
- data_format='nchw')
- pytorch_r = net(i1)
- for d1, d2 in zip(rknn_r, pytorch_r):
- d1 = d1.ravel()
- d2 = d2.detach().numpy().ravel()
- E_D(d1, d2)
- cos_d(d1, d2)
- print("---")
希望能够得到解答~谢谢
|
|