{"id":1168,"date":"2025-07-29T09:21:29","date_gmt":"2025-07-29T01:21:29","guid":{"rendered":"https:\/\/www.hgfwq.com\/blog\/?p=1168"},"modified":"2025-07-29T09:21:29","modified_gmt":"2025-07-29T01:21:29","slug":"%e5%a6%82%e4%bd%95%e5%ae%89%e8%a3%85%e5%92%8c%e4%bd%bf%e7%94%a8onnx%e4%bb%a5%e6%8f%90%e5%8d%87%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0%e6%a8%a1%e5%9e%8b%e4%ba%a4%e4%ba%92%e6%80%a7","status":"publish","type":"post","link":"https:\/\/www.hgfwq.com\/blog\/1168.html","title":{"rendered":"\u5982\u4f55\u5b89\u88c5\u548c\u4f7f\u7528ONNX\u4ee5\u63d0\u5347\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u4ea4\u4e92\u6027"},"content":{"rendered":"<p><h3>\u4ec0\u4e48\u662fONNX\uff1f<\/h3>\n<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.hgfwq.com\/blog\/wp-content\/uploads\/2025\/07\/NG72i.jpg\" alt=\"\u5982\u4f55\u5b89\u88c5\u548c\u4f7f\u7528ONNX\u4ee5\u63d0\u5347\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u4ea4\u4e92\u6027\" title=\"\u5982\u4f55\u5b89\u88c5\u548c\u4f7f\u7528ONNX\u4ee5\u63d0\u5347\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u4ea4\u4e92\u6027\"><\/p>\n<p><p>ONNX\uff0c\u5373Open Neural Network Exchange\uff0c\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u6a21\u578b\u683c\u5f0f\uff0c\u65e8\u5728\u89e3\u51b3\u4e0d\u540c\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u4e4b\u95f4\u6a21\u578b\u8f6c\u6362\u7684\u95ee\u9898\u3002\u5b83\u80fd\u591f\u4f7f\u6a21\u578b\u5728\u4e0d\u540c\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u548c\u5de5\u5177\u4e4b\u95f4\u65e0\u7f1d\u8fc1\u79fb\uff0c\u63d0\u9ad8\u4e86\u6a21\u578b\u7684\u517c\u5bb9\u6027\u548c\u4e92\u64cd\u4f5c\u6027\u3002<\/p>\n<\/p>\n<p><h3>ONNX\u5305\u542b\u54ea\u4e9b\u5185\u5bb9\uff1f<\/h3>\n<\/p>\n<p><p>ONNX\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\u548c\u4e00\u7ec4\u64cd\u4f5c\uff0c\u7528\u4e8e\u63cf\u8ff0\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u3002\u5b83\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u5173\u952e\u7ec4\u6210\u90e8\u5206\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><p>Graph\uff1a\u63cf\u8ff0\u6a21\u578b\u7ed3\u6784\u7684\u56fe\uff0c\u5305\u542b\u591a\u4e2a\u8282\u70b9\u548c\u8fb9\uff0c\u8282\u70b9\u4ee3\u8868\u64cd\u4f5c\uff0c\u8fb9\u4ee3\u8868\u6570\u636e\u6d41\u3002<\/p>\n<\/li>\n<li>\n<p><p>TensorProto\uff1a\u63cf\u8ff0\u6570\u636e\u7684\u57fa\u672c\u7c7b\u578b\u548c\u5c5e\u6027\uff0c\u5982\u5f62\u72b6\u3001\u6570\u636e\u7c7b\u578b\u7b49\u3002<\/p>\n<\/li>\n<li>\n<p><p>OperatorProto\uff1a\u5b9a\u4e49\u4e86ONNX\u652f\u6301\u7684\u5404\u7c7b\u64cd\u4f5c\uff0c\u5982\u5377\u79ef\u3001\u6c60\u5316\u3001\u5f52\u4e00\u5316\u7b49\u3002<\/p>\n<\/li>\n<li>\n<p><p>AttributeProto\uff1a\u5b9a\u4e49\u4e86\u64cd\u4f5c\u8282\u70b9\u7684\u5c5e\u6027\uff0c\u5982\u5377\u79ef\u5c42\u7684\u5377\u79ef\u6838\u5927\u5c0f\u3001\u6b65\u957f\u7b49\u3002<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u5982\u4f55\u4f7f\u7528ONNX\uff1f<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528ONNX\u4e3b\u8981\u5206\u4e3a\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><p>\u6784\u5efaONNX\u6a21\u578b\uff1a\u4f7f\u7528\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff08\u5982TensorFlow\u3001PyTorch\u7b49\uff09\u8bad\u7ec3\u6a21\u578b\u540e\uff0c\u5c06\u6a21\u578b\u8f6c\u6362\u4e3aONNX\u683c\u5f0f\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u5bfc\u51faONNX\u6a21\u578b\uff1a\u5c06ONNX\u6a21\u578b\u5bfc\u51fa\u4e3a\u6587\u4ef6\uff0c\u4fbf\u4e8e\u5728\u5176\u4ed6\u6846\u67b6\u548c\u5de5\u5177\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u52a0\u8f7dONNX\u6a21\u578b\uff1a\u5728\u76ee\u6807\u6846\u67b6\u548c\u5de5\u5177\u4e2d\u52a0\u8f7dONNX\u6a21\u578b\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u63a8\u7406\uff1a\u4f7f\u7528\u52a0\u8f7d\u7684ONNX\u6a21\u578b\u8fdb\u884c\u63a8\u7406\uff0c\u83b7\u53d6\u6a21\u578b\u8f93\u51fa\u3002<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u63a8\u8350\u4f7f\u7528ONNX\u7684\u539f\u56e0\uff1f<\/h3>\n<\/p>\n<p><p>\u63a8\u8350\u4f7f\u7528ONNX\u7684\u539f\u56e0\u6709\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><p>\u63d0\u9ad8\u6a21\u578b\u4e92\u64cd\u4f5c\u6027\uff1aONNX\u652f\u6301\u591a\u79cd\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff0c\u4fbf\u4e8e\u6a21\u578b\u5728\u4e0d\u540c\u6846\u67b6\u4e4b\u95f4\u8fc1\u79fb\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u8de8\u5e73\u53f0\u90e8\u7f72\uff1aONNX\u6a21\u578b\u53ef\u4ee5\u5728\u591a\u4e2a\u5e73\u53f0\u548c\u8bbe\u5907\u4e0a\u8fd0\u884c\uff0c\u5982CPU\u3001GPU\u3001\u79fb\u52a8\u7aef\u7b49\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u6613\u4e8e\u8c03\u8bd5\u548c\u4f18\u5316\uff1aONNX\u63d0\u4f9b\u6e05\u6670\u7684\u6a21\u578b\u7ed3\u6784\u63cf\u8ff0\uff0c\u4fbf\u4e8e\u8c03\u8bd5\u548c\u4f18\u5316\u6a21\u578b\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u793e\u533a\u652f\u6301\uff1aONNX\u62e5\u6709\u6d3b\u8dc3\u7684\u5f00\u6e90\u793e\u533a\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6587\u6863\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/li>\n<\/ul>\n<p><h3>ONNX\u7684\u9002\u7528\u573a\u666f\uff1f<\/h3>\n<\/p>\n<p><p>ONNX\u9002\u7528\u4e8e\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><p>\u6a21\u578b\u8f6c\u6362\uff1a\u5c06\u6a21\u578b\u4ece\u4e00\u79cd\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u8f6c\u6362\u4e3aONNX\u683c\u5f0f\uff0c\u4fbf\u4e8e\u5728\u4e0d\u540c\u6846\u67b6\u95f4\u8fc1\u79fb\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u6a21\u578b\u90e8\u7f72\uff1a\u5c06ONNX\u6a21\u578b\u90e8\u7f72\u5230\u4e0d\u540c\u5e73\u53f0\u548c\u8bbe\u5907\uff0c\u5982\u79fb\u52a8\u7aef\u3001\u8fb9\u7f18\u8ba1\u7b97\u7b49\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u6a21\u578b\u4f18\u5316\uff1a\u5229\u7528ONNX\u8fdb\u884c\u6a21\u578b\u4f18\u5316\uff0c\u5982\u6a21\u578b\u538b\u7f29\u3001\u91cf\u5316\u7b49\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u6a21\u578b\u5206\u6790\uff1a\u901a\u8fc7ONNX\u5206\u6790\u6a21\u578b\u7ed3\u6784\uff0c\u4e86\u89e3\u6a21\u578b\u5185\u90e8\u4fe1\u606f\u3002<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u5982\u4f55\u5c06TensorFlow\u6a21\u578b\u8f6c\u6362\u4e3aONNX\uff1f<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5c06TensorFlow\u6a21\u578b\u8f6c\u6362\u4e3aONNX\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><p>\u5b89\u88c5TensorFlow\u548cONNX\u8f6c\u6362\u5de5\u5177\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u5bfc\u5165TensorFlow\u6a21\u578b\u5e76\u8bad\u7ec3\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u4f7f\u7528`tf2onnx`\u5de5\u5177\u5c06TensorFlow\u6a21\u578b\u8f6c\u6362\u4e3aONNX\u683c\u5f0f\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u68c0\u67e5\u8f6c\u6362\u540e\u7684ONNX\u6a21\u578b\u662f\u5426\u6b63\u786e\u3002<\/p>\n<\/li>\n<\/ol>\n<p><pre><code>pip install tf2onnx<\/p>\r\n<p>python -m tf2onnx.convert --input tf_model.pb --output onnx_model.onnx --input-fmt tensorflow --output-fmt onnx<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5982\u4f55\u5c06PyTorch\u6a21\u578b\u8f6c\u6362\u4e3aONNX\uff1f<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5c06PyTorch\u6a21\u578b\u8f6c\u6362\u4e3aONNX\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><p>\u5b89\u88c5PyTorch\u548cONNX\u8f6c\u6362\u5de5\u5177\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u5b9a\u4e49PyTorch\u6a21\u578b\u5e76\u8bad\u7ec3\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u4f7f\u7528`torch.onnx.export`\u51fd\u6570\u5c06PyTorch\u6a21\u578b\u8f6c\u6362\u4e3aONNX\u683c\u5f0f\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u68c0\u67e5\u8f6c\u6362\u540e\u7684ONNX\u6a21\u578b\u662f\u5426\u6b63\u786e\u3002<\/p>\n<\/li>\n<\/ol>\n<p><pre><code>import torch<\/p>\r\n<p>import onnx<\/p>\r\n<p>import onnxruntime as ort<\/p>\r\n\r\n<p> \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6a21\u578b<\/p>\r\n<p>model = torch.nn.Linear(2, 2)<\/p>\r\n<p>dummy_input = torch.randn(1, 2)<\/p>\r\n\r\n<p> \u5c06\u6a21\u578b\u8f6c\u6362\u4e3aONNX\u683c\u5f0f<\/p>\r\n<p>torch.onnx.export(model, dummy_input, \"model.onnx\")<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5982\u4f55\u4f7f\u7528ONNX\u6a21\u578b\u8fdb\u884c\u63a8\u7406\uff1f<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5728Python\u4e2d\u4f7f\u7528ONNX\u6a21\u578b\u8fdb\u884c\u63a8\u7406\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><p>\u5b89\u88c5ONNX\u8fd0\u884c\u65f6\u5e93\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u52a0\u8f7dONNX\u6a21\u578b\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u521b\u5efa\u8f93\u5165\u6570\u636e\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u6267\u884c\u63a8\u7406\u5e76\u83b7\u53d6\u8f93\u51fa\u3002<\/p>\n<\/li>\n<\/ol>\n<p><pre><code>import onnx<\/p>\r\n<p>import onnxruntime as ort<\/p>\r\n\r\n<p> \u52a0\u8f7dONNX\u6a21\u578b<\/p>\r\n<p>session = ort.InferenceSession(\"model.onnx\")<\/p>\r\n\r\n<p> \u521b\u5efa\u8f93\u5165\u6570\u636e<\/p>\r\n<p>input_data = {<\/p>\r\n<p>    \"input\": np.random.randn(1, 2)<\/p>\r\n<p>}<\/p>\r\n\r\n<p> \u6267\u884c\u63a8\u7406<\/p>\r\n<p>output = session.run(None, input_data)<\/p>\r\n\r\n<p>print(output)<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><b>\u5982\u4f55\u786e\u4fddONNX\u6a21\u578b\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u4e0d\u4e22\u5931\u7cbe\u5ea6\uff1f<\/b><\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fddONNX\u6a21\u578b\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u4e0d\u4e22\u5931\u7cbe\u5ea6\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u63aa\u65bd\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><p>\u9009\u62e9\u5408\u9002\u7684ONNX\u7248\u672c\u548c\u8f6c\u6362\u5de5\u5177\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u68c0\u67e5\u6a21\u578b\u7ed3\u6784\u548c\u53c2\u6570\uff0c\u786e\u4fdd\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u6ca1\u6709\u9519\u8bef\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u5728\u8f6c\u6362\u524d\u8fdb\u884c\u6a21\u578b\u9a8c\u8bc1\uff0c\u786e\u4fdd\u6a21\u578b\u5728\u539f\u59cb\u6846\u67b6\u4e2d\u7684\u7cbe\u5ea6\u3002<\/p>\n<\/li>\n<\/ul>\n<p><b>ONNX\u652f\u6301\u54ea\u4e9b\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff1f<\/b><\/p>\n<p><p>ONNX\u652f\u6301\u4ee5\u4e0b\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><p>TensorFlow<\/p>\n<\/li>\n<li>\n<p><p>PyTorch<\/p>\n<\/li>\n<li>\n<p><p>Keras<\/p>\n<\/li>\n<li>\n<p><p>MXNet<\/p>\n<\/li>\n<li>\n<p><p>Microsoft Cognitive Toolkit (CNTK)<\/p>\n<\/li>\n<\/ul>\n<p><b>\u5982\u4f55\u5904\u7406ONNX\u6a21\u578b\u4e2d\u7684\u4e0d\u786e\u5b9a\u64cd\u4f5c\uff1f<\/b><\/p>\n<p><p>\u5728\u5904\u7406ONNX\u6a21\u578b\u4e2d\u7684\u4e0d\u786e\u5b9a\u64cd\u4f5c\u65f6\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><p>\u624b\u52a8\u4fee\u6539\u6a21\u578b\uff0c\u5c06\u4e0d\u786e\u5b9a\u64cd\u4f5c\u66ff\u6362\u4e3a\u786e\u5b9a\u7684\u64cd\u4f5c\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u4f7f\u7528ONNX\u7684`onnxsim`\u5de5\u5177\u5bf9\u6a21\u578b\u8fdb\u884c\u7b80\u5316\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u67e5\u9605\u76f8\u5173\u8d44\u6599\u548c\u793e\u533a\u8ba8\u8bba\uff0c\u5bfb\u627e\u5904\u7406\u4e0d\u786e\u5b9a\u64cd\u4f5c\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662fONNX\uff1f ONNX\uff0c\u5373Open Neural Network Exchange\uff0c\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u6a21\u578b\u683c\u5f0f\uff0c\u65e8\u5728\u89e3\u51b3\u4e0d\u540c\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u4e4b\u95f4\u6a21\u578b\u8f6c\u6362\u7684\u95ee\u9898\u3002\u5b83\u80fd\u591f\u4f7f\u6a21\u578b\u5728\u4e0d\u540c\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u548c\u5de5\u5177\u4e4b\u95f4\u65e0\u7f1d\u8fc1\u79fb\uff0c\u63d0\u9ad8\u4e86\u6a21\u578b\u7684\u517c\u5bb9\u6027\u548c\u4e92\u64cd\u4f5c\u6027\u3002 O&#8230;<\/p>\n","protected":false},"author":1,"featured_media":1169,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[468,469,310],"topic":[],"class_list":["post-1168","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-haiwai","tag-onnx","tag-469","tag-310"],"_links":{"self":[{"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/posts\/1168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/comments?post=1168"}],"version-history":[{"count":1,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/posts\/1168\/revisions"}],"predecessor-version":[{"id":1171,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/posts\/1168\/revisions\/1171"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/media\/1169"}],"wp:attachment":[{"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/media?parent=1168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/categories?post=1168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/tags?post=1168"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/topic?post=1168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}