In [1]:
from synset2vec import Synset2Vec
from im2vec import Image2Vec
from tagger import ZeroshotTagger
from simpleknn.bigfile import BigFile
from constant import ROOT_PATH as rootpath
In [2]:
%run -i build_label_vec.py ilsvrc12_test1k
%run -i build_label_vec.py ilsvrc12_test1k_2hop
In [3]:
# load image / label embedding models
i2v = Image2Vec()
s2v = Synset2Vec()
tagger = ZeroshotTagger()
In [4]:
# get prediction scores for the known label set Y0, which is currently ilsvrc12_test1k
# In the following example we use socres computed in advance.
# Alternatively,call a pre-trained CNN model to get the scores on the fly
image_collection = 'imagenet2hop-random2k'
test_image_id = 'n01495006_2522'
pY0 = 'dascaffeprob'
feat_dir = os.path.join(rootpath, image_collection, 'FeatureData', pY0)
feat_file = BigFile(feat_dir)
score_vec = feat_file.read_one(test_image_id) #
assert (len(score_vec) == 1000)
In [5]:
# perform zero-shot image tagging
img_embedding_vec = i2v.embedding(score_vec)
res = tagger.predict(img_embedding_vec, topk=5)
print ([(label, s2v.explain(label), score) for (label,score) in res])