{"id":12653,"date":"2023-02-01T21:08:46","date_gmt":"2023-02-01T13:08:46","guid":{"rendered":"http:\/\/139.9.1.231\/?p=12653"},"modified":"2023-02-01T21:08:48","modified_gmt":"2023-02-01T13:08:48","slug":"onnx-5","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2023\/02\/01\/onnx-5\/","title":{"rendered":"ONNX \u6a21\u578b\u7684\u4fee\u6539\u4e0e\u8c03\u8bd5"},"content":{"rendered":"\n<p>\u8f6c\u81ea\uff1a<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/516920606\" target=\"_blank\" rel=\"noreferrer noopener\">\u6a21\u578b\u90e8\u7f72\u5165\u95e8\u6559\u7a0b\uff08\u4e94\uff09\uff1aONNX \u6a21\u578b\u7684\u4fee\u6539\u4e0e\u8c03\u8bd5<\/a><\/p>\n\n\n\n<p>\u4e00\u76f4\u4ee5\u6765\uff0c\u6211\u4eec\u90fd\u662f\u901a\u8fc7 PyTorch \u6765\u5bfc\u51fa ONNX \u6a21\u578b\u7684\uff0c\u57fa\u672c\u6ca1\u6709\u5355\u72ec\u63a2\u7a76\u8fc7 ONNX \u6a21\u578b\u7684\u6784\u9020\u77e5\u8bc6\u3002<br>\u4e0d\u77e5\u9053\u5927\u5bb6\u4f1a\u4e0d\u4f1a\u6709\u8fd9\u6837\u4e00\u4e9b\u7591\u95ee\uff1aONNX \u6a21\u578b\u5728\u5e95\u5c42\u662f\u7528\u4ec0\u4e48\u683c\u5f0f\u5b58\u50a8\u7684\uff1f\u5982\u4f55\u4e0d\u4f9d\u8d56\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff0c\u53ea\u7528 ONNX \u7684 API \u6765\u6784\u9020\u4e00\u4e2a ONNX \u6a21\u578b\uff1f\u5982\u679c\u6ca1\u6709\u6e90\u4ee3\u7801\uff0c\u53ea\u6709\u4e00\u4e2a ONNX \u6a21\u578b\uff0c\u8be5\u5982\u4f55\u5bf9\u8fd9\u4e2a\u6a21\u578b\u8fdb\u884c\u8c03\u8bd5\uff1f\u522b\u6025\uff0c\u4eca\u5929\u6211\u4eec\u5c31\u6765\u4e3a\u5927\u5bb6\u4e00\u4e00\u63ed\u6653\u3002<br>\u5728\u8fd9\u671f\u6559\u7a0b\u91cc\uff0c\u6211\u4eec\u5c06\u56f4\u7ed5 ONNX \u8fd9\u4e00\u5957\u795e\u7ecf\u7f51\u7edc\u5b9a\u4e49\u6807\u51c6\u672c\u8eab\uff0c\u63a2\u7a76 ONNX \u6a21\u578b\u7684\u6784\u9020\u3001\u8bfb\u53d6\u3001\u5b50\u6a21\u578b\u63d0\u53d6\u3001\u8c03\u8bd5\u3002\u9996\u5148\uff0c\u6211\u4eec\u4f1a\u5b66\u4e60 ONNX \u7684\u5e95\u5c42\u8868\u793a\u65b9\u5f0f\u3002\u4e4b\u540e\uff0c\u6211\u4eec\u4f1a\u7528 ONNX API \u6784\u9020\u548c\u8bfb\u53d6\u6a21\u578b\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f1a\u5229\u7528 ONNX \u63d0\u4f9b\u7684\u5b50\u6a21\u578b\u63d0\u53d6\u529f\u80fd\uff0c\u5b66\u4e60\u5982\u4f55\u8c03\u8bd5 ONNX \u6a21\u578b\u3002<\/p>\n\n\n\n<h2 id=\"h_516920606_0\">ONNX \u7684\u5e95\u5c42\u5b9e\u73b0<\/h2>\n\n\n\n<h3 id=\"h_516920606_1\">ONNX \u7684\u5b58\u50a8\u683c\u5f0f<\/h3>\n\n\n\n<p>ONNX \u5728\u5e95\u5c42\u662f\u7528&nbsp;<strong>Protobuf&nbsp;<\/strong>\u5b9a\u4e49\u7684\u3002Protobuf\uff0c\u5168\u79f0 Protocol Buffer\uff0c\u662f Google \u63d0\u51fa\u7684\u4e00\u5957\u8868\u793a\u548c\u5e8f\u5217\u5316\u6570\u636e\u7684\u673a\u5236\u3002\u4f7f\u7528 Protobuf \u65f6\uff0c\u7528\u6237\u9700\u8981\u5148\u5199\u4e00\u4efd\u6570\u636e\u5b9a\u4e49\u6587\u4ef6\uff0c\u518d\u6839\u636e\u8fd9\u4efd\u5b9a\u4e49\u6587\u4ef6\u628a\u6570\u636e\u5b58\u50a8\u8fdb\u4e00\u4efd\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002\u53ef\u4ee5\u8bf4\uff0c\u6570\u636e\u5b9a\u4e49\u6587\u4ef6\u5c31\u662f\u6570\u636e\u7c7b\uff0c\u4e8c\u8fdb\u5236\u6587\u4ef6\u5c31\u662f\u6570\u636e\u7c7b\u7684\u5b9e\u4f8b\u3002<br>\u8fd9\u91cc\u7ed9\u51fa\u4e00\u4e2a Protobuf \u6570\u636e\u5b9a\u4e49\u6587\u4ef6\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>message Person { \n  required string name = 1; \n  required int32 id = 2; \n  optional string email = 3; \n} <\/code><\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u5b9a\u4e49\u8868\u793a\u5728&nbsp;<code>Person<\/code>&nbsp;\u8fd9\u79cd\u6570\u636e\u7c7b\u578b\u4e2d\uff0c\u5fc5\u987b\u5305\u542b&nbsp;<code>name<\/code>\u3001<code>id<\/code>&nbsp;\u8fd9\u4e24\u4e2a\u5b57\u6bb5\uff0c\u9009\u62e9\u6027\u5305\u542b&nbsp;<code>email<\/code>\u5b57\u6bb5\u3002\u6839\u636e\u8fd9\u4efd\u5b9a\u4e49\u6587\u4ef6\uff0c\u7528\u6237\u5c31\u53ef\u4ee5\u9009\u62e9\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u5b9a\u4e49\u4e00\u4e2a\u542b\u6709\u6210\u5458\u53d8\u91cf&nbsp;<code>name<\/code>\u3001<code>id<\/code>\u3001<code>email<\/code>&nbsp;\u7684&nbsp;<code>Person<\/code>&nbsp;\u7c7b\uff0c\u628a\u8fd9\u4e2a\u7c7b\u7684\u67d0\u4e2a\u5b9e\u4f8b\u7528 Protobuf \u5b58\u50a8\u6210\u4e8c\u8fdb\u5236\u6587\u4ef6\uff1b\u53cd\u4e4b\uff0c\u7528\u6237\u4e5f\u53ef\u4ee5\u7528\u4e8c\u8fdb\u5236\u6587\u4ef6\u548c\u5bf9\u5e94\u7684\u6570\u636e\u5b9a\u4e49\u6587\u4ef6\uff0c\u8bfb\u53d6\u51fa\u4e00\u4e2a&nbsp;<code>Person<\/code>&nbsp;\u7c7b\u7684\u5b9e\u4f8b\u3002<br>\u800c\u5bf9\u4e8e ONNX \uff0cProtobuf \u7684\u6570\u636e\u5b9a\u4e49\u6587\u4ef6\u5728\u5176<a href=\"https:\/\/github.com\/onnx\/onnx\/tree\/main\/onnx\" target=\"_blank\" rel=\"noreferrer noopener\">\u5f00\u6e90\u5e93<\/a>\uff0c\u8fd9\u4e9b\u6587\u4ef6\u5b9a\u4e49\u4e86\u795e\u7ecf\u7f51\u7edc\u4e2d\u6a21\u578b\u3001\u8282\u70b9\u3001\u5f20\u91cf\u7684\u6570\u636e\u7c7b\u578b\u89c4\u8303\uff1b\u800c\u4e8c\u8fdb\u5236\u6587\u4ef6\u5c31\u662f\u6211\u4eec\u719f\u6089\u7684\u201c.onnx&#8221;\u6587\u4ef6\uff0c\u6bcf\u4e00\u4e2a onnx \u6587\u4ef6\u6309\u7167\u6570\u636e\u5b9a\u4e49\u89c4\u8303\uff0c\u5b58\u50a8\u4e86\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\u7684\u6240\u6709\u76f8\u5173\u6570\u636e\u3002\u76f4\u63a5\u7528 Protobuf \u751f\u6210 ONNX \u6a21\u578b\u8fd8\u662f\u6bd4\u8f83\u9ebb\u70e6\u7684\u3002\u5e78\u8fd0\u7684\u662f\uff0cONNX \u63d0\u4f9b\u4e86\u5f88\u591a\u5b9e\u7528 API\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5b8c\u5168\u4e0d\u4e86\u89e3 Protobuf \u7684\u524d\u63d0\u4e0b\uff0c\u6784\u9020\u548c\u8bfb\u53d6 ONNX \u6a21\u578b\u3002<\/p>\n\n\n\n<h3 id=\"h_516920606_2\"><br>ONNX \u7684\u7ed3\u6784\u5b9a\u4e49<\/h3>\n\n\n\n<p><br>\u5728\u7528 API \u5bf9 ONNX \u6a21\u578b\u8fdb\u884c\u64cd\u4f5c\u4e4b\u524d\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u5148\u4e86\u89e3\u4e00\u4e0b ONNX \u7684\u7ed3\u6784\u5b9a\u4e49\u89c4\u5219\uff0c\u5b66\u4e60\u4e00\u4e0b ONNX \u5728 Protobuf \u5b9a\u4e49\u6587\u4ef6\u91cc\u662f\u600e\u6837\u63cf\u8ff0\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\u7684\u3002<br>\u56de\u60f3\u4e00\u4e0b\uff0c\u795e\u7ecf\u7f51\u7edc\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u8ba1\u7b97\u56fe\u3002\u8ba1\u7b97\u56fe\u7684\u8282\u70b9\u662f\u7b97\u5b50\uff0c\u8fb9\u662f\u53c2\u4e0e\u8fd0\u7b97\u7684\u5f20\u91cf\u3002\u800c\u901a\u8fc7\u53ef\u89c6\u5316 ONNX \u6a21\u578b\uff0c\u6211\u4eec\u77e5\u9053 ONNX \u8bb0\u5f55\u4e86\u6240\u6709\u7b97\u5b50\u8282\u70b9\u7684\u5c5e\u6027\u4fe1\u606f\uff0c\u5e76\u628a\u53c2\u4e0e\u8fd0\u7b97\u7684\u5f20\u91cf\u4fe1\u606f\u5b58\u50a8\u5728\u7b97\u5b50\u8282\u70b9\u7684\u8f93\u5165\u8f93\u51fa\u4fe1\u606f\u4e2d\u3002\u4e8b\u5b9e\u4e0a\uff0cONNX \u6a21\u578b\u7684\u7ed3\u6784\u53ef\u4ee5\u7528\u7c7b\u56fe\u5927\u81f4\u8868\u793a\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/pic2.zhimg.com\/v2-03c35109502af4dad352914cbbe8b9b9_r.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u5982\u56fe\u6240\u793a\uff0c\u4e00\u4e2a ONNX \u6a21\u578b\u53ef\u4ee5\u7528&nbsp;<code>ModelProto<\/code>&nbsp;\u7c7b\u8868\u793a\u3002<code>ModelProto<\/code>&nbsp;\u5305\u542b\u4e86\u7248\u672c\u3001\u521b\u5efa\u8005\u7b49\u65e5\u5fd7\u4fe1\u606f\uff0c\u8fd8\u5305\u542b\u4e86\u5b58\u50a8\u8ba1\u7b97\u56fe\u7ed3\u6784\u7684&nbsp;<code>graph<\/code>\u3002<code>GraphProto<\/code>&nbsp;\u7c7b\u5219\u7531\u8f93\u5165\u5f20\u91cf\u4fe1\u606f\u3001\u8f93\u51fa\u5f20\u91cf\u4fe1\u606f\u3001\u8282\u70b9\u4fe1\u606f\u7ec4\u6210\u3002\u5f20\u91cf\u4fe1\u606f&nbsp;<code>ValueInfoProto<\/code>&nbsp;\u7c7b\u5305\u62ec\u5f20\u91cf\u540d\u3001\u57fa\u672c\u6570\u636e\u7c7b\u578b\u3001\u5f62\u72b6\u3002\u8282\u70b9\u4fe1\u606f&nbsp;<code>NodeProto<\/code>&nbsp;\u7c7b\u5305\u542b\u4e86\u7b97\u5b50\u540d\u3001\u7b97\u5b50\u8f93\u5165\u5f20\u91cf\u540d\u3001\u7b97\u5b50\u8f93\u51fa\u5f20\u91cf\u540d\u3002<br>\u8ba9\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u3002\u5047\u5982\u6211\u4eec\u6709\u4e00\u4e2a\u63cf\u8ff0&nbsp;<code>output=a*x+b<\/code>&nbsp;\u7684 ONNX \u6a21\u578b&nbsp;<code>model<\/code>\uff0c\u7528&nbsp;<code>print(model)<\/code>&nbsp;\u53ef\u4ee5\u8f93\u51fa\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ir_version: 8 \ngraph { \n  node { \n    input: \"a\" \n    input: \"x\" \n    output: \"c\" \n    op_type: \"Mul\" \n  } \n  node { \n    input: \"c\" \n    input: \"b\" \n    output: \"output\" \n    op_type: \"Add\" \n  } \n  name: \"linear_func\" \n  input { \n    name: \"a\" \n    type { \n      tensor_type { \n        elem_type: 1 \n        shape { \n          dim {dim_value: 10} \n          dim {dim_value: 10} \n        } \n      } \n    } \n  } \n  input { \n    name: \"x\" \n    type { \n      tensor_type { \n        elem_type: 1 \n        shape { \n          dim {dim_value: 10} \n          dim {dim_value: 10} \n        } \n      } \n    } \n  } \n  input { \n    name: \"b\" \n    type { \n      tensor_type { \n        elem_type: 1 \n        shape { \n          dim {dim_value: 10} \n          dim {dim_value: 10} \n        } \n      } \n    } \n  } \n  output { \n    name: \"output\" \n    type { \n      tensor_type { \n        elem_type: 1 \n        shape { \n          dim { dim_value: 10} \n          dim { dim_value: 10} \n        } \n      } \n    } \n  } \n} \nopset_import {version: 15} <\/code><\/pre>\n\n\n\n<p>\u5bf9\u5e94\u4e0a\u6587\u4e2d\u7684\u7c7b\u56fe\uff0c\u8fd9\u4e2a\u6a21\u578b\u7684\u4fe1\u606f\u7531&nbsp;<code>ir_version<\/code>\uff0c<code>opset_import<\/code>&nbsp;\u7b49\u5168\u5c40\u4fe1\u606f\u548c&nbsp;<code>graph<\/code>&nbsp;\u56fe\u4fe1\u606f\u7ec4\u6210\u3002\u800c&nbsp;<code>graph<\/code>&nbsp;\u5305\u542b\u4e00\u4e2a\u4e58\u6cd5\u8282\u70b9\u3001\u4e00\u4e2a\u52a0\u6cd5\u8282\u70b9\u3001\u4e09\u4e2a\u8f93\u5165\u5f20\u91cf&nbsp;<code>a, x, b<\/code>&nbsp;\u4ee5\u53ca\u4e00\u4e2a\u8f93\u51fa\u5f20\u91cf&nbsp;<code>output<\/code>\u3002\u5728\u4e0b\u4e00\u8282\u91cc\uff0c\u6211\u4eec\u4f1a\u7528 API \u6784\u9020\u51fa\u8fd9\u4e2a\u6a21\u578b\uff0c\u5e76\u8f93\u51fa\u8fd9\u6bb5\u7ed3\u679c\u3002<\/p>\n\n\n\n<h2 id=\"h_516920606_3\">\u8bfb\u5199 ONNX \u6a21\u578b<\/h2>\n\n\n\n<h3 id=\"h_516920606_4\">\u6784\u9020 ONNX \u6a21\u578b<\/h3>\n\n\n\n<p><br>\u5728\u4e0a\u4e00\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u77e5\u9053\u4e86 ONNX \u6a21\u578b\u662f\u6309\u4ee5\u4e0b\u7684\u7ed3\u6784\u7ec4\u7ec7\u8d77\u6765\u7684\uff1a<\/p>\n\n\n\n<ul><li>ModelProto<ul><li>GraphProto<ul><li>NodeProto<\/li><li>ValueInfoProto<\/li><\/ul><\/li><\/ul><\/li><\/ul>\n\n\n\n<p>\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u629b\u5f00 PyTorch\uff0c\u5c1d\u8bd5\u5b8c\u5168\u7528 ONNX \u7684 Python API \u6784\u9020\u4e00\u4e2a\u63cf\u8ff0\u7ebf\u6027\u51fd\u6570&nbsp;<code>output=a*x+b<\/code>&nbsp;\u7684 ONNX \u6a21\u578b\u3002\u6211\u4eec\u5c06\u6839\u636e\u4e0a\u9762\u7684\u7ed3\u6784\uff0c\u81ea\u5e95\u5411\u4e0a\u5730\u6784\u9020\u8fd9\u4e2a\u6a21\u578b\u3002<br>\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u7528&nbsp;<code>helper.make_tensor_value_info<\/code>&nbsp;\u6784\u9020\u51fa\u4e00\u4e2a\u63cf\u8ff0\u5f20\u91cf\u4fe1\u606f\u7684&nbsp;<code>ValueInfoProto<\/code>&nbsp;\u5bf9\u8c61\u3002\u5982\u524d\u9762\u7684\u7c7b\u56fe\u6240\u793a\uff0c\u6211\u4eec\u8981\u4f20\u5165\u5f20\u91cf\u540d\u3001\u5f20\u91cf\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u3001\u5f20\u91cf\u5f62\u72b6\u8fd9\u4e09\u4e2a\u4fe1\u606f\u3002\u5728 ONNX \u4e2d\uff0c\u4e0d\u7ba1\u662f\u8f93\u5165\u5f20\u91cf\u8fd8\u662f\u8f93\u51fa\u5f20\u91cf\uff0c\u5b83\u4eec\u7684\u8868\u793a\u65b9\u5f0f\u90fd\u662f\u4e00\u6837\u7684\u3002\u56e0\u6b64\uff0c\u8fd9\u91cc\u6211\u4eec\u7528\u7c7b\u4f3c\u7684\u65b9\u5f0f\u4e3a\u4e09\u4e2a\u8f93\u5165&nbsp;<code>a, x, b<\/code>&nbsp;\u548c\u4e00\u4e2a\u8f93\u51fa&nbsp;<code>output<\/code>&nbsp;\u6784\u9020&nbsp;<code>ValueInfoProto<\/code>&nbsp;\u5bf9\u8c61\u3002\u5982\u4e0b\u9762\u7684\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import onnx \nfrom onnx import helper \nfrom onnx import TensorProto \n \na = helper.make_tensor_value_info('a', TensorProto.FLOAT, &#91;10, 10]) \nx = helper.make_tensor_value_info('x', TensorProto.FLOAT, &#91;10, 10]) \nb = helper.make_tensor_value_info('b', TensorProto.FLOAT, &#91;10, 10]) \noutput = helper.make_tensor_value_info('output', TensorProto.FLOAT, &#91;10, 10]) <\/code><\/pre>\n\n\n\n<p>\u4e4b\u540e\uff0c\u6211\u4eec\u8981\u6784\u9020\u7b97\u5b50\u8282\u70b9\u4fe1\u606f&nbsp;<code>NodeProto<\/code>\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728&nbsp;<code>helper.make_node<\/code>&nbsp;\u4e2d\u4f20\u5165\u7b97\u5b50\u7c7b\u578b\u3001\u8f93\u5165\u7b97\u5b50\u540d\u3001\u8f93\u51fa\u7b97\u5b50\u540d\u8fd9\u4e09\u4e2a\u4fe1\u606f\u6765\u5b9e\u73b0\u3002\u6211\u4eec\u8fd9\u91cc\u5148\u6784\u9020\u4e86\u63cf\u8ff0&nbsp;<code>c=a*x<\/code>&nbsp;\u7684\u4e58\u6cd5\u8282\u70b9\uff0c\u518d\u6784\u9020\u4e86&nbsp;<code>output=c+b<\/code>&nbsp;\u7684\u52a0\u6cd5\u8282\u70b9\u3002\u5982\u4e0b\u9762\u7684\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mul = helper.make_node('Mul', &#91;'a', 'x'], &#91;'c']) \nadd = helper.make_node('Add', &#91;'c', 'b'], &#91;'output']) <\/code><\/pre>\n\n\n\n<p>\u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u56fe\u4e00\u822c\u662f\u7528\u4e00\u4e2a\u8282\u70b9\u96c6\u548c\u4e00\u4e2a\u8fb9\u96c6\u8868\u793a\u7684\u3002\u800c ONNX \u5de7\u5999\u5730\u628a\u8fb9\u7684\u4fe1\u606f\u4fdd\u5b58\u5728\u4e86\u8282\u70b9\u4fe1\u606f\u91cc\uff0c\u7701\u53bb\u4e86\u4fdd\u5b58\u8fb9\u96c6\u7684\u6b65\u9aa4\u3002\u5728 ONNX \u4e2d\uff0c\u5982\u679c\u67d0\u8282\u70b9\u7684\u8f93\u5165\u540d\u548c\u4e4b\u524d\u67d0\u8282\u70b9\u7684\u8f93\u51fa\u540d\u76f8\u540c\uff0c\u5c31\u9ed8\u8ba4\u8fd9\u4e24\u4e2a\u8282\u70b9\u662f\u76f8\u8fde\u7684\u3002\u5982\u4e0a\u9762\u7684\u4f8b\u5b50\u6240\u793a\uff1a<code>Mul<\/code>&nbsp;\u8282\u70b9\u5b9a\u4e49\u4e86\u8f93\u51fa&nbsp;<code>c<\/code>\uff0c<code>Add<\/code>&nbsp;\u8282\u70b9\u5b9a\u4e49\u4e86\u8f93\u5165&nbsp;<code>c<\/code>\uff0c\u5219&nbsp;<code>Mul<\/code>&nbsp;\u8282\u70b9\u548c&nbsp;<code>Add<\/code>&nbsp;\u8282\u70b9\u662f\u76f8\u8fde\u7684\u3002<br>\u6b63\u662f\u56e0\u4e3a\u6709\u8fd9\u79cd\u8fb9\u7684\u9690\u5f0f\u5b9a\u4e49\u89c4\u5219\uff0c\u6240\u4ee5 ONNX \u5bf9\u8282\u70b9\u7684\u8f93\u5165\u6709\u4e00\u5b9a\u7684\u8981\u6c42\uff1a\u4e00\u4e2a\u8282\u70b9\u7684\u8f93\u5165\uff0c\u8981\u4e48\u662f\u6574\u4e2a\u6a21\u578b\u7684\u8f93\u5165\uff0c\u8981\u4e48\u662f\u4e4b\u524d\u67d0\u4e2a\u8282\u70b9\u7684\u8f93\u51fa\u3002\u5982\u679c\u6211\u4eec\u628a&nbsp;<code>a, x, b<\/code>&nbsp;\u4e2d\u7684\u67d0\u4e2a\u8f93\u5165\u8282\u70b9\u4ece\u8ba1\u7b97\u56fe\u4e2d\u62ff\u51fa\uff08\u8fd9\u4e2a\u64cd\u4f5c\u4f1a\u5728\u4e4b\u540e\u7684\u4ee3\u7801\u4e2d\u4ecb\u7ecd\uff09\uff0c\u6216\u8005\u628a&nbsp;<code>Mul<\/code>&nbsp;\u7684\u8f93\u51fa\u4ece&nbsp;<code>c<\/code>&nbsp;\u6539\u6210&nbsp;<code>d<\/code>\uff0c\u5219\u6700\u7ec8\u7684 ONNX \u6a21\u578b\u90fd\u662f\u4e0d\u6ee1\u8db3\u6807\u51c6\u7684\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u4e00\u4e2a\u4e0d\u6ee1\u8db3\u6807\u51c6\u7684 ONNX \u6a21\u578b\u53ef\u80fd\u65e0\u6cd5\u88ab\u63a8\u7406\u5f15\u64ce\u6b63\u786e\u8bc6\u522b\u3002ONNX \u63d0\u4f9b\u4e86 API&nbsp;<code>onnx.checker.check_model<\/code>&nbsp;\u6765\u5224\u65ad\u4e00\u4e2a ONNX \u6a21\u578b\u662f\u5426\u6ee1\u8db3\u6807\u51c6\u3002<\/p><\/blockquote>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u7528&nbsp;<code>helper.make_graph<\/code>&nbsp;\u6765\u6784\u9020\u8ba1\u7b97\u56fe&nbsp;<code>GraphProto<\/code>\u3002<code>helper.make_graph<\/code>&nbsp;\u51fd\u6570\u9700\u8981\u4f20\u5165\u8282\u70b9\u3001\u56fe\u540d\u79f0\u3001\u8f93\u5165\u5f20\u91cf\u4fe1\u606f\u3001\u8f93\u51fa\u5f20\u91cf\u4fe1\u606f\u8fd9 4 \u4e2a\u53c2\u6570\u3002\u5982\u4e0b\u9762\u7684\u4ee3\u7801\u6240\u793a\uff0c\u6211\u4eec\u628a\u4e4b\u524d\u6784\u9020\u51fa\u6765\u7684&nbsp;<code>NodeProto<\/code>&nbsp;\u5bf9\u8c61\u548c&nbsp;<code>ValueInfoProto<\/code>&nbsp;\u5bf9\u8c61\u6309\u7167\u987a\u5e8f\u4f20\u5165\u5373\u53ef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>graph = helper.make_graph(&#91;mul, add], 'linear_func', &#91;a, x, b], &#91;output]) <\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc&nbsp;<code>make_graph<\/code>&nbsp;\u7684\u8282\u70b9\u53c2\u6570\u6709\u4e00\u4e2a\u8981\u6c42\uff1a\u8ba1\u7b97\u56fe\u7684\u8282\u70b9\u5fc5\u987b\u4ee5\u62d3\u6251\u5e8f\u7ed9\u51fa\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u62d3\u6251\u5e8f\u662f\u4e0e\u6709\u5411\u56fe\u7684\u76f8\u5173\u7684\u6570\u5b66\u6982\u5ff5\u3002\u5982\u679c\u6309\u62d3\u6251\u5e8f\u904d\u5386\u6240\u6709\u8282\u70b9\u7684\u8bdd\uff0c\u80fd\u4fdd\u8bc1\u6bcf\u4e2a\u8282\u70b9\u7684\u8f93\u5165\u90fd\u80fd\u5728\u4e4b\u524d\u8282\u70b9\u7684\u8f93\u51fa\u91cc\u627e\u5230\uff08\u5bf9\u4e8e ONNX \u6a21\u578b\uff0c\u6211\u4eec\u628a\u8ba1\u7b97\u56fe\u7684\u8f93\u5165\u5f20\u91cf\u4e5f\u770b\u6210\u201c\u4e4b\u524d\u7684\u8f93\u51fa\u201d\uff09\u3002<\/p><\/blockquote>\n\n\n\n<p>\u5982\u679c\u5bf9\u8fd9\u4e2a\u6982\u5ff5\u4e0d\u719f\u4e5f\u6ca1\u6709\u5173\u7cfb\uff0c\u6211\u4eec\u4ee5\u521a\u521a\u6784\u9020\u51fa\u6765\u7684\u8fd9\u4e2a\u8ba1\u7b97\u56fe\u4e3a\u7814\u7a76\u5bf9\u8c61\uff0c\u901a\u8fc7\u4e0b\u56fe\u5c55\u793a\u7684\u4e24\u4e2a\u4f8b\u5b50\u6765\u76f4\u89c2\u7406\u89e3\u62d3\u6251\u5e8f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/pic3.zhimg.com\/v2-9c1c3882c88d9b9068c6ac6595add552_r.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u53ea\u5173\u6ce8&nbsp;<code>Mul<\/code>&nbsp;\u548c&nbsp;<code>Add<\/code>&nbsp;\u8282\u70b9\u4ee5\u53ca\u5b83\u4eec\u4e4b\u95f4\u7684\u8fb9&nbsp;<code>c<\/code>\u3002\u5728\u60c5\u51b5 1 \u4e2d\uff1a\u5982\u679c\u6211\u4eec\u7684\u8282\u70b9\u4ee5&nbsp;<code>[Mul, Add]<\/code>&nbsp;\u987a\u5e8f\u7ed9\u51fa\uff0c\u90a3\u4e48\u904d\u5386\u5230&nbsp;<code>Add<\/code>&nbsp;\u65f6\uff0c\u5b83\u7684\u8f93\u5165&nbsp;<code>c<\/code>&nbsp;\u53ef\u4ee5\u5728\u4e4b\u524d\u7684<code>Mul<\/code>\u7684\u8f93\u51fa\u4e2d\u627e\u5230\u3002\u4f46\u662f\uff0c\u5982\u60c5\u51b5 2 \u6240\u793a\uff1a\u5982\u679c\u6211\u4eec\u7684\u8282\u70b9\u4ee5&nbsp;<code>[Add, Mul]<\/code>&nbsp;\u7684\u987a\u5e8f\u7ed9\u51fa\uff0c\u90a3\u4e48&nbsp;<code>Add<\/code>&nbsp;\u5c31\u627e\u4e0d\u5230\u8f93\u5165\u8fb9\uff0c\u8ba1\u7b97\u56fe\u4e5f\u65e0\u6cd5\u6210\u529f\u6784\u9020\u51fa\u6765\u4e86\u3002\u8fd9\u91cc\u7684&nbsp;<code>[Mul, Add]<\/code>&nbsp;\u5c31\u662f\u7b26\u5408\u6709\u5411\u56fe\u7684\u62d3\u6251\u5e8f\u7684\uff0c\u800c&nbsp;<code>[Add, Mul]<\/code>&nbsp;\u5219\u4e0d\u6ee1\u8db3\u3002<\/p>\n\n\n\n<p>\u6700\u540e\uff0c\u6211\u4eec\u7528&nbsp;<code>helper.make_model<\/code>&nbsp;\u628a\u8ba1\u7b97\u56fe&nbsp;<code>GraphProto<\/code>&nbsp;\u5c01\u88c5\u8fdb\u6a21\u578b&nbsp;<code>ModelProto<\/code>&nbsp;\u91cc\uff0c\u4e00\u4e2a ONNX \u6a21\u578b\u5c31\u6784\u9020\u5b8c\u6210\u4e86\u3002<code>make_model<\/code>&nbsp;\u51fd\u6570\u4e2d\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u6a21\u578b\u5236\u4f5c\u8005\u3001\u7248\u672c\u7b49\u4fe1\u606f\uff0c\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u6211\u4eec\u6ca1\u6709\u6dfb\u52a0\u989d\u5916\u7684\u4fe1\u606f\u3002\u5982\u4e0b\u9762\u7684\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>model = helper.make_model(graph) <\/code><\/pre>\n\n\n\n<p>\u6784\u9020\u5b8c\u6a21\u578b\u4e4b\u540e\uff0c\u6211\u4eec\u7528\u4e0b\u9762\u8fd9\u4e09\u884c\u4ee3\u7801\u6765\u68c0\u67e5\u6a21\u578b\u6b63\u786e\u6027\u3001\u628a\u6a21\u578b\u4ee5\u6587\u672c\u5f62\u5f0f\u8f93\u51fa\u3001\u5b58\u50a8\u5230\u4e00\u4e2a &#8220;.onnx&#8221; \u6587\u4ef6\u91cc\u3002\u8fd9\u91cc\u7528&nbsp;<code>onnx.checker.check_model<\/code>&nbsp;\u6765\u68c0\u67e5\u6a21\u578b\u662f\u5426\u6ee1\u8db3 ONNX \u6807\u51c6\u662f\u5fc5\u8981\u7684\uff0c\u56e0\u4e3a\u65e0\u8bba\u6a21\u578b\u662f\u5426\u6ee1\u8db3\u6807\u51c6\uff0cONNX \u90fd\u5141\u8bb8\u6211\u4eec\u7528&nbsp;<code>onnx.save<\/code>&nbsp;\u5b58\u50a8\u6a21\u578b\u3002\u6211\u4eec\u80af\u5b9a\u4e0d\u5e0c\u671b\u751f\u6210\u4e00\u4e2a\u4e0d\u6ee1\u8db3\u6807\u51c6\u7684\u6a21\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>onnx.checker.check_model(model) \nprint(model) \nonnx.save(model, 'linear_func.onnx') <\/code><\/pre>\n\n\n\n<p>\u6210\u529f\u6267\u884c\u8fd9\u4e9b\u4ee3\u7801\u7684\u8bdd\uff0c\u7a0b\u5e8f\u4f1a\u4ee5\u6587\u672c\u683c\u5f0f\u8f93\u51fa\u6a21\u578b\u7684\u4fe1\u606f\uff0c\u5176\u5185\u5bb9\u5e94\u8be5\u548c\u6211\u4eec\u5728\u4e0a\u4e00\u8282\u5c55\u793a\u7684\u8f93\u51fa\u4e00\u6837\u3002<br>\u6574\u7406\u4e00\u4e0b\uff0c\u7528 ONNX Python API \u6784\u9020\u6a21\u578b\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import onnx \nfrom onnx import helper \nfrom onnx import TensorProto \n \n<em># input and output <\/em>\na = helper.make_tensor_value_info('a', TensorProto.FLOAT, &#91;10, 10]) \nx = helper.make_tensor_value_info('x', TensorProto.FLOAT, &#91;10, 10]) \nb = helper.make_tensor_value_info('b', TensorProto.FLOAT, &#91;10, 10]) \noutput = helper.make_tensor_value_info('output', TensorProto.FLOAT, &#91;10, 10]) \n \n<em># Mul <\/em>\nmul = helper.make_node('Mul', &#91;'a', 'x'], &#91;'c']) \n \n<em># Add <\/em>\nadd = helper.make_node('Add', &#91;'c', 'b'], &#91;'output']) \n \n<em># graph and model <\/em>\ngraph = helper.make_graph(&#91;mul, add], 'linear_func', &#91;a, x, b], &#91;output]) \nmodel = helper.make_model(graph) \n \n<em># save model <\/em>\nonnx.checker.check_model(model) \nprint(model) \nonnx.save(model, 'linear_func.onnx') <\/code><\/pre>\n\n\n\n<p>\u8001\u89c4\u77e9\uff0c\u6211\u4eec\u53ef\u4ee5\u7528 ONNX Runtime \u8fd0\u884c\u6a21\u578b\uff0c\u6765\u770b\u770b\u6a21\u578b\u662f\u5426\u6b63\u786e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import onnxruntime \nimport numpy as np \n \nsess = onnxruntime.InferenceSession('linear_func.onnx') \na = np.random.rand(10, 10).astype(np.float32) \nb = np.random.rand(10, 10).astype(np.float32) \nx = np.random.rand(10, 10).astype(np.float32) \n \noutput = sess.run(&#91;'output'], {'a': a, 'b': b, 'x': x})&#91;0] \n \nassert np.allclose(output, a * x + b) <\/code><\/pre>\n\n\n\n<p>\u4e00\u5207\u987a\u5229\u7684\u8bdd\uff0c\u8fd9\u6bb5\u4ee3\u7801\u4e0d\u4f1a\u6709\u4efb\u4f55\u62a5\u9519\u4fe1\u606f\u3002\u8fd9\u8bf4\u660e\u6211\u4eec\u7684\u6a21\u578b\u7b49\u4ef7\u4e8e\u6267\u884c&nbsp;<code>a * x + b<\/code>&nbsp;\u8fd9\u4e2a\u8ba1\u7b97\u3002<\/p>\n\n\n\n<h3 id=\"h_516920606_5\"><br>\u8bfb\u53d6\u5e76\u4fee\u6539 ONNX \u6a21\u578b<\/h3>\n\n\n\n<p>\u901a\u8fc7\u7528 API \u6784\u9020 ONNX \u6a21\u578b\uff0c\u6211\u4eec\u5df2\u7ecf\u5f7b\u5e95\u641e\u61c2\u4e86 ONNX \u7531\u54ea\u4e9b\u6a21\u5757\u7ec4\u6210\u3002\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u8be5\u5982\u4f55\u8bfb\u53d6\u73b0\u6709\u7684&#8221;.onnx&#8221;\u6587\u4ef6\u5e76\u4ece\u4e2d\u63d0\u53d6\u6a21\u578b\u4fe1\u606f\u3002<br>\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u8bfb\u53d6\u4e00\u4e2a ONNX \u6a21\u578b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import onnx \nmodel = onnx.load('linear_func.onnx') \nprint(model) <\/code><\/pre>\n\n\n\n<p>\u4e4b\u524d\u5728\u8f93\u51fa\u6a21\u578b\u65f6\uff0c\u6211\u4eec\u4f20\u7ed9&nbsp;<code>onnx.save<\/code>&nbsp;\u7684\u662f\u4e00\u4e2a&nbsp;<code>ModelProto<\/code>&nbsp;\u7684\u5bf9\u8c61\u3002\u540c\u7406\uff0c\u7528\u4e0a\u9762\u7684&nbsp;<code>onnx.load<\/code>&nbsp;\u8bfb\u53d6 ONNX \u6a21\u578b\u65f6\uff0c\u6211\u4eec\u6536\u83b7\u7684\u4e5f\u662f\u4e00\u4e2a&nbsp;<code>ModelProto<\/code>&nbsp;\u7684\u5bf9\u8c61\u3002\u8f93\u51fa\u8fd9\u4e2a\u5bf9\u8c61\u540e\uff0c\u6211\u4eec\u5e94\u8be5\u5f97\u5230\u548c\u4e4b\u524d\u5b8c\u5168\u76f8\u540c\u7684\u8f93\u51fa\u3002<br>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u6765\u770b\u770b\u600e\u4e48\u628a\u56fe&nbsp;<code>GraphProto<\/code>\u3001\u8282\u70b9&nbsp;<code>NodeProto<\/code>\u3001\u5f20\u91cf\u4fe1\u606f&nbsp;<code>ValueInfoProto<\/code>&nbsp;\u8bfb\u53d6\u51fa\u6765\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>graph = model.graph \nnode = graph.node \ninput = graph.input \noutput = graph.output \nprint(node) \nprint(input) \nprint(output) <\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528\u5982\u4e0a\u8fd9\u4e9b\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u5206\u522b\u8bbf\u95ee\u6a21\u578b\u7684\u56fe\u3001\u8282\u70b9\u3001\u5f20\u91cf\u4fe1\u606f\u3002\u8fd9\u91cc\u5927\u5bb6\u6216\u8bb8\u4f1a\u6709\u7591\u95ee\uff1a\u8be5\u600e\u6837\u627e\u51fa&nbsp;<code>graph.node,graph.input<\/code>&nbsp;\u4e2d&nbsp;<code>node, input<\/code>&nbsp;\u8fd9\u4e9b\u5c5e\u6027\u540d\u79f0\u5462\uff1f\u5176\u5b9e\uff0c\u5c5e\u6027\u7684\u540d\u79f0\u5c31\u5199\u5728\u6bcf\u4e2a\u5bf9\u8c61\u7684\u8f93\u51fa\u91cc\u3002\u6211\u4eec\u4ee5&nbsp;<code>print(node)<\/code>&nbsp;\u7684\u8f93\u51fa\u4e3a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;input: \"a\" \ninput: \"x\" \noutput: \"c\" \nop_type: \"Mul\" \n, input: \"c\" \ninput: \"b\" \noutput: \"output\" \nop_type: \"Add\" \n] <\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u6bb5\u8f93\u51fa\u4e2d\uff0c\u6211\u4eec\u80fd\u770b\u51fa&nbsp;<code>node<\/code>&nbsp;\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5217\u8868\u4e2d\u7684\u5bf9\u8c61\u6709\u5c5e\u6027&nbsp;<code>input, output, op_type<\/code>\uff08\u8fd9\u91cc&nbsp;<code>input<\/code>&nbsp;\u4e5f\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5b83\u5305\u542b\u7684\u4e24\u4e2a\u5143\u7d20\u90fd\u663e\u793a\u51fa\u6765\u4e86\uff09\u3002\u6211\u4eec\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u6765\u83b7\u53d6&nbsp;<code>node<\/code>&nbsp;\u91cc\u7b2c\u4e00\u4e2a\u8282\u70b9&nbsp;<code>Mul<\/code>&nbsp;\u7684\u5c5e\u6027\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>node_0 = node&#91;0] \nnode_0_inputs = node_0.input \nnode_0_outputs = node_0.output \ninput_0 = node_0_inputs&#91;0] \ninput_1 = node_0_inputs&#91;1] \noutput = node_0_outputs&#91;0] \nop_type = node_0.op_type \n \nprint(input_0) \nprint(input_1) \nprint(output) \nprint(op_type) \n \n<em># Output <\/em>\n\"\"\" \na \nx \nc \nMul \n\"\"\" <\/code><\/pre>\n\n\n\n<p>\u5f53\u6211\u4eec\u60f3\u77e5\u9053 ONNX \u6a21\u578b\u67d0\u6570\u636e\u5bf9\u8c61\u6709\u54ea\u4e9b\u5c5e\u6027\u65f6\uff0c\u6211\u4eec\u4e0d\u5fc5\u53bb\u7ffb ONNX \u6587\u6863\uff0c\u53ea\u9700\u8981\u5148\u628a\u6570\u636e\u5bf9\u8c61\u8f93\u51fa\u4e00\u4e0b\uff0c\u7136\u540e\u5728\u8f93\u51fa\u7ed3\u679c\u627e\u51fa\u5c5e\u6027\u540d\u5373\u53ef\u3002<br>\u8bfb\u53d6 ONNX \u6a21\u578b\u7684\u4fe1\u606f\u540e\uff0c\u4fee\u6539 ONNX \u6a21\u578b\u5c31\u662f\u4e00\u4ef6\u5f88\u8f7b\u677e\u7684\u4e8b\u4e86\u3002\u6211\u4eec\u65e2\u53ef\u4ee5\u6309\u7167\u4e0a\u4e00\u5c0f\u8282\u7684\u6a21\u578b\u6784\u9020\u65b9\u6cd5\uff0c\u65b0\u5efa\u8282\u70b9\u548c\u5f20\u91cf\u4fe1\u606f\uff0c\u4e0e\u539f\u6709\u6a21\u578b\u7ec4\u5408\u6210\u4e00\u4e2a\u65b0\u7684\u6a21\u578b\uff0c\u4e5f\u53ef\u4ee5\u5728\u4e0d\u8fdd\u53cd ONNX \u89c4\u8303\u7684\u524d\u63d0\u4e0b\u76f4\u63a5\u4fee\u6539\u67d0\u4e2a\u6570\u636e\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<br>\u8fd9\u91cc\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u76f4\u63a5\u4fee\u6539\u6a21\u578b\u5c5e\u6027\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import onnx \nmodel = onnx.load('linear_func.onnx') \n \nnode = model.graph.node \nnode&#91;1].op_type = 'Sub' \n \nonnx.checker.check_model(model) \nonnx.save(model, 'linear_func_2.onnx') <\/code><\/pre>\n\n\n\n<p>\u5728\u8bfb\u5165\u4e4b\u524d\u7684&nbsp;<code>linear_func.onnx<\/code>&nbsp;\u6a21\u578b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u7b2c\u4e8c\u4e2a\u8282\u70b9\u7684\u7c7b\u578b&nbsp;<code>node[1].op_type<\/code>\uff0c\u628a\u52a0\u6cd5\u53d8\u6210\u51cf\u6cd5\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u7684\u6a21\u578b\u63cf\u8ff0\u7684\u662f&nbsp;<code>a * x - b<\/code>&nbsp;\u8fd9\u4e2a\u7ebf\u6027\u51fd\u6570\u3002\u5927\u5bb6\u611f\u5174\u8da3\u7684\u8bdd\uff0c\u53ef\u4ee5\u7528 ONNX Runtime \u8fd0\u884c\u65b0\u6a21\u578b&nbsp;<code>linear_func_2.onnx<\/code>\uff0c\u6765\u9a8c\u8bc1\u4e00\u4e0b\u5b83\u548c&nbsp;<code>a * x - b<\/code>&nbsp;\u662f\u5426\u7b49\u4ef7\u3002<\/p>\n\n\n\n<h2 id=\"h_516920606_6\">\u8c03\u8bd5 ONNX \u6a21\u578b<\/h2>\n\n\n\n<p>\u5728\u5b9e\u9645\u90e8\u7f72\u4e2d\uff0c\u5982\u679c\u7528\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u5bfc\u51fa\u7684 ONNX \u6a21\u578b\u51fa\u4e86\u95ee\u9898\uff0c\u4e00\u822c\u8981\u901a\u8fc7\u4fee\u6539\u6846\u67b6\u7684\u4ee3\u7801\u6765\u89e3\u51b3\uff0c\u800c\u4e0d\u4f1a\u4ece ONNX \u5165\u624b\uff0c\u6211\u4eec\u628a ONNX \u6a21\u578b\u5f53\u6210\u4e00\u4e2a\u4e0d\u53ef\u4fee\u6539\u7684\u9ed1\u76d2\u770b\u5f85\u3002<br>\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u6df1\u5165\u5b66\u4e60\u4e86 ONNX \u7684\u539f\u7406\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5bf9 ONNX \u6a21\u578b\u672c\u8eab\u8fdb\u884c\u8c03\u8bd5\u4e86\u3002\u5728\u8fd9\u4e00\u8282\u91cc\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u8be5\u5982\u4f55\u5de7\u5999\u5229\u7528 ONNX \u63d0\u4f9b\u7684\u5b50\u6a21\u578b\u63d0\u53d6\u529f\u80fd\uff0c\u5bf9 ONNX \u6a21\u578b\u8fdb\u884c\u8c03\u8bd5\u3002<\/p>\n\n\n\n<h3 id=\"h_516920606_7\">\u5b50\u6a21\u578b\u63d0\u53d6<\/h3>\n\n\n\n<p>ONNX \u5b98\u65b9\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u5b50\u6a21\u578b\u63d0\u53d6\uff08extract\uff09\u7684\u529f\u80fd\u3002\u5b50\u6a21\u578b\u63d0\u53d6\uff0c\u987e\u540d\u601d\u4e49\uff0c\u5c31\u662f\u4ece\u4e00\u4e2a\u7ed9\u5b9a\u7684 ONNX \u6a21\u578b\u4e2d\uff0c\u62ff\u51fa\u4e00\u4e2a\u5b50\u6a21\u578b\u3002\u8fd9\u4e2a\u5b50\u6a21\u578b\u7684\u8282\u70b9\u96c6\u3001\u8fb9\u96c6\u90fd\u662f\u539f\u6a21\u578b\u4e2d\u5bf9\u5e94\u96c6\u5408\u7684\u5b50\u96c6\u3002\u8ba9\u6211\u4eec\u6765\u7528 PyTorch \u5bfc\u51fa\u4e00\u4e2a\u590d\u6742\u4e00\u70b9\u7684 ONNX \u6a21\u578b\uff0c\u5e76\u5728\u5b83\u7684\u57fa\u7840\u4e0a\u6267\u884c\u63d0\u53d6\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch \n \nclass Model(torch.nn.Module): \n \n    def __init__(self): \n        super().__init__() \n        self.convs1 = torch.nn.Sequential(torch.nn.Conv2d(3, 3, 3), \n                                          torch.nn.Conv2d(3, 3, 3), \n                                          torch.nn.Conv2d(3, 3, 3)) \n        self.convs2 = torch.nn.Sequential(torch.nn.Conv2d(3, 3, 3), \n                                          torch.nn.Conv2d(3, 3, 3)) \n        self.convs3 = torch.nn.Sequential(torch.nn.Conv2d(3, 3, 3), \n                                          torch.nn.Conv2d(3, 3, 3)) \n        self.convs4 = torch.nn.Sequential(torch.nn.Conv2d(3, 3, 3), \n                                          torch.nn.Conv2d(3, 3, 3), \n                                          torch.nn.Conv2d(3, 3, 3)) \n    def forward(self, x): \n        x = self.convs1(x) \n        x1 = self.convs2(x) \n        x2 = self.convs3(x) \n        x = x1 + x2 \n        x = self.convs4(x) \n        return x \n \nmodel = Model() \ninput = torch.randn(1, 3, 20, 20) \n \ntorch.onnx.export(model, input, 'whole_model.onnx') <\/code><\/pre>\n\n\n\n<p><br>\u8fd9\u4e2a\u6a21\u578b\u7684\u53ef\u89c6\u5316\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff08\u63d0\u53d6\u5b50\u6a21\u578b\u9700\u8981\u8f93\u5165\u8fb9\u7684\u5e8f\u53f7\uff0c\u4e3a\u4e86\u5927\u5bb6\u65b9\u9762\u9605\u8bfb\uff0c\u8fd9\u5e45\u56fe\u6807\u51fa\u4e86\u4e4b\u540e\u8981\u7528\u5230\u7684\u8fb9\u7684\u5e8f\u53f7\uff09\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"441\" height=\"1024\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2023\/02\/image-20-441x1024.png\" alt=\"\" class=\"wp-image-12656\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2023\/02\/image-20-441x1024.png 441w, http:\/\/139.9.1.231\/wp-content\/uploads\/2023\/02\/image-20-129x300.png 129w, http:\/\/139.9.1.231\/wp-content\/uploads\/2023\/02\/image-20.png 474w\" sizes=\"(max-width: 441px) 100vw, 441px\" \/><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u5728\u524d\u9762\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5b66\u8fc7\uff0cONNX \u7684\u8fb9\u7528\u540c\u540d\u5f20\u91cf\u8868\u793a\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8fd9\u91cc\u7684\u8fb9\u5e8f\u53f7\uff0c\u5b9e\u9645\u4e0a\u662f\u524d\u4e00\u4e2a\u8282\u70b9\u7684\u8f93\u51fa\u5f20\u91cf\u5e8f\u53f7\u548c\u540e\u4e00\u4e2a\u8282\u70b9\u7684\u8f93\u5165\u5f20\u91cf\u5e8f\u53f7\u3002\u7531\u4e8e\u8fd9\u4e2a\u6a21\u578b\u662f\u7528 PyTorch \u5bfc\u51fa\u7684\uff0c\u8fd9\u4e9b\u5f20\u91cf\u5e8f\u53f7\u90fd\u662f PyTorch \u81ea\u52a8\u751f\u6210\u7684\u3002<\/p><\/blockquote>\n\n\n\n<p><br>\u63a5\u7740\uff0c\u6211\u4eec\u53ef\u4ee5\u4e0b\u9762\u7684\u4ee3\u7801\u63d0\u53d6\u51fa\u4e00\u4e2a\u5b50\u6a21\u578b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import onnx  \n \nonnx.utils.extract_model('whole_model.onnx', 'partial_model.onnx', &#91;'22'], &#91;'28']) <\/code><\/pre>\n\n\n\n<p>\u5b50\u6a21\u578b\u7684\u53ef\u89c6\u5316\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/pic1.zhimg.com\/v2-0183e3d79d3b4fecf70c15ce33c37a20_r.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u901a\u8fc7\u89c2\u5bdf\u4ee3\u7801\u548c\u8f93\u51fa\u56fe\uff0c\u5e94\u8be5\u4e0d\u96be\u731c\u51fa\u8fd9\u6bb5\u4ee3\u7801\u7684\u4f5c\u7528\u662f\u628a\u539f\u8ba1\u7b97\u56fe\u4ece\u8fb9 22 \u5230\u8fb9 28 \u7684\u5b50\u56fe\u63d0\u53d6\u51fa\u6765\uff0c\u5e76\u7ec4\u6210\u4e00\u4e2a\u5b50\u6a21\u578b\u3002<code>onnx.utils.extract_model<\/code>&nbsp;\u5c31\u662f\u5b8c\u6210\u5b50\u6a21\u578b\u63d0\u53d6\u7684\u51fd\u6570\uff0c\u5b83\u7684\u53c2\u6570\u5206\u522b\u662f\u539f\u6a21\u578b\u8def\u5f84\u3001\u8f93\u51fa\u6a21\u578b\u8def\u5f84\u3001\u5b50\u6a21\u578b\u7684\u8f93\u5165\u8fb9\uff08\u8f93\u5165\u5f20\u91cf\uff09\u3001\u5b50\u6a21\u578b\u7684\u8f93\u51fa\u8fb9\uff08\u8f93\u51fa\u5f20\u91cf\uff09\u3002<br>\u76f4\u89c2\u5730\u6765\u770b\uff0c\u5b50\u6a21\u578b\u63d0\u53d6\u5c31\u662f\u628a\u8f93\u5165\u8fb9\u5230\u8f93\u51fa\u8fb9\u4e4b\u95f4\u7684\u5168\u90e8\u8282\u70b9\u90fd\u53d6\u51fa\u6765\u3002\u90a3\u4e48\uff0c\u8fd9\u4e2a\u529f\u80fd\u5728\u4f7f\u7528\u4e0a\u6709\u4ec0\u4e48\u9650\u5236\u5462\uff1f\u57fa\u4e8e&nbsp;<code>whole_model.onnx<\/code>, \u6211\u4eec\u6765\u770b\u4e00\u770b\u4e09\u4e2a\u5b50\u6a21\u578b\u63d0\u53d6\u7684\u793a\u4f8b\u3002<\/p>\n\n\n\n<p><strong>\u6dfb\u52a0\u989d\u5916\u8f93\u51fa<\/strong><\/p>\n\n\n\n<p>\u6211\u4eec\u5728\u63d0\u53d6\u65f6\u65b0\u8bbe\u5b9a\u4e86\u4e00\u4e2a\u8f93\u51fa\u5f20\u91cf\uff0c\u5982\u4e0b\u9762\u7684\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>onnx.utils.extract_model('whole_model.onnx', 'submodel_1.onnx', &#91;'22'], &#91;'27', '31']) <\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5b50\u6a21\u578b\u4f1a\u6dfb\u52a0\u4e00\u6761\u628a\u5f20\u91cf\u8f93\u51fa\u7684\u65b0\u8fb9\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/pic4.zhimg.com\/v2-9d94c3e72ec6613cca014f4b33bcfce3_r.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u6dfb\u52a0\u5197\u4f59\u8f93\u5165<\/strong><\/p>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u8fd8\u662f\u50cf\u5f00\u59cb\u4e00\u6837\u63d0\u53d6\u8fb9 22 \u5230\u8fb9 28 \u4e4b\u95f4\u7684\u5b50\u6a21\u578b\uff0c\u4f46\u662f\u591a\u6dfb\u52a0\u4e86\u4e00\u4e2a\u8f93\u5165&nbsp;<code>input.1<\/code>\uff0c\u90a3\u4e48\u63d0\u53d6\u51fa\u7684\u5b50\u6a21\u578b\u4f1a\u6709\u4e00\u4e2a\u5197\u4f59\u7684\u8f93\u5165&nbsp;<code>input.1<\/code>\uff0c\u5982\u4e0b\u9762\u7684\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>onnx.utils.extract_model('whole_model.onnx', 'submodel_2.onnx', &#91;'22', 'input.1'], &#91;'28']) <\/code><\/pre>\n\n\n\n<p>\u4ece\u4e0b\u56fe\u53ef\u4ee5\u770b\u5230\uff1a\u65e0\u8bba\u7ed9\u8fd9\u4e2a\u8f93\u5165\u4f20\u5165\u4ec0\u4e48\u503c\uff0c\u90fd\u4e0d\u4f1a\u5f71\u54cd\u5b50\u6a21\u578b\u7684\u8f93\u51fa\u3002\u53ef\u4ee5\u8ba4\u4e3a\u5982\u679c\u53ea\u7528\u5b50\u6a21\u578b\u7684\u90e8\u5206\u8f93\u5165\u5c31\u80fd\u5f97\u5230\u8f93\u51fa\uff0c\u90a3\u4e48\u90a3\u4e9b\u201d\u8f83\u65e9\u201c\u7684\u591a\u51fa\u6765\u7684\u8f93\u5165\u5c31\u662f\u5197\u4f59\u7684\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/pic3.zhimg.com\/v2-26796a616cc96e18a7083d06b02dcbca_r.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>\u8f93\u5165\u4fe1\u606f\u4e0d\u8db3<\/strong><\/p>\n\n\n\n<p>\u8fd9\u6b21\uff0c\u6211\u4eec\u5c1d\u8bd5\u63d0\u53d6\u7684\u5b50\u6a21\u578b\u8f93\u5165\u662f\u8fb9 24\uff0c\u8f93\u51fa\u662f\u8fb9 28\u3002\u5982\u4e0b\u9762\u7684\u4ee3\u7801\u548c\u56fe\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em># Error <\/em>\nonnx.utils.extract_model('whole_model.onnx', 'submodel_3.onnx', &#91;'24'], &#91;'28']) <\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/pic2.zhimg.com\/v2-9cf596e9f9587595ed4dfbc4d45ceeb5_r.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u4ece\u56fe\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u60f3\u901a\u8fc7\u8fb9 24 \u8ba1\u7b97\u8fb9 28 \u7684\u7ed3\u679c\uff0c\u81f3\u5c11\u8fd8\u9700\u8981\u8f93\u5165\u8fb9 26\uff0c\u6216\u8005\u66f4\u4e0a\u9762\u7684\u8fb9\u3002\u4ec5\u51ed\u501f\u8fb9 24 \u662f\u65e0\u6cd5\u8ba1\u7b97\u51fa\u8fb9 28 \u7684\u7ed3\u679c\u7684\uff0c\u56e0\u6b64\u8fd9\u6837\u63d0\u53d6\u5b50\u6a21\u578b\u4f1a\u62a5\u9519\u3002<br><br>\u901a\u8fc7\u4e0a\u9762\u51e0\u4e2a\u4f7f\u7528\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u6574\u7406\u51fa\u5b50\u6a21\u578b\u63d0\u53d6\u7684\u5b9e\u73b0\u539f\u7406\uff1a\u65b0\u5efa\u4e00\u4e2a\u6a21\u578b\uff0c\u628a\u7ed9\u5b9a\u7684\u8f93\u5165\u548c\u8f93\u51fa\u586b\u5165\u3002\u4e4b\u540e\u628a\u56fe\u7684\u6240\u6709\u6709\u5411\u8fb9\u53cd\u5411\uff0c\u4ece\u8f93\u51fa\u8fb9\u5f00\u59cb\u904d\u5386\u8282\u70b9\uff0c\u78b0\u5230\u8f93\u5165\u8fb9\u5219\u505c\u6b62\uff0c\u628a\u8fd9\u6837\u904d\u5386\u5f97\u5230\u7684\u8282\u70b9\u505a\u4e3a\u5b50\u6a21\u578b\u7684\u8282\u70b9\u3002<br>\u5982\u679c\u8fd8\u6ca1\u6709\u5f7b\u5e95\u5f04\u61c2\u8fd9\u4e2a\u63d0\u53d6\u539f\u7406\uff0c\u6ca1\u5173\u7cfb\uff0c\u6211\u4eec\u53ea\u8981\u5c3d\u91cf\u4fdd\u8bc1\u5728\u586b\u5199\u5b50\u6a21\u578b\u7684\u8f93\u5165\u8f93\u51fa\u65f6\uff0c\u8ba9\u8f93\u51fa\u6070\u597d\u53ef\u4ee5\u7531\u8f93\u5165\u51b3\u5b9a\u5373\u53ef\u3002<\/p>\n\n\n\n<h3 id=\"h_516920606_8\">\u8f93\u51fa ONNX \u4e2d\u95f4\u8282\u70b9\u7684\u503c<\/h3>\n\n\n\n<p>\u5728\u4f7f\u7528 ONNX \u6a21\u578b\u65f6\uff0c\u6700\u5e38\u89c1\u7684\u4e00\u4e2a\u9700\u6c42\u662f\u80fd\u591f\u7528\u63a8\u7406\u5f15\u64ce\u8f93\u51fa\u4e2d\u95f4\u8282\u70b9\u7684\u503c\u3002\u8fd9\u591a\u89c1\u4e8e\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u6a21\u578b\u548c ONNX \u6a21\u578b\u7684\u7cbe\u5ea6\u5bf9\u9f50\u4e2d\uff0c\u56e0\u4e3a\u53ea\u8981\u80fd\u591f\u8f93\u51fa\u4e2d\u95f4\u8282\u70b9\u7684\u503c\uff0c\u5c31\u80fd\u5b9a\u4f4d\u5230\u7cbe\u5ea6\u51fa\u73b0\u504f\u5dee\u7684\u7b97\u5b50\u3002\u6211\u4eec\u6765\u770b\u770b\u5982\u4f55\u7528\u5b50\u6a21\u578b\u63d0\u53d6\u5b9e\u73b0\u8fd9\u4e00\u4efb\u52a1\u3002<br>\u5728\u521a\u521a\u7684\u7b2c\u4e00\u4e2a\u5b50\u6a21\u578b\u63d0\u53d6\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u6761\u539f\u6765\u6a21\u578b\u4e2d\u4e0d\u5b58\u5728\u7684\u8f93\u51fa\u8fb9\u3002\u7528\u540c\u6837\u7684\u539f\u7406\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4fdd\u6301\u539f\u6709\u8f93\u5165\u8f93\u51fa\u4e0d\u53d8\u7684\u540c\u65f6\uff0c\u65b0\u589e\u52a0\u4e00\u4e9b\u8f93\u51fa\uff0c\u63d0\u53d6\u51fa\u4e00\u4e2a\u80fd\u8f93\u51fa\u4e2d\u95f4\u8282\u70b9\u7684\u201d\u5b50\u6a21\u578b\u201c\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> onnx.utils.extract_model('whole_model.onnx', 'more_output_model.onnx', &#91;'input.1'], &#91;'31', '23', '25', '27'])<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u5b50\u6a21\u578b\u4e2d\uff0c\u6211\u4eec\u5728\u4fdd\u6301\u539f\u6709\u7684\u8f93\u5165&nbsp;<code>input.1<\/code>\uff0c\u8f93\u51fa&nbsp;<code>31<\/code>&nbsp;\u7684\u540c\u65f6\uff0c\u628a\u5176\u4ed6\u51e0\u4e2a\u8fb9\u52a0\u5165\u4e86\u8f93\u51fa\u4e2d\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/pic1.zhimg.com\/80\/v2-eb0e51ba7e6d6e920dc638d65678b4cc_720w.webp\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u8fd9\u6837\uff0c\u7528 ONNX Runtime \u8fd0\u884c&nbsp;<code>more_output_model.onnx<\/code>&nbsp;\u8fd9\u4e2a\u6a21\u578b\u65f6\uff0c\u6211\u4eec\u5c31\u80fd\u5f97\u5230\u66f4\u591a\u7684\u8f93\u51fa\u4e86\u3002<br>\u4e3a\u4e86\u65b9\u4fbf\u8c03\u8bd5\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u628a\u539f\u6a21\u578b\u62c6\u5206\u6210\u591a\u4e2a\u4e92\u4e0d\u76f8\u4ea4\u7684\u5b50\u6a21\u578b\u3002\u8fd9\u6837\uff0c\u5728\u6bcf\u6b21\u8c03\u8bd5\u65f6\uff0c\u53ef\u4ee5\u53ea\u5bf9\u539f\u6a21\u578b\u7684\u90e8\u5206\u5b50\u6a21\u5757\u8c03\u8bd5\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>onnx.utils.extract_model('whole_model.onnx', 'debug_model_1.onnx', &#91;'input.1'], &#91;'23']) \nonnx.utils.extract_model('whole_model.onnx', 'debug_model_2.onnx', &#91;'23'], &#91;'25']) \nonnx.utils.extract_model('whole_model.onnx', 'debug_model_3.onnx', &#91;'23'], &#91;'27']) \nonnx.utils.extract_model('whole_model.onnx', 'debug_model_4.onnx', &#91;'25', '27'], &#91;'31']) <\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u628a\u539f\u6765\u8f83\u4e3a\u590d\u6742\u7684\u6a21\u578b\u62c6\u6210\u4e86\u56db\u4e2a\u8f83\u4e3a\u7b80\u5355\u7684\u5b50\u6a21\u578b\uff0c\u5982\u4e0b\u56fe\u6240\u793a\u3002\u5728\u8c03\u8bd5\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u8c03\u8bd5\u9876\u5c42\u7684\u5b50\u6a21\u578b\uff0c\u786e\u8ba4\u9876\u5c42\u5b50\u6a21\u578b\u65e0\u8bef\u540e\uff0c\u628a\u5b83\u7684\u8f93\u51fa\u505a\u4e3a\u540e\u9762\u5b50\u6a21\u578b\u7684\u8f93\u5165\u3002<br>\u6bd4\u5982\u5bf9\u4e8e\u8fd9\u4e9b\u5b50\u6a21\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u8c03\u8bd5\u7b2c\u4e00\u4e2a\u5b50\u6a21\u578b\uff0c\u5e76\u5b58\u50a8\u8f93\u51fa 23\u3002\u4e4b\u540e\u628a\u5f20\u91cf 23 \u505a\u4e3a\u7b2c\u4e8c\u4e2a\u548c\u7b2c\u4e09\u4e2a\u5b50\u6a21\u578b\u7684\u8f93\u5165\uff0c\u8c03\u8bd5\u8fd9\u4e24\u4e2a\u6a21\u578b\u3002\u6700\u540e\u7528\u540c\u6837\u65b9\u6cd5\u8c03\u8bd5\u7b2c\u56db\u4e2a\u5b50\u6a21\u578b\u3002\u53ef\u4ee5\u8bf4\uff0c\u6709\u4e86\u5b50\u6a21\u578b\u63d0\u53d6\u529f\u80fd\uff0c\u54ea\u6015\u662f\u9762\u5bf9\u4e00\u4e2a\u5e9e\u5927\u7684\u6a21\u578b\uff0c\u6211\u4eec\u4e5f\u80fd\u591f\u4ece\u4e2d\u63d0\u53d6\u51fa\u6709\u95ee\u9898\u7684\u5b50\u6a21\u5757\uff0c\u7ec6\u81f4\u5730\u53ea\u5bf9\u8fd9\u4e2a\u5b50\u6a21\u5757\u8c03\u8bd5\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/pic1.zhimg.com\/v2-950fd1851366dc480cb7126c623fbe90_r.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u5b50\u6a21\u578b\u63d0\u53d6\u56fa\u7136\u662f\u4e00\u4e2a\u4fbf\u5229\u7684 ONNX \u8c03\u8bd5\u5de5\u5177\u3002\u4f46\u662f\uff0c\u5728\u5b9e\u9645\u7684\u60c5\u51b5\u4e2d\uff0c\u6211\u4eec\u4e00\u822c\u662f\u7528 PyTorch \u7b49\u6846\u67b6\u5bfc\u51fa ONNX \u6a21\u578b\u3002\u8fd9\u91cc\u6709\u4e24\u4e2a\u95ee\u9898\uff1a<\/p>\n\n\n\n<ol><li>\u4e00\u65e6 PyTorch \u6a21\u578b\u6539\u53d8\uff0cONNX \u6a21\u578b\u7684\u8fb9\u5e8f\u53f7\u4e5f\u4f1a\u6539\u53d8\u3002\u8fd9\u6837\u6bcf\u6b21\u63d0\u53d6\u540c\u6837\u7684\u5b50\u6a21\u5757\u65f6\u90fd\u8981\u91cd\u65b0\u53bb ONNX \u6a21\u578b\u91cc\u67e5\u5e8f\u53f7\uff0c\u5982\u6b64\u7e41\u7410\u7684\u8c03\u8bd5\u65b9\u6cd5\u662f\u4e0d\u4f1a\u5728\u5b9e\u8df5\u4e2d\u91c7\u7528\u7684\u3002<\/li><li>\u5373\u4f7f\u6211\u4eec\u80fd\u4fdd\u8bc1 ONNX \u7684\u8fb9\u5e8f\u53f7\u4e0d\u53d1\u751f\u6539\u53d8\uff0c\u6211\u4eec\u4e5f\u96be\u4ee5\u628a PyTorch \u4ee3\u7801\u548c ONNX \u8282\u70b9\u5bf9\u5e94\u8d77\u6765\u2014\u2014\u5f53\u6a21\u578b\u7ed3\u6784\u53d8\u5f97\u5341\u5206\u590d\u6742\u65f6\uff0c\u8981\u8bc6\u522b ONNX \u4e2d\u6bcf\u4e2a\u8282\u70b9\u7684\u542b\u4e49\u662f\u4e0d\u53ef\u80fd\u7684\u3002<\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u81ea\uff1a\u6a21\u578b\u90e8\u7f72\u5165\u95e8\u6559\u7a0b\uff08\u4e94\uff09\uff1aONNX \u6a21\u578b\u7684\u4fee\u6539\u4e0e\u8c03\u8bd5 \u4e00\u76f4\u4ee5\u6765\uff0c\u6211\u4eec\u90fd\u662f\u901a\u8fc7 PyTorch \u6765\u5bfc\u51fa ON &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2023\/02\/01\/onnx-5\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">ONNX \u6a21\u578b\u7684\u4fee\u6539\u4e0e\u8c03\u8bd5<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,4,26],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/12653"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=12653"}],"version-history":[{"count":3,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/12653\/revisions"}],"predecessor-version":[{"id":12657,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/12653\/revisions\/12657"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=12653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=12653"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=12653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}