Download TIMM-NOTES.md from uv-scripts/classification: direct link, hf CLI and curl.
- Browser
- Download file 7.35 kB
-
https://huggingface.co/datasets/uv-scripts/classification/resolve/main/TIMM-NOTES.md
- Command line
-
hf download hf://datasets/uv-scripts/classification/TIMM-NOTES.md
-
curl -L -o TIMM-NOTES.md https://huggingface.co/datasets/uv-scripts/classification/resolve/main/TIMM-NOTES.md
timm script: options and behaviour details
Notes behind train-timm.py. The README has what you need to run it; this file records the full
options, what the script decides for you, and the timm details found while writing it.
The script is a thin layer over timm's own train.py. It does not re-implement training,
evaluation or model building: it prepares the data, runs train.py, and pushes what train.py
produced.
Options
| Option | Default | Notes |
|---|---|---|
--model |
timm/convnext_tiny.in12k_ft_in1k |
a Hub ID (timm/<name> or any org/name timm checkpoint) or a bare timm name |
--epochs |
10 | |
--batch-size |
64 | per GPU |
--lr |
5e-5 | for one GPU's batch; passed as --lr-base with --lr-base-size = --batch-size |
--weight-decay |
0.05 | |
--img-size |
the model's pretrained size | fixed-size models (ViT and others) are built at this size |
--image-column, --label-column |
auto | |
--train-split, --val-split |
train, auto |
|
--val-size |
0.1 | used only when the script carves a validation split |
--max-train-samples, --max-val-samples |
all | for quick tests |
--threshold |
0.5 | multi-label decision threshold (train.py --multilabel-threshold) |
--num-workers |
(CPU_CORES − 1) / GPUs on HF Jobs |
per GPU |
--private |
off | create the output repo as private |
--skip-reload-check |
off | skip reloading the pushed model |
Passing flags to train.py
Everything after a literal -- goes to timm train.py unchanged. On HF Jobs, put another --
right after hf jobs uv run and its flags; otherwise hf takes the only -- as its own separator
and the script receives --loss and the like. See python train.py --help in the timm repo for
its options.
Architecture and preprocessing flags (--gp, --mean, --std, --crop-pct, --model-kwargs,
--model-ema, ...) work: the pushed model is rebuilt from the arguments train.py saved in its
checkpoint. Flags that clash with what the script sets from the dataset are refused: --dataset,
--data-dir, the split and key flags, --num-classes, --task, --target-format, --class-map
(it would change the label order), --model, --output, --experiment, -c/--config and --split-bn.
--split-bn is refused because it changes the checkpoint layout after the model is built.
Abbreviations of these (for example --mod) are refused too. --epochs, --batch-size and --lr
after -- override the script's values, as in train.py. Use the script's own options for these,
though. The one warmup epoch is set from --epochs, so -- --epochs 1 would spend the whole run in
warmup. --lr-base-size is set from the script's --batch-size, so -- --batch-size 8 also changes
the learning rate. Train.py values that the card reports, such as --multilabel-threshold, are read
back from the checkpoint, so a passthrough value shows up correctly.
What the script decides
- Task. A
ClassLabellabel column gives single-label training (cross-entropy; top-1 and top-5). AList(ClassLabel)column gives multi-label training (BCE; mAP, micro, macro and sample F1). - Columns. The image column is the first
Imagefeature, preferringimage,img,pictureorphoto. The label column preferslabel,labels,category,classortarget. - Validation split. The first of
validation,valid,valortest. If there is none, a seeded 10% split is carved fromtrain, stratified for single-label data. A carved or capped split is written to a local parquet copy fortrain.py; a slice such astrain[:90%]would be neither seeded nor stratified. - Training defaults. AdamW, one warmup epoch, then a cosine schedule stepped per update, AMP
(bfloat16 on GPUs with compute capability 8.0 or higher, float16 otherwise).
train.pykeeps the best checkpoint on its validation metric. - Several GPUs. The script runs
train.pywithtorch.distributed.runon every GPU the flavor has, the same as timm'sdistributed_train.sh. It passes--lr-baseand--lr-base-size, so timm sets the learning rate for the global batch with its own rule: square-root scaling for AdamW. Nothing spans several Jobs.
What gets pushed
The script saves the model with timm's save_for_hf and uploads it with the model card in one
commit:
model.safetensors, andconfig.jsonwithlabel_names,num_classes,task, the preprocessing the model was trained with, andeval_metrics. Multi-label models also recordmultilabel_threshold.- A model card with the labels,
train.py's validation metrics for the best epoch, and the command to reproduce the run.
After the push, the script reloads the model with timm.create_model("hf-hub:...") and stops with
an error if the labels or the logits differ from the trained model.
timm details
train.pyversion. The script fetchestrain.pyat the tag of the installed timm release (for examplev1.0.31), so the trainer and the library always match. timm 1.0.31 is the first PyPI release with--task multilabel.- Rebuilding the model.
model_best.pth.tarholdstrain.py's full arguments. The script rebuilds the model withresolve_model_input_argsandcreate_model, astrain.pydoes (the training-only drop-rate arguments are left out), so fixed-size models at another--img-size(ViT, LeViT, VOLO, ...) load correctly. timm cannot load PiT's pretrained weights at another size (pit_ti_224at 256 stops intrain.pywith apos_embedsize mismatch, before training starts). - Output repo. The script creates the output repo before training, so a bad name or a token that cannot create the repo fails in seconds rather than after the run. (A token that can read an existing repo but not write to it still fails only at the upload.)
- Metrics. The card's metrics are the
eval_*columns oftrain.py'ssummary.csvfor the best epoch. With--model-ema,train.pyvalidates the EMA weights and the script pushes them. With--model-ema-force-cpu,train.pyvalidates the regular weights, so the script pushes those.mapis timm's mean average precision and can differ from scikit-learn's per-class average precision. timm'ssample_f1scores images with no labels as 0. - Multi-label flags.
--task multilabelneeds an explicit--num-classes, which the script passes, along with--target-format indices(timm's default for aList(ClassLabel)column). - Warmup.
train.pydefaults to 5 warmup epochs and does not shorten them for short runs. Without--sched-on-updatesthe learning rate changes once per epoch. With lr 2e-4, 2 epochs and no warmup, beans fell to chance; the script passes one warmup epoch and--sched-on-updates. - Learning rate and CNNs. The default
--lr5e-5 suits the ConvNeXt and ViT backbones. In 3 epochs on beans,resnet50.a1_in1k,efficientnet_b0.ra_in1kandmobilenetv4_conv_small.e2400_r224_in1kreached only 47-63% top-1 with it (ConvNeXt-tiny and ViT-small: 97.7%). Give CNNs a higher--lrand more epochs. - Default backbone.
convnext_tiny.in12k_ft_in1k(28M parameters, Apache-2.0). DINOv3 backbones were considered, but their licence would carry over to every trained model.