【问题标题】:How to open .pbtxt file?如何打开 .pbtxt 文件?
【发布时间】:2019-03-18 09:59:59
【问题描述】:

尝试更改 tensorflow 对象检测模型的标签,但无法打开 pbtxt 文件。能告诉我有没有什么应用可以打开吗?

【问题讨论】:

标签: tensorflow deep-learning conv-neural-network


【解决方案1】:

即您的 pbtxt 文件名为 graphfilename.pbtxt

示例

import tensorflow as tf
from tensorflow.core.framework import graph_pb2 as gpb
from google.protobuf import text_format as pbtf

gdef = gpb.GraphDef()

with open('graphfilename.pbtxt', 'r') as fh:
    graph_str = fh.read()

pbtf.Parse(graph_str, gdef)

tf.import_graph_def(gdef)

【讨论】:

  • 我认为你误解了这个问题。他只是询问如何打开标签文件来训练 Tensorflow 模型。这可以通过一个简单的文本编辑器来完成。
【解决方案2】:

这是读取 label_map.pbtxt 文件的解决方案。它不需要任何 protobuf 导入,因此适用于所有 TF 版本。

def read_label_map(label_map_path):

    item_id = None
    item_name = None
    items = {}
    
    with open(label_map_path, "r") as file:
        for line in file:
            line.replace(" ", "")
            if line == "item{":
                pass
            elif line == "}":
                pass
            elif "id" in line:
                item_id = int(line.split(":", 1)[1].strip())
            elif "name" in line:
                item_name = line.split(":", 1)[1].replace("'", "").strip()

            if item_id is not None and item_name is not None:
                items[item_name] = item_id
                item_id = None
                item_name = None

    return items

print ([i for i in read_label_map("label_map.pbtxt")])

【讨论】:

  • 这正是我想要的。谢谢,伙计,让事情变得如此简单。
  • 来到这里并使用了这个。请注意,如果您的 ID 中包含 id 或 name,这将不起作用。我添加了 id: 和 name: 并为我工作,但请确保您的数据中没有。
【解决方案3】:

刚刚修改了@leenremm 的代码,因为它不适合我!

def read_label_map(label_map_path):

    item_id = None
    item_name = None
    items = {}

    with open(label_map_path, "r") as file:
        for line in file:
            line.replace(" ", "")
            if line == "item{":
                pass
            elif line == "}":
                pass
            elif "id" in line:
                item_id = int(line.split(":", 1)[1].strip())
            elif "display_name" in line:
                item_name = line.split(" ")[-1].replace("\"", " ").strip()
            if item_id is not None and item_name is not None:
                items[item_id] = item_name
                item_id = None
                item_name = None

    return items

print(read_label_map(pbtxt_path))

该函数将返回一个字典,您可以在其中通过类别 ID 轻松“获取()”类别名称。

label_map = read_label_map(label_map_path)
label_map.get(2)

【讨论】:

    【解决方案4】:

    您可以在任何文本编辑器中打开它,例如 Sublime Text 或您的操作系统默认提供的任何内容。

    如果你使用的是 Linux/macOS 系统,你也可以在终端中通过定向到它的目录并写入来打开它

    nano {filename}.pbtxt
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-28
      • 2020-03-05
      • 2022-10-08
      • 1970-01-01
      • 2019-10-26
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      相关资源
      最近更新 更多