{"id":48,"date":"2023-12-20T11:32:51","date_gmt":"2023-12-20T03:32:51","guid":{"rendered":"http:\/\/blog.thirdbody.cn\/?p=48"},"modified":"2023-12-22T15:12:57","modified_gmt":"2023-12-22T07:12:57","slug":"%e5%a6%82%e4%bd%95%e5%b0%86pytorch%e6%a8%a1%e5%9e%8b%e8%bd%ac%e6%8d%a2%e5%88%b0open-neural-network-exchangeonnx%ef%bc%88%e4%b8%80%ef%bc%89","status":"publish","type":"post","link":"http:\/\/blog.thirdbody.cn\/index.php\/2023\/12\/20\/%e5%a6%82%e4%bd%95%e5%b0%86pytorch%e6%a8%a1%e5%9e%8b%e8%bd%ac%e6%8d%a2%e5%88%b0open-neural-network-exchangeonnx%ef%bc%88%e4%b8%80%ef%bc%89\/","title":{"rendered":"\u5982\u4f55\u5c06PyTorch\u6a21\u578b\u8f6c\u6362\u5230Open Neural Network Exchange(ONNX)\uff08\u4e00\uff09"},"content":{"rendered":"<h3>1. \u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u90e8\u7f72\u7684\u96be\u70b9<\/h3>\n<ul>\n<li>\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u57fa\u4e8ePyTorch\/TensorFlow\u7b49\u6846\u67b6\u90e8\u7f72\uff0c\u8fd9\u4e9b\u4f9d\u8d56\u73af\u5883\u4e0d\u9002\u5408\u5728\u79fb\u52a8\u8bbe\u5907\u6216\u5f00\u53d1\u677f\u4e2d\u5b89\u88c5\uff1b<\/li>\n<li>\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u4e00\u822c\u6bd4\u8f83\u5e9e\u5927\uff0c\u9700\u8981\u5f3a\u5927\u7684\u7b97\u529b\u652f\u6301\uff0c\u5728\u79fb\u52a8\u8bbe\u5907\u4e0a\u8fdb\u884c\u63a8\u7406\u65f6\uff0c\u9700\u8981\u4f18\u5316\u6a21\u578b\u6548\u7387\uff0c\u4ee5\u8fbe\u5230\u5b9e\u65f6\u8fd0\u884c\u7684\u76ee\u7684<\/li>\n<\/ul>\n<p>\u200b        \u4ee5\u4e0a\u4e24\u4e2a\u96be\u70b9\u4f7f\u5f97\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u96be\u4ee5\u5728\u79fb\u52a8\u7aef\u8bbe\u5907\u6216\u5f00\u53d1\u677f\u4e0a\u76f4\u63a5\u8fd0\u884c\uff0c\u4e3a\u4e86\u80fd\u591f\u6d88\u9664\u4e0d\u540c\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u7684\u5dee\u5f02\uff0c\u4ee5\u51cf\u5c11\u4f18\u5316\u6a21\u578b\u6548\u7387\u7684\u5de5\u4f5c\u91cf\uff0c\u81ea\u7136\u800c\u7136\u60f3\u5230\u80fd\u5426\u5c06\u4e0d\u540c\u6846\u67b6\u3001\u4e0d\u540c\u683c\u5f0f\u4e4b\u95f4\u7684\u6a21\u578b\u8f6c\u6362\u6210\u7b26\u5408\u9884\u5b9a\u4e49\u6807\u51c6\u7684\u4e2d\u95f4\u683c\u5f0f\uff0c\u5c06\u4e2d\u95f4\u8868\u793a\u8f93\u5165\u5230\u5bf9\u5e94\u786c\u4ef6\u5e73\u53f0\u7684\u63a8\u7406\u5f15\u64ce\u4e0a\uff0c\u8fbe\u5230\u4f18\u5316\u63a8\u7406\u6548\u7387\u7684\u76ee\u7684\uff0cONNX(Open Neural Network Exchange)\u5c31\u662f\u4e00\u79cd\u4e3b\u6d41\u7684\u4e2d\u95f4\u683c\u5f0f\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/onnx0.png\" alt=\"\" \/><\/p>\n<p>\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5c06\u4e00\u4e2aPyTorch\u6a21\u578b\u8f6c\u6362\u6210ONNX\u683c\u5f0f\uff0c\u5e76\u4f7f\u7528Python\u7b2c\u4e09\u65b9\u5305<code>onnxruntime<\/code>\u5bf9\u8f6c\u6362\u540e\u7684ONNX\u6a21\u578b\u8fdb\u884c\u63a8\u7406\u3002<\/p>\n<h3>2. \u4ecePyTorch\u5230ONNX<\/h3>\n<ol>\n<li>\n<p>\u9996\u5148\u4f7f\u7528PyTorch\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u7ebf\u6027\u6a21\u578b\u5982\u4e0b:<\/p>\n<pre><code class=\"language-python\">import torch\nimport torch.nn as nn\nclass LinearModel(nn.Module):\n   def __init__(self, ndim):\n       super(LinearModel, self).__init__()\n       self.ndim = ndim\n       self.weights = nn.Parameter(torch.randn(ndim, 1, dtype=torch.float32))\n       self.bias = nn.Parameter(torch.randn(1, dtype=torch.float32))\n       self.relu = nn.ReLU(inplace=True)\n   def forward(self, x):\n       y = x @ self.weights + self.bias\n       y = self.relu(y)\n       return y<\/code><\/pre>\n<\/li>\n<li>\n<p>\u5c06PyTorch\u6a21\u578b\u4fdd\u5b58\u5230\u672c\u5730:<\/p>\n<pre><code class=\"language-python\"># \u8f93\u5165\u7ef4\u5ea6\nin_dim = 13\n\n# \u521d\u59cb\u5316\u6a21\u578b\nlm = LinearModel(in_dim)\n\n# 1.1 \u4fdd\u5b58\u6a21\u578b\npt_model_path = '.\/models\/linearModel.pt'\ntorch.save(lm, pt_model_path)<\/code><\/pre>\n<\/li>\n<li>\n<p>\u4f7f\u7528PyTorch API\u5c06PyTorch\u683c\u5f0f\u7684\u6a21\u578b\u8f6c\u6362\u5230ONNX\u683c\u5f0f:<\/p>\n<pre><code class=\"language-python\">batch_size = 64          # \u6279\u6b21\u5927\u5c0f\n# \u968f\u673a\u751f\u6210\u8f93\u5165\u6570\u636e\nx = torch.randn(batch_size, in_dim, dtype=torch.float32)\n\ntorch_model = torch.load(pt_model_path, map_location=torch.device('cpu'))\ntorch_model_output = torch_model(x)\n\ntorch.onnx.export(torch_model, x, '.\/models\/linearModel.onnx', opset_version=11,\n                 input_names=['input'], output_names=['output'])<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u200b        torch.onnx.export\u7684\u524d\u4e09\u4e2a\u5fc5\u9009\u53c2\u6570\u5206\u522b\u6307\uff1a\u8981\u8f6c\u6362\u7684\u539f\u59cb\u6a21\u578b(\u6b64\u5904\u4e3apytorch\u683c\u5f0f\u7684\u7ebf\u6027\u6a21\u578b)\u3001\u6a21\u578b\u7684\u4e00\u4e2a\u968f\u673a\u8f93\u5165\u3001\u4fdd\u5b58\u7684onnx\u683c\u5f0f\u6a21\u578b\u7684\u8def\u5f84\uff0c<br \/>\ninput_names\u548coutput_names\u5206\u522b\u4e3a\u8f93\u5165\u548c\u8f93\u51fa\u540d\uff0c\u540e\u7eed\u63a8\u7406\u8981\u4f7f\u7528\u3002<\/p>\n<p>\u200b        \u6b64\u5904\u4e3a\u4ec0\u4e48\u8981\u4f20\u5165\u8f93\u5165<code>x<\/code>\u5462\uff1f\u8fd9\u662f\u56e0\u4e3aPyTorch\u5c06\u6a21\u578b\u8f6c\u6362\u6210ONNX\u683c\u5f0f\u65f6\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4fdd\u5b58\u7684\u662f\u6a21\u578b\u7684\u9759\u6001\u56fe(\u8c03\u7528torch.jit.trace)\uff0c\u9700\u8981\u4f7f\u7528\u8f93\u5165<code>x<\/code>\u6267\u884c\u4e00\u6b21\u8ba1\u7b97\uff0c\u8ffd\u8e2a\u8ba1\u7b97\u6d41\u7a0b\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u5230ONNX\u683c\u5f0f\u3002<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528<a href=\"https:\/\/netron.app\/\">netron<\/a>\u67e5\u770bONNX\u683c\u5f0f\u7684\u6a21\u578b:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/onnx12.png\" style=\"zoom:67%;\" \/><\/p>\n<ol start=\"4\">\n<li>\n<p>\u4f7f\u7528<code>onnx<\/code>\u5305\u68c0\u67e5\u8f6c\u6362\u7684ONNX\u6a21\u578b\u662f\u5426\u6b63\u786e\uff1a<\/p>\n<pre><code class=\"language-python\">import onnx\nonnx_model = onnx.load(onnx_model_path)\n\n# \u4f7f\u7528onnx\u81ea\u5e26\u7684API\u68c0\u67e5\u6a21\u578b\u662f\u5426\u6b63\u786e\u8f6c\u6362\ntry:\n   onnx.checker.check_model(onnx_model)\nexcept Exception:\n   print(\"Model incorrect\")\nelse:\n   print(\"Model correct\")<\/code><\/pre>\n<\/li>\n<li>\n<p>\u4f7f\u7528<code>onnxruntime<\/code>\u8fdb\u884c\u63a8\u7406<\/p>\n<pre><code class=\"language-python\">import onnxruntime as ort\nlinear_model_session = ort.InferenceSession(onnx_model_path)\nort_output = linear_model_session.run(['output'],\n                                     { 'input': x.numpy() })\nimport numpy as np\n# \u68c0\u67e5PyTorch\u6a21\u578b\u7684\u8f93\u51fa\u548cONNX\u6a21\u578b\u7684\u8f93\u51fa\u662f\u5426\u4e00\u81f4\nnp.allclose(torch_model_output.detach().numpy(), np.array(ort_output))<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3>3. \u5c0f\u7ed3<\/h3>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86PyTorch\u6a21\u578b\u8f6c\u6362\u5230ONNX\u683c\u5f0f\u7684\u4e00\u822c\u6d41\u7a0b\uff0c\u5173\u4e8e\u5982\u4f55\u5b9e\u73b0\u52a8\u6001batchsize\u7b49\u5185\u5bb9\u5c06\u5728\u540e\u7eed\u6587\u7ae0\u4e2d\u9610\u8ff0\u3002\u672c\u6587\u7684\u4ee3\u7801\u548c\u8fd0\u884c\u73af\u5883\u53ef\u4ee5\u53c2\u89c1<a href=\"http:\/\/code.3body.fun:10000\/notebooks\/Python\/blog_thirdbody\/onnx1.ipynb\">\u4ee3\u7801<\/a>(\u5bc6\u7801\uff1a<code>123456<\/code>)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u90e8\u7f72\u7684\u96be\u70b9 \u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u57fa\u4e8ePyTorch\/TensorFlow\u7b49\u6846\u67b6\u90e8\u7f72\uff0c\u8fd9\u4e9b\u4f9d\u8d56\u73af\u5883\u4e0d\u9002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":49,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25,24],"tags":[27,26,28],"class_list":["post-48","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-model_deployment","category-deep_learning","tag-onnx","tag-pytorch","tag-28"],"_links":{"self":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts\/48","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":2,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":51,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts\/48\/revisions\/51"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/media\/49"}],"wp:attachment":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}