Skip to main content

A vision transformer for training on MNIST

Project description

Python package mnistvit

A PyTorch-only implementation of a vision transformer (ViT) for training on MNIST, achieving 99.65% test accuracy with default parameters and without pre-training. The ViT architecture and learning parameters can be configured easily. Code for hyperparameter optimization is provided as well.

The code is intended to be used for learning about vision transformers. With MNIST as a simple and well understood dataset, the importance of various hyperparameters can be explored.

Requirements

The package requires Python 3.10 or greater and additionally requires the torch and torchvision packages. For hyperparameter optimization, additionally ray[tune] and optuna are required. The ViT itself requires torch only.

Installation

To install the mnistvit package, run the following command in the parent directory of the repository:

pip install mnistvit

To install the package with hyperparameter optimization support:

pip install mnistvit[tune]

Usage

To train a model with default parameters:

python -m mnistvit.train

The script will produce a file config.json with the model configuration and file model.pt containing the trained model. Use the -h argument for a list of options.

To evaluate the test set accuracy of the model stored in model.pt with the configuration in config.json:

python -m mnistvit.predict --use-accuracy

To predict the class of the digit stored in the file sample.jpg:

python -m mnistvit.predict --image-file sample.jpg

For hyperparameter optimization with default search parameters:

python -m mnistvit.tune

A trained model is available on Hugging Face.

License

mnistvit is released under the GPLv3 license, as found in the LICENSE file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mnistvit-1.4.1.tar.gz (25.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mnistvit-1.4.1-py3-none-any.whl (27.4 kB view details)

Uploaded Python 3

File details

Details for the file mnistvit-1.4.1.tar.gz.

File metadata

  • Download URL: mnistvit-1.4.1.tar.gz
  • Upload date:
  • Size: 25.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for mnistvit-1.4.1.tar.gz
Algorithm Hash digest
SHA256 82c9a61d61573c22c009af16daed1a9960f8812a62841000afc8b7e03419886b
MD5 54951d7fdc3e7ef0c057cddf3c7324fc
BLAKE2b-256 730ce89b395fe8f5df7ed96306ff8fa15679d2d368d7af1f66657e46a2e4fe44

See more details on using hashes here.

File details

Details for the file mnistvit-1.4.1-py3-none-any.whl.

File metadata

  • Download URL: mnistvit-1.4.1-py3-none-any.whl
  • Upload date:
  • Size: 27.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for mnistvit-1.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 65cd18ef84c8f8f8f2ffaf136ec2ff5820617f42f7b36387445ae2336091691f
MD5 e6939d786109cb55900d8d9e53b20a0d
BLAKE2b-256 9f701eaea681a7e35ddfd9bbca30d531a25f8fcc6269add38edd37d9789dd9ed

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page