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?

Harshal Deore
  • 1,050
  • 1
  • 11
  • 11
  • 1
    Possible duplicate of [open tensorflow graph from file](https://stackoverflow.com/questions/40305996/open-tensorflow-graph-from-file) – Onkar Musale Mar 18 '19 at 10:10
  • @OnkarMusale the labels pbtxt file in the object detection API is not a TF Graph, but a label map ([see MSCOCO example here](https://github.com/tensorflow/models/blob/master/research/object_detection/data/mscoco_label_map.pbtxt)). The OP doesn't need to load the graph, but literally just open the text file and modify it – GPhilo Mar 18 '19 at 10:51

5 Answers5

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)
Shariful Islam
  • 526
  • 3
  • 10
  • 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 '19 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")])
leenremm
  • 1,083
  • 13
  • 19
  • This is exactly what I was looking for. Thanks, man for making it so simple. – Achyut Sarma May 13 '21 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 '21 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
YoungChul
  • 165
  • 12
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)
STARmin
  • 25
  • 1
  • 9
blitu12345
  • 3,473
  • 1
  • 20
  • 21
-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.