philipperemy / yolo-9000

YOLO9000: Better, Faster, Stronger - Real-Time Object Detection. 9000 classes!
Apache License 2.0
1.18k stars 309 forks source link

How to parse the 9k.tree file to create a graph? #51

Closed ghost closed 2 years ago

ghost commented 2 years ago

Hello. I want to parse the tree to find common ancestors of the detections (sedan and van are both cars, so I want to assign the car label and not the labels referring to the car types) up to a certain ancestor (since the physical object label is the progenitor class for every other class label).

I opened the 9k.tree file and tried to understand how to create a graph, but I couldn't understand the numbers in the right column. Can you help me understand how this file works?

ghost commented 2 years ago

I found the solution. In the 9k.tree file, the line number is the index of that node and the number on the right is the index of the line that contains the parent node.

n00002452 -1
n00020827 -1
n00002684 -1
n11425580 -1
n05220461 0
n09225146 0

So the first 4 nodes are all root nodes, i.e. no parents. The last two are children of node at index 0, which is the first node listed.

So it'd be like this

├───0: n00002452 
│   ├───4: n05220461 
│   └───5: n09225146 
├───1: n00020827
├───2: n00002684 
├───3: n11425580  

Code to create a graph using the networkx library

    # get all 9k names
    names_file = open('data/9k.names')
    names_9k = []
    for name in names_file:
        names_9k.append(name.strip('\n'))

    # dictionary that represents graph of 9k names
    tree_file = open('data/9k.tree')
    dag_9k = {}
    line_counter = 0
    for line in tree_file:
        _, parent_node = line.strip('\n').split(' ')
        name = names_9k[line_counter]
        if name not in dag_9k:
            dag_9k[name] = []
        if int(parent_node) != -1:
            parent_node_label = names_9k[int(parent_node)]
            dag_9k[parent_node_label].append(name)
        line_counter += 1
    tree_graph = nx.DiGraph(dag_9k)
philipperemy commented 2 years ago

Awesome! Thanks for sharing :)