Tencent / ncnn

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

onnx2ncnn 不支持pytorch的Squeeze算子 #1298

Closed zczjx closed 5 years ago

zczjx commented 5 years ago
zczjx commented 5 years ago

torch.squeeze() 在 MNN中是可以支持的

nihui commented 5 years ago

用 view 替代 torch.squeeze

zczjx commented 5 years ago

用 view 替代 torch.squeeze 我之前就是用的view, 在TensorRT, MNN上都有问题,才换成squeeze, squeeze在TensorRT和MNN上都可以正常运行,ncnn不工作

  • 代码如下 def forward(self, img): feature = self.conv(img) return feature.view((feature.shape[0], feature.shape[1]), -1)

./ncnn_mnist_classify ./t10k-images-idx3-ubyte ./t10k-labels-idx1-ubyte ./ncnn.bin ./ncnn.param loading mnist data...... dataset_magic: 0x00000803 num_dataset: 0x00002710 height: 0x0000001c width: 0x0000001c labels_magic: 0x00000801 num_labels: 0x00002710 img_size: 784 finish loading 10000 items

layer Shape not exists or registered Segmentation fault (core dumped)

zczjx commented 5 years ago

view的问题需要再开一个 issue吗?

nihui commented 5 years ago

用view,然后用 onnx-simplifer 将这些 squeeze gather 什么什么的自动去掉 https://github.com/Tencent/ncnn/wiki/use-ncnn-with-pytorch-or-onnx#simplify-onnx-model

zczjx commented 5 years ago

用view,然后用 onnx-simplifer 将这些 squeeze gather 什么什么的自动去掉 https://github.com/Tencent/ncnn/wiki/use-ncnn-with-pytorch-or-onnx#simplify-onnx-model

我之前试过这个方法解决view,有些网络用simplifier可以解决,但是还有一些会有一些小bug, 精度跑飞,因为onnx-simplifier 这个项目我看更新迭代的也不快,投入的资源也不多,估计很多小bug都没有精力去fix,所以作为研究学习用这个办法去解决还可以,但是如果实际产品部署,估计onnx-simplifier会不靠谱

之前我和 https://github.com/xindongzhang 在知乎上讨论过这个问题( 见评论区https://zhuanlan.zhihu.com/p/76605363 ),考虑到还有TensorRT和MNN的兼容性,最好还是用Conv2d 1x1的方法替代全连接 Dense Layer, 避免使用view

zczjx commented 5 years ago

ncnn 有没有计划之后支持 torch.squeeze() 的feature呢?如果以后支持的话,这个issue先保留在这里吧

nihui commented 5 years ago

转换squeeze https://github.com/Tencent/ncnn/commit/e8bb88830dc9629be77bba23a4e7651500d1bbe0

zczjx commented 5 years ago

转换squeeze e8bb888

大神牛逼

nihui commented 2 months 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