|
NPU Team回复: 253049
1.如果要跑你这个模型,rknn.config(optimization_level=0)设置一下,如果不设置的话卷积和relu在硬件上会merge。
2.rknn.init_runtime 请指定设备,因为驱动更新比模拟器快,硬件跑的结果可能会和模拟器跑的结果不一样。
直接跑我这个test.py是能得到正确结果的。
- # -*- 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()
- net.eval()
- 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',
- optimization_level=0,
- 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(target='rk1808')
- 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("---")
|
|