huggingface / huggingface/pytorch-image-models

[FEATURE] Save dataset_train.reader.class_to_idx inside the model and load it by default during inference and validation

Open
#1,817 2 comments 2 reactions 0 assignees View on GitHub
enhancement
Dominant language
Python
Stars
37.1k
Forks
5.2k
Avg merge
1d 13h
Merged PRs (30d)
34

Description

**Is your feature request related to a problem? Please describe.**
I'm not using imagenet, but during inference it loads the imagenet class_map by default.

**Describe the solution you'd like**
Instead, the class_to_idx from the dataset_train.reader.class_to_idx should just be saved somewhere inside the model, and be loaded into class_map during inference by default. and of course, if someone still wants to override the class_map for whatever reason, they could still do so.

I'd make a PR myself, but y'all probably have other consideration for exactly where to save/load it, so here's a sample solution.

Sample solution:

```
# load
if args.class_map == ''
ckpt = torch.load(args.checkpoint, map_location='cpu')
args.class_map = ckpt.get('class_to_idx', '')

# save
output_dir = Path(output_dir)
best_model = list(output_dir.rglob("*best*"))[0]
ckpt = torch.load(best_model, map_location='cpu')
ckpt["class_to_idx"] = class_to_idx
torch.save(ckpt, best_model)

```
**Describe alternatives you've considered**
We could save the class_to_idx into a class map file and ship it along side the model, but that's cumbersome and tedious. The proposed solution just works by default.

**Additional context**
The same should probably be done with the args.yaml file. There are a ton of timm models on hugging face with pretrained weights, but no args.yaml file with them which makes it near impossible to reproduce their results.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.