Closed zczjx closed 5 years ago
torch.squeeze() 在 MNN中是可以支持的
用 view 替代 torch.squeeze
用 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)
转换的结果: ../tools/onnx2ncnn ./cnn_lenet_torch_view.onnx Shape not supported yet! Gather not supported yet! '# axis=0' Shape not supported yet! Gather not supported yet! '# axis=0' Unsqueeze not supported yet! '# axes 7' Unsqueeze not supported yet! '# axes 7'
运行结果
./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)
view的问题需要再开一个 issue吗?
用view,然后用 onnx-simplifer 将这些 squeeze gather 什么什么的自动去掉 https://github.com/Tencent/ncnn/wiki/use-ncnn-with-pytorch-or-onnx#simplify-onnx-model
用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
ncnn 有没有计划之后支持 torch.squeeze() 的feature呢?如果以后支持的话,这个issue先保留在这里吧
针对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
用途: pytorch 需要用1x1卷积层替代全连接dense layer 其中Conv2d的输出需要用Squeeze做dim reshape转换
log: $ ../tools/onnx2ncnn ./cnn_cov_dense_lenet_torch.onnx Squeeze not supported yet! # axes 7
pytorch Squeeze 介绍: https://pytorch.org/docs/stable/torch.html#indexing-slicing-joining-mutating-ops torch.squeeze(input, dim=None, out=None) → Tensor Returns a tensor with all the dimensions of input of size 1 removed.
参考pytorch代码: ` class lenet(nn.Module): def init(self): super(lenet, self).init() self.conv = nn.Sequential(nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2), nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2), nn.Conv2d(in_channels=16, out_channels=256, kernel_size=4), nn.ReLU(), nn.Conv2d(in_channels=256, out_channels=120, kernel_size=1), nn.ReLU(), nn.Conv2d(in_channels=120, out_channels=84, kernel_size=1), nn.ReLU(), nn.Conv2d(in_channels=84, out_channels=10, kernel_size=1))
def forward(self, img): feature = self.conv(img) return torch.squeeze(feature) `