2

Trying to change the labels from the tensorflow object detection model but Not able to open pbtxt file. Can tell me if there is any application to open it?

2

5 Answers 5

5

i.e your pbtxt file name is graphfilename.pbtxt

Example

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)
1
  • 2
    I think you're misunderstanding the question. He is simply asking about how to open the label file for training a Tensorflow model. Which can be done with a simple text editor.
    – YoungChul
    Mar 18, 2019 at 10:48
4

Here is a solution to read the label_map.pbtxt file. It doesn't require any protobuf imports, so it works across all versions of 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")])
2
  • This is exactly what I was looking for. Thanks, man for making it so simple. May 13, 2021 at 16:20
  • 1
    came here and used this. One note, if your IDs include id or name in them this won't work. I added id: and name: and worked for me, but just make sure your data doesn't have that in it.
    – BaseballR
    Nov 18, 2021 at 21:47
3

You can open it in any text editor, like Sublime Text or whatever your OS has to offer by default.

If you are on a Linux/macOS system, you can also open it in Terminal by directing to its directory and writing

nano {filename}.pbtxt
3

Just modified the @leenremm's code as it wasnt working out for me!

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))

The function will return a dictionary where you can easily "get()" the category names by the category id.

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

You can open it using colab notebook, first mount google drive, using:

from google.colab import drive
drive.mount('/content/drive')

Then choose your .pdtxt file from drive files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.