https://github.com/alan-turing-institute/arc-locomoset
Low-Cost Model Selection for Transformers
Science Score: 33.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
○.zenodo.json file
-
○DOI references
-
✓Academic publication links
Links to: arxiv.org -
✓Committers with academic emails
3 of 4 committers (75.0%) from academic institutions -
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (13.1%) to scientific vocabulary
Keywords from Contributors
Repository
Low-Cost Model Selection for Transformers
Basic Info
- Host: GitHub
- Owner: alan-turing-institute
- License: mit
- Language: Jupyter Notebook
- Default Branch: develop
- Size: 12.4 MB
Statistics
- Stars: 1
- Watchers: 6
- Forks: 0
- Open Issues: 15
- Releases: 1
Metadata Files
README.md
LoCoMoSeT: Low-Cost Model Selection for Transformers
This project compared several metrics for predicting the best pre-trained models for fine-tuning on a new downstream task, without having to fine-tune each model. In particular, it explored the case of transferring ImageNet pre-trained vision transformers (ViTs) to several new image classification datasets. The metrics can be classified as zero-cost, such as number of model parameters or claimed ImageNet validation accuracy, or low-cost (much lower than fine-tuning), such as those proposed by Renggli et al. and Kaichao You et al..
This repository contains the code used to run experiments fine-tuning a pool of ViTs on several new datasets, computing the metrics on the same models/datasets, and comparing the rankings of the models by the metrics to the actual achieve fine-tuned model accuracies.
Installation
Clone this repository
Install with
pip(or see the developer setup below for using a poetry environment instead):
bash
pip install .
- If using LogME, NCE or LEEP clone the following repository https://github.com/thuml/LogME into
src:
bash
git clone https://github.com/thuml/LogME.git src/locomoset/LogME
Usage
Download ImageNet
ImageNet-1k is gated so you need to login with a HuggingFace token to download it (they're under https://huggingface.co/settings/tokens in your account settings). Log in to the HuggingFace CLI:
bash
huggingface-cli login
Once you've done this, head on over to https://huggingface.co/datasets/imagenet-1k, read the terms and conditions and if happy to proceed agree to them. Then run:
bash
python -c "import datasets; datasets.load_dataset('imagenet-1k')"
But note this will take a long time (hours).
Config Files
To run either metrics or training in LoCoMoSeT (see below), metrics and/or training config files are required. Examples are given in examplemetricsconfig.yaml and exampletrainconfig.yaml for metrics and training configs respectively.
Both kinds of config should contain:
caches: Contains cache locations and extra caching related arguments:models,datasets,wandb: Where to cache HuggingFace models & datasets and wandb runs & artifacts respectively.preprocess_cache: Set todisk,ram, ortmpto cache preprocessed data to disk (default), memory, or a temporary directory.tmp_dir: Overwrites the location of the temporary directory (usually only relevant ifpreprocess_cacheistmp, and generally should only be set if the default tmp dir for the OS is not large enough, e.g. on Baskerville this can be set to a path in/scratch-globalif you need more disk quota than what's available in/tmp).writer_batch_size: How many images to cache in memory before writing to disk. during preprocessing (relevant ifpreprocess_cacheisdiskortmp)
dataset_name: Name of the dataset on HuggingFacedataset_args: Contains dataset split/column selection parameters:train_split,val_split,test_split: Training, validation, and test_split names. Should either already exist or they will be generated. Should NOT be the sameval_size,test_size: Percentages (0.-1.) or integers denoting the size of the validation and test sets (if a percentage, as a percentage of the WHOLE dataset) to be generated. Can benull(None) if the corresponding split already existsimage_field,label_field: Dataset column containing the image and class label
model_name: Name of the model to be used on HuggingFacerandom_state: Seed for random number generationrun_name: Name for the wandb runsave_dir: Directory in which to save resultsuse_wandb: Set totrueto log results to wandbn_samples: The training dataset size, ornullto use the whole train split.keep_labels: A list of labels denoting which labels to keep - all images corresponding to other labels are dropped. Can benullto keep all
If use_wandb is true, then under wandb_args the following shoud additionally be specified:
entity: Wandb entity nameproject: Wandb project namejob_type: Job type to group wandb runs with. Should bemetricsortrainlog_model: How to handle model logging in wandb
Metrics configs should additionally contain:
metrics_samples: How many samples to compute the metrics with. This will be a subset of the training dataset so should be less than or equal to then_samplesvalue. Ornullto compute them with the whole train split (i.e. withn_samplesimages).local_save: Set totrueto locally save a copy of the resultsmetrics: A list of metrics implemented in src/locomost/metrics to be usedmetric_kwargs: A list of the patternmetric_name: kwarg_1: valueof kwargs to be passed to each metric if desired. Note that every metric used does not need to exist in this
Train configs should additionally contain the following nested under dataset_args:
train_split: Name of the data split to train onval_split: Name of the data split to evaluate on. If the same astrain_split, thetrain_splitwill itself be randomly split for training and evaluation
Along with any further training_args, which are all directly passed to HuggingFace TrainingArguments, for example:
eval_steps: Steps between each evaluationevaluation_strategy: HuggingFace evaluation strategylogging_strategy: HuggingFace logging strategynum_train_epochs: Number of epochs to train model foroutput_dir: Directory to store outputs inoverwrite_output_dir: Whether to overwrite the output directorysave_strategy: HuggingFace saving strategyuse_mps_device: Whether to use MPS
Since in practice you will likely wish to run many jobs together, LoCoMoSeT provides support for top-level configs from which you can generate many lower-level configs. Top-level configs can contain parameters for metrics scans, model training, or both. Broadly, this should contain the arguments laid out above, with some additional arguments and changes. An example is given in exampletopconfig.yaml
The additional arguments are:
config_dir: Location to store subconfigsslurm_template_name: Name of the slurm template to be used. If set tonull, it will be picked from src/locomoset/configuse_bask: Set toTrueif you wish to run the jobs on baskerville (HPC used in our research - for uses outside the Turing, this means a slurm script will be generated alongside the configs)
If use_bask is True, then you should include the following additional arguments nested under bask. They should be further nested under train and/or metrics as required:
job_name: Baskerville job namewalltime: Maximum runtime for the Baskerville job. Format is dd-hh:mm:ssnode_number: Number of nodes to usegpu_number: Number of GPUs to usecpu_per_gpu: Number of CPUs per GPU
The changes are:
models: Replacesmodel, contains a list of HuggingFace model namesrandom_states: Replacesrandom_state, contains a list of seeds to generate scripts over.keep_labels: Now a list of lists to generate scripts over
To generate configs from the top level config, run
bash
locomoset_gen_configs <top_level_config_file_path>
This will generate training and/or metrics configs across all combinations of model, dataset, and random state. locomoset_gen_configs will automatically detect whether your top-level config contains training and/or metrics-related arguments and will generate both kinds of config accordingly.
Run a metric scan
With the environment activated (poetry shell):
bash
locomoset_run_metrics <config_file_path>
For an example config file see configs/config_wp1.yaml.
This script will compute metrics scores for a given model, dataset, and random state.
Train a model
With the environment activated (poetry shell):
bash
locomoset_run_train <config_file_path>
This script will train a model for a given model name, dataset, and random state.
Save plots
Metric Scores vs. No. of Images
This plot shows how the metric values (y-axis) change with the number of images (samples) used to compute them (x-axis). Ideally the metric should converge to some fixed value which does not change much after the number of images is increased. The number of images it takes to get a reliable performance prediction determines how long it takes to compute the metric, so metrics that converge after seeing fewer images are preferable.
To make a plot of metric scores vs. actual fine-tuned performance performance:
bash
locomoset_plot_vs_samples <PATH_TO_RESULTS_DIR>
Where <PATH_TO_RESULTS_DIR> is the path to a directory containing JSON files produced by a metric scan (see above).
You can also run locomoset_plot_vs_samples --help to see the arguments.
Metric Scores vs. Fine-Tuned Performance
This plot shows the predicted performance score for each model from one of the low-cost metrics on the x-axis, and the actual fine-tuned performance of the models on that dataset on the y-axis. A high quality metric should have high correlation between its score (which is meant to reflect the transferability of the model to the new dataset) and the actual fine-tuned model performance.
To make this plot:
bash
locomoset_plot_vs_actual <PATH_TO_RESULTS_DIR> --scores_file <path_to_scores_file> --n_samples <n_samples>
Where:
<PATH_TO_RESULTS_DIR>is the path to a directory containing JSON files produced by a metric scan (see above).<path_to_scores_file>is a mapping between model names and fine-tuned performance on ImageNet-1k, such as the file configs/scores_imagenet1k.yaml in this repo.<n_samples>sets the no. of samples (images) the metric was computed with to plot. Usually a metrics scan includes results with different numbers of images, but for this plot the different metrics should be compared using a fixed no. of images only.
You can also run locomoset_plot_vs_actual --help to see the arguments.
Development
Developer Setup
- Install dependencies with Poetry
bash
poetry install
- If using LogME, NCE or LEEP clone the following repository https://github.com/thuml/LogME into
src:
bash
git clone https://github.com/thuml/LogME.git src/locomoset/LogME
- Install pre-commit hooks:
bash
poetry run pre-commit install --install-hooks
Common Commands/Tasks
To run commands in the poetry virtual environment (in a terminal), either:
- Prefix the command you want to run with
poetry run - e.g.
poetry run python myscript.py - Enter the virtual environment with
poetry shelland then run commands as normal - then exit the virtual environment with
exit
- Prefix the command you want to run with
To run tests:
bash
poetry run pytest
To run linters:
- If you have setup pre-commit
flake8,black, andisortwill run automatically before making commits - Or you can run them manually:
bash poetry run black . poetry run isort . poetry run flake8- If you have setup pre-commit
Owner
- Name: The Alan Turing Institute
- Login: alan-turing-institute
- Kind: organization
- Email: info@turing.ac.uk
- Website: https://turing.ac.uk
- Repositories: 477
- Profile: https://github.com/alan-turing-institute
The UK's national institute for data science and artificial intelligence.
GitHub Events
Total
Last Year
Committers
Last synced: 12 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| eddableheath | e****h@t****k | 215 |
| Jack Roberts | j****s@t****k | 156 |
| philswatton | p****n@g****m | 33 |
| Edmund Dable-Heath | m****5@b****k | 1 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 12 months ago
All Time
- Total issues: 74
- Total pull requests: 32
- Average time to close issues: 2 months
- Average time to close pull requests: 23 days
- Total issue authors: 4
- Total pull request authors: 3
- Average comments per issue: 2.35
- Average comments per pull request: 1.5
- Merged pull requests: 23
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 0
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 0
- Pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- jack89roberts (42)
- eddableheath (15)
- philswatton (2)
- lannelin (1)
Pull Request Authors
- jack89roberts (16)
- eddableheath (11)
- philswatton (3)
Top Labels
Issue Labels
Pull Request Labels
Dependencies
- actions/checkout v3 composite
- actions/setup-python v4 composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- stefanzweifel/git-auto-commit-action v4 composite
- 189 dependencies
- black ^23.9.1
- datasets ^2.13.0
- evaluate ^0.4.0
- h5py ^3.9.0
- isort ^5.12.0
- matplotlib ^3.7.2
- numba ^0.57.1
- python ^3.10
- pyyaml ^6.0
- scikit-learn ^1.3.0
- torch ^2.0, !=2.0.1
- torchvision ^0.15
- transformers ^4.30.2