Created
November 12, 2025 19:00
-
-
Save yberreby/668014577e1da807d89d07ad2791fdbd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # /// script | |
| # requires-python = ">=3.11" | |
| # dependencies = [ | |
| # "torchvision==0.24.0", | |
| # ] | |
| # /// | |
| """ | |
| Tiny script to export the ordered list of ImageNet-1K classes to a JSON file in the same directory as itself. | |
| Thanks to the dependency specification above, you can run it with: | |
| uv run --script export_in1k_classes.py | |
| """ | |
| import json | |
| from pathlib import Path | |
| out_dir = Path(__file__).parent | |
| out_file = out_dir / "in1k_classes.json" | |
| if __name__ == "__main__": | |
| # Scoped this import on purpose. We only use this to get the categories. | |
| from torchvision.models import ResNet50_Weights | |
| categories = list(ResNet50_Weights.IMAGENET1K_V2.meta["categories"]) | |
| print(f"Found {len(categories)} categories") | |
| print("First few: ", categories[:5]) | |
| print("Last few: ", categories[-5:]) | |
| print("Exporting to:", out_file) | |
| with open(out_file, "w") as f: | |
| json.dump(categories, f) | |
| how_to_load = f""" | |
| with open("{out_file}", "r") as f: | |
| categories = json.load(f) | |
| print(categories[:5]) | |
| """ | |
| print("Load it by running:") | |
| print() | |
| print(how_to_load) | |
| print() | |
| print("Let's try it out with this exact code!") | |
| print("Output:") | |
| exec(how_to_load) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment