I always find a list of classes on Github that represent the synset ID and name of each Imagenet class label. I need to view the WordNet hierarchy of ImageNet as a tree so I can prune some classes that I don't need based on conceptual relevance. This source is not good enough as it is not easy to look through.
2 Answers
See this website: https://observablehq.com/@mbostock/imagenet-hierarchy
You can also create the tree by yourself using the metadata in the ImageNet devkit.
-
$\begingroup$ While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review $\endgroup$– EthanCommented Sep 7, 2022 at 4:35
You can use nltk package in Python to get the hypernyms (parents) of each class. The following code saves the 6 most super-ordinate categories for each class label.
I downloaded the LOC_synset_mapping.txt file from here.
from nltk.corpus import wordnet as wn
import pandas as pd
import numpy as np
#-----------------------------
#read synset ids for classes:
#-----------------------------
map_fname=base_dir+'/LOC_synset_mapping.txt'
synset_ids_1000=np.zeros(1000)
l=0;
with open(map_fname, 'r') as f:
for line in f:
synset_id_s = line.split()[0]
synset_id=int(synset_id_s[1:])
synset_ids_1000[l]=synset_id
l=l+1
#-------------------------
#create the list of parents for all classes:
#-----------------------------
all_list=[["-" for j in range(7)] for i in range(1000)]
for i in range(1000):
synset=wn.synset_from_pos_and_offset('n',int(synset_ids_1000[i]))
hyper_list=[]
while synset.hypernyms():
synset = synset.hypernyms()[0]
hyper_list.append(synset.name())
hyper_list.insert(0,'null')
hyper_list.insert(0,'null')
all_list[i][:]=hyper_list[:-7:-1]
df=pd.DataFrame(all_list)
To see parents of the a specific class, e.g. 11th class:
df.iloc[10]
0 entity.n.01
1 physical_entity.n.01
2 object.n.01
3 whole.n.02
4 living_thing.n.01
5 organism.n.01
6 animal.n.01
to see number of different unique labels at a specific depth, e.g. 3:
df.iloc[:,3].value_counts()
whole.n.02 946
substance.n.07 22
solid.n.01 16
geological_formation.n.01 9
person.n.01 3
sign.n.02 1
signal.n.01 1
shape.n.02 1
substance.n.01 1