Tencent / ncnn

ncnn is a high-performance neural network inference framework optimized for the mobile platform
Other
20.14k stars 4.14k forks source link

用ONNX转NCNN,和PNNX转NCNN都遇到问题,记录如下,希望大神帮忙看看 #4218

Open duany049 opened 1 year ago

duany049 commented 1 year ago

error log | 日志或报错信息 | ログ

使用PNNX转换,报错如下: ./pnnx ../../../../../../gnn_tablet/gnn_pnnx.pt inputshape=[9,7],[2,54],[1,3,1472,1024]

pnnxparam = ../../../../../../gnn_tablet/gnn_pnnx.pnnx.param pnnxbin = ../../../../../../gnn_tablet/gnn_pnnx.pnnx.bin pnnxpy = ../../../../../../gnn_tablet/gnn_pnnx_pnnx.py ncnnparam = ../../../../../../gnn_tablet/gnn_pnnx.ncnn.param ncnnbin = ../../../../../../gnn_tablet/gnn_pnnx.ncnn.bin ncnnpy = ../../../../../../gnn_tablet/gnn_pnnx_ncnn.py optlevel = 2 device = cpu inputshape = [9,7]f32,[2,54]f32,[1,3,1472,1024]f32 inputshape2 = customop = moduleop = ############# pass_level0 inline module = GCN_model_lstm_resnet18_nofpn_small_gcn_uselineGAT3_nontext.GCN inline module = GCN_model_lstm_resnet18_nofpn_small_gcn_uselineGAT3_nontext.LstmFPN inline module = torch_geometric.nn.conv.gcn_conv.GCNConv inline module = torch_geometric.nn.dense.linear.Linear inline module = torchvision.models.resnet.BasicBlock inline module = torchvision.ops.roi_pool.RoIPool inline module = GCN_model_lstm_resnet18_nofpn_small_gcn_uselineGAT3_nontext.GCN inline module = GCN_model_lstm_resnet18_nofpn_small_gcn_uselineGAT3_nontext.LstmFPN inline module = torch_geometric.nn.conv.gcn_conv.GCNConv inline module = torch_geometric.nn.dense.linear.Linear inline module = torchvision.models.resnet.BasicBlock inline module = torchvision.ops.roi_pool.RoIPool 140 73 imgh.1 imgw.1 199 200 201 202 211 212 213 214 215 input.5 217 224 225 226 227 228 input.7 230 240 241 242 243 244 247 248 input.9 250 257 258 259 260 261 input.11 263 273 274 275 276 277 280 281 input.13 283 290 291 292 293 294 input.15 296 306 307 308 309 310 313 314 input.2 316 323 324 325 326 327 input.8 329 331 337 344 345 348 349 352 353 356 357 input.3 360 conv60.1 363 364 conv30.1 367 conv40.1 370 conv50.1 374 383 384 385 386 387 388 389 390 31 38 b.7 49 52 54 58 60 63 66 72 imgpos.1 imgpos0.1 grid.1 81 input.6 out1.3 89 91 93 97 99 104 106 111 3946 imgpos1.1 imgpos2.1 grid0.1 out3.1 out10.1 out11.1 out12.1 out30.1 out31.1 out32.1 139 feat_img.1 399 400 401 402 403 404 405 406 408 411 413 414 a.1 b.1 484 bboxes.1 bboxes0.1 487 x1.1 489 y1.1 491 x2.1 493 y2.1 495 x10.1 3995 498 y10.1 3998 501 x20.1 4001 504 y20.1 w.1 h.1 4006 509 510 4008 511 512 4010 513 514 4012 515 516 4014 517 518 4016 519 520 geo_map.1 524 528 dim_each.1 533 pp.1 pp0.1 538 ps.1 pst_encoding.1 542 543 544 545 546 547 548 549 551 bbox_info.1 x.1 bias.2 num_nodes.2 577 edge_attr.2 581 582 mask.2 loop_index.2 585 loop_index0.2 loop_attr.2 inv_mask.2 592 593 595 loop_attr0.2 599 other.2 602 604 edge_index.2 row.2 src.2 index.2 out.2 deg.2 deg0.2 614 deg1.2 617 618 620 edge_weight.2 weight.2 624 index0.2 x_j.2 src0.2 629 other0.2 src1.2 index1.2 634 out0.2 out1.2 639 x0.1 bias.4 num_nodes.4 663 edge_attr.4 667 668 mask.4 loop_index.4 671 loop_index0.4 loop_attr.4 inv_mask.4 678 679 681 loop_attr0.4 685 other.4 688 690 edge_index.4 row.4 src.4 index.4 out.4 deg.4 deg0.4 700 deg1.4 703 704 706 edge_weight.4 weight.4 710 index0.4 x_j.4 src0.4 715 other0.4 src1.4 index1.4 720 out0.4 out1.4 725 x3.1 bias.1 num_nodes.1 749 edge_attr.1 753 754 mask.1 loop_index.1 757 loop_index0.1 loop_attr.1 inv_mask.1 764 765 767 loop_attr0.1 771 other.1 774 776 edge_index.1 row.1 src.1 index.1 out.1 deg.1 deg0.1 786 deg1.1 789 790 792 edge_weight.1 weight.1 796 index0.1 x_j.1 src0.1 801 other0.1 src1.1 index1.1 806 out0.1 out1.1 811 node_feature.1 bboxes_a.1 bboxes_b.1 817 818 merge_rects.1 bboxes_tensor_1.1 822 823 824 825 826 827 828 829 830 831 832 833 bboxes_tensor_2.1 836 837 838 839 840 841 842 843 844 845 846 847 bboxes_tensor_3.1 850 851 852 853 854 855 856 857 858 859 860 861 bboxes_tensor_4.1 864 865 866 867 868 869 870 871 872 873 874 875 bboxes_tensor_10.1 878 879 880 881 882 883 884 885 886 887 888 889 bboxes_tensor_20.1 892 893 894 895 896 897 898 899 900 901 902 903 bboxes_tensor_30.1 906 907 908 909 910 911 912 913 914 915 916 917 bboxes_tensor_40.1 920 921 922 923 924 925 926 927 928 929 930 931 bboxes_tensor_11.1 934 935 936 937 938 939 940 941 942 943 944 945 bboxes_tensor_21.1 948 949 950 951 952 953 954 955 956 957 958 959 bboxes_tensor_31.1 962 963 964 965 966 967 968 969 970 971 972 973 bboxes_tensor_41.1 976 977 978 979 980 981 982 983 984 985 986 987 bboxes_tensor_12.1 990 991 992 993 994 995 996 997 998 999 1000 1001 bboxes_tensor_22.1 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 bboxes_tensor_32.1 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 bboxes_tensor_42.1 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 bboxes_tensor_13.1 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 bboxes_tensor_23.1 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 bboxes_tensor_33.1 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 bboxes_tensor_43.1 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 bboxes_tensor_14.1 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 bboxes_tensor_24.1 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 bboxes_tensor_34.1 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 bboxes_tensor_44.1 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 bboxes_tensor_15.1 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 bboxes_tensor_25.1 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 bboxes_tensor_35.1 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 bboxes_tensor_45.1 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 bboxes_tensor_16.1 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 bboxes_tensor_26.1 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 bboxes_tensor_36.1 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 bboxes_tensor_46.1 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 bboxes_tensor_17.1 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 bboxes_tensor_27.1 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 bboxes_tensor_37.1 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 bboxes_tensor_47.1 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 bboxes_tensor_18.1 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 bboxes_tensor_28.1 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 bboxes_tensor_38.1 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 bboxes_tensor_48.1 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 bboxes_tensor_19.1 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 bboxes_tensor_29.1 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 bboxes_tensor_39.1 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 bboxes_tensor_49.1 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 bboxes_tensor_110.1 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 bboxes_tensor_210.1 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 bboxes_tensor_310.1 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 bboxes_tensor_410.1 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 bboxes_tensor_111.1 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 bboxes_tensor_211.1 1508 1509

terminate called after throwing an instance of 'std::runtime_error' what(): The following operation failed in the TorchScript interpreter. Traceback of TorchScript, serialized code (most recent call last): File "code/torch/GCN_model_lstm_resnet18_nofpn_small_gcn_uselineGAT3_nontext.py", line 207, in forward 65 = torch.copy(_64, _61) bbox_info = torch.to(torch.view(pst_encoding, [_51, -1]), 6) x = torch.cat([bbox_info, feat_img], 1)


    x0 = torch.leaky_relu((conv1).forward(x, links, ))
    x3 = torch.leaky_relu((conv2).forward(x0, links, ))

Traceback of TorchScript, original code (most recent call last):
/dfs/data/workspace/project/gnn_tablet/./GCN_model_lstm_resnet18_nofpn_small_gcn_uselineGAT3_nontext.py(97): forward
/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py(1118): _slow_forward
/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py(1130): _call_impl
/dfs/data/workspace/project/gnn_tablet/./GCN_model_lstm_resnet18_nofpn_small_gcn_uselineGAT3_nontext.py(815): forward
/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py(1118): _slow_forward
/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py(1130): _call_impl
/opt/conda/lib/python3.8/site-packages/torch/jit/_trace.py(967): trace_module
/opt/conda/lib/python3.8/site-packages/torch/jit/_trace.py(750): trace
pnnx.py(74): <module>
RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 9 but got size 0 for tensor number 1 in the list.

Aborted (core dumped)

## model | 模型 | モデル
1. 自定义的模型,里面有torch-geometric.GCNConv, resnet, RoIPool, Conv, leaky_relu, dropout, batch_normal等结构
2. 

## how to reproduce | 复现步骤 | 再現方法
1. 将模型转换为torchScript模型
    # 原始模型能正常运行,验证通过
    m_out = model(rects, links, img) 
    mod = torch.jit.trace(model, example_inputs=[rects, links, img]) 
   # torchScript模型正常运行,验证通过
    mod_out = mod(rects, links, img)
    # 保存模型
    mod.save("gnn_pnnx.pt")
2. ./pnnx  gnn_pnnx.pt inputshape=[9,7],[2,54],[1,3,1472,1024]   # 模型转换报如上错误

## 环境如下:
torch==1.13.0a0+340c412
torch-geometric==2.0.2
ncnn和pnnx都是基于最新代码编译的
duany049 commented 1 year ago

@nihui 麻烦大神们帮忙看看,辛苦了

duany049 commented 1 year ago

4219 使用onnx转ncnn模型,也有问题,存在大量不支持的操作

nihui commented 1 month ago

针对onnx模型转换的各种问题,推荐使用最新的pnnx工具转换到ncnn In view of various problems in onnx model conversion, it is recommended to use the latest pnnx tool to convert your model to ncnn

pip install pnnx
pnnx model.onnx inputshape=[1,3,224,224]

详细参考文档 Detailed reference documentation https://github.com/pnnx/pnnx https://github.com/Tencent/ncnn/wiki/use-ncnn-with-pytorch-or-onnx#how-to-use-pnnx