|
我又尝试了一下先转成onnx再转成rknn。发现了一个问题,当反卷积层的输入通道数不等于输出通道数时,会出现如下错误:
D RKNN output shape(convolution): (1 16 16 1280)
D Real output shape: (1, 16, 16, 1280)
D Process ConvTranspose_30_9 ...
D RKNN output shape(deconvolution): (1 32 32 1280)
E Catch exception when building RKNN model!
E Traceback (most recent call last):
E File "rknn/api/rknn_base.py", line 737, in rknn.api.rknn_base.RKNNBase.build
E File "rknn/api/rknn_base.py", line 1644, in rknn.api.rknn_base.RKNNBase._quantize2
E File "rknn/base/RKNNlib/app/medusa/quantization.py", line 105, in rknn.base.RKNNlib.app.medusa.quantization.Quantization.run
E File "rknn/base/RKNNlib/app/medusa/quantization.py", line 44, in rknn.base.RKNNlib.app.medusa.quantization.Quantization._run_quantization
E File "rknn/base/RKNNlib/app/medusa/workspace.py", line 129, in rknn.base.RKNNlib.app.medusa.workspace.Workspace.run
E File "rknn/base/RKNNlib/app/medusa/workspace.py", line 99, in rknn.base.RKNNlib.app.medusa.workspace.Workspace._setup_graph
E File "rknn/base/RKNNlib/app/medusa/workspace.py", line 100, in rknn.base.RKNNlib.app.medusa.workspace.Workspace._setup_graph
E File "rknn/base/RKNNlib/RKNNnetbuilder.py", line 274, in rknn.base.RKNNlib.RKNNnetbuilder.RKNNNetBuilder.build
E File "rknn/base/RKNNlib/RKNNnetbuilder.py", line 278, in rknn.base.RKNNlib.RKNNnetbuilder.RKNNNetBuilder.build
E File "rknn/base/RKNNlib/RKNNnetbuilder.py", line 305, in rknn.base.RKNNlib.RKNNnetbuilder.RKNNNetBuilder.build_layer
E File "rknn/base/RKNNlib/RKNNnetbuilder.py", line 305, in rknn.base.RKNNlib.RKNNnetbuilder.RKNNNetBuilder.build_layer
E File "rknn/base/RKNNlib/RKNNnetbuilder.py", line 305, in rknn.base.RKNNlib.RKNNnetbuilder.RKNNNetBuilder.build_layer
E [Previous line repeated 2 more times]
E File "rknn/base/RKNNlib/RKNNnetbuilder.py", line 331, in rknn.base.RKNNlib.RKNNnetbuilder.RKNNNetBuilder.build_layer
E File "rknn/base/RKNNlib/RKNNnetbuilder.py", line 336, in rknn.base.RKNNlib.RKNNnetbuilder.RKNNNetBuilder.build_layer
E File "rknn/base/RKNNlib/layer/RKNNlayer.py", line 287, in rknn.base.RKNNlib.layer.RKNNlayer.RKNNLayer.compute_tensor
E File "rknn/base/RKNNlib/layer/deconvolution.py", line 100, in rknn.base.RKNNlib.layer.deconvolution.Deconvolution.compute_out_tensor
E File "/Users/gyq/DeepLearning/CenterNet_in_mac/venv/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 1242, in conv2d_transpose
E filter.get_shape()[2]))
E ValueError: output_shape does not match filter's output channels, 1280 != 16
如果两个通道数相等则不会出现错误。
- import torch
- import torch.nn as nn
- import cv2
- from rknn.api import RKNN
- #
- channels = 1280
- model = nn.Sequential(
- nn.Conv2d(
- 3, 32,
- kernel_size=4, stride=2,
- padding=1, dilation=1, bias=False),
- nn.Conv2d(
- 32, 32,
- kernel_size=4, stride=2,
- padding=1, dilation=1, bias=False),
- nn.Conv2d(
- 32, 32,
- kernel_size=4, stride=2,
- padding=1, dilation=1, bias=False),
- nn.Conv2d(
- 32, 32,
- kernel_size=4, stride=2,
- padding=1, dilation=1, bias=False),
- nn.Conv2d(
- 32, channels,
- kernel_size=4, stride=2,
- padding=1, dilation=1, bias=False),
- # 当ConvTranspose2d输入通道数不等于输出通道数是,在build过程中会出错
- # 加一层conv2D转换一下后就可以跑通了
- # nn.Conv2d(
- # channels, 16,
- # kernel_size=3, stride=1,
- # padding=1, dilation=1, bias=False),
- nn.ConvTranspose2d(
- in_channels=channels,
- # in_channels=16,
- out_channels=16,
- kernel_size=4,
- stride=2,
- padding=1,
- output_padding=0,
- bias=True),
- nn.BatchNorm2d(16, momentum=0.1),
- nn.ReLU(inplace=True),
- nn.ConvTranspose2d(
- in_channels=16,
- out_channels=16,
- kernel_size=4,
- stride=2,
- padding=1,
- output_padding=0,
- bias=False),
- nn.BatchNorm2d(16, momentum=0.1),
- nn.ReLU(inplace=True),
- nn.ConvTranspose2d(
- in_channels=16,
- out_channels=16,
- kernel_size=4,
- stride=2,
- padding=1,
- output_padding=0,
- bias=False),
- nn.BatchNorm2d(16, momentum=0.1),
- nn.ReLU(inplace=True),
- )
- # 保存为onnx
- torch.onnx.export(model,
- torch.Tensor(1, 3, 512, 512),
- 'test_error2.onnx',
- verbose=True,
- do_constant_folding=False, # 是否执行常量折叠优化
- input_names=["input"], # 输入名
- output_names=["output"], # 输出名
- )
- input_size_list = [[3, 512, 512]]
- # Create RKNN object
- rknn = RKNN(verbose=True)
- print('--> Loading model')
- ret = rknn.load_onnx(model='test_error2.onnx')
- print('--> building model')
- ret = rknn.build(do_quantization=True, dataset='dataset.txt')
- print('--> export model')
- ret = rknn.export_rknn('test_error2.rknn')
- ret = rknn.load_rknn('test_error2.rknn')
- img = cv2.imread('space_shuttle_512.jpg')
- ret = rknn.init_runtime()
- print('--> inference')
- outputs = rknn.inference(inputs=[img])
- print(outputs[0].shape)
|
|