Toybrick

关于pytorch转onnx的反卷积问题

18651669016

新手上路

积分
11
楼主
发表于 2020-3-2 17:21:53    查看: 29887|回复: 19 | [复制链接]    打印 | 只看该作者
centernet的pytorch模型,转成onnx后,使用rknn.load_onnx(model.onnx)调用,出错。 模型中包含pytorch的反卷积操作nn.ConvTranspose2d。请问rknn是不是不支持pytorch的反卷积操作?

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

gyq_

注册会员

积分
76
沙发
发表于 2020-4-1 15:53:04 | 只看该作者
我也遇到了同样的问题,请问楼主解决了吗
回复

使用道具 举报

jefferyzhang

版主

积分
13580
板凳
发表于 2020-4-1 15:55:00 | 只看该作者
本帖最后由 jefferyzhang 于 2020-4-1 15:56 编辑

1. pytorch必须是1.2版本
2. 反卷积可以支持,verbose打开看下上面转换的错误log是什么
3. 转的这个onnx模型转rknn之前先加载推理下看是否能成功
回复

使用道具 举报

gyq_

注册会员

积分
76
地板
发表于 2020-4-1 16:20:29 | 只看该作者
我的是直接转换pt文件。
D RKNN output shape(batchnormalize): (0 16 16 320)
D Process convolution_at_input136.1_36 ...
D RKNN output shape(convolution): (0 16 16 1280)
D Process batch_norm_at_input137.1_35 ...
D RKNN output shape(batchnormalize): (0 16 16 1280)
D Process hardtanh__at_input138.1_34 ...
D RKNN output shape(relun): (0 16 16 1280)
D Process convolution_at_input139.1_33 ...
D RKNN output shape(convolution): (0 8 8 1280)
D Process batch_norm_at_input140.1_32 ...
D RKNN output shape(batchnormalize): (0 8 8 1280)
D Process relu__at_input141.1_31 ...
D RKNN output shape(relu): (0 8 8 1280)
D Process convolution_at_input142.1_30 ...
D RKNN output shape(convolution): (0 4 4 16)
D Process batch_norm_at_input143.1_29 ...
D RKNN output shape(batchnormalize): (0 4 4 16)
D Process relu__at_input144.1_28 ...
D RKNN output shape(relu): (0 4 4 16)
D Process convolution_at_input145.1_27 ...
D RKNN output shape(convolution): (0 2 2 16)
D Process batch_norm_at_input146.1_26 ...
D RKNN output shape(batchnormalize): (0 2 2 16)
D Process relu__at_input147.1_24 ...
D RKNN output shape(relu): (0 2 2 16)
D Process convolution_at_input148.1_18 ...
D RKNN output shape(convolution): (0 2 2 64)
D Process relu__at_input149.1_12 ...
D RKNN output shape(relu): (0 2 2 64)
D Process convolution_at_hm_x.1_6 ...


D Process output_of_convolution_at_2269_5 ...
D RKNN output shape(output): (0 2 2 2)
I Build mobilenet1 complete.
D Optimizing network with force_1d_tensor, swapper, merge_layer, auto_fill_bn, resize_nearest_transformer, auto_fill_multiply, merge_avgpool_conv1x1, auto_fill_zero_bias, proposal_opt_import
D Merge ['convolution_at_input145.1_27', 'batch_norm_at_input146.1_26'] (convolution)
D Merge ['convolution_at_input142.1_30', 'batch_norm_at_input143.1_29'] (convolution)
D Merge ['convolution_at_input139.1_33', 'batch_norm_at_input140.1_32'] (convolution)
E Catch exception when loading pytorch model: /Users/gyq/DeepLearning/CenterNet_in_mac/mobilenet1.pt!
E Traceback (most recent call last):
E   File "rknn/api/rknn_base.py", line 567, in rknn.api.rknn_base.RKNNBase.load_pytorch
Load pytorch model failed!
E   File "rknn/base/RKNNlib/app/importer/import_pytorch.py", line 121, in rknn.base.RKNNlib.app.importer.import_pytorch.ImportPytorch.run
E   File "rknn/base/RKNNlib/app/helper/mergehelper.py", line 155, in rknn.base.RKNNlib.app.helper.mergehelper.MergeHelper.merge
E   File "rknn/base/RKNNlib/optimize/optimizer.py", line 299, in rknn.base.RKNNlib.optimize.optimizer.Optimizer.apply
E   File "rknn/base/RKNNlib/optimize/rules/merge_layer.py", line 81, in rknn.base.RKNNlib.optimize.rules.merge_layer.MergeLayer.apply
E   File "rknn/base/RKNNlib/optimize/rules/merge_layer.py", line 105, in rknn.base.RKNNlib.optimize.rules.merge_layer.MergeLayer._loop
E   File "rknn/base/RKNNlib/optimize/rules/merge_layer_ext_proc.py", line 86, in rknn.base.RKNNlib.optimize.rules.merge_layer_ext_proc.m_l1_bn
E ValueError: operands could not be broadcast together with shapes (4,4,16,1280) (16,)
回复

