From 30f7e74383548ba0a464a2822c40a6075d4a4222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=AD=E4=B8=9A=E5=BA=86?= Date: Wed, 18 Aug 2021 03:07:49 +0000 Subject: [PATCH 1/2] rename --- ...16\347\232\204\351\227\256\351\242\230.md" | 55 ++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) rename "Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\346\200\247\350\203\275\350\260\203\344\274\230/\344\274\230\345\214\226transpose\345\210\260\346\234\200\345\220\216\344\270\200\346\240\271\350\275\264\345\201\232softmax\346\200\247\350\203\275\344\275\216\347\232\204\351\227\256\351\242\230.md" => "Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\346\200\247\350\203\275\350\260\203\344\274\230/\344\274\230\345\214\226\345\234\250\346\234\200\345\220\216\344\270\200\346\240\271\350\275\264\345\201\232softmax\346\200\247\350\203\275\344\275\216\347\232\204\351\227\256\351\242\230.md" (39%) diff --git "a/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\346\200\247\350\203\275\350\260\203\344\274\230/\344\274\230\345\214\226transpose\345\210\260\346\234\200\345\220\216\344\270\200\346\240\271\350\275\264\345\201\232softmax\346\200\247\350\203\275\344\275\216\347\232\204\351\227\256\351\242\230.md" "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\346\200\247\350\203\275\350\260\203\344\274\230/\344\274\230\345\214\226\345\234\250\346\234\200\345\220\216\344\270\200\346\240\271\350\275\264\345\201\232softmax\346\200\247\350\203\275\344\275\216\347\232\204\351\227\256\351\242\230.md" similarity index 39% rename from "Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\346\200\247\350\203\275\350\260\203\344\274\230/\344\274\230\345\214\226transpose\345\210\260\346\234\200\345\220\216\344\270\200\346\240\271\350\275\264\345\201\232softmax\346\200\247\350\203\275\344\275\216\347\232\204\351\227\256\351\242\230.md" rename to "Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\346\200\247\350\203\275\350\260\203\344\274\230/\344\274\230\345\214\226\345\234\250\346\234\200\345\220\216\344\270\200\346\240\271\350\275\264\345\201\232softmax\346\200\247\350\203\275\344\275\216\347\232\204\351\227\256\351\242\230.md" index 233c595..be095bd 100644 --- "a/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\346\200\247\350\203\275\350\260\203\344\274\230/\344\274\230\345\214\226transpose\345\210\260\346\234\200\345\220\216\344\270\200\346\240\271\350\275\264\345\201\232softmax\346\200\247\350\203\275\344\275\216\347\232\204\351\227\256\351\242\230.md" +++ "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\346\200\247\350\203\275\350\260\203\344\274\230/\344\274\230\345\214\226\345\234\250\346\234\200\345\220\216\344\270\200\346\240\271\350\275\264\345\201\232softmax\346\200\247\350\203\275\344\275\216\347\232\204\351\227\256\351\242\230.md" @@ -1,3 +1,4 @@ +1.transpose到最后一根轴做softmax的场景: ``` import sys import onnx @@ -23,4 +24,56 @@ if __name__ == '__main__': onnx.save(model, sys.argv[2]) ``` python3 fix_softmax_transpose.py ./wideresnet_dybs.onnx ./wideresnet_dybs_fix.onnx -导出onnx时如果softmax不是在最后一根轴上计算时会通过transpose转换到最后一根轴计算,然而softmax在最后一根轴上计算时性能低,故修改onnx使softmax在原轴上计算。 \ No newline at end of file +导出onnx时如果softmax不是在最后一根轴上计算时会通过transpose转换到最后一根轴计算,然而softmax在最后一根轴上计算时性能低,故修改onnx使softmax在原轴上计算。 +2.softmax在最后一根轴计算的场景: +``` +import sys +import onnx +from onnx import helper, optimizer + +input_model=sys.argv[1] +output_model=sys.argv[2] +model=onnx.load(input_model) +onnx.checker.check_model(model) +model_nodes=model.graph.node + +def getNodeByName(nodes, name: str): + for n in nodes: + if n.name == name: + return n + return -1 + +Transpose_before = onnx.helper.make_node( + 'Transpose', + name='Transpose_before_softmax', + inputs=['272'], + outputs=['before_softmax'], + perm=[2,0,1] +) + +soft_max_new = onnx.helper.make_node( + 'Softmax', + name='Softmax_150', + inputs=['before_softmax'], + outputs=['after_softmax'], + axis=0 +) + +Transpose_after = onnx.helper.make_node( + 'Transpose', + name='Transpose_after_softmax', + inputs=["after_softmax"], + outputs=['scores'], + perm=[1,2,0] +) + +model_nodes.remove(getNodeByName(model_nodes, 'Softmax_150')) +model_nodes.append(Transpose_before) +model_nodes.append(soft_max_new) +model_nodes.append(Transpose_after) + +print("vgg16-ssd softmax node adapted") +onnx.save(model, output_model) +``` +python3 fix_softmax_transpose.py ./vgg16-ssd_dybs.onnx ./vgg16-ssd_dybs_fix.onnx +softmax在最后一根轴上计算时性能低,故修改onnx,先transpose最后一根轴到第一根轴,使softmax在第一根轴上计算,然后再transpose恢复 \ No newline at end of file -- Gitee From dc7d418cf157489074ec322a3feb9f3dbf1cad7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=AD=E4=B8=9A=E5=BA=86?= Date: Wed, 18 Aug 2021 03:08:21 +0000 Subject: [PATCH 2/2] =?UTF-8?q?update=20Ascend-PyTorch=E7=A6=BB=E7=BA=BF?= =?UTF-8?q?=E6=8E=A8=E7=90=86=E6=8C=87=E5=AF=BC/=E4=B8=93=E9=A2=98?= =?UTF-8?q?=E6=A1=88=E4=BE=8B/=E6=80=A7=E8=83=BD=E8=B0=83=E4=BC=98/?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=9C=A8=E6=9C=80=E5=90=8E=E4=B8=80=E6=A0=B9?= =?UTF-8?q?=E8=BD=B4=E5=81=9Asoftmax=E6=80=A7=E8=83=BD=E4=BD=8E=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\203\275\344\275\216\347\232\204\351\227\256\351\242\230.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\346\200\247\350\203\275\350\260\203\344\274\230/\344\274\230\345\214\226\345\234\250\346\234\200\345\220\216\344\270\200\346\240\271\350\275\264\345\201\232softmax\346\200\247\350\203\275\344\275\216\347\232\204\351\227\256\351\242\230.md" "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\346\200\247\350\203\275\350\260\203\344\274\230/\344\274\230\345\214\226\345\234\250\346\234\200\345\220\216\344\270\200\346\240\271\350\275\264\345\201\232softmax\346\200\247\350\203\275\344\275\216\347\232\204\351\227\256\351\242\230.md" index be095bd..92fba27 100644 --- "a/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\346\200\247\350\203\275\350\260\203\344\274\230/\344\274\230\345\214\226\345\234\250\346\234\200\345\220\216\344\270\200\346\240\271\350\275\264\345\201\232softmax\346\200\247\350\203\275\344\275\216\347\232\204\351\227\256\351\242\230.md" +++ "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\346\200\247\350\203\275\350\260\203\344\274\230/\344\274\230\345\214\226\345\234\250\346\234\200\345\220\216\344\270\200\346\240\271\350\275\264\345\201\232softmax\346\200\247\350\203\275\344\275\216\347\232\204\351\227\256\351\242\230.md" @@ -24,7 +24,7 @@ if __name__ == '__main__': onnx.save(model, sys.argv[2]) ``` python3 fix_softmax_transpose.py ./wideresnet_dybs.onnx ./wideresnet_dybs_fix.onnx -导出onnx时如果softmax不是在最后一根轴上计算时会通过transpose转换到最后一根轴计算,然而softmax在最后一根轴上计算时性能低,故修改onnx使softmax在原轴上计算。 +导出onnx时如果softmax不是在最后一根轴上计算时会通过transpose转换到最后一根轴计算,然而softmax在最后一根轴上计算时性能低,故修改onnx使softmax在原轴上计算。 2.softmax在最后一根轴计算的场景: ``` import sys -- Gitee