如何打开 .pbtxt 文件?

Har*_*ore 0 deep-learning conv-neural-network tensorflow

尝试更改 tensorflow 对象检测模型中的标签,但无法打开 pbtxt 文件。可以告诉我是否有任何应用程序可以打开它吗?

Sha*_*lam 5

即你的 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)
Run Code Online (Sandbox Code Playgroud)

  • 我认为你误解了这个问题。他只是在问如何打开标签文件来训练 Tensorflow 模型。这可以用一个简单的文本编辑器来完成。 (2认同)

lee*_*emm 5

这是读取 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("'", "").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")])
Run Code Online (Sandbox Code Playgroud)