Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use args.class_map for labels in inference.py #2319

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import torch

from timm.data import create_dataset, create_loader, resolve_data_config, ImageNetInfo, infer_imagenet_subset
from timm.data.readers.class_map import load_class_map
from timm.data.dataset_info import get_dataset_info_from_class_map
from timm.layers import apply_test_time_pool
from timm.models import create_model
from timm.utils import AverageMeter, setup_default_logging, set_jit_fuser, ParseKwargs
Expand Down Expand Up @@ -241,18 +243,25 @@ def main():

to_label = None
if args.label_type in ('name', 'description', 'detail'):
imagenet_subset = infer_imagenet_subset(model)
if imagenet_subset is not None:
dataset_info = ImageNetInfo(imagenet_subset)
dataset_info = None
if args.class_map:
class_map = load_class_map(args.class_map)
dataset_info = get_dataset_info_from_class_map(class_map)
else:
imagenet_subset = infer_imagenet_subset(model)
if imagenet_subset is not None:
dataset_info = ImageNetInfo(imagenet_subset)
else:
_logger.error("Cannot deduce ImageNet subset from model, no labelling will be performed.")

if dataset_info:
if args.label_type == 'name':
to_label = lambda x: dataset_info.index_to_label_name(x)
elif args.label_type == 'detail':
to_label = lambda x: dataset_info.index_to_description(x, detailed=True)
else:
to_label = lambda x: dataset_info.index_to_description(x)
to_label = np.vectorize(to_label)
else:
_logger.error("Cannot deduce ImageNet subset from model, no labelling will be performed.")

top_k = min(args.topk, args.num_classes)
batch_time = AverageMeter()
Expand Down
5 changes: 5 additions & 0 deletions timm/data/dataset_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,8 @@ def index_to_label_name(self, index) -> str:
def index_to_description(self, index: int, detailed: bool = False) -> str:
label = self.index_to_label_name(index)
return self.label_name_to_description(label, detailed=detailed)


def get_dataset_info_from_class_map(class_map):
label_names = {v: k for k, v in class_map.items()}
return CustomDatasetInfo(label_names)