- class ZeroPadModel(nn.Module):
- def __init__(self):
- super(ZeroPadModel, self).__init__()
- self.static_padding = nn.ZeroPad2d((0,1,0,1))
- def forward(self, x):
- x = self.static_padding(x)
- return x
- # === torch 模型初始化 ===
- # net = Conv2dStaticSamePaddingModel(image_size)
- net = ZeroPadModel()
- print("==== network ====")
- print(net)
- net.eval()
- # === 转化1: torch2onnx ===
- print("--> torch model inference result")
- input_tensor = torch.rand(1,3, image_size, image_size)
- torch_out = torch.onnx._export(net, input_tensor, ONNX_MODEL, export_params=True)
- # === 转化2: onnx2rknn ===
- from rknn.api import RKNN
- rknn = RKNN()
- print('--> Loading model')
- ret = rknn.load_onnx(model=ONNX_MODEL)
- if ret != 0:
- print('Load resnet50v2 failed!')
- exit(ret)
- print('done')
- # Build model
- print('--> Building model')
- ret = rknn.build(do_quantization=False, dataset='./dataset.txt')
- if ret != 0:
- print('Build resnet50 failed!')
- exit(ret)
- print('done')
- # Export rknn model
- print('--> Export RKNN model')
- ret = rknn.export_rknn(RKNN_MODEL)
- if ret != 0:
- print('Export resnet50v2.rknn failed!')
- exit(ret)
- print('done')
- # === rknn inference ===
- # init runtime environment
- print("--> Init runtime environment")
- ret = rknn.init_runtime()
- if ret != 0:
- print("Init runtime environment failed")
- exit(ret)
- print('done')
- # inference
- print("--> Running rknn model")
- rknn_input = input_tensor.numpy().transpose(0,2,3,1)
- rknn_outputs = rknn.inference(inputs=[rknn_input], data_format='nhwc')[0][0]
- # === torch inference ===
- torch_inference_result = net(input_tensor)[0].detach().cpu().numpy()
- # === compare & show results ===
- print("--> compare inference")
- print("input shape: ", input_tensor.shape)
- print("rknn input shape: ", rknn_input.shape)
- print("torch_inference shape: ", torch_inference_result.shape)
- print("rknn_outputs shape: ", rknn_outputs.shape)
- print("max error: ", np.max(torch_inference_result - rknn_outputs))
- print("~~~~~~ torch model infer output ~~~~~~")
- print(torch_inference_result)
- print("~~~~~~ rknn model infer output ~~~~~~")
- print(rknn_outputs)
复制代码
输出:jefferyzhang 发表于 2020-4-24 15:58
我先帮你把问题报给NPU部门。。
jefferyzhang 发表于 2020-4-27 16:27
这个我不知道,我只有权利给他们提问题,他们解决了发布后才会通知我。。。 ...
- tensor([[[[ 0., 1.],
- [ 2., 3.]],
- [[ 4., 5.],
- [ 6., 7.]],
- [[ 8., 9.],
- [10., 11.]]]])
复制代码
- [[[ 0. 1. 0.]
- [ 2. 3. 0.]
- [ 0. 0. 0.]]
- [[ 4. 5. 0.]
- [ 6. 7. 0.]
- [ 0. 0. 0.]]
- [[ 8. 9. 0.]
- [10. 11. 0.]
- [ 0. 0. 0.]]]
复制代码
- [[[ 0. 1. 1.]
- [ 2. 3. 3.]
- [ 2. 3. 3.]]
- [[ 4. 5. 5.]
- [ 6. 7. 7.]
- [ 6. 7. 7.]]
- [[ 8. 9. 9.]
- [10. 11. 11.]
- [10. 11. 11.]]]
复制代码
kkkaaa 发表于 2020-4-27 18:00
版主,我发现 ZeroPad2d 这个op 从 torch -> onnx -> rknn 为啥结果会不一致了
op = nn.ZeroPad2d((0,1, ...
jefferyzhang 发表于 2020-4-27 18:23
如果和原模型不和肯定是我们bug。。。
目前pytorch刚开始支持,问题还是会有的。。 ...
jefferyzhang 发表于 2020-5-25 11:46
NPU Team 答复:
1.3.2已经修复,请尝试下1.3.2是否正常
250242
iamher0 发表于 2020-6-10 14:10
我在1.3.2依然遇到这个问题:
KeyError: 'aten::constant_pad_nd'
欢迎光临 Toybrick (https://t.rock-chips.com/) | Powered by Discuz! X3.3 |