使用道具 举报

jefferyzhang

版主

积分
13580
5#
发表于 2020-4-1 16:26:16 | 只看该作者
gyq_ 发表于 2020-4-1 16:20
我的是直接转换pt文件。
D RKNN output shape(batchnormalize): (0 16 16 320)
D Process convolution_at_i ...

确认pytorch是不是1.2版本,高了低了都不行。
在确认你这个pth模型用这个pytorch1.2是否可以读出来推理
回复

使用道具 举报

gyq_

注册会员

积分
76
6#
发表于 2020-4-1 16:33:58 | 只看该作者
jefferyzhang 发表于 2020-4-1 16:26
确认pytorch是不是1.2版本,高了低了都不行。
在确认你这个pth模型用这个pytorch1.2是否可以读出来推理
...

pytorch确认是1.2.0,模型也可以读出来推理
回复

使用道具 举报

jefferyzhang

版主

积分
13580
7#
发表于 2020-4-1 16:37:51 | 只看该作者
gyq_ 发表于 2020-4-1 16:33
pytorch确认是1.2.0,模型也可以读出来推理

试下这个beta版本:

rknn_toolkit v1.3.1 beta3:
链接: https://pan.baidu.com/s/1Kn2FGAdF_j3CMLNEsC3OPw 提取码: rcds
回复

使用道具 举报

gyq_

注册会员

积分
76
8#
发表于 2020-4-1 19:25:05 | 只看该作者
jefferyzhang 发表于 2020-4-1 16:37
试下这个beta版本:

rknn_toolkit v1.3.1 beta3:

还是会出现一样的错误
回复

使用道具 举报

gyq_

注册会员

积分
76
9#
发表于 2020-4-2 00:31:47 | 只看该作者
jefferyzhang 发表于 2020-4-1 16:37
试下这个beta版本:

rknn_toolkit v1.3.1 beta3:
  1. import torch
  2. import torch.nn as nn
  3. import os
  4. import math
  5. import logging
  6. import torch.utils.model_zoo as model_zoo
  7. import math
  8. import numpy as np
  9. import cv2
  10. from rknn.api import RKNN
  11. import rknn.api

  12. import torchvision.models as models



  13. model = nn.Sequential(
  14.     nn.ConvTranspose2d(
  15.                     in_channels=1280,
  16.                     out_channels=16,
  17.                     kernel_size=4,
  18.                     stride=2,
  19.                     padding=1,
  20.                     output_padding=0,
  21.                     bias=True),
  22.     # nn.BatchNorm2d(16, momentum=0.1),
  23.     nn.ReLU(inplace=True),

  24.     nn.ConvTranspose2d(
  25.                     in_channels=16,
  26.                     out_channels=16,
  27.                     kernel_size=4,
  28.                     stride=2,
  29.                     padding=1,
  30.                     output_padding=0,
  31.                     bias=True),
  32.     nn.BatchNorm2d(16, momentum=0.1),
  33.     nn.ReLU(inplace=True),

  34.     nn.ConvTranspose2d(
  35.                     in_channels=16,
  36.                     out_channels=16,
  37.                     kernel_size=4,
  38.                     stride=2,
  39.                     padding=1,
  40.                     output_padding=0,
  41.                     bias=True),
  42.     nn.BatchNorm2d(16, momentum=0.1),
  43.     nn.ReLU(inplace=True),

  44. )

  45. trace_model = torch.jit.trace(model, torch.Tensor(1, 1280, 16, 16))
  46. trace_model.save('test_error.pt')

  47. input_size_list = [[1280, 16, 16]]

  48. # Create RKNN object
  49. rknn = RKNN(verbose=True)
  50. print('--> Loading model')
  51. ret = rknn.load_pytorch(model='test_error.pt', input_size_list=input_size_list)
