I am using a keras based vgg model as an image classifier. From this webpage:
https://machinelearningmastery.com/use-pre-trained-vgg-model-classify-objects-photographs/
But I am interested to find out the parent category of a prediction. For example, if the model predicts a dog, I would like to know its parent category which is animal. I do not know if there is a way to use the Imagenet tree for this problem?
ImageNet
uses WordNet
to create the hierarchy of classes. You can access the object synset in the following way:
import nltk
from nltk.corpus import wordnet
nltk.download('wordnet')
dog = wordnet.synsets('dog')[0]
To access the parents of the dog
, call:
dog.hypernyms()
It might return more than 1 result which is problematic.
What you can do now, is to check if any of this parents is an actual ImageNet
class. To get the wnid
of the symset, just call dog.offset()
.
To reverse the process:
from nltk.corpus import wordnet as wn
wn.synset_from_pos_and_offset('n',2084071)