torch->onnx->ncnn转置全连接之后h维度变为1

vzgqcmou  于 2个月前  发布在  其他
关注(0)|答案(9)|浏览(27)

我的OCR模型通过卷积得到的BCHW分别为1, 256, 1, 18,之后Permute转置[0, 2, 3, 1]得到BHWC为1, 1, 18, 256,再经过一个256x79的全连接,之后queeze掉H,输出18x79:

转成ncnn之后:

在xcode调试,发现最后一个Permute层的输入Mat,h=1, w=18, c=256, 输出:h=18, w=256, c=1, 乘法BinaryOp之后变为h=1, w=79, c=1, 似乎是跟TensorRT一样不能支持C维度的全连接(要Reshape成 B, HxWxC?),把h消掉了。

请问这是不是我模型优化/转换时的问题呢?或者要怎么修改模型结构/后处理读取数据避免这种问题?

text_rec_sim.onnx.zip

6ovsh4lw

6ovsh4lw1#

可以permute后,加个 reshape h=动态 w=256 二维,把最后的 Squeeze 删掉

Permute          Transpose_140            1 1 322 323 0=3
InnerProduct     MatMul_141               1 1 323 325 0=79 1=0 2=20224
Permute          Transpose_140            1 1 322 323 0=3
Reshape          reshape2d                1 1 323 reshape2d 0=256 1=-1
InnerProduct     MatMul_141               1 1 reshape2d 325 0=79 1=0 2=20224
avkwfej4

avkwfej42#

把最后一个permute以及之后替换为:

Permute          Transpose_140            1 1 322 323 0=3
Reshape          reshape2d                1 1 323 reshape2d 0=256 1=-1
InnerProduct     MatMul_141               1 1 reshape2d 325 0=79 1=0 2=20224
BinaryOp         Add_142                  2 1 325 182 326 0=0

然后看了一下中间的矩阵,InnerProduct的结果是w=79 h=1 c=1, 我的18又不见了:(

au9on6nz

au9on6nz4#

可以了 万分感谢
好像最后用1x1卷积也能顺利预测

pinkon5k

pinkon5k5#

现在还有个问题 全连接MatMul之后得到的w=79 h=18 c=1 然后加入Add操作 Add的输出节点就找不到了

zvms9eto

zvms9eto6#

现在还有个问题 全连接MatMul之后得到的w=79 h=18 c=1 然后加入Add操作 Add的输出节点就找不到了

ncnnoptimize 过一遍就可以啦

i1icjdpr

i1icjdpr7#

用了还是不行呢 find_blob_index_by_name output failed 有空帮我看下呀
text_rec_sim.onnx.zip

oxalkeyp

oxalkeyp8#

你是不是 param bin 文件用错了,比如用了老的文件

b4lqfgs4

b4lqfgs49#

都是最新的,我执行了以下步骤:

  1. python3 -m onnxsim text_rec.onnx text_rec_sim.onnx
  2. ./onnx2ncnn text_rec_sim.onnx lpr1.param lpr1.bin
  3. ../ncnnoptimize lpr1.param lpr1.bin lpr.param lpr.bin 65536
  4. 修改lpr.param最后部分:
Permute          Transpose_206            1 1 436 437 0=3
Reshape          reshape2d                1 1 437 reshape2d 0=256 1=-1
InnerProduct     MatMul_207               1 1 reshape2d output 0=79 1=1 2=20224

提示 find_blob_index_by_name output failed
我的最新模型文件:
text_rec.onnx.zip

相关问题