Elffuss BraidHub

kikocis/ncnn public

master · 2.2 KiBRawPermalink
1# Tencent is pleased to support the open source community by making ncnn available.
2#
3# Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
4#
5# Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6# in compliance with the License. You may obtain a copy of the License at
7#
8# https://opensource.org/licenses/BSD-3-Clause
9#
10# Unless required by applicable law or agreed to in writing, software distributed
11# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12# CONDITIONS OF ANY KIND, either express or implied. See the License for the
13# specific language governing permissions and limitations under the License.
14
15import torch
16import torch.nn as nn
17import torch.nn.functional as F
18from packaging import version
19
20class Model(nn.Module):
21 def __init__(self):
22 super(Model, self).__init__()
23
24 def forward(self, x, y, w):
25 if version.parse(torch.__version__) < version.parse('1.12'):
26 x = F.upsample_nearest(x, size=(72,128))
27 x = F.upsample_nearest(x, scale_factor=2)
28
29 y = F.upsample_nearest(y, size=(20,72,96))
30 y = F.upsample_nearest(y, scale_factor=3)
31 else:
32 x = F.upsample_nearest(x, size=(12,12))
33 x = F.upsample_nearest(x, scale_factor=2)
34
35 y = F.upsample_nearest(y, size=(8,10,9))
36 y = F.upsample_nearest(y, scale_factor=3)
37
38 w = F.upsample_nearest(w, scale_factor=(2.976744,2.976744))
39 return x, y, w
40
41def test():
42 net = Model()
43 net.eval()
44
45 torch.manual_seed(0)
46 x = torch.rand(1, 12, 24, 64)
47 y = torch.rand(1, 4, 10, 24, 32)
48 w = torch.rand(1, 8, 86, 86)
49
50 a = net(x, y, w)
51
52 # export onnx
53 torch.onnx.export(net, (x, y, w), "test_F_upsample_nearest.onnx")
54
55 # onnx to pnnx
56 import os
57 os.system("../../src/pnnx test_F_upsample_nearest.onnx inputshape=[1,12,24,64],[1,4,10,24,32],[1,8,86,86]")
58
59 # pnnx inference
60 import test_F_upsample_nearest_pnnx
61 b = test_F_upsample_nearest_pnnx.test_inference()
62
63 for a0, b0 in zip(a, b):
64 if not torch.equal(a0, b0):
65 return False
66 return True
67
68if __name__ == "__main__":
69 if test():
70 exit(0)
71 else:
72 exit(1)