google-research / big_transfer

Official repository for the "Big Transfer (BiT): General Visual Representation Learning" paper.
https://arxiv.org/abs/1912.11370
Apache License 2.0
1.5k stars 175 forks source link

Class names for ImageNet-21k pre-trained models #7

Closed issamemari closed 4 years ago

issamemari commented 4 years ago

Hi there! I would like to use one of the ImageNet-21k pre-trained models as is, without fine tuning. Where can I find the mapping between output indices and class names?

lucasb-eyer commented 4 years ago

Great question, we forgot to add this to the README and will add it later!

You can find the WordNet IDs of the classes here and the WordNet lemmas of the classes here. We have not actually double-checked the files yet, so if you try it out, please let us know if it looks correct!

issamemari commented 4 years ago

Thank you for your response @lucasb-eyer. I tried the file you linked and the output seems indeed correct. Here's some code for those interested:

import numpy as np
import torch

from bit.models import ResNetV2
from torchvision import transforms
from PIL import Image
from nltk.corpus import wordnet as wn

wordnet_ids = open("/home/issa/Desktop/wordnet_ids.txt", "r").readlines()
wordnet_ids = [x.strip() for x in wordnet_ids]
wordnet_ids = [int(x[1:]) for x in wordnet_ids]

model = ResNetV2(ResNetV2.BLOCK_UNITS["r101"], width_factor=1, zero_head=False)
model.load_from(np.load("/home/issa/Desktop/BiT-M-R101x1.npz"))
model.eval()

transform_test = transforms.Compose(
    [
        transforms.Resize(224),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
    ]
)

image = Image.open("/home/issa/Downloads/DSC_0611.jpg")
output = model(transform_test(image).unsqueeze(0))[0]

for index in torch.argsort(output, descending=True)[:10]:
    print(wn.synset_from_pos_and_offset('n', wordnet_ids[int(index)]))
lucasb-eyer commented 4 years ago

Great, thank you for the update and the code example!

ZhiyuanChen commented 3 years ago

Great question, we forgot to add this to the README and will add it later!

You can find the WordNet IDs of the classes here and the WordNet lemmas of the classes here. We have not actually double-checked the files yet, so if you try it out, please let us know if it looks correct!

Hi,

I checked the file provided, but it seems it does not contain all label in imagenet. For example, "n11738203", "European pasqueflower, Pulsatilla vulgaris, Anemone plusatilla" is not in the list

peiyingxin commented 2 years ago

Great question, we forgot to add this to the README and will add it later! You can find the WordNet IDs of the classes here and the WordNet lemmas of the classes here. We have not actually double-checked the files yet, so if you try it out, please let us know if it looks correct!

Hi,

I checked the file provided, but it seems it does not contain all label in imagenet. For example, "n11738203", "European pasqueflower, Pulsatilla vulgaris, Anemone plusatilla" is not in the list

Hi, have you solve this problem?

mosjel commented 1 year ago

Hi! Can you please just put the text file with 21841 classes and lines. Current file has 21843 lines instead!

mosjel commented 1 year ago

Please remove the line 9206 and 15028 in both lemma and id files. line 9206 and line 15028 must be omitted in both files. Then you can use them comfortably. If you do not do that after class 9204 you will receive the wrong answer.