- 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
复制代码
- # -*- 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("---")
复制代码
希望能够得到解答~谢谢 jefferyzhang 发表于 2020-5-18 17:10
我已经报告给NPU部门了,等他们回复
kkkaaa 发表于 2020-5-21 17:58
没有反馈吗?
jefferyzhang 发表于 2020-5-21 18:13
没这么快吧,你要急的话要通过公司FAE反馈。
kkkaaa 发表于 2020-5-22 10:03
好的谢谢。我以为是个小 bug
jefferyzhang 发表于 2020-5-22 10:19
再小的bug都要等他们下一个版本出来才能测试。他们不会为任何一个个人出单独版本,NPU的工程编译一次就是 ...
jefferyzhang 发表于 2020-5-22 11:01
没有办法,这里只是社区,我们也只是跟你们一样的工程师而已。
要么等,要么联系FAE。 ...
kkkaaa 发表于 2020-5-22 11:07
I see... 你们的 NPU 部门连这是不是一个 bug 都不会立马反馈给你们是吧。。。
看来用 rknn 还是最好限制 ...
- # -*- 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("---")
复制代码
欢迎光临 Toybrick (https://t.rock-chips.com/) | Powered by Discuz! X3.3 |