{"id":1154411,"date":"2025-01-13T17:50:58","date_gmt":"2025-01-13T09:50:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1154411.html"},"modified":"2025-01-13T17:51:01","modified_gmt":"2025-01-13T09:51:01","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96pbtxt%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1154411.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6pbtxt\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25184030\/ef78db83-19f3-48cd-a6fb-c38b8ebc5eee.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6pbtxt\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6pbtxt\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528TensorFlow\u7684\u51fd\u6570\u3001\u5c06pbtxt\u6587\u4ef6\u89e3\u6790\u4e3a\u6587\u672c\u683c\u5f0f\u3001\u4f7f\u7528Protobuf\u89e3\u6790\u3002<\/strong>\u8fd9\u91cc\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528TensorFlow\u7684\u51fd\u6570\u6765\u8bfb\u53d6pbtxt\u6587\u4ef6\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u4fbf\u800c\u4e14\u975e\u5e38\u9002\u5408\u5904\u7406\u4e0eTensorFlow\u76f8\u5173\u7684\u6a21\u578b\u548c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>TensorFlow\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5de5\u5177\u6765\u7b80\u5316\u4e0epbtxt\u6587\u4ef6\u7684\u4ea4\u4e92\u3002pbtxt\u6587\u4ef6\u901a\u5e38\u662f\u7528\u6765\u63cf\u8ff0TensorFlow\u56fe\uff08graph\uff09\u7ed3\u6784\u7684\u6587\u672c\u683c\u5f0f\u6587\u4ef6\u3002\u5728TensorFlow\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>tf.io.gfile.GFile<\/code>\u6765\u8bfb\u53d6pbtxt\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>tf.compat.v1.GraphDef<\/code>\u6765\u89e3\u6790\u8fd9\u4e9b\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528TensorFlow\u8bfb\u53d6pbtxt\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>1. \u5b89\u88c5TensorFlow<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5TensorFlow\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install tensorflow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bfb\u53d6pbtxt\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>tf.io.gfile.GFile<\/code>\u548c<code>tf.compat.v1.GraphDef<\/code>\u6765\u8bfb\u53d6\u548c\u89e3\u6790pbtxt\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>def read_pbtxt_file(file_path):<\/p>\n<p>    # \u521b\u5efa\u4e00\u4e2aGraphDef\u5bf9\u8c61<\/p>\n<p>    graph_def = tf.compat.v1.GraphDef()<\/p>\n<p>    # \u4f7f\u7528GFile\u6765\u8bfb\u53d6pbtxt\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>    with tf.io.gfile.GFile(file_path, &#39;r&#39;) as f:<\/p>\n<p>        # \u89e3\u6790\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>        graph_def.ParseFromString(f.read())<\/p>\n<p>    return graph_def<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = &#39;path\/to\/your\/model.pbtxt&#39;<\/p>\n<p>graph_def = read_pbtxt_file(file_path)<\/p>\n<h2><strong>\u6253\u5370\u8bfb\u53d6\u7684GraphDef\u5bf9\u8c61<\/strong><\/h2>\n<p>print(graph_def)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8be6\u7ec6\u89e3\u6790\u5185\u5bb9<\/h4>\n<\/p>\n<p><p><code>tf.compat.v1.GraphDef<\/code>\u5bf9\u8c61\u5305\u542b\u4e86TensorFlow\u56fe\u7684\u6240\u6709\u8282\u70b9\u548c\u7ed3\u6784\u4fe1\u606f\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u8fd9\u4e2a\u5bf9\u8c61\u6765\u83b7\u53d6\u66f4\u8be6\u7ec6\u7684\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u904d\u5386<code>GraphDef<\/code>\u5bf9\u8c61\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for node in graph_def.node:<\/p>\n<p>    print(&quot;Node name: &quot;, node.name)<\/p>\n<p>    print(&quot;Operation type: &quot;, node.op)<\/p>\n<p>    for input_name in node.input:<\/p>\n<p>        print(&quot;Input: &quot;, input_name)<\/p>\n<p>    print(&quot;\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5c06pbtxt\u6587\u4ef6\u89e3\u6790\u4e3a\u6587\u672c\u683c\u5f0f<\/h3>\n<\/p>\n<p><h4>1. \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570\u8bfb\u53d6pbtxt\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u89e3\u6790\u4e3a\u6587\u672c\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_pbtxt_as_text(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>    return content<\/p>\n<p>file_path = &#39;path\/to\/your\/model.pbtxt&#39;<\/p>\n<p>pbtxt_content = read_pbtxt_as_text(file_path)<\/p>\n<p>print(pbtxt_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u89e3\u6790\u6587\u672c\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u89e3\u6790pbtxt\u6587\u4ef6\u7684\u6587\u672c\u5185\u5bb9\uff0c\u901a\u5e38\u53ef\u4ee5\u501f\u52a9\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u5176\u4ed6\u6587\u672c\u5904\u7406\u5de5\u5177\u6765\u8fdb\u884c\u89e3\u6790\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def parse_pbtxt_content(content):<\/p>\n<p>    nodes = re.findall(r&#39;node\\s*\\{(.*?)\\}&#39;, content, re.DOTALL)<\/p>\n<p>    parsed_nodes = []<\/p>\n<p>    for node in nodes:<\/p>\n<p>        name = re.search(r&#39;name:\\s*&quot;([^&quot;]+)&quot;&#39;, node).group(1)<\/p>\n<p>        op = re.search(r&#39;op:\\s*&quot;([^&quot;]+)&quot;&#39;, node).group(1)<\/p>\n<p>        inputs = re.findall(r&#39;input:\\s*&quot;([^&quot;]+)&quot;&#39;, node)<\/p>\n<p>        parsed_nodes.append({<\/p>\n<p>            &#39;name&#39;: name,<\/p>\n<p>            &#39;op&#39;: op,<\/p>\n<p>            &#39;inputs&#39;: inputs<\/p>\n<p>        })<\/p>\n<p>    return parsed_nodes<\/p>\n<p>parsed_nodes = parse_pbtxt_content(pbtxt_content)<\/p>\n<p>for node in parsed_nodes:<\/p>\n<p>    print(node)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Protobuf\u89e3\u6790<\/h3>\n<\/p>\n<p><h4>1. \u5b89\u88c5protobuf\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5protobuf\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install protobuf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u89e3\u6790pbtxt\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u7f16\u5199\u89e3\u6790pbtxt\u6587\u4ef6\u7684\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>from google.protobuf import text_format<\/p>\n<p>def read_pbtxt_with_protobuf(file_path):<\/p>\n<p>    graph_def = tf.compat.v1.GraphDef()<\/p>\n<p>    with tf.io.gfile.GFile(file_path, &#39;r&#39;) as f:<\/p>\n<p>        text_format.Merge(f.read(), graph_def)<\/p>\n<p>    return graph_def<\/p>\n<p>file_path = &#39;path\/to\/your\/model.pbtxt&#39;<\/p>\n<p>graph_def = read_pbtxt_with_protobuf(file_path)<\/p>\n<p>print(graph_def)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8be6\u7ec6\u89e3\u6790\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u4e0e\u4f7f\u7528TensorFlow\u8bfb\u53d6pbtxt\u6587\u4ef6\u7684\u65b9\u5f0f\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u904d\u5386<code>GraphDef<\/code>\u5bf9\u8c61\u6765\u83b7\u53d6\u8be6\u7ec6\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for node in graph_def.node:<\/p>\n<p>    print(&quot;Node name: &quot;, node.name)<\/p>\n<p>    print(&quot;Operation type: &quot;, node.op)<\/p>\n<p>    for input_name in node.input:<\/p>\n<p>        print(&quot;Input: &quot;, input_name)<\/p>\n<p>    print(&quot;\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u548c\u89e3\u6790pbtxt\u6587\u4ef6\u3002<strong>\u4f7f\u7528TensorFlow\u7684\u51fd\u6570<\/strong>\u662f\u6700\u5e38\u7528\u4e14\u65b9\u4fbf\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5904\u7406TensorFlow\u76f8\u5173\u7684\u6a21\u578b\u548c\u6570\u636e\u3002<strong>\u5c06pbtxt\u6587\u4ef6\u89e3\u6790\u4e3a\u6587\u672c\u683c\u5f0f<\/strong>\u7684\u65b9\u6cd5\u9002\u5408\u5bf9\u6587\u4ef6\u5185\u5bb9\u8fdb\u884c\u624b\u52a8\u5206\u6790\u548c\u5904\u7406\u3002<strong>\u4f7f\u7528Protobuf\u89e3\u6790<\/strong>\u7684\u65b9\u6cd5\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u53ef\u9760\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528Protobuf\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\u6765\u89e3\u6790\u548c\u5904\u7406pbtxt\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u6765\u9009\u62e9\u9002\u5408\u7684\u65b9\u5f0f\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u5904\u7406pbtxt\u6587\u4ef6\u3002\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5904\u7406TensorFlow\u6a21\u578b\u548c\u6570\u636e\uff0c\u63d0\u5347\u5728<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u548c\u6df1\u5ea6\u5b66\u4e60\u4e2d\u7684\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u52a0\u8f7dpbtxt\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u52a0\u8f7dpbtxt\u6587\u4ef6\u901a\u5e38\u4f7f\u7528TensorFlow\u5e93\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>tf.io.gfile.GFile<\/code>\u51fd\u6570\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u6216\u8005\u4f7f\u7528TensorFlow\u7684\u89e3\u6790\u529f\u80fd\u6765\u89e3\u6790\u6a21\u578b\u7ed3\u6784\u3002\u786e\u4fdd\u60a8\u5df2\u7ecf\u5b89\u88c5TensorFlow\u5e93\uff0c\u5e76\u6309\u7167\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u52a0\u8f7dpbtxt\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-python\">import tensorflow as tf\n\nwith tf.io.gfile.GFile(&#39;path\/to\/your\/file.pbtxt&#39;, &#39;r&#39;) as f:\n    pbtxt_content = f.read()\nprint(pbtxt_content)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u6253\u5f00\u5e76\u8bfb\u53d6\u6307\u5b9a\u8def\u5f84\u7684pbtxt\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\u6216\u89e3\u6790\u5185\u5bb9\u3002<\/p>\n<p><strong>pbtxt\u6587\u4ef6\u7684\u7ed3\u6784\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>pbtxt\u6587\u4ef6\u662fProtocol Buffers\u6587\u672c\u683c\u5f0f\u7684\u4e00\u79cd\u8868\u793a\uff0c\u901a\u5e38\u7528\u4e8e\u4fdd\u5b58\u673a\u5668\u5b66\u4e60\u6a21\u578b\u7684\u7ed3\u6784\u548c\u914d\u7f6e\u3002\u5b83\u7684\u7ed3\u6784\u662f\u5c42\u6b21\u5316\u7684\uff0c\u5305\u542b\u591a\u4e2a\u8282\u70b9\uff0c\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u6a21\u578b\u7684\u4e00\u4e2a\u90e8\u5206\uff0c\u5982\u5c42\u3001\u64cd\u4f5c\u548c\u53c2\u6570\u3002\u4e86\u89e3pbtxt\u6587\u4ef6\u7684\u7ed3\u6784\u6709\u52a9\u4e8e\u60a8\u66f4\u597d\u5730\u89e3\u6790\u548c\u4f7f\u7528\u5176\u4e2d\u7684\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528pbtxt\u6587\u4ef6\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u9519\u8bef\u9700\u8981\u6ce8\u610f\uff1f<\/strong><br \/>\u5728\u5904\u7406pbtxt\u6587\u4ef6\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u6587\u4ef6\u8def\u5f84\u9519\u8bef\u3001\u6587\u4ef6\u683c\u5f0f\u4e0d\u6b63\u786e\u4ee5\u53ca\u7f3a\u5c11\u5fc5\u8981\u7684TensorFlow\u7248\u672c\u652f\u6301\u3002\u786e\u4fdd\u6587\u4ef6\u8def\u5f84\u6b63\u786e\uff0c\u5e76\u4e14pbtxt\u6587\u4ef6\u7b26\u5408TensorFlow\u7684\u8981\u6c42\u3002\u5982\u679c\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\u9047\u5230\u95ee\u9898\uff0c\u53ef\u4ee5\u68c0\u67e5TensorFlow\u7684\u7248\u672c\uff0c\u5e76\u67e5\u770b\u76f8\u5173\u7684\u6587\u6863\u4ee5\u83b7\u5f97\u66f4\u591a\u6307\u5bfc\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6pbtxt\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528TensorFlow\u7684\u51fd\u6570\u3001\u5c06pbtxt\u6587\u4ef6\u89e3\u6790\u4e3a\u6587\u672c\u683c\u5f0f\u3001\u4f7f\u7528Prot [&hellip;]","protected":false},"author":3,"featured_media":1154420,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154411"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1154411"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154411\/revisions"}],"predecessor-version":[{"id":1154423,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154411\/revisions\/1154423"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1154420"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1154411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1154411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1154411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}