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

Inference.py script is not returning the custom labels on test images #2419

Open
nitin-dominic opened this issue Jan 20, 2025 · 1 comment
Open
Assignees
Labels
bug Something isn't working

Comments

@nitin-dominic
Copy link

Hello:

I recently tried to run "inference.py" script on my custom dataset with custom labels (only 3). I trained the model EfficientNet and pushed it to the HuggingFace platform. Now, when i try to reload the checkpoints to run inference tests on my own dataset, I get this error:

"ERROR: Cannot deduce ImageNet subset from model, no labelling will be performed."

Any fixes or solutions will be appreciated.

@nitin-dominic nitin-dominic added the bug Something isn't working label Jan 20, 2025
@rwightman
Copy link
Collaborator

@nitin-dominic if the model is pushed with label_names and optionally label_descriptions and those are in the config.json it will work with the Hub API inference but yeah, inference.py is not handling this properly...

The API does this

        self.dataset_info = None
        label_names = self.model.pretrained_cfg.get("label_names", None)
        label_descriptions = self.model.pretrained_cfg.get("label_descriptions", None)

        if label_names is None:
            # if no labels added to config, use imagenet labeller in timm
            imagenet_subset = infer_imagenet_subset(self.model)
            if imagenet_subset:
                self.dataset_info = ImageNetInfo(imagenet_subset)
            else:
                # fallback label names
                label_names = [f"LABEL_{i}" for i in range(self.model.num_classes)]

        if self.dataset_info is None:
            self.dataset_info = CustomDatasetInfo(
                label_names=label_names,
                label_descriptions=label_descriptions,
            )

the inference.py code is just doing

    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)
            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.")

Also need a mechanism to allow loading those fields from a stand alone json for models that aren't on the hub or pushed with the needed metadata (e.g. just load a yaml or json with those fields)

An example model config with those labels & descriptions https://huggingface.co/timm/eva02_large_patch14_clip_336.merged2b_ft_inat21/raw/main/config.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants