Skip to content

Repository files navigation

ViT_PyTorch

This is a simple PyTorch implementation of Vision Transformer (ViT) described in the paper "An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale"

image

Usage

Download pre-trained weights.

python convert.py jax/weights/path/ converted/weights/path --model_name B_16_384

Train ViT

You can train ViT on your own dataset by following command :

python train.py /train/data/folder/ --valid_dir /validation/data/folder/ --pretrained_weights /pre-trained/weights/path 

Check out the Colab for preparing data, fine-tuning the model, and inference.

Details about training arguments :

ArgumentDescriptionTypeDefault
train_dirDirectory of training data.strrequired argument
valid_dirDirectory of validation data.strNone
valid_rateProportion of validation sample split from training data.floatNone
output_dirDirectory of output results where trained weights and training history will be stored.strNone
model_configModle arch configuration. (config path or arch name, e.g. "B_16_384")strB_16_384
pretrained_weightsFilename of pre-trained weights. Train from scratch if 'None'.strNone
freeze_extractorIf True, freeze the feature extractor weights to fine-tune the classification head.boolTrue
batch_sizeBatch size.int64
init_lrInitial learning rate.float1e-3
weight_decayWeight decay (L2 penalty).float1e-5
beta1Adam 'betas' param 1.float0.9
beta2Adam 'betas' param 2.float0.999
max_epochMaximun training epochs.int100
patientImproved patient for early stopping.intNone
monitorMetric to be monitored. ('loss' or 'acc')strloss
min_deltaMinimum change in the monitored metric to qualify as an improvement.float0.0
save_bestWhether to save weights from the epoch with the best monitored metric.boolTrue
warmupWarmup epochs.int0
schedulerTraining scheduler. ('cosine', 'step' or 'exp')strNone
t_maxMaximum number of iterations. (cosine scheduler)int10
eta_minMinimum learning rate. (cosine scheduler)float0.0
step_sizePeriod of learning rate decay. (step scheduler)int10
gammaMultiplicative factor of learning rate decay. (step/exp scheduler)float0.1
image_sizeInput image size.int384
crop_marginMargin for random cropping.int32
horizontal_flipHorizontal flip probability.float0.5
rotationDegree for random rotation.float10.
deviceComputation device. ('cuda' or 'cpu')strcuda
random_seedRandom seed in this repo.int427

About

This is a simple PyTorch implementation of Vision Transformer (ViT) described in the paper "An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale"

Topics

Resources

Stars

25 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages