|
本帖最后由 luxiaofei 于 2020-3-12 17:03 编辑
pytorch等版本都是正确的,可以运行官方pytorch转rknn的demo
出错部分的代码:
net = SiamRPN()
net.load_state_dict(torch.load(join(realpath(dirname(__file__)), 'SiamRPNVOT.model')))
net.eval()
net2 = torch.jit.trace(net, (torch.Tensor(1,3,271,271),torch.Tensor(1,3,127,127)))
net2.save('./net2.pt')
model = './net2.pt'
input_size_list = [[3, 271, 271], [3, 127, 127]]
rknn = RKNN()
# pre-process config
print('--> config model')
rknn.config(channel_mean_value='0 0 0 1#0 0 0 1', reorder_channel='0 1 2#0 1 2')
print('done')
# Load pytorch model
print('--> Loading model')
ret = rknn.load_pytorch(model=model, input_size_list=input_size_list)
if ret != 0:
print('Load pytorch model failed!')
exit(ret)
print('done')
# Build model
print('--> Building model')
ret = rknn.build(do_quantization=False)
if ret != 0:
print('Build pytorch failed!')
exit(ret)
print('done')
报错部分的信息:
--> Loading model
/usr/local/lib/python3.6/dist-packages/onnx_tf/common/__init__.py:87: UserWarning: FrontendHandler.get_outputs_names is deprecated. It will be removed in future release.. Use node.outputs instead.
warnings.warn(message)
./net2.pt ********************
WARNING: Token 'COMMENT' defined, but not used
WARNING: There is 1 unused token
Syntax error in input! LexToken(<,'<',5,162)
done
--> Building model
W Genreate input meta fail, please check model.
W External input meta file "/tmp/tmpekkbyfa9/net2_inputmeta.yml" is not exists.
Traceback (most recent call last):
File "/home/xiaofei/PycharmProjects/DaSiam_0311/demo.py", line 43, in <module>
ret = rknn.build(do_quantization=False)
File "/usr/local/lib/python3.6/dist-packages/rknn/api/rknn.py", line 238, in build
ret = self.rknn_base.build(do_quantization=do_quantization, dataset=dataset, pack_vdata=pre_compile, batch_size=rknn_batch_size)
File "rknn/api/rknn_base.py", line 719, in rknn.api.rknn_base.RKNNBase.build
File "rknn/api/rknn_base.py", line 2249, in rknn.api.rknn_base.RKNNBase._generate_inputmeta
IndexError: list index out of range
pytorch模型的定义:
class SiamRPN(nn.Module):
def __init__(self, size=1, feature_out=256, anchor=5):
configs = [3, 96, 256, 384, 384, 256]
configs = list(map(lambda x: 3 if x==3 else x*size, configs))
feat_in = configs[-1]
super(SiamRPN, self).__init__()
self.featureExtract = nn.Sequential(
nn.Conv2d(configs[0], configs[1] , kernel_size=11, stride=2),
nn.BatchNorm2d(configs[1]),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.ReLU(inplace=True),
nn.Conv2d(configs[1], configs[2], kernel_size=5),
nn.BatchNorm2d(configs[2]),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.ReLU(inplace=True),
nn.Conv2d(configs[2], configs[3], kernel_size=3),
nn.BatchNorm2d(configs[3]),
nn.ReLU(inplace=True),
nn.Conv2d(configs[3], configs[4], kernel_size=3),
nn.BatchNorm2d(configs[4]),
nn.ReLU(inplace=True),
nn.Conv2d(configs[4], configs[5], kernel_size=3),
nn.BatchNorm2d(configs[5]),
)#ALexNet
self.anchor = anchor
self.feature_out = feature_out
self.conv_r1 = nn.Conv2d(feat_in, feature_out*4*anchor, 3)
self.conv_r2 = nn.Conv2d(feat_in, feature_out, 3)
self.conv_cls1 = nn.Conv2d(feat_in, feature_out*2*anchor, 3)
self.conv_cls2 = nn.Conv2d(feat_in, feature_out, 3)
self.regress_adjust = nn.Conv2d(4*anchor, 4*anchor, 1)
self.r1_kernel = []
self.cls1_kernel = []
def forward(self, x,z):
z_f = self.featureExtract(z)
r1_kernel_raw = self.conv_r1(z_f)
cls1_kernel_raw = self.conv_cls1(z_f)
kernel_size = r1_kernel_raw.data.size()[-1]
self.r1_kernel = r1_kernel_raw.view(self.anchor*4, self.feature_out, kernel_size, kernel_size)
self.cls1_kernel = cls1_kernel_raw.view(self.anchor*2, self.feature_out, kernel_size, kernel_size)
x_f = self.featureExtract(x)
return self.regress_adjust(F.conv2d(self.conv_r2(x_f), self.r1_kernel)), \
F.conv2d(self.conv_cls2(x_f), self.cls1_kernel)
|
|