plantnet / PlantNet-300K

[NeurIPS2021] A plant image dataset with high label ambiguity and a long-tailed distribution
https://doi.org/10.5281/zenodo.5645731
BSD 2-Clause "Simplified" License
167 stars 31 forks source link

mapping of numerical label to actual class #16

Closed a-sajjad72 closed 5 months ago

a-sajjad72 commented 5 months ago

I had used the pre-trainded model for recognizing the plant images. I had the index of the prediced class. but nowhere found the mapping of the actual classes. How do i get the actual class name for the predicted class. Here is the code which i had tried

import torch
from torchvision import transforms
from PIL import Image
from utils import load_model
from torchvision.models import resnet18
filename = 'resnet18_weights_best_acc.tar' # pre-trained model path
use_gpu = False  # load weights on the gpu
model = resnet18(num_classes=1081) # 1081 classes in Pl@ntNet-300K

load_model(model, filename=filename, use_gpu=use_gpu)

image_path = 'images/3.jpg'
image = Image.open(image_path)

transform = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

input_tensor = transform(image).unsqueeze(0)
model.eval()
with torch.no_grad():
    output = model(input_tensor)

predicted_class = output.max(1)
print(f"Predicted class index: {predicted_class}")
garcinc commented 5 months ago

Hi,

You should use torch.argmax for finding the predicted class. You will find attached the json file mapping the class index to the species id. If you want the actual name, you can use then use the file "plantnet300K_species_id_2_name.json" to map the species id to the species name. I hope this helps. class_idx_to_species_id.json

josephsalmon commented 5 months ago

I think you mention this link @garcinc : https://lab.plantnet.org/seafile/d/bed81bc15e8944969cf6/ right? Yet, it could be exposed on the README.md for other users to find it quickly, wdyt?

garcinc commented 5 months ago

Yes this is the correct link. I updated the README.md and added class_idx_to_species_id.json to the seafile.

a-sajjad72 commented 5 months ago

thanks @josephsalmon @garcinc for your kind assistance. You guys really made my day. I wish I could make that PR.