复制代码


第一个batchnorm如果注释掉,就能转成功;如果保留,就会出现上面的报错。请问这是为什么?
回复

使用道具 举报

gyq_

注册会员

积分
76
10#
发表于 2020-4-2 00:41:04 | 只看该作者
D Process input_of_graph/out1_10 ...
D RKNN output shape(input): (0 16 16 1280)
D Process convolution_at_input0.1_9 ...
D RKNN output shape(convolution): (0 8 8 1280)
D Process batch_norm_at_input1.1_8 ...
D RKNN output shape(batchnormalize): (0 8 8 1280)
D Process relu__at_input2.1_7 ...
D RKNN output shape(relu): (0 8 8 1280)
D Process convolution_at_input3.1_6 ...
D RKNN output shape(convolution): (0 4 4 16)
D Process batch_norm_at_input4.1_5 ...
D RKNN output shape(batchnormalize): (0 4 4 16)
D Process relu__at_input5.1_4 ...
D RKNN output shape(relu): (0 4 4 16)
D Process convolution_at_input6.1_3 ...
D RKNN output shape(convolution): (0 2 2 16)
D Process batch_norm_at_input7.1_2 ...
D RKNN output shape(batchnormalize): (0 2 2 16)
D Process relu__at_135_1 ...
D RKNN output shape(relu): (0 2 2 16)
D Process output_of_relu__at_135_0 ...
D RKNN output shape(output): (0 2 2 16)
I Build test_error complete.
D Optimizing network with force_1d_tensor, swapper, merge_layer, auto_fill_bn, resize_nearest_transformer, auto_fill_multiply, merge_avgpool_conv1x1, auto_fill_zero_bias, proposal_opt_import
D Merge ['convolution_at_input6.1_3', 'batch_norm_at_input7.1_2'] (convolution)
D Merge ['convolution_at_input3.1_6', 'batch_norm_at_input4.1_5'] (convolution)
D Merge ['convolution_at_input0.1_9', 'batch_norm_at_input1.1_8'] (convolution)
E Catch exception when loading pytorch model: test_error.pt!
E Traceback (most recent call last):
E   File "rknn/api/rknn_base.py", line 567, in rknn.api.rknn_base.RKNNBase.load_pytorch
E   File "rknn/base/RKNNlib/app/importer/import_pytorch.py", line 121, in rknn.base.RKNNlib.app.importer.import_pytorch.ImportPytorch.run
E   File "rknn/base/RKNNlib/app/helper/mergehelper.py", line 155, in rknn.base.RKNNlib.app.helper.mergehelper.MergeHelper.merge
E   File "rknn/base/RKNNlib/optimize/optimizer.py", line 299, in rknn.base.RKNNlib.optimize.optimizer.Optimizer.apply
E   File "rknn/base/RKNNlib/optimize/rules/merge_layer.py", line 81, in rknn.base.RKNNlib.optimize.rules.merge_layer.MergeLayer.apply
E   File "rknn/base/RKNNlib/optimize/rules/merge_layer.py", line 105, in rknn.base.RKNNlib.optimize.rules.merge_layer.MergeLayer._loop
E   File "rknn/base/RKNNlib/optimize/rules/merge_layer_ext_proc.py", line 86, in rknn.base.RKNNlib.optimize.rules.merge_layer_ext_proc.m_l1_bn
E ValueError: operands could not be broadcast together with shapes (4,4,16,1280) (16,)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

产品中心 购买渠道 开源社区 Wiki教程 资料下载 关于Toybrick


快速回复 返回顶部 返回